1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 // ==== Thread Management ====
4 \addtogroup CMSIS_RTOS_ThreadMgmt Thread Management
5 \ingroup CMSIS_RTOS CMSIS_RTOSv2
6 \brief Define, create, and control thread functions.
8 The Thread Management function group allows defining, creating, and controlling thread functions in the system. The function
9 \b main is a special thread function that is started at system initialization and has the initial priority
13 Threads can be in the following states:
14 - \b RUNNING: The thread that is currently running is in the \b RUNNING state. Only one thread at a time can be in this
16 - \b READY: Threads which are ready to run are in the \b READY state. Once the \b RUNNING thread has terminated or is
17 \b WAITING, the next \b READY thread with the highest priority becomes the \b RUNNING thread.
18 - \b WAITING: Threads that are waiting for an event to occur are in the \b WAITING state.
19 - \b INACTIVE: Threads that are not created or terminated are in the \b INACTIVE state. These threads typically consume no
22 \image html "ThreadStatus.png" "Thread State and State Transitions"
24 A CMSIS-RTOS assumes that threads are scheduled as shown in the figure <b>Thread State and State Transitions</b>. The thread
25 states change as follows:
26 - A thread is created using the function \ref osThreadNew. This puts the thread into the \b READY or \b RUNNING state
27 (depending on the thread priority).
28 - CMSIS-RTOS is pre-emptive. The active thread with the highest priority becomes the \b RUNNING thread provided it does not
29 wait for any event. The initial priority of a thread is defined with the \ref osThreadAttr_t but may be changed during
30 execution using the function \ref osThreadSetPriority.
31 - The \b RUNNING thread transfers into the \b WAITING state when it is waiting for an event.
32 - Active threads can be terminated any time using the function \ref osThreadTerminate. Threads can terminate also by just
33 returning from the thread function. Threads that are terminated are in the \b INACTIVE state and typically do not consume
34 any dynamic memory resources.
38 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
43 MUST REMAIN UNCHANGED: the enumeration shall be consistent in every CMSIS-RTOS.
46 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
51 MUST REMAIN UNCHANGED: the enumeration shall be consistent in every CMSIS-RTOS.
53 The \b osPriority_t value specifies the priority for a thread. The default thread priority should be \a osPriorityNormal.
54 If a Thread is active that has a higher priority than the currently executing thread, then a thread switch occurs immediately
55 to execute the new task.
57 To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a <b>priority inheritance</b> method.
58 A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a thread
59 with a lower priority.
62 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
65 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
67 \typedef void *(*os_thread_func_t) (void *argument)
72 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
77 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
79 \struct osThreadAttr_t
82 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
93 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
95 \fn osThreadId_t osThreadNew (os_thread_func_t func, void *argument, const osThreadAttr_t *attr)
98 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
100 Start a thread function by adding it to the Active Threads list and set it to state \b READY. Arguments fo the thread function are passed
101 using the parameter pointer \em *argument. When the priority of the created thread function is higher than the current \b RUNNING thread,
102 the created thread function starts instantly and becomes the new \b RUNNING thread. Thread sttributes are defined with the parameter pointer \em attr.
103 Attributes include settings for thread priority, stack size, or memory allocation.
106 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
110 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
112 \fn osThreadId_t osThreadGetId (void)
115 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
117 Get the thread ID of the current running thread.
120 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
124 void ThreadGetId_example (void) {
125 osThreadId id; // id for the currently running thread
127 id = osThreadGetId ();
129 // Failed to get the id; not in a thread
134 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
136 \fn osThreadState_t osThreadGetState (osThreadId_t thread_id)
139 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
141 Return the state of the thread identified by parameter \em thread_id.
142 See \ref osThreadState_t for possible states.
145 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
147 \fn osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority)
150 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
152 Change the priority of an active thread.
154 \ref CMSIS_RTOS_Status
155 - \em osOK: the priority of the specified thread has been changed successfully.
156 - \em osErrorParameter: the value of the parameter \em thread_id or parameter \em priority is incorrect.
157 - \em osErrorResource: parameter \em thread_id refers to a thread that is not an active thread.
158 - \em osErrorISR: the function \b osThreadSetPriority cannot be called from interrupt service routines.
161 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
165 #include "cmsis_os2.h"
167 void Thread_1 (void const *arg) { // Thread function
168 osThreadId id; // id for the currently running thread
169 osPriority pr; // thread priority
170 osStatus status; // status of the executed function
173 id = osThreadGetId (); // Obtain ID of current running thread
176 status = osThreadSetPriority (id, osPriorityBelowNormal);
177 if (status == osOK) {
178 // Thread priority changed to BelowNormal
181 // Failed to set the priority
185 // Failed to get the id
191 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
193 \fn osPriority_t osThreadGetPriority (osThreadId_t thread_id)
196 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
198 Get the priority of an active thread. In case of failure, the value \b osPriorityError is returned.
201 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
205 #include "cmsis_os2.h"
207 void Thread_1 (void const *arg) { // Thread function
208 osThreadId id; // id for the currently running thread
209 osPriority priority; // thread priority
211 id = osThreadGetId (); // Obtain ID of current running thread
214 priority = osThreadGetPriority (id);
217 // Failed to get the id
222 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
224 \fn osStatus_t osThreadYield (void)
227 MUST REMAIN UNCHANGED: the function osThreadNew shall be consistent in every CMSIS-RTOS.
229 Pass control to the next thread that is in state \b READY. If there is no other thread in state \b READY,
230 then the current thread continues execution and no thread switching occurs.
233 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
237 #include "cmsis_os2.h"
239 void Thread_1 (void const *arg) { // Thread function
240 osStatus status; // status of the executed function
243 status = osThreadYield(); //
244 if (status != osOK) {
245 // thread switch not occurred, not in a thread function
251 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
253 \fn osStatus_t osThreadAbortWait (osThreadId_t thread_id)
255 Forces a thread in WAITING stated, specified with \em thread_id, to continue operation.
256 Functions that will put a thread into WAITING state are:
257 \ref osEventFlagsWait and \ref osThreadFlagsWait,
258 \ref osDelay and \ref osDelayUntil,
259 \ref osMutexAcquire and \ref osSemaphoreAcquire,
260 \ref osMessageQueueGet,
263 The return value of the waiting function is unspecified.
266 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
268 \fn osStatus_t osThreadSuspend (osThreadId_t thread_id)
271 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
273 Sets the thread identified by parameter \em thread_id into the state \em Suspended (\ref osThreadSuspended).
274 The thread is not executed until restarted with the function \ref osThreadResume.
277 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
279 \fn osStatus_t osThreadResume (osThreadId_t thread_id)
282 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
284 Restart a thread defined by parameter \em thread_id that has been stopped with \ref osThreadSuspend.
285 If the \b RUNNING thread has a higher priority, then the thread is put in state \b WAITING.
290 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
292 \fn osStatus_t osThreadDetach (osThreadId_t thread_id)
294 Changes the attribute of a thread specified in \em thread_id to \ref osThreadDetached. Detached threads are not joinable with \ref osThreadJoin.
295 When a detached thread is terminated all resources are returned to the system. The behaviour of \ref osThreadDetach on an already detached thread is undefined.
298 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
303 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
305 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
307 \fn osStatus_t osThreadJoin (osThreadId_t thread_id, void **exit_ptr)
309 Waits for the thread specified by \em thread_id to terminate.
310 If that thread has already terminated, then \ref osThreadJoin returns immediately.
311 The thread referred to by thread_id must joinable. By default threads are created with the attribute \ref osThreadJoinable. The thread may not have been detached by \ref osThreadDetach.
314 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
317 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
322 #include "cmsis_os2.h"
324 void *worker(void *arg)
331 int join_threads(void)
333 osThreadId_t th1, th2;
335 th1 = osThreadNew(worker, ¶m, NULL);
336 th2 = osThreadNew(worker, ¶m, NULL);
338 (void) osThreadJoin(th1, NULL);
339 (void) osThreadJoin(th2, NULL);
345 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
347 \fn __NO_RETURN void osThreadExit (void *exit_ptr)
350 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
352 Exit the \b RUNNING thread to an exit point defined by parameter pointer \em exit_ptr.
355 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
357 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
359 \fn osStatus_t osThreadTerminate (osThreadId_t thread_id)
362 MUST REMAIN UNCHANGED: the function shall be consistent in every CMSIS-RTOS.
364 Remove the thread function from the active thread list. If the thread is currently RUNNING the execution will stop.
367 Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
370 #include "cmsis_os2.h"
372 void *Thread_1 (void c*arg); // function prototype for Thread_1
374 void ThreadTerminate_example (void) {
378 id = osThreadNew (Thread_1, NULL, NULL); // create the thread
380 status = osThreadTerminate (id); // stop the thread
381 if (status == osOK) {
382 // Thread was terminated successfully
385 // Failed to terminate a thread
393 // these struct members must stay outside the group to avoid double entries in documentation
395 \var osThreadAttr_t::attr_bits
397 The following predefined bit masks can be assigned to set options for a thread object.
399 Bit Mask | Description
400 :-----------------------|:-----------------------------------------
401 osThreadJoinable | Thread is created in a joinable state (default).
402 osThreadDettached | Thread is created in a detached state.
404 \var osThreadAttr_t::cb_mem
406 Pointer to a memory location for the thread object. This can optionally be used for custom memory management systems.
407 Specifytoken{NULL} to use the kernel memory management.
409 \var osThreadAttr_t::cb_size
411 The size of the memory block passed with \ref cb_mem. Must be the size of a thread object or larger.
413 \var osThreadAttr_t::name
415 String with a human readable name of the thread object.
417 \var osThreadAttr_t::priority
419 Specifies the initial thread priority with a value from #osPriority_t.
421 \var osThreadAttr_t::reserved
423 Reserved for future use. Must be \token{0}.
425 \var osThreadAttr_t::stack_mem
427 Pointer to a memory location for the thread stack. This can optionally be used for custom memory management systems.
428 Specify \token{NULL} to use the kernel memory management.
430 \var osThreadAttr_t::stack_mem
432 The size of the stack specified by \ref stack_mem in Bytes.