]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/cmsis_os2_Thread.txt
Unified CMSIS-RTOS2 documentation for Thread and Thread Flags to reference to thread...
[cmsis] / CMSIS / DoxyGen / RTOS2 / src / cmsis_os2_Thread.txt
1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 //  ==== Thread Management ====
3 /** 
4 \addtogroup CMSIS_RTOS_ThreadMgmt Thread Management
5 \ingroup CMSIS_RTOS CMSIS_RTOSv2
6 \brief Define, create, and control thread functions.
7 \details 
8 The Thread Management function group allows defining, creating, and controlling thread functions in the system.
9
10 \note Thread management functions cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
11
12 \anchor ThreadStates
13 Thread states
14 -------------
15 Threads can be in the following states:
16
17  - \b RUNNING: The thread that is currently running is in the \ref ThreadStates "RUNNING" state. Only one thread at a time can be in this
18    state.
19  - \b READY: Threads which are ready to run are in the \ref ThreadStates "READY" state. Once the \ref ThreadStates "RUNNING" thread has terminated, or is
20    \ref ThreadStates "BLOCKED", the next \ref ThreadStates "READY" thread with the highest priority becomes the \ref ThreadStates "RUNNING" thread.
21  - \b BLOCKED: Threads that are blocked either delayed, waiting for an event to occur or suspended are in the \ref ThreadStates "BLOCKED"
22    state.
23  - \b TERMINATED: When \ref osThreadTerminate is called, threads are \ref ThreadStates "TERMINATED" with resources not yet released. 
24  - \b INACTIVE: Threads that are not created or have been terminated with all resources released are in the \ref ThreadStates "INACTIVE" state.
25  
26 \image html "ThreadStatus.png" "Thread State and State Transitions"
27
28
29 A CMSIS-RTOS assumes that threads are scheduled as shown in the figure <b>Thread State and State Transitions</b>. The thread
30 states change as follows:
31  - A thread is created using the function \ref osThreadNew. This puts the thread into the \ref ThreadStates "READY" or \ref ThreadStates "RUNNING" state
32    (depending on the thread priority).
33  - CMSIS-RTOS is pre-emptive. The active thread with the highest priority becomes the \ref ThreadStates "RUNNING" thread provided it does not
34    wait for any event. The initial priority of a thread is defined with the \ref osThreadAttr_t but may be changed during
35    execution using the function \ref osThreadSetPriority.
36  - The \ref ThreadStates "RUNNING" thread transfers into the \ref ThreadStates "BLOCKED" state when it is delayed, waiting for an event or suspended.
37  - Active threads can be terminated any time using the function \ref osThreadTerminate. Threads can terminate also by just
38    returning from the thread function. Threads that are terminated are in the \ref ThreadStates "INACTIVE" state and typically do not consume
39    any dynamic memory resources. 
40
41 \note 
42 Refer to \ref threadConfig for RTX5 configuration options.
43  
44 Thread Examples
45 ===============
46 The following examples show various scenarios to create threads:
47  
48 <b>Example 1 - Create a simple thread</b> 
49
50 Create a thread out of the function thread1 using all default values for thread attributes and memory from the
51 \ref GlobalMemoryPool.
52  
53 \code
54 __NO_RETURN void thread1 (void *argument) {
55   // ...
56   for (;;) {}
57 }
58  
59 int main (void) {
60   osKernelInitialize();
61   ;
62   osThreadNew(thread1, NULL, NULL);    // Create thread with default settings
63   ;
64   osKernelStart(); 
65 }
66 \endcode
67
68 <b>Example 2 - Create thread with stack non-default stack size</b>
69  
70 Similar to the simple thread all attributes are default. The stack is dynamically allocated from the \ref GlobalMemoryPool
71  
72 \ref osThreadAttr_t.stack_size is used to pass the stack size in Bytes to osThreadNew.
73
74 \code
75 __NO_RETURN void thread1 (void *argument) {
76   // ...
77   for (;;) {}
78 }
79  
80 const osThreadAttr_t thread1_attr = {
81   .stack_size = 1024                             // Create the thread stack with a size of 1024 bytes
82 };
83  
84 int main (void) {
85   ;  
86   osThreadNew(thread1, NULL, &thread1_attr);     // Create thread with custom sized stack memory
87   ;
88 }
89 \endcode
90
91 <b>Example 3 - Create thread with statically allocated stack</b>
92  
93 Similar to the simple thread all attributes are default. The stack is statically allocated using the \c uint64_t array
94 \c thread1_stk_1. This allocates 64*8 Bytes (=512 Bytes) with an alignment of 8 Bytes (mandatory for Cortex-M stack memory). 
95  
96 \ref osThreadAttr_t.stack_mem holds a pointer to the stacks lowest address. 
97  
98 \ref osThreadAttr_t.stack_size is used to pass the stack size in Bytes to osThreadNew.
99
100 \code
101 __NO_RETURN void thread1 (void *argument) {
102   // ...
103   for (;;) {}
104 }
105  
106 static uint64_t thread1_stk_1[64];
107  
108 const osThreadAttr_t thread1_attr = {
109   .stack_mem  = &thread1_stk_1[0],
110   .stack_size = sizeof(thread1_stk_1)
111 };
112  
113 int main (void) {
114   ;  
115   osThreadNew(thread1, NULL, &thread1_attr);    // Create thread with statically allocated stack memory
116   ;
117 }
118 \endcode
119
120 <b>Example 4 - Thread with statically allocated task control block</b>
121  
122 Typically this method is chosen together with a statically allocated stack as shown in Example 2. 
123 \code 
124 #include "cmsis_os2.h"
125  
126 //include rtx_os.h for types of RTX objects
127 #include "rtx_os.h"
128
129 __NO_RETURN void thread1 (void *argument) {
130   // ...
131   for (;;) {}
132 }
133  
134 static osRtxThread_t thread1_tcb;
135  
136 const osThreadAttr_t thread1_attr = {
137   .cb_mem  = &thread1_tcb,
138   .cb_size = sizeof(thread1_tcb),
139 };
140  
141 int main (void) {
142   ;
143   osThreadNew(thread1, NULL, &thread1_attr);    // Create thread with custom tcb memory
144   ;
145 }
146 \endcode
147
148 <b>Example 5 - Create thread with a different priority</b> 
149  
150 The default priority of RTX is \ref osPriorityNormal. Often you want to run a task with a higher or lower priority. Using the
151 \ref osThreadAttr_t control structure you can set any initial priority required.
152
153 \code
154 __NO_RETURN void thread1 (void *argument) {
155   // ...
156   for (;;) {}
157 }
158  
159 const osThreadAttr_t thread1_attr = {
160   .priority = osPriorityHigh            //Set initial thread priority to high   
161 };
162  
163 int main (void) {
164   ;
165   osThreadNew(thread1, NULL, &thread1_attr);   
166   ;
167 }
168 \endcode
169
170 <b>Example 6 - Joinable threads</b>
171  
172 In this example a master thread creates four threads with the \ref osThreadJoinable attribute. These will do some work and
173 return using the \ref osThreadExit call after finished. \ref osThreadJoin is used to synchronize the thread termination. 
174
175
176 \code 
177 __NO_RETURN void worker (void *argument) {     
178     ; // work a lot on data[] 
179     osDelay(1000);       
180     osThreadExit();
181 }
182  
183 __NO_RETURN void thread1 (void *argument) {
184   osThreadAttr_t worker_attr;
185   osThreadId_t worker_ids[4];
186   uint8_t data[4][10];
187
188   memset(&worker_attr, 0, sizeof(worker_attr));
189   worker_attr.attr_bits = osThreadJoinable;
190   
191   worker_ids[0] = osThreadNew(worker, &data[0][0], &worker_attr);    
192   worker_ids[1] = osThreadNew(worker, &data[1][0], &worker_attr);    
193   worker_ids[2] = osThreadNew(worker, &data[2][0], &worker_attr);    
194   worker_ids[3] = osThreadNew(worker, &data[3][0], &worker_attr);    
195  
196   osThreadJoin(worker_ids[0]);
197   osThreadJoin(worker_ids[1]);
198   osThreadJoin(worker_ids[2]);
199   osThreadJoin(worker_ids[3]);
200  
201   osThreadExit(); 
202 }
203 \endcode
204    
205 @{
206 */
207 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
208 /**
209 \enum osThreadState_t
210 \details
211 State of a thread as retrieved by \ref osThreadGetState. In case \b osThreadGetState fails or if it is called from an ISR, it
212 will return \c osThreadError, otherwise it returns the thread state.
213
214 \var osThreadState_t::osThreadInactive
215 \details The thread is created but not actively used, or has been terminated.
216
217 \var osThreadState_t::osThreadReady
218 \details The thread is ready for execution but not currently running.
219
220 \var osThreadState_t::osThreadRunning
221 \details The thread is currently running.
222
223 \var osThreadState_t::osThreadBlocked
224 \details The thread is currently blocked (delayed, waiting for an event or suspended).
225
226 \var osThreadState_t::osThreadTerminated
227 \details The thread is terminated and all its resources are freed.
228
229 \var osThreadState_t::osThreadError
230 \details The thread thread has raised an error condition and cannot be scheduled.
231 */
232
233 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
234 /**
235 \enum osPriority_t
236 \details
237
238 The \b osPriority_t value specifies the priority for a thread. The default thread priority should be \a osPriorityNormal.
239 If an active thread becomes ready that has a higher priority than the currently running thread then a thread switch occurs
240 immediately. The system continues executing the thread with the higher priority.
241
242 To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a <b>priority inheritance</b>
243 method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a
244 thread with a lower priority. Thus causing the high priority thread potentially beeing blocked forever by another thread
245 with lower priority. To come over this issue the low priority thread controlling the resource should be treated as having
246 the higher priority until it releases the resource.
247
248 \note Priority inheritance only applies to mutexes.
249
250 \var osPriority_t::osPriorityIdle
251 \details This lowest priority should not be used for any other thread. 
252
253 \var osPriority_t::osPriorityISR 
254 \details This highest priority might be used by the RTOS implementation but must not be used for any user thread.
255 */
256
257 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
258 /**
259 \typedef void (*osThreadFunc_t) (void *argument)
260 \details Entry function for threads. Setting up a new thread (\ref osThreadNew) will start execution with a call into this
261 entry function. The optional argument can be used to hand over arbitrary user data to the thread, i.e. to identify the thread
262 or for runtime parameters.
263
264 \param[in] argument Arbitrary user data set on \ref osThreadNew.
265 */
266
267 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
268 /**
269 \typedef osThreadId_t
270 */
271
272 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
273 /**
274 \struct osThreadAttr_t
275 */
276
277 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
278 /**
279 \def osThreadJoinable
280 \details
281 A thread in this state can be joined using \ref osThreadJoin.
282 */
283
284 /**
285 \def osThreadDetached
286 \details
287 A thread in this state cannot be joined using \ref osThreadJoin.
288 */
289
290 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
291 /**
292 \fn osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
293 \details
294 The function \b osThreadNew starts a thread function by adding it to the list of active threads and sets it to state
295 \ref ThreadStates "READY". Arguments for the thread function are passed using the parameter pointer \a *argument. When the priority of the
296 created thread function is higher than the current \ref ThreadStates "RUNNING" thread, the created thread function starts instantly and
297 becomes the new \ref ThreadStates "RUNNING" thread. Thread attributes are defined with the parameter pointer \a attr. Attributes include
298 settings for thread priority, stack size, or memory allocation.
299
300 The function can be safely called before the RTOS is
301 started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
302
303 The function \b osThreadNew returns the pointer to the thread object identifier or \token{NULL} in case of an error.
304
305 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
306 */
307
308 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
309 /**
310 \fn const char *osThreadGetName (osThreadId_t thread_id)
311 \details
312 The function \b osThreadGetName returns the pointer to the name string of the thread identified by parameter \a thread_id or
313 \token{NULL} in case of an error.
314
315 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
316
317 <b>Code Example</b>
318 \code
319 void ThreadGetName_example (void)  {
320   char id;                                           // id for the currently running thread
321    
322   id = osThreadGetName ();
323   if (id == NULL) {
324     // Failed to get the thread name; not in a thread
325   }
326 }
327 \endcode
328 */
329
330 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
331 /**
332 \fn osThreadId_t osThreadGetId (void)
333 \details
334 The function \b osThreadGetId returns the thread object ID of the currently running thread or NULL in case of an error.
335
336 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
337
338 <b>Code Example</b>
339 \code
340 void ThreadGetId_example (void)  {
341   osThreadId_t id;                                           // id for the currently running thread
342    
343   id = osThreadGetId ();
344   if (id == NULL) {
345     // Failed to get the id; not in a thread
346   }
347 }
348 \endcode
349 */
350 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
351 /**
352 \fn osThreadState_t osThreadGetState (osThreadId_t thread_id)
353 \details
354 The function \b osThreadGetState returns the state of the thread identified by parameter \a thread_id. In case it fails or
355 if it is called from an ISR, it will return \c osThreadError, otherwise it returns the thread state (refer to
356 \ref osThreadState_t for the list of thread states).
357  
358 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
359 */
360
361 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
362 /**
363 \fn osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority)
364 \details
365 The function \b osThreadSetPriority changes the priority of an active thread specified by the parameter \a thread_id to the
366 priority specified by the parameter \a priority. 
367
368 Possible \ref osStatus_t return values:
369     - \em osOK: the priority of the specified thread has been changed successfully.
370     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid or \a priority is incorrect.
371     - \em osErrorResource: thread specified by parameter \em thread_id is in an invalid thread state.
372     - \em osErrorISR: the function \b osThreadSetPriority cannot be called from interrupt service routines.
373
374 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
375
376 <b>Code Example</b>
377 \code
378 #include "cmsis_os2.h"
379  
380 void Thread_1 (void const *arg)  {                          // Thread function
381   osThreadId_t id;                                          // id for the currently running thread
382   osStatus_t   status;                                      // status of the executed function
383    
384   :  
385   id = osThreadGetId ();                                    // Obtain ID of current running thread
386    
387   status = osThreadSetPriority (id, osPriorityBelowNormal); // Set thread priority
388   if (status == osOK)  {
389     // Thread priority changed to BelowNormal
390   }
391   else {
392     // Failed to set the priority
393   }
394   :  
395 }
396 \endcode
397 */
398 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
399 /**
400 \fn osPriority_t osThreadGetPriority (osThreadId_t thread_id)
401 \details
402 The function \b osThreadGetPriority returns the priority of an active thread specified by the parameter \a thread_id.
403
404 Possible \ref osPriority_t return values:
405     - \em priority: the priority of the specified thread.
406     - \em osPriorityError: priority cannot be determined or is illegal. It is also returned when the function is called from
407       \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
408
409 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
410
411 <b>Code Example</b>
412 \code
413 #include "cmsis_os2.h"
414  
415 void Thread_1 (void const *arg)  {                         // Thread function
416   osThreadId_t id;                                         // id for the currently running thread
417   osPriority_t priority;                                   // thread priority
418    
419   id = osThreadGetId ();                                   // Obtain ID of current running thread
420   priority = osThreadGetPriority (id);                     // Obtain the thread priority
421 }
422 \endcode
423 */
424 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
425 /**
426 \fn osStatus_t osThreadYield (void)
427 \details
428 The function \b osThreadYield passes control to the next thread with the same priority that is in the \ref ThreadStates "READY" state. 
429 If there is no other thread with the same priority in state \ref ThreadStates "READY", then the current thread continues execution and
430 no thread switch occurs. \b osThreadYield does not set the thread to state \ref ThreadStates "BLOCKED". Thus no thread with a lower
431 priority will be scheduled even if threads in state \ref ThreadStates "READY" are available.
432
433 Possible \ref osStatus_t return values:
434     - \em osOK: control has been passed to the next thread successfully.
435     - \em osError: an unspecified error has occurred.
436     - \em osErrorISR: the function \b osThreadYield cannot be called from interrupt service routines.
437
438 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
439 \note This function <b>has no impact</b> when called when the kernel is locked, see \ref osKernelLock.
440
441 <b>Code Example</b>
442 \code
443 #include "cmsis_os2.h"
444  
445 void Thread_1 (void const *arg)  {                         // Thread function
446   osStatus_t   status;                                     // status of the executed function
447   :
448   while (1)  {
449     status = osThreadYield();                              // 
450     if (status != osOK)  {
451       // an error occurred
452     }
453   }
454 }
455 \endcode
456 */
457 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
458 /**
459 \fn osStatus_t osThreadSuspend (osThreadId_t thread_id)
460 \details
461 The function \b osThreadSuspend suspends the execution of the thread identified by parameter \em thread_id. The thread is put
462 into the \ref ThreadStates "BLOCKED" state (\ref osThreadBlocked). Suspending the running thread will cause a context switch to another
463 thread in \ref ThreadStates "READY" state immediately. The suspended thread is not executed until explicitly resumed with the function \ref osThreadResume. 
464
465 Threads that are already \ref ThreadStates "BLOCKED" are removed from any wait list and become ready when they are resumed.
466 Thus it is not recommended to suspend an already blocked thread.
467
468 Possible \ref osStatus_t return values:
469     - \em osOK: the thread has been suspended successfully.
470     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
471     - \em osErrorResource: thread specified by parameter \em thread_id is in an invalid thread state.
472     - \em osErrorISR: the function \b osThreadSuspend cannot be called from interrupt service routines.
473
474 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
475 \note This function <b>must not</b> be called to suspend the running thread when the kernel is locked, i.e. \ref osKernelLock.
476
477 */
478
479 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
480 /**
481 \fn osStatus_t osThreadResume (osThreadId_t thread_id)
482 \details
483 The function \b osThreadResume puts the thread identified by parameter \em thread_id (which has to be in \ref ThreadStates "BLOCKED" state)
484 back to the \ref ThreadStates "READY" state. If the resumed thread has a higher priority than the running thread a context switch occurs immediately.
485
486 The thread becomes ready regardless of the reason why the thread was blocked. Thus it is not recommended to resume a thread not suspended
487 by \ref osThreadSuspend.
488
489 Functions that will put a thread into \ref ThreadStates "BLOCKED" state are:
490 \ref osEventFlagsWait and \ref osThreadFlagsWait,
491 \ref osDelay and \ref osDelayUntil,
492 \ref osMutexAcquire and \ref osSemaphoreAcquire,
493 \ref osMessageQueueGet,
494 \ref osMemoryPoolAlloc,
495 \ref osThreadJoin,
496 \ref osThreadSuspend.
497
498 Possible \ref osStatus_t return values:
499     - \em osOK: the thread has been resumed successfully.
500     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
501     - \em osErrorResource: thread specified by parameter \em thread_id is in an invalid thread state.
502     - \em osErrorISR: the function \b osThreadResume cannot be called from interrupt service routines.
503
504 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
505 \note This function <b>may be</b> called when kernel is locked (\ref osKernelLock). Under this circumstances
506         a potential context switch is delayed until the kernel gets unlocked, i.e. \ref osKernelUnlock or \ref osKernelRestoreLock.
507
508 */
509
510 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
511 /**
512 \fn osStatus_t osThreadDetach (osThreadId_t thread_id)
513 \details
514 The function \b osThreadDetach changes the attribute of a thread (specified by \em thread_id) to \ref osThreadDetached.
515 Detached threads are not joinable with \ref osThreadJoin. When a detached thread is terminated, all resources are returned to
516 the system. The behavior of \ref osThreadDetach on an already detached thread is undefined.
517
518 Possible \ref osStatus_t return values:
519     - \em osOK: the attribute of the specified thread has been changed to detached successfully.
520     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
521     - \em osErrorResource: thread specified by parameter \em thread_id is in an invalid thread state.
522     - \em osErrorISR: the function \b osThreadDetach cannot be called from interrupt service routines.
523
524 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
525 */
526
527 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
528 /**
529 \fn osStatus_t osThreadJoin (osThreadId_t thread_id)
530 \details
531 The function \b osThreadJoin waits for the thread specified by \em thread_id to terminate. If that thread has already
532 terminated, then \ref osThreadJoin returns immediately. The thread must be joinable. By default threads are created with the
533 attribute \ref osThreadDetached.
534
535 Possible \ref osStatus_t return values:
536     - \em osOK: if the thread has already been terminated and joined or once the thread has been terminated and the join
537       operations succeeds.
538     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
539     - \em osErrorResource: parameter \em thread_id is \token{NULL} or refers to a thread that is not an active thread or the
540       thread is not joinable.
541     - \em osErrorISR: the function \b osThreadJoin cannot be called from interrupt service routines.
542
543 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
544 */
545
546 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
547 /**
548 \fn __NO_RETURN void osThreadExit (void)
549 \details
550
551 The function \b osThreadExit terminates the calling thread. This allows the thread to be synchronized with \ref osThreadJoin.
552
553 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
554
555 \b Code \b Example
556 \code
557 __NO_RETURN void worker (void *argument) {
558   // do something
559   osDelay(1000);
560   osThreadExit();
561 }
562 \endcode
563 */
564
565 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
566 /**
567 \fn osStatus_t osThreadTerminate (osThreadId_t thread_id)
568 \details
569 The function \b osThreadTerminate removes the thread specified by parameter \a thread_id from the list of active threads. If
570 the thread is currently \ref ThreadStates "RUNNING", the thread terminates and the execution continues with the next \ref ThreadStates "READY" thread. If no
571 such thread exists, the function will not terminate the running thread, but return \em osErrorResource.
572
573 Possible \ref osStatus_t return values:
574     - \em osOK: the specified thread has been removed from the active thread list successfully.
575     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
576     - \em osErrorResource: thread specified by parameter \em thread_id is in an invalid thread state or no
577       other \ref ThreadStates "READY" thread exists.
578     - \em osErrorISR: the function \b osThreadTerminate cannot be called from interrupt service routines.
579
580 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
581 \note Avoid calling the function with a \em thread_id that does not exist or has been terminated already.
582
583 <b>Code Example</b>
584 \code
585 #include "cmsis_os2.h"
586  
587 void Thread_1 (void *arg);                             // function prototype for Thread_1
588  
589 void ThreadTerminate_example (void) {
590   osStatus_t status;
591   osThreadId_t id;
592  
593   id = osThreadNew (Thread_1, NULL, NULL);             // create the thread
594                                                        // do something
595   status = osThreadTerminate (id);                     // stop the thread
596   if (status == osOK) {
597                                                        // Thread was terminated successfully
598     }
599   else {
600                                                        // Failed to terminate a thread
601     }
602 }
603 \endcode
604 */
605
606 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
607 /**
608 \fn uint32_t osThreadGetStackSize (osThreadId_t thread_id)
609 \details
610 The function \b osThreadGetStackSize returns the stack size of the thread specified by parameter \a thread_id. In case of an
611 error, it returns \token{0}.
612
613 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
614 */
615
616 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
617 /**
618 \fn uint32_t osThreadGetStackSpace (osThreadId_t thread_id); 
619 \details
620 The function \b osThreadGetStackSpace returns the size of unused stack space for the thread specified by parameter
621 \a thread_id. Stack watermark recording during execution needs to be enabled (refer to \ref threadConfig). In case of an
622 error, it returns \token{0}.
623
624 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
625 */
626
627 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
628 /**
629 \fn uint32_t osThreadGetCount (void)
630 \details
631 The function \b osThreadGetCount returns the number of active threads or \token{0} in case of an error.
632
633 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
634 */
635
636 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
637 /**
638 \fn uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items)
639 \details
640 The function \b osThreadEnumerate returns the number of enumerated threads or \token{0} in case of an error.
641
642 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
643 */
644
645 /// @}
646
647
648 // these struct members must stay outside the group to avoid double entries in documentation
649 /**
650 \var osThreadAttr_t::attr_bits
651 \details
652 The following predefined bit masks can be assigned to set options for a thread object.
653
654 | Bit Mask                | Description                                             |
655 | :---------------------- | :------------------------------------------------------ |
656 | \ref osThreadDetached   | Create thread in a detached mode (default).             |
657 | \ref osThreadJoinable   | Create thread in joinable mode, see \ref osThreadJoin.  |
658
659
660 \var osThreadAttr_t::cb_mem
661 \details
662 Pointer to a memory location for the thread object. This can optionally be used for custom memory management systems. 
663 Specify \token{NULL} to use the kernel memory management.
664
665 \var osThreadAttr_t::cb_size
666 \details
667 The size of the memory block passed with \ref cb_mem. Must be the size of a thread control block object or larger.
668
669 \var osThreadAttr_t::name
670 \details
671 String with a human readable name of the thread object.
672
673 \var osThreadAttr_t::priority
674 \details
675 Specifies the initial thread priority with a value from #osPriority_t.
676
677 \var osThreadAttr_t::reserved
678 \details
679 Reserved for future use. Must be \token{0}.
680
681 \var osThreadAttr_t::stack_mem
682 \details
683 Pointer to a memory location for the thread stack, \b must be 64-bit aligned. This can optionally be used for custom memory management systems. 
684 Specify \token{NULL} to use the kernel memory management.
685
686 \var osThreadAttr_t::tz_module
687 \details
688 TrustZone Thread Context Management Identifier to allocate context memory for threads. The RTOS kernel that runs in
689 non-secure state calls the interface functions defined by the header file TZ_context.h.
690 See <a href="../../Core/html/group__context__trustzone__functions.html">TrustZone RTOS Context Management</a>.
691
692 \var osThreadAttr_t::stack_size
693 \details
694 The size of the stack in Bytes.
695 */