csp_thread.h
Go to the documentation of this file.
1 /*
2 Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3 Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
4 Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
5 
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10 
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
29 #ifndef _CSP_THREAD_H_
30 #define _CSP_THREAD_H_
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <stdint.h>
37 #include <csp/csp.h>
38 
39 /* POSIX interface */
40 #if defined(CSP_POSIX) || defined(CSP_MACOSX)
41 
42 #include <pthread.h>
43 #include <unistd.h>
44 
45 #define csp_thread_exit() pthread_exit(NULL)
46 
47 #define csp_thread_kill(thread) pthread_cancel((pthread_t)thread); pthread_join((pthread_t)thread, NULL);
48 
49 typedef pthread_t csp_thread_handle_t;
50 typedef void * csp_thread_return_t;
51 
52 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t task_name(void * param)
53 #define CSP_TASK_RETURN NULL
54 
55 #define csp_sleep_ms(time_ms) usleep(time_ms * 1000);
56 
57 #endif // CSP_POSIX
58 
59 /* Windows interface */
60 #if defined(CSP_WINDOWS)
61 
62 #include <Windows.h>
63 #undef interface
64 #include <process.h>
65 
66 #define csp_thread_exit() _endthreadex(0)
67 
68 typedef HANDLE csp_thread_handle_t;
69 typedef unsigned int csp_thread_return_t;
70 
71 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t __attribute__((stdcall)) task_name(void * param)
72 #define CSP_TASK_RETURN 0
73 
74 #define csp_sleep_ms(time_ms) Sleep(time_ms);
75 
76 #endif // CSP_WINDOWS
77 
78 /* FreeRTOS interface */
79 #if defined(CSP_FREERTOS)
80 
81 #include <FreeRTOS.h>
82 #include <task.h>
83 
84 #if INCLUDE_vTaskDelete
85 #define csp_thread_exit() vTaskDelete(NULL)
86 #else
87 #define csp_thread_exit()
88 #endif
89 
90 #if INCLUDE_vTaskDelete
91 #define csp_thread_kill(thread) vTaskDelete(NULL)
92 #else
93 #define csp_thread_kill()
94 #endif
95 
96 typedef xTaskHandle csp_thread_handle_t;
97 typedef void csp_thread_return_t;
98 
99 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t task_name(void * param)
100 #define CSP_TASK_RETURN
101 
102 #define csp_sleep_ms(time_ms) vTaskDelay(time_ms / portTICK_RATE_MS);
103 
104 #endif // CSP_FREERTOS
105 
106 #ifndef CSP_WINDOWS
107 int csp_thread_create(csp_thread_return_t (* routine)(void *), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle);
108 #else
109 int csp_thread_create(csp_thread_return_t (* routine)(void *)__attribute__((stdcall)), const char * const thread_name, unsigned short stack_depth, void * parameters, unsigned int priority, csp_thread_handle_t * handle);
110 #endif
111 
112 #ifdef __cplusplus
113 } /* extern "C" */
114 #endif
115 
116 #endif // _CSP_THREAD_H_
117 
118 /* @} */
int csp_thread_create(csp_thread_return_t(*routine)(void *), const char *const thread_name, unsigned short stack_depth, void *parameters, unsigned int priority, csp_thread_handle_t *handle)