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 typedef pthread_t csp_thread_handle_t;
48 typedef void * csp_thread_return_t;
49 
50 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t task_name(void * param)
51 #define CSP_TASK_RETURN NULL
52 
53 #define csp_sleep_ms(time_ms) usleep(time_ms * 1000);
54 
55 #endif // CSP_POSIX
56 
57 /* Windows interface */
58 #if defined(CSP_WINDOWS)
59 
60 #include <Windows.h>
61 #undef interface
62 #include <process.h>
63 
64 #define csp_thread_exit() _endthreadex(0)
65 
66 typedef HANDLE csp_thread_handle_t;
67 typedef unsigned int csp_thread_return_t;
68 
69 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t __attribute__((stdcall)) task_name(void * param)
70 #define CSP_TASK_RETURN 0
71 
72 #define csp_sleep_ms(time_ms) Sleep(time_ms);
73 
74 #endif // CSP_WINDOWS
75 
76 /* FreeRTOS interface */
77 #if defined(CSP_FREERTOS)
78 
79 #include <FreeRTOS.h>
80 #include <task.h>
81 
82 #if INCLUDE_vTaskDelete
83 #define csp_thread_exit() vTaskDelete(NULL)
84 #else
85 #define csp_thread_exit()
86 #endif
87 
88 typedef xTaskHandle csp_thread_handle_t;
89 typedef void csp_thread_return_t;
90 
91 #define CSP_DEFINE_TASK(task_name) csp_thread_return_t task_name(void * param)
92 #define CSP_TASK_RETURN
93 
94 #define csp_sleep_ms(time_ms) vTaskDelay(time_ms / portTICK_RATE_MS);
95 
96 #endif // CSP_FREERTOS
97 
98 #ifndef CSP_WINDOWS
99 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);
100 #else
101 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);
102 #endif
103 
104 #ifdef __cplusplus
105 } /* extern "C" */
106 #endif
107 
108 #endif // _CSP_THREAD_H_
109 
110 /* @} */
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)