RTI Connext DDS Micro  Version 2.4.6
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
osapi_thread.h
Go to the documentation of this file.
1 /*
2  * FILE: osapi_thread.h - Definition of System API
3  *
4  * Copyright 2012-2015 Real-Time Innovations, Inc.
5  *
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  *
11  * Modification History
12  * --------------------
13  * 12mar2012,tk Written
14  */
15 /*e \file
16  * \brief Thread interface definition
17  */
18 #ifndef osapi_thread_h
19 #define osapi_thread_h
20 
21 #include "osapi/osapi_config.h"
22 #ifndef osapi_dll_h
23 #include "osapi/osapi_dll.h"
24 #endif
25 #ifndef osapi_types_h
26 #include "osapi/osapi_types.h"
27 #endif
28 
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #endif
33 
34 /*e \defgroup OSAPI_ThreadClass OSAPI Thread
35  * \ingroup OSAPIModule
36 */
37 
38 /*e \ingroup OSAPI_ThreadClass
39 
40  \brief Abstract Thread API.
41 */
42 #ifdef INCLUDE_POSIX
43 #include <pthread.h>
44 #define OSAPI_ThreadHandle pthread_t
45 #define OSAPI_ThreadId pthread_t
46 #elif OSAPI_PLATFORM == OSAPI_PLATFORM_WINDOWS
47 #include <windows.h>
48 #include <process.h>
49 #define OSAPI_ThreadHandle HANDLE
50 #define OSAPI_ThreadId HANDLE
51 #elif OSAPI_PLATFORM == OSAPI_PLATFORM_STOS
52 #define OSAPI_ThreadHandle RTI_INT32
53 #define OSAPI_ThreadId RTI_INT32
54 #elif OSAPI_PLATFORM == OSAPI_PLATFORM_STELLARIS
55 #define OSAPI_ThreadHandle RTI_INT32
56 #define OSAPI_ThreadId RTI_INT32
57 #elif OSAPI_PLATFORM == OSAPI_PLATFORM_FREERTOS
58 #ifndef INC_FREERTOS_H
59 #include "FreeRTOS.h"
60 #endif
61 #ifndef TASK_H
62 #include "task.h"
63 #endif
64 #define OSAPI_ThreadHandle xTaskHandle
65 #define OSAPI_ThreadId xTaskHandle
66 #elif OSAPI_PLATFORM == OSAPI_PLATFORM_VXWORKS
67 #define OSAPI_ThreadId int
68 #define OSAPI_ThreadHandle int
69 #else
70 #error "thread handle not ported"
71 #endif
72 
73 /* these are #defined so users can pass in any int they want for priority as well
74  * i.e., OS-native thread priorities
75  */
76 #define OSAPI_THREAD_PRIORITY_LOW -1
77 #define OSAPI_THREAD_PRIORITY_BELOW_NORMAL -2
78 #define OSAPI_THREAD_PRIORITY_NORMAL -3
79 #define OSAPI_THREAD_PRIORITY_ABOVE_NORMAL -4
80 #define OSAPI_THREAD_PRIORITY_HIGH -5
81 
82 #define OSAPI_THREAD_USE_OSDEFAULT_STACKSIZE 0
83 
84 /*e \ingroup OSAPI_ThreadClass
85  * Thread options
86  */
87 typedef RTI_UINT32 OSAPI_ThreadOptions;
88 
89 /*e \ingroup OSAPI_ThreadClass
90  Use only the default options the OS gives you.
91 */
92 #define OSAPI_THREAD_DEFAULT_OPTIONS 0x00
93 /*e \ingroup OSAPI_ThreadClass
94  Support floating point.
95 */
96 #define OSAPI_THREAD_FLOATING_POINT 0x01
97 /*e \ingroup OSAPI_ThreadClass
98  Support standard I/O.
99 */
100 #define OSAPI_THREAD_STDIO 0x02
101 /*e \ingroup OSAPI_ThreadClass
102  Run in real-time priority mode.
103 */
104 #define OSAPI_THREAD_REALTIME_PRIORITY 0x08
105 /*e \ingroup OSAPI_ThreadClass
106  Insist on the specified priority and fail if the OS doesn't like it.
107 */
108 #define OSAPI_THREAD_PRIORITY_ENFORCE 0x10
109 /*e \ingroup OSAPI_ThreadClass
110  Support the ability to asynchronously cancel the thread.
111 */
112 #define OSAPI_THREAD_CANCEL_ASYNCHRONOUS 0x20
113 
114 /*e \ingroup OSAPI_ThreadClass
115  *
116  */
117 struct OSAPI_ThreadProperty
118 {
119  /*e Hint on required stack-size */
120  RTI_UINT32 stack_size;
121 
122  /*e Hint on required priority */
123  RTI_INT32 priority;
124 
125  /*e Hint on thread priorites if support by the OS */
126  OSAPI_ThreadOptions options;
127 };
128 
129 /*e \ingroup OSAPI_ThreadClass
130  * Initializer for thread properties
131  */
132 #define OSAPI_THREAD_PROPERTY_DEFAULT \
133 { \
134  OSAPI_THREAD_USE_OSDEFAULT_STACKSIZE, \
135  OSAPI_THREAD_PRIORITY_NORMAL, \
136  OSAPI_THREAD_DEFAULT_OPTIONS \
137 }
138 
139 #define OSAPI_ThreadProperty_INITIALIZER OSAPI_THREAD_PROPERTY_DEFAULT
140 /*e
141  * \ingroup OSAPI_ThreadClass
142  * \brief Thread info
143  *
144  */
146 {
147  /*e Stop executing thread */
149 
150  /*e Whether or not the created thread is preemptive */
152 
153  /*e Parameter passed by thread creator. Passed to thread. */
154  void *user_data;
155 };
156 
157 /*e \ingroup OSAPI_ThreadClass
158  *
159  * \brief Thread task signature.
160  *
161  * \param[in] thread_info Thread information structure
162  *
163  * \return RTI_TRUE on successful execution, RTI_FALSE on failure.
164  */
165 FUNCTION_SHOULD_TYPEDEF(
166 RTI_BOOL
167 (*OSAPI_ThreadRoutine)(struct OSAPI_ThreadInfo *thread_info)
168 )
169 
170 /*e \ingroup OSAPI_ThreadClass
171  *
172  * \brief Abstract thread class.
173  */
174 struct OSAPI_Thread;
175 
176 
177 /*e \ingroup OSAPI_ThreadClass
178  *
179  * \brief Wakeup user-thread
180  *
181  * \details
182  *
183  * If a user-defined thread function is blocking, e.g. waiting for
184  * data, and the user wants to delete the thread, it is necessary to
185  * unblock the user-thread. The user must provide a function which can
186  * unblock a thread. This function calls the wake up function to wake up a
187  * blocked user thread.
188  *
189  * \param [in] self OSAPI_Thread to wakeup
190  *
191  * \return RTI_TRUE on success, RTI_FALSE on failure.
192  *
193  * \sa \ref OSAPI_Thread_start
194  */
195 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
196 OSAPI_Thread_wakeup(struct OSAPI_Thread *self);
197 
198 /*e \ingroup OSAPI_ThreadClass
199  *
200  * \brief Start a specific thread.
201  *
202  * \param [in] me Thread to wake up
203  *
204  * \return RTI_TRUE on success, RTI_FALSE on failure.
205  *
206  * \sa \ref OSAPIPortingIntroClass, \ref OSAPI_Thread_create
207  */
208 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
209 OSAPI_Thread_start(struct OSAPI_Thread *me);
210 
211 
212 /*e \ingroup OSAPI_ThreadClass
213  * \brief Destroy a specific thread.
214  *
215  * \param [in] self Thread.
216  *
217  * \return RTI_TRUE on success, RTI_FALSE on failure.
218  *
219  * \sa \ref OSAPIPortingIntroClass, \ref OSAPI_Thread_create
220  */
221 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
222 OSAPI_Thread_destroy(struct OSAPI_Thread *self);
223 
224 /*e \ingroup OSAPI_ThreadClass
225  * \brief Suspend a thread for a specified amount of time.
226  *
227  * \param [in] ms Sleep time.
228  *
229  * \sa \ref OSAPIPortingIntroClass
230  */
231 OSAPIDllExport void
232 OSAPI_Thread_sleep(RTI_UINT32 ms);
233 
234 /*e \ingroup OSAPI_ThreadClass
235  *
236  * \brief Create a thread.
237  *
238  * \param [in] name The name of the thread.
239  *
240  * \param [in] properties Thread properties. These properties are hints.
241  *
242  * \param [in] user_routine Thread task. The thread task cannot assume that
243  * it can block; thus it must be written such that
244  * it can be called repeatedly.
245  *
246  * \param [in] user_data Parameters passed to the thread task.
247  *
248  * \param [in] wakeup_routine Routine to wake up a thread, called to delete
249  * a thread.
250  *
251  * \return Handle to stopped thread on success, NULL on failure.
252  *
253  * \sa \ref OSAPIPortingIntroClass, \ref OSAPI_Thread_destroy
254  */
255 MUST_CHECK_RETURN OSAPIDllExport struct OSAPI_Thread*
256 OSAPI_Thread_create(const char *name,
257  const struct OSAPI_ThreadProperty *properties,
258  OSAPI_ThreadRoutine user_routine,
259  void *user_data,
260  OSAPI_ThreadRoutine wakeup_routine);
261 
262 
263 /*e \ingroup OSAPI_ThreadClass
264  *
265  * \brief Return thread ID.
266  *
267  * \return thread ID of the calling thread.
268  *
269  * \sa \ref OSAPIPortingIntroClass, \ref OSAPI_Thread_create
270  */
271 OSAPIDllExport OSAPI_ThreadId
272 OSAPI_Thread_self(void);
273 
274 #ifdef __cplusplus
275 } /* extern "C" */
276 #endif
277 
278 
279 #endif /* osapi_thread_h */

RTI Connext DDS Micro Version 2.4.6 Copyright © Mon Jan 25 2016 Real-Time Innovations, Inc