urtware / urt_osal.h @ 6882b76c
History | View | Annotate | Download (29.595 KB)
1 | 46471486 | Thomas Schöpping | /*
|
---|---|---|---|
2 | µRtWare is a lightweight publish/subscribe middleware for real-time
|
||
3 | applications. It was developed as part of the software habitat for the
|
||
4 | Autonomous Mini Robot [1] (AMiRo) but can be used for other purposes as well.
|
||
5 | |||
6 | Copyright (C) 2018..2020 Thomas Schöpping et al.
|
||
7 | |||
8 | This program is free software: you can redistribute it and/or modify
|
||
9 | it under the terms of the GNU General Public License as published by
|
||
10 | the Free Software Foundation, either version 3 of the License, or
|
||
11 | (at your option) any later version.
|
||
12 | |||
13 | This program is distributed in the hope that it will be useful,
|
||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | GNU General Public License for more details.
|
||
17 | |||
18 | You should have received a copy of the GNU General Public License
|
||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
20 | */
|
||
21 | |||
22 | #ifndef URT_OSAL_H
|
||
23 | #define URT_OSAL_H
|
||
24 | |||
25 | #include <urtware.h> |
||
26 | |||
27 | /*============================================================================*/
|
||
28 | /* VERSION */
|
||
29 | /*============================================================================*/
|
||
30 | |||
31 | /**
|
||
32 | * @brief The µRtWare operating system abstraction layer interface major version.
|
||
33 | * @note Changes of the major version imply incompatibilities.
|
||
34 | */
|
||
35 | #define URT_OSAL_VERSION_MAJOR 0 |
||
36 | |||
37 | /**
|
||
38 | * @brief The µRtWare operating system abstraction layer interface minor version.
|
||
39 | * @note A higher minor version implies new functionalty, but all old interfaces are still available.
|
||
40 | */
|
||
41 | #define URT_OSAL_VERSION_MINOR 1 |
||
42 | |||
43 | /*============================================================================*/
|
||
44 | /* DEPENDENCIES */
|
||
45 | /*============================================================================*/
|
||
46 | |||
47 | #include <stdint.h> |
||
48 | |||
49 | /*============================================================================*/
|
||
50 | /* DEBUG */
|
||
51 | /*============================================================================*/
|
||
52 | |||
53 | #if defined(__cplusplus)
|
||
54 | extern "C" { |
||
55 | #endif /* defined(__cplusplus) */ |
||
56 | |||
57 | #if !defined(urtDebugAssert) || defined(__DOXYGEN__)
|
||
58 | /**
|
||
59 | * @brief Assert function to check a given condition.
|
||
60 | *
|
||
61 | * @param[in] condition The condition to check.
|
||
62 | */
|
||
63 | void urtDebugAssert(const bool condition); |
||
64 | #endif /* !defined(urtDebugAssert) */ |
||
65 | |||
66 | #if defined(__cplusplus)
|
||
67 | } |
||
68 | #endif /* defined(__cplusplus) */ |
||
69 | |||
70 | /*============================================================================*/
|
||
71 | /* MUTEX */
|
||
72 | /*============================================================================*/
|
||
73 | |||
74 | /*
|
||
75 | * The following type must be defined by the implementation:
|
||
76 | *
|
||
77 | * urt_osMutex_t
|
||
78 | * Type to represent a mutex lock object.
|
||
79 | * Is only used via pointers by the API.
|
||
80 | */
|
||
81 | |||
82 | #if defined(__cplusplus)
|
||
83 | extern "C" { |
||
84 | #endif /* defined(__cplusplus) */ |
||
85 | |||
86 | #if !defined(urtMutexInit) || defined(__DOXYGEN__)
|
||
87 | /**
|
||
88 | * @brief Initialize a mutex lock object.
|
||
89 | *
|
||
90 | * @param[in] mutex Pointer to the mutex lock object to initialize.
|
||
91 | * Must not be NULL.
|
||
92 | */
|
||
93 | void urtMutexInit(urt_osMutex_t* mutex);
|
||
94 | #endif /* !defined(urtMutexInit) */ |
||
95 | |||
96 | #if !defined(urtMutexLock) || defined(__DOXYGEN__)
|
||
97 | /**
|
||
98 | * @brief Lock a mutex lock.
|
||
99 | * @details The calling thread is blocked until the mutex lock could be acquired.
|
||
100 | *
|
||
101 | * @param[in] mutex Pointer to the mutex lock to acquire.
|
||
102 | * Must not be NULL.
|
||
103 | */
|
||
104 | void urtMutexLock(urt_osMutex_t* mutex);
|
||
105 | #endif /* !defined(urtMutexLock) */ |
||
106 | |||
107 | #if !defined(urtMutexTryLock) || defined(__DOXYGEN__)
|
||
108 | /**
|
||
109 | * @brief Try to lock a mutex lock.
|
||
110 | * @details This function does not block but returns immediately.
|
||
111 | *
|
||
112 | * @param[in] mutex Pointer to the mutex lock to acquire.
|
||
113 | *
|
||
114 | * @return Indicator whether the lock was acquired.
|
||
115 | * @retval true The lock was acquired successfully.
|
||
116 | * @retval false The lock could not be acquired.
|
||
117 | */
|
||
118 | bool urtMutexTryLock(urt_osMutex_t* mutex);
|
||
119 | #endif /* !defined(urtMutexTryLock) */ |
||
120 | |||
121 | #if !defined(urtMutexUnlock) || defined(__DOXYGEN__)
|
||
122 | /**
|
||
123 | * @brief Unlock an owned mutex lock.
|
||
124 | *
|
||
125 | * @param[in] mutex Pointer to the mutex lock to unlock.
|
||
126 | * Must not be NULL.
|
||
127 | */
|
||
128 | void urtMutexUnlock(urt_osMutex_t* mutex);
|
||
129 | #endif /* !defined(urtMutexUnlock) */ |
||
130 | |||
131 | #if defined(__cplusplus)
|
||
132 | } |
||
133 | #endif /* defined(__cplusplus) */ |
||
134 | |||
135 | /*============================================================================*/
|
||
136 | /* CONDITION VARIABLE */
|
||
137 | /*============================================================================*/
|
||
138 | |||
139 | /*
|
||
140 | * The following type must be defined by the implementation:
|
||
141 | *
|
||
142 | * urt_osCondvar_t
|
||
143 | * Type to represent a condition variable object.
|
||
144 | * Is only used via pointers by the API.
|
||
145 | */
|
||
146 | |||
147 | /**
|
||
148 | * @brief Status type of condition variables.
|
||
149 | */
|
||
150 | typedef enum { |
||
151 | URT_CONDVAR_WAITSTATUS_SIGNAL = 1, /**< The condition variable has been signaled individually. */ |
||
152 | URT_CONDVAR_WAITSTATUS_BROADCAST = 2, /**< The condition variable has been signal via broadcast. */ |
||
153 | URT_CONDVAR_WAITSTATUS_TIMEOUT = 0, /**< Waiting for the condition variable timed out. */ |
||
154 | } urt_osCondvarWaitStatus_t; |
||
155 | |||
156 | #if defined(__cplusplus)
|
||
157 | extern "C" { |
||
158 | #endif /* defined(__cplusplus) */ |
||
159 | |||
160 | #if !defined(urtCondvarInit) || defined(__DOXYGEN__)
|
||
161 | /**
|
||
162 | * @brief Initialize a condition variable object.
|
||
163 | *
|
||
164 | * @param[in] condvar Pointer to the object to initialize.
|
||
165 | * Must not be NULL.
|
||
166 | */
|
||
167 | void urtCondvarInit(urt_osCondvar_t* condvar);
|
||
168 | #endif /* !defined(urtCondvarInit) */ |
||
169 | |||
170 | #if !defined(urtCondvarSignal) || defined(__DOXYGEN__)
|
||
171 | /**
|
||
172 | * @brief Signal a condition variable (wake one waiting thread).
|
||
173 | *
|
||
174 | * @param[in] condvar Pointer to the condition variable to signal.
|
||
175 | * Must not be NULL.
|
||
176 | */
|
||
177 | void urtCondvarSignal(urt_osCondvar_t* condvar);
|
||
178 | #endif /* !defined(urtCondvarSignal) */ |
||
179 | |||
180 | #if !defined(urtCondvarBroadcast) || defined(__DOXYGEN__)
|
||
181 | /**
|
||
182 | * @brief Signal a condition variable via broadcast (wake all waiting threads).
|
||
183 | *
|
||
184 | * @param[in] condvar Pointer to the condition variable to signal via broadcast.
|
||
185 | * Must not be NULL.
|
||
186 | */
|
||
187 | void urtCondvarBroadcast(urt_osCondvar_t* condvar);
|
||
188 | #endif /* !defined(urtCondvarBroadcast) */ |
||
189 | |||
190 | 0de5bed8 | Thomas Schöpping | #if (!defined(urtCondvarWait) && (URT_CFG_OSAL_CONDVAR_TIMEOUT == true)) || defined(__DOXYGEN__) |
191 | 46471486 | Thomas Schöpping | /**
|
192 | * @brief Wait for a condition variable to be signaled or timeout.
|
||
193 | *
|
||
194 | * @param[in] condvar Pointer to the condition variabel to wait for.
|
||
195 | * Must not be NULL.
|
||
196 | * @param[in] mutex Pointer to the associated mutex lock.
|
||
197 | * Must not be NULL.
|
||
198 | * Mutex must be acquired when this function is called.
|
||
199 | * @param[in] timeout Timeout (in microseconds) when the function will return even without the condition variable being signaled.
|
||
200 | * Use URT_DELAY_INFINITE to deactivate timeout.
|
||
201 | * @return
|
||
202 | */
|
||
203 | urt_osCondvarWaitStatus_t urtCondvarWait(urt_osCondvar_t* condvar, urt_osMutex_t* mutex, urt_delay_t timeout); |
||
204 | 0de5bed8 | Thomas Schöpping | #else /* !defined(urtCondvarWait) && (URT_CFG_OSAL_CONDVAR_TIMEOUT == true) */ |
205 | urt_osCondvarWaitStatus_t urtCondvarWait(urt_osCondvar_t* condvar, urt_osMutex_t* mutex); |
||
206 | #endif /* !defined(urtCondvarWait) && (URT_CFG_OSAL_CONDVAR_TIMEOUT == true) */ |
||
207 | 46471486 | Thomas Schöpping | |
208 | #if defined(__cplusplus)
|
||
209 | } |
||
210 | #endif /* defined(__cplusplus) */ |
||
211 | |||
212 | /*============================================================================*/
|
||
213 | /* EVENTS */
|
||
214 | /*============================================================================*/
|
||
215 | |||
216 | /*
|
||
217 | * The following types and constants must be defined by the implementation:
|
||
218 | *
|
||
219 | * urt_osEventSource_t
|
||
220 | * Type to represent an event source object.
|
||
221 | * Is only used via pointers by the API.
|
||
222 | *
|
||
223 | * urt_osEventListener_t
|
||
224 | * Type to represent an event listener object.
|
||
225 | * Is only used via pointers by the API.
|
||
226 | *
|
||
227 | * urt_osEventMask_t
|
||
228 | * Integer type to represent event masks.
|
||
229 | *
|
||
230 | * urt_osEventFlags_t
|
||
231 | * Integer type to represent event flags.
|
||
232 | *
|
||
233 | * URT_EVENTMASK_MAXPRIO
|
||
234 | * Constant of type urt_osEventMask_t to specify the event mask that is
|
||
235 | * handled with maximum priority.
|
||
236 | * This constant is typically either 0x0..1 (low values have high priority)
|
||
237 | * or 0x8..0 (high values have high priority).
|
||
238 | * A value of 0 indicates that priorities are not supported.
|
||
239 | 4ea18a07 | Svenja | *
|
240 | * URT_EVENTMASK_ALL
|
||
241 | * Constant of type urt_osEventMask_t to filter no events at all.
|
||
242 | * Typically is 0xF..F.
|
||
243 | 46471486 | Thomas Schöpping | */
|
244 | |||
245 | /**
|
||
246 | * @brief Enum type to distinguish differnt variants how to wait for events.
|
||
247 | */
|
||
248 | typedef enum { |
||
249 | URT_EVENT_WAIT_ONE = 0, /**< Wait for exactly one of the specified events. */ |
||
250 | URT_EVENT_WAIT_ANY = 1, /**< Wait for any (one or more) of the specified events. */ |
||
251 | URT_EVENT_WAIT_ALL = 2, /**< Wait for all of the specified events. */ |
||
252 | } urt_osEventWait_t; |
||
253 | |||
254 | #if defined(__cplusplus)
|
||
255 | extern "C" { |
||
256 | #endif /* defined(__cplusplus) */ |
||
257 | |||
258 | #if !defined(urtEventSourceInit) || defined(__DOXYGEN__)
|
||
259 | /**
|
||
260 | * @brief Initialize an event source object.
|
||
261 | *
|
||
262 | * @param[in] source Pointer to the event source object to initialize.
|
||
263 | * Must not be NULL.
|
||
264 | */
|
||
265 | void urtEventSourceInit(urt_osEventSource_t* source);
|
||
266 | #endif /* !defined(urtEventSourceInit) */ |
||
267 | |||
268 | #if !defined(urtEventSourceBroadcast) || defined(__DOXYGEN__)
|
||
269 | /**
|
||
270 | * @brief Broadcast an event with specified flags.
|
||
271 | *
|
||
272 | * @param[in] source Pointer to the event source to broadcast the event.
|
||
273 | * Must not be NULL.
|
||
274 | * @param[in] flags Flags to be set at the recieveing listeners.
|
||
275 | */
|
||
276 | void urtEventSourceBroadcast(urt_osEventSource_t* source, urt_osEventFlags_t flags);
|
||
277 | #endif /* !defined(urtEventSourceBroadcast) */ |
||
278 | |||
279 | #if !defined(urtEventListenerInit) || defined(__DOXYGEN__)
|
||
280 | /**
|
||
281 | * @brief Initialize an event listener object.
|
||
282 | * Must not be NULL.
|
||
283 | *
|
||
284 | * @param[in] listener Pointer to the event listener object to initialize.
|
||
285 | */
|
||
286 | void urtEventListenerInit(urt_osEventListener_t* listener);
|
||
287 | #endif /* !defined(urtEventListenerInit) */ |
||
288 | |||
289 | #if !defined(urtEventListenerGetFlags) || defined(__DOXYGEN__)
|
||
290 | /**
|
||
291 | * @brief Retrieve the currently set flags of an event listener.
|
||
292 | * @details Flags are not cleared in the listener.
|
||
293 | *
|
||
294 | * @param[in] listener Pointer to the event listener to recieve the flags from.
|
||
295 | * Must not be NULL.
|
||
296 | *
|
||
297 | * @return Currently set flags.
|
||
298 | */
|
||
299 | urt_osEventFlags_t urtEventListenerGetFlags(urt_osEventListener_t* listener); |
||
300 | #endif /* !defined(urtEventListenerGetFlags) */ |
||
301 | |||
302 | #if !defined(urtEventListenerClearFlags) || defined(__DOXYGEN__)
|
||
303 | /**
|
||
304 | * @brief Retrieve and clear the currently set flags from an event listener.
|
||
305 | *
|
||
306 | * @param[in] listener Pointer to the event listener to recieve the flags from.
|
||
307 | * Must not be NULL.
|
||
308 | *
|
||
309 | * @return The flags that had been set at the listener.
|
||
310 | */
|
||
311 | urt_osEventFlags_t urtEventListenerClearFlags(urt_osEventListener_t* listener); |
||
312 | #endif /* !defined(urtEventListenerClearFlags) */ |
||
313 | |||
314 | #if !defined(urtEventRegister) || defined(__DOXYGEN__)
|
||
315 | /**
|
||
316 | * @brief Register an event source to a listener.
|
||
317 | *
|
||
318 | * @param[in] source Pointer to the event source to register.
|
||
319 | * Must not be NULL.
|
||
320 | * @param[in] listener Pointer to the event listener to register.
|
||
321 | * Must not be NULL.
|
||
322 | * @param[in] mask Mask to be set at the listening thread on an event.
|
||
323 | * @param[in] flags Flags to be set by default at the listener on an event,
|
||
324 | */
|
||
325 | void urtEventRegister(urt_osEventSource_t* source, urt_osEventListener_t* listener, urt_osEventMask_t mask, urt_osEventFlags_t flags);
|
||
326 | #endif /* !defined(urtEventRegister) */ |
||
327 | |||
328 | #if !defined(urtEventUnregister) || defined(__DOXYGEN__)
|
||
329 | /**
|
||
330 | * @brief Unregister an associated event source and listener.
|
||
331 | *
|
||
332 | * @param[in] source Pointer to the event source to unregister.
|
||
333 | * Must not be NULL.
|
||
334 | * @param[in] listener Pointer to the event listener to unregister.
|
||
335 | * Must not be NULL.
|
||
336 | */
|
||
337 | void urtEventUnregister(urt_osEventSource_t* source, urt_osEventListener_t* listener);
|
||
338 | #endif /* !defined(urtEventUnregister) */ |
||
339 | |||
340 | #if !defined(urtEventWait) || defined(__DOXYGEN__)
|
||
341 | /**
|
||
342 | * @brief Wait for one/any/all event(s) to be recieved or a timeout.
|
||
343 | *
|
||
344 | * @param[in] mask The mask of event to wait for.
|
||
345 | * @param[in] type Specificator whether to wait for exactly one, any combination or all of the specified mask flags.
|
||
346 | * @param[in] timeout Timeout when the function shall return even if no event was recieved.
|
||
347 | *
|
||
348 | * @return The recieved event mask.
|
||
349 | */
|
||
350 | urt_osEventMask_t urtEventWait(urt_osEventMask_t mask, urt_osEventWait_t type, urt_delay_t timeout); |
||
351 | #endif /* !defined(urtEventWait) */ |
||
352 | |||
353 | #if defined(__cplusplus)
|
||
354 | } |
||
355 | #endif /* defined(__cplusplus) */ |
||
356 | |||
357 | /*============================================================================*/
|
||
358 | /* STREAMS */
|
||
359 | /*============================================================================*/
|
||
360 | |||
361 | #if defined(__cplusplus)
|
||
362 | extern "C" { |
||
363 | #endif /* defined(__cplusplus) */ |
||
364 | |||
365 | #if !defined(urtPrintf) || defined(__DOXYGEN__)
|
||
366 | /**
|
||
367 | * @brief Prints a formatted string to standard output.
|
||
368 | *
|
||
369 | * @param[in] fmt The formatted string to be printed.
|
||
370 | *
|
||
371 | * @return Number of characters printed.
|
||
372 | */
|
||
373 | int urtPrintf(char* fmt, ...); |
||
374 | #endif /* !defined(urtPrintf) */ |
||
375 | |||
376 | #if !defined(urtErrPrintf) || defined(__DOXYGEN__)
|
||
377 | /**
|
||
378 | * @brief Prints a formatted string to error output.
|
||
379 | *
|
||
380 | * @param[in] fmt The formatted string to be printed.
|
||
381 | *
|
||
382 | * @return Number of characters printed.
|
||
383 | */
|
||
384 | int urtErrPrintf(char* fmt, ...); |
||
385 | #endif /* !defined(urtErrPrintf) */ |
||
386 | |||
387 | #if defined(__cplusplus)
|
||
388 | } |
||
389 | #endif /* defined(__cplusplus) */ |
||
390 | |||
391 | /*============================================================================*/
|
||
392 | /* TIME */
|
||
393 | /*============================================================================*/
|
||
394 | |||
395 | /*
|
||
396 | * The following type must be defined by the implementation:
|
||
397 | *
|
||
398 | * urt_osTime_t
|
||
399 | * Type to represent a time object.
|
||
400 | * This type may be of any precision.
|
||
401 | * Is only used via pointers by the API.
|
||
402 | */
|
||
403 | |||
404 | #if defined(__cplusplus)
|
||
405 | extern "C" { |
||
406 | #endif /* defined(__cplusplus) */ |
||
407 | |||
408 | #if !defined(urtTime2Us) || defined(__DOXYGEN__)
|
||
409 | /**
|
||
410 | * @brief Convert an OS time to microsecond value.
|
||
411 | *
|
||
412 | * @param[in] t The time to convert.
|
||
413 | * Must not be NULL
|
||
414 | *
|
||
415 | * @return The equivalent time in microseconds.
|
||
416 | */
|
||
417 | uint64_t urtTime2Us(urt_osTime_t* t); |
||
418 | #endif /* !defined(urtTime2Us) */ |
||
419 | |||
420 | #if !defined(urtTimeNow) || defined(__DOXYGEN__)
|
||
421 | /**
|
||
422 | * @brief Get the current system time.
|
||
423 | *
|
||
424 | * @return Current system time as provide by the operating system.
|
||
425 | */
|
||
426 | urt_osTime_t urtTimeNow(void);
|
||
427 | #endif /* !defined(urtTimeNow) */ |
||
428 | |||
429 | #if !defined(urtTimeAddUs) || defined(__DOXYGEN__)
|
||
430 | /**
|
||
431 | * @brief Add microseconds to a specified system time.
|
||
432 | *
|
||
433 | * @param[in] time Pointer to the time value to be incremented.
|
||
434 | * Must not be NULL.
|
||
435 | * @param[in] offset Amount of microseconds to add.
|
||
436 | */
|
||
437 | void urtTimeAddUs(urt_osTime_t* time, urt_delay_t offset);
|
||
438 | #endif /* !defined(urtTimeAddUs) */ |
||
439 | |||
440 | #if defined(__cplusplus)
|
||
441 | } |
||
442 | #endif /* defined(__cplusplus) */ |
||
443 | |||
444 | /*============================================================================*/
|
||
445 | /* THREAD */
|
||
446 | /*============================================================================*/
|
||
447 | |||
448 | /*
|
||
449 | * The following types, constants and macros must be defined by the
|
||
450 | * implementation:
|
||
451 | *
|
||
452 | * urt_osThread_t
|
||
453 | * Type to represent an thread object.
|
||
454 | * Is only used via pointers by the API.
|
||
455 | *
|
||
456 | * urt_osThreadPrio_t
|
||
457 | * Integer type to represent thread priorities.
|
||
458 | *
|
||
459 | * URT_THREAD_PRIO_LOW_MIN
|
||
460 | * Constant of type urt_osThreadPrio_t, specifying the minimum priority for
|
||
461 | * low-priority threads.
|
||
462 | * Must be greater than the idle thread priority.
|
||
463 | *
|
||
464 | * URT_THREAD_PRIO_LOW_MAX
|
||
465 | * Constant of type urt_osThreadPrio_t, specifying the maximum priority for
|
||
466 | * low-priority threads.
|
||
467 | * Must be greater or equal URT_THREAD_PRIO_LOW_MIN.
|
||
468 | *
|
||
469 | * URT_THREAD_PRIO_NORMAL_MIN
|
||
470 | * Constant of type urt_osThreadPrio_t, specifying the minimum priority for
|
||
471 | * normal-priority threads.
|
||
472 | * Must be greater than URT_THREAD_PRIO_LOW_MAX.
|
||
473 | *
|
||
474 | * URT_THREAD_PRIO_NORMAL_MAX
|
||
475 | * Constant of type urt_osThreadPrio_t, specifying the maximum priority for
|
||
476 | * normal-priority threads.
|
||
477 | * Must be greater or equal URT_THREAD_PRIO_NORMAL_MIN.
|
||
478 | *
|
||
479 | * URT_THREAD_PRIO_HIGH_MIN
|
||
480 | * Constant of type urt_osThreadPrio_t, specifying the minimum priority for
|
||
481 | * high-priority threads.
|
||
482 | * Must be greater than URT_THREAD_PRIO_NORMAL_MAX.
|
||
483 | *
|
||
484 | * URT_THREAD_PRIO_HIGH_MAX
|
||
485 | * Constant of type urt_osThreadPrio_t, specifying the maximum priority for
|
||
486 | * high-priority threads.
|
||
487 | * Must be greater or equal URT_THREAD_PRIO_HIGH_MIN.
|
||
488 | *
|
||
489 | * URT_THREAD_PRIO_RT_MIN
|
||
490 | * Constant of type urt_osThreadPrio_t, specifying the minimum priority for
|
||
491 | * real-time threads.
|
||
492 | * Must be greater than URT_THREAD_PRIO_HIGH_MAX.
|
||
493 | *
|
||
494 | * URT_THREAD_PRIO_RT_MAX
|
||
495 | * Constant of type urt_osThreadPrio_t, specifying the maximum priority for
|
||
496 | * real-time threads.
|
||
497 | * Must be greater or equal URT_THREAD_PRIO_HIGH_MIN but lower than the
|
||
498 | * control/root thread priority.
|
||
499 | *
|
||
500 | * URT_THREAD_SLEEP_MAX
|
||
501 | * Constant of type float specifying the maximum time (in seconds) a thread
|
||
502 | * may sleep via the urtThreadSleep() function.
|
||
503 | *
|
||
504 | * URT_THREAD_SSLEEP_MAX
|
||
505 | * Constant of type urt_delay_t specifying the maximum number of seconds a
|
||
506 | * thread may sleep via the urtThreadSSleep() function.
|
||
507 | *
|
||
508 | * URT_THREAD_MSLEEP_MAX
|
||
509 | * Constant of type urt_delay_t specifying the maximum number of milliseconds
|
||
510 | * a thread may sleep via the urtThreadMSleep() function.
|
||
511 | *
|
||
512 | * URT_THREAD_USLEEP_MAX
|
||
513 | * Constant of type urt_delay_t specifying the maximum number of microseconds
|
||
514 | * a thread may sleep via the urtThreadUSleep() function.
|
||
515 | *
|
||
516 | * URT_THREAD_MEMORY(varname, stacksize)
|
||
517 | * Macro function to staically allocate a variable 'varname', which holds all
|
||
518 | * memory required to hold a thread object with the specified stacksize.
|
||
519 | */
|
||
520 | |||
521 | /**
|
||
522 | * @brief Enum type for various thread states.
|
||
523 | */
|
||
524 | typedef enum { |
||
525 | URT_THREAD_STATE_INACTIVE = 0, /**< The thread has not been started yet. */ |
||
526 | URT_THREAD_STATE_RUNNING = 1, /**< The thread is currently being executed on a CPU. */ |
||
527 | URT_THREAD_STATE_READY = 2, /**< The thread is ready to be scheduled for execution. */ |
||
528 | URT_THREAD_STATE_SLEEPING = 3, /**< The thread is currently sleeping for a defined amount of time. */ |
||
529 | URT_THREAD_STATE_SUSPENDED = 4, /**< Execution of the thread has been suspended. */ |
||
530 | URT_THREAD_STATE_WAITING = 5, /**< The thread is currently waiting for an event. */ |
||
531 | URT_THREAD_STATE_TERMINATED = 6, /**< The thread has been terminated. */ |
||
532 | } urt_osThreadState_t; |
||
533 | |||
534 | /**
|
||
535 | * @brief Enum type for various termination signals.
|
||
536 | */
|
||
537 | typedef enum { |
||
538 | URT_THREAD_TERMINATE_REQUEST = 15, /**< Request a thread to terminate in a cooperative manner. */ |
||
539 | URT_THREAD_TERMINATE_KILL = 9, /**< Terminate a thread in a non-cooperative manner (force). */ |
||
540 | } urt_osThreadTerminateSignal_t; |
||
541 | |||
542 | /**
|
||
543 | * @brief Thread function.
|
||
544 | *
|
||
545 | * @param[in] arg Pointer to optional arguments.
|
||
546 | * May be NULL.
|
||
547 | */
|
||
548 | typedef void (*urt_osThreadFunction_t)(void* arg); |
||
549 | |||
550 | #if defined(__cplusplus)
|
||
551 | extern "C" { |
||
552 | #endif /* defined(__cplusplus) */ |
||
553 | |||
554 | #if !defined(urtThreadInit) || defined(__DOXYGEN__)
|
||
555 | /**
|
||
556 | * @brief Initialize a thread object.
|
||
557 | * @details The new thread becomes a child of the calling thread.
|
||
558 | *
|
||
559 | * @param[in] memory Pointer to the memory where to create the thread object.
|
||
560 | * Must not be NULL.
|
||
561 | * @param[in] size Size of the memory.
|
||
562 | * @param[in] prio Initial priority of the thread.
|
||
563 | * @param[in] func Pointer to the thread function the thread shall execute.
|
||
564 | * Must not be NULL.
|
||
565 | * @param[in] arg Pointer to optional function arguments.
|
||
566 | * May be NULL.
|
||
567 | *
|
||
568 | * @return Pointer to the created thread object.
|
||
569 | */
|
||
570 | urt_osThread_t* urtThreadInit(void* memory, size_t size, urt_osThreadPrio_t prio, urt_osThreadFunction_t func, void* arg); |
||
571 | #endif /* !defined(urtThreadInit) */ |
||
572 | |||
573 | #if !defined(urtThreadStart) || defined(__DOXYGEN__)
|
||
574 | /**
|
||
575 | * @brief Start execution of a thread.
|
||
576 | *
|
||
577 | * @param[in] thread Pointer to the thread to start.
|
||
578 | * Must not be NULL.
|
||
579 | */
|
||
580 | void urtThreadStart(urt_osThread_t* thread);
|
||
581 | #endif /* !defined(urtThreadStart) */ |
||
582 | |||
583 | #if !defined(urtThreadYield) || defined(__DOXYGEN__)
|
||
584 | /**
|
||
585 | * @brief Yield execution of the calling thread.
|
||
586 | */
|
||
587 | void urtThreadYield(void); |
||
588 | #endif /* !defined(urtThreadYield) */ |
||
589 | |||
590 | #if !defined(urtThreadGetPriority) || defined(__DOXYGEN__)
|
||
591 | /**
|
||
592 | * @brief Retrieve the current priority of the calling thread.
|
||
593 | *
|
||
594 | * @return Current priority.
|
||
595 | */
|
||
596 | urt_osThreadPrio_t urtThreadGetPriority(void);
|
||
597 | #endif /* !defined(urtThreadGetPriority) */ |
||
598 | |||
599 | #if !defined(urtThreadSetPriority) || defined(__DOXYGEN__)
|
||
600 | /**
|
||
601 | * @brief Set the priority of the calling thread.
|
||
602 | *
|
||
603 | * @param[in] prio The new priority to set.
|
||
604 | */
|
||
605 | void urtThreadSetPriority(urt_osThreadPrio_t prio);
|
||
606 | #endif /* !defined(urtThreadSetPriority) */ |
||
607 | |||
608 | #if !defined(urtThreadSleep) || defined(__DOXYGEN__)
|
||
609 | /**
|
||
610 | * @brief The calling thread sleeps for the given amount of time.
|
||
611 | *
|
||
612 | * @param[in] seconds Time in seconds to sleep.
|
||
613 | * Must not be greater than URT_THREAD_SLEEP_MAX.
|
||
614 | */
|
||
615 | void urtThreadSleep(float seconds); |
||
616 | #endif /* !defined(urtThreadSleep) */ |
||
617 | |||
618 | #if !defined(urtThreadSSleep) || defined(__DOXYGEN__)
|
||
619 | /**
|
||
620 | * @brief The calling thread sleeps for the given amount of seconds.
|
||
621 | *
|
||
622 | * @param[in] seconds Time in seconds to sleep.
|
||
623 | * Must not be greater than URT_THREAD_SSLEP_MAX.
|
||
624 | */
|
||
625 | void urtThreadSSleep(unsigned int seconds); |
||
626 | #endif /* !defined(urtThreadSSleep) */ |
||
627 | |||
628 | #if !defined(urtThreadMSleep) || defined(__DOXYGEN__)
|
||
629 | /**
|
||
630 | * @brief The calling thread sleeps for the given amount of milliseconds.
|
||
631 | *
|
||
632 | * @param[in] milliseconds Time in milliseconds to sleep.
|
||
633 | * Must not be greater than URT_THREAD_MSLEP_MAX.
|
||
634 | */
|
||
635 | void urtThreadMSleep(unsigned int milliseconds); |
||
636 | #endif /* !defined(urtThreadMSleep) */ |
||
637 | |||
638 | #if !defined(urtThreadUSleep) || defined(__DOXYGEN__)
|
||
639 | /**
|
||
640 | * @brief The calling thread sleeps for the gven amount of microseconds.
|
||
641 | *
|
||
642 | * @param[in] microseconds Time in microseconds to sleep.
|
||
643 | * Must not be greater than URT_THREAD_USLEP_MAX.
|
||
644 | */
|
||
645 | void urtThreadUSleep(urt_delay_t microseconds);
|
||
646 | #endif /* !defined(urtThreadUSleep) */ |
||
647 | |||
648 | #if !defined(urtThreadSleepUntil) || defined(__DOXYGEN__)
|
||
649 | /**
|
||
650 | * @brief The calling thread sleeps until the given point in time.
|
||
651 | *
|
||
652 | * @param[in] time Time when the thread shall wake up.
|
||
653 | * If the current is already beyond the argument, the thread will not sleep at all.
|
||
654 | */
|
||
655 | void urtThreadSleepUntil(urt_osTime_t time);
|
||
656 | #endif /* !defined(urtThreadSleepUntil) */ |
||
657 | |||
658 | #if !defined(urtThreadExit) || defined(__DOXYGEN__)
|
||
659 | /**
|
||
660 | * @brief The calling thread stops execution and terminates.
|
||
661 | */
|
||
662 | void urtThreadExit(void); |
||
663 | #endif /* !defined(urtThreadExit) */ |
||
664 | |||
665 | #if !defined(urtThreadTerminate) || defined(__DOXYGEN__)
|
||
666 | /**
|
||
667 | * @brief Send a termination signal to a thread.
|
||
668 | *
|
||
669 | * @param[in] thread Thread to be signaled.
|
||
670 | * Must not be NULL.
|
||
671 | * @param[in] sig Type of termination.
|
||
672 | */
|
||
673 | void urtThreadTerminate(urt_osThread_t* thread, urt_osThreadTerminateSignal_t sig);
|
||
674 | #endif /* !defined(urtThreadTerminate) */ |
||
675 | |||
676 | 6c5df8c1 | Thomas Schöpping | #if !defined(urtThreadShouldTerminate) || defined(__DOXYGEN__)
|
677 | /**
|
||
678 | * @brief Retrieve whether the calling thread has been requested to terminate.
|
||
679 | *
|
||
680 | * @return Indicator, whether the thread shoud terminate.
|
||
681 | */
|
||
682 | bool urtThreadShouldTerminate(void); |
||
683 | #endif /* !defined(urtThreadShouldTerminate) */ |
||
684 | |||
685 | 46471486 | Thomas Schöpping | #if !defined(urtThreadJoin) || defined(__DOXYGEN__)
|
686 | /**
|
||
687 | * @brief Wait for a thread to terminate.
|
||
688 | *
|
||
689 | * @param[in] thread Pointer to the thread to wait for.
|
||
690 | * Must not be NULL.
|
||
691 | */
|
||
692 | void urtThreadJoin(urt_osThread_t* thread);
|
||
693 | #endif /* !defined(urtThreadJoin) */ |
||
694 | |||
695 | #if !defined(urtThreadGetState) || defined(__DOXYGEN__)
|
||
696 | /**
|
||
697 | * @brief Get the current execution status of a thread.
|
||
698 | *
|
||
699 | * @param[in] thread Pointer to the thread to get the status for.
|
||
700 | * Must not be NULL.
|
||
701 | *
|
||
702 | * @return Execution status of the secified thread.
|
||
703 | */
|
||
704 | urt_osThreadState_t urtThreadGetState(urt_osThread_t* thread); |
||
705 | #endif /* !defined(urtThreadGetState) */ |
||
706 | |||
707 | #if !defined(urtThreadGetSelf) || defined(__DOXYGEN__)
|
||
708 | /**
|
||
709 | * @brief Retrieve the thread object of the calling thread.
|
||
710 | *
|
||
711 | * @return Pointer to the thread that is currrently executed.
|
||
712 | */
|
||
713 | urt_osThread_t* urtThreadGetSelf(void);
|
||
714 | #endif /* !defined(urtThreadGetSelf) */ |
||
715 | |||
716 | #if !defined(urtThreadGetChildren) || defined(__DOXYGEN__)
|
||
717 | /**
|
||
718 | * @brief Retrieve the first child of a thread.
|
||
719 | *
|
||
720 | * @param[in] thread Pointer to the thread to retrieve the child from.
|
||
721 | * Must not be NULL.
|
||
722 | *
|
||
723 | * @return Pointer to the first child thread.
|
||
724 | * May be NULL if the thread has no children.
|
||
725 | */
|
||
726 | urt_osThread_t* urtThreadGetChildren(urt_osThread_t* thread); |
||
727 | #endif /* !defined(urtThreadGetChildren) */ |
||
728 | |||
729 | #if !defined(urtThreadGetSibling) || defined(__DOXYGEN__)
|
||
730 | /**
|
||
731 | * @brief Retrieve the next sibling of a thread.
|
||
732 | *
|
||
733 | * @param[in] thread Pointer to the thread to retrieve the sibling from.
|
||
734 | *
|
||
735 | * @return Pointer to the next sibling of the thread.
|
||
736 | * May be NULL if the thread does not have any more siblings.
|
||
737 | */
|
||
738 | urt_osThread_t* urtThreadGetSibling(urt_osThread_t* thread); |
||
739 | #endif /* !defined(urtThreadGetSibling) */ |
||
740 | |||
741 | #if !defined(urtThreadGetParent) || defined(__DOXYGEN__)
|
||
742 | /**
|
||
743 | * @brief Retrieve the parent of a thread.
|
||
744 | *
|
||
745 | * @param[in] thread Pointer to the thread to retrieve the parent from.
|
||
746 | *
|
||
747 | * @return Pointer to the parent thread.
|
||
748 | * May be NULL if the specified thread is root.
|
||
749 | */
|
||
750 | urt_osThread_t* urtThreadGetParent(urt_osThread_t* thread); |
||
751 | #endif /* !defined(urtThreadGetParent) */ |
||
752 | |||
753 | #if defined(__cplusplus)
|
||
754 | } |
||
755 | #endif /* defined(__cplusplus) */ |
||
756 | |||
757 | /*============================================================================*/
|
||
758 | /* TIMER */
|
||
759 | /*============================================================================*/
|
||
760 | |||
761 | /*
|
||
762 | * The following type must be defined by the implementation:
|
||
763 | *
|
||
764 | * urt_osTimer_t
|
||
765 | * Type to represent a timer object.
|
||
766 | * Is only used via pointers by the API.
|
||
767 | */
|
||
768 | |||
769 | /**
|
||
770 | * @brief Timer callback function type.
|
||
771 | * @details A timer executes a callback when its time ran out and it fires.
|
||
772 | *
|
||
773 | * @param[in] params Pointer to optional parameters for the callback function.
|
||
774 | * May be NULL.
|
||
775 | */
|
||
776 | typedef void (*urt_osTimerCallback_t)(void* parameter); |
||
777 | |||
778 | #if defined(__cplusplus)
|
||
779 | extern "C" { |
||
780 | #endif /* defined(__cplusplus) */ |
||
781 | |||
782 | #if !defined(urtTimerInit) || defined(__DOXYGEN__)
|
||
783 | /**
|
||
784 | * @brief Initialize a timer object.
|
||
785 | *
|
||
786 | * @param[in] timer Pointer to the timer object to initialize.
|
||
787 | * Must not be NULL.
|
||
788 | */
|
||
789 | void urtTimerInit(urt_osTimer_t* timer);
|
||
790 | #endif /* !defined(urtTimerInit) */ |
||
791 | |||
792 | #if !defined(urtTimerSet) || defined(__DOXYGEN__)
|
||
793 | /**
|
||
794 | * @brief Set a timer in single-shot mode.
|
||
795 | *
|
||
796 | * @param[in] timer Pointer to the timer to be armed.
|
||
797 | * Must not be NULL.
|
||
798 | * @param[in] delay Time after which the timer shall fire.
|
||
799 | * @param[in] callback Pointer to the function to be called when the timer fires.
|
||
800 | * Must not be NULL.
|
||
801 | * @param[in] cbparams Optional parameters for the callback function.
|
||
802 | * May be NULL.
|
||
803 | */
|
||
804 | void urtTimerSet(urt_osTimer_t* timer, urt_delay_t delay, urt_osTimerCallback_t callback, void* cbparams); |
||
805 | #endif /* !defined(urtTimerSet) */ |
||
806 | |||
807 | #if !defined(urtTimerSetPeriodic) || defined(__DOXYGEN__)
|
||
808 | /**
|
||
809 | * @brief Set a timer in periodic mode.
|
||
810 | *
|
||
811 | * @param[in] timer Pointer to the timer to be armed.
|
||
812 | * Must not be NULL.
|
||
813 | * @param[in] period Time after which the timer shall fire periodically.
|
||
814 | * @param[in] callback Pointer to the function to be called each time the timer fires.
|
||
815 | * Must not be NULL.
|
||
816 | * @param[in] cbparams Optional parameters for the callback function.
|
||
817 | * May be NULL.
|
||
818 | */
|
||
819 | void urtTimerSetPeriodic(urt_osTimer_t* timer, urt_delay_t period, urt_osTimerCallback_t callback, void* cbparams); |
||
820 | #endif /* !defined(urtTimerSetPeriodic) */ |
||
821 | |||
822 | #if !defined(urtTimerReset) || defined(__DOXYGEN__)
|
||
823 | /**
|
||
824 | * @brief Reset and disarm a timer.
|
||
825 | *
|
||
826 | * @param[in] timer Pointer to the timer to be reset.
|
||
827 | * Must not be NULL.
|
||
828 | */
|
||
829 | void urtTimerReset(urt_osTimer_t* timer);
|
||
830 | #endif /* !defined(urtTimerReset) */ |
||
831 | |||
832 | #if !defined(urtTimerIsArmed) || defined(__DOXYGEN__)
|
||
833 | /**
|
||
834 | * @brief Retrieve the current status of a timer.
|
||
835 | *
|
||
836 | * @param[in] timer Pointer to the timer to inspect.
|
||
837 | * Must not be NULL.
|
||
838 | *
|
||
839 | * @return Flag, indicating whether or not the timer is currently armed.
|
||
840 | */
|
||
841 | bool urtTimerIsArmed(urt_osTimer_t* timer);
|
||
842 | #endif /* !defined(urtTimerIsArmed) */ |
||
843 | |||
844 | #if defined(__cplusplus)
|
||
845 | } |
||
846 | #endif /* defined(__cplusplus) */ |
||
847 | |||
848 | #endif /* URT_OSAL_H */ |