]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/cmsis_os2_Thread.txt
RTOS2: Functions osXxxGetName allowed to be called from Interrupt Service Routines
[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 (applies to \ref joinable_threads "joinable threads).
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 preemptive. 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 \anchor thread_examples
45 Thread Examples
46 ===============
47 The following examples show various scenarios to create threads:
48  
49 <b>Example 1 - Create a simple thread</b> 
50
51 Create a thread out of the function thread1 using all default values for thread attributes and memory from the
52 \ref GlobalMemoryPool.
53  
54 \code
55 __NO_RETURN void thread1 (void *argument) {
56   // ...
57   for (;;) {}
58 }
59  
60 int main (void) {
61   osKernelInitialize();
62   ;
63   osThreadNew(thread1, NULL, NULL);     // Create thread with default settings
64   ;
65   osKernelStart(); 
66 }
67 \endcode
68
69 <b>Example 2 - Create thread with stack non-default stack size</b>
70  
71 Similar to the simple thread all attributes are default. The stack is dynamically allocated from the \ref GlobalMemoryPool
72  
73 \ref osThreadAttr_t.stack_size is used to pass the stack size in Bytes to \ref osThreadNew.
74
75 \code
76 __NO_RETURN void thread1 (void *argument) {
77   // ...
78   for (;;) {}
79 }
80  
81 const osThreadAttr_t thread1_attr = {
82   .stack_size = 1024                            // Create the thread stack with a size of 1024 bytes
83 };
84  
85 int main (void) {
86   ;  
87   osThreadNew(thread1, NULL, &thread1_attr);    // Create thread with custom sized stack memory
88   ;
89 }
90 \endcode
91
92 <b>Example 3 - Create thread with statically allocated stack</b>
93  
94 Similar to the simple thread all attributes are default. The stack is statically allocated using the \c uint64_t array
95 \c thread1_stk_1. This allocates 64*8 Bytes (=512 Bytes) with an alignment of 8 Bytes (mandatory for Cortex-M stack memory). 
96  
97 \ref osThreadAttr_t.stack_mem holds a pointer to the stacks lowest address. 
98  
99 \ref osThreadAttr_t.stack_size is used to pass the stack size in Bytes to \ref osThreadNew.
100
101 \code
102 __NO_RETURN void thread1 (void *argument) {
103   // ...
104   for (;;) {}
105 }
106  
107 static uint64_t thread1_stk_1[64];
108  
109 const osThreadAttr_t thread1_attr = {
110   .stack_mem  = &thread1_stk_1[0],
111   .stack_size = sizeof(thread1_stk_1)
112 };
113  
114 int main (void) {
115   ;  
116   osThreadNew(thread1, NULL, &thread1_attr);    // Create thread with statically allocated stack memory
117   ;
118 }
119 \endcode
120
121 <b>Example 4 - Thread with statically allocated task control block</b>
122  
123 Typically this method is chosen together with a statically allocated stack as shown in Example 2. 
124 \code 
125 #include "cmsis_os2.h"
126  
127 //include rtx_os.h for types of RTX objects
128 #include "rtx_os.h"
129
130 __NO_RETURN void thread1 (void *argument) {
131   // ...
132   for (;;) {}
133 }
134  
135 static osRtxThread_t thread1_tcb;
136  
137 const osThreadAttr_t thread1_attr = {
138   .cb_mem  = &thread1_tcb,
139   .cb_size = sizeof(thread1_tcb),
140 };
141  
142 int main (void) {
143   ;
144   osThreadNew(thread1, NULL, &thread1_attr);    // Create thread with custom tcb memory
145   ;
146 }
147 \endcode
148
149 <b>Example 5 - Create thread with a different priority</b> 
150  
151 The default priority of RTX is \ref osPriorityNormal. Often you want to run a task with a higher or lower priority. Using the
152 \ref osThreadAttr_t control structure you can set any initial priority required.
153
154 \code
155 __NO_RETURN void thread1 (void *argument) {
156   // ...
157   for (;;) {}
158 }
159  
160 const osThreadAttr_t thread1_attr = {
161   .priority = osPriorityHigh                    //Set initial thread priority to high   
162 };
163  
164 int main (void) {
165   ;
166   osThreadNew(thread1, NULL, &thread1_attr);
167   ;
168 }
169 \endcode
170
171 \anchor joinable_threads
172 <b>Example 6 - Joinable threads</b>
173  
174 In this example a master thread creates four threads with the \ref osThreadJoinable attribute. These will do some work and
175 return using the \ref osThreadExit call after finished. \ref osThreadJoin is used to synchronize the thread termination. 
176
177
178 \code 
179 __NO_RETURN void worker (void *argument) {
180   ; // work a lot on data[] 
181   osDelay(1000U);
182   osThreadExit();
183 }
184  
185 __NO_RETURN void thread1 (void *argument) {
186   osThreadAttr_t worker_attr;
187   osThreadId_t worker_ids[4];
188   uint8_t data[4][10];
189
190   memset(&worker_attr, 0, sizeof(worker_attr));
191   worker_attr.attr_bits = osThreadJoinable;
192  
193   worker_ids[0] = osThreadNew(worker, &data[0][0], &worker_attr);
194   worker_ids[1] = osThreadNew(worker, &data[1][0], &worker_attr);
195   worker_ids[2] = osThreadNew(worker, &data[2][0], &worker_attr);
196   worker_ids[3] = osThreadNew(worker, &data[3][0], &worker_attr);
197  
198   osThreadJoin(worker_ids[0]);
199   osThreadJoin(worker_ids[1]);
200   osThreadJoin(worker_ids[2]);
201   osThreadJoin(worker_ids[3]);
202  
203   osThreadExit(); 
204 }
205 \endcode
206    
207 @{
208 */
209 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
210 /**
211 \enum osThreadState_t
212 \details
213 State of a thread as retrieved by \ref osThreadGetState. In case \b osThreadGetState fails or if it is called from an ISR, it
214 will return \c osThreadError, otherwise it returns the thread state.
215
216 \var osThreadState_t::osThreadInactive
217 \details The thread is created but not actively used, or has been terminated (returned for static control block allocation, when memory pools are used \ref osThreadError is returned as the control block is no longer valid)
218
219 \var osThreadState_t::osThreadReady
220 \details The thread is ready for execution but not currently running.
221
222 \var osThreadState_t::osThreadRunning
223 \details The thread is currently running.
224
225 \var osThreadState_t::osThreadBlocked
226 \details The thread is currently blocked (delayed, waiting for an event or suspended).
227
228 \var osThreadState_t::osThreadTerminated
229 \details The thread is terminated and all its resources are not yet freed (applies to \ref joinable_threads "joinable threads).
230
231 \var osThreadState_t::osThreadError
232 \details The thread does not exist (has raised an error condition) and cannot be scheduled.
233 */
234
235 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
236 /**
237 \enum osPriority_t
238 \details
239
240 The \b osPriority_t value specifies the priority for a thread. The default thread priority should be \a osPriorityNormal.
241 If an active thread becomes ready that has a higher priority than the currently running thread then a thread switch occurs
242 immediately. The system continues executing the thread with the higher priority.
243
244 To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a <b>priority inheritance</b>
245 method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a
246 thread with a lower priority. Thus causing the high priority thread potentially being blocked forever by another thread
247 with lower priority. To come over this issue the low priority thread controlling the resource should be treated as having
248 the higher priority until it releases the resource.
249
250 \note Priority inheritance only applies to mutexes.
251
252 \var osPriority_t::osPriorityIdle
253 \details This lowest priority should not be used for any other thread. 
254
255 \var osPriority_t::osPriorityISR 
256 \details This highest priority might be used by the RTOS implementation but must not be used for any user thread.
257 */
258
259 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
260 /**
261 \typedef void (*osThreadFunc_t) (void *argument)
262 \details Entry function for threads. Setting up a new thread (\ref osThreadNew) will start execution with a call into this
263 entry function. The optional argument can be used to hand over arbitrary user data to the thread, i.e. to identify the thread
264 or for runtime parameters.
265
266 \param[in] argument Arbitrary user data set on \ref osThreadNew.
267 */
268
269 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
270 /**
271 \typedef osThreadId_t
272 \details Returned by:
273 - \ref osThreadNew
274 - \ref osThreadGetId
275 - \ref osThreadEnumerate
276 - \ref osMutexGetOwner
277 */
278
279 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
280 /**
281 \struct osThreadAttr_t
282 \details
283 Specifies the following attributes for the \ref osThreadNew function.
284 */
285
286 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
287 /**
288 \def osThreadJoinable
289 \details
290 Bitmask for a thread that can be joined.
291 Intended for use in \e attr_bits of \ref osThreadAttr_t type argument for \ref osThreadNew function.
292
293 A thread in this state can be joined using \ref osThreadJoin.
294 */
295
296 /**
297 \def osThreadDetached
298 \details
299 Bitmask for a thread that cannot be joined.
300 Intended for use in \e attr_bits of \ref osThreadAttr_t type argument for \ref osThreadNew function.
301
302 A thread in this state cannot be joined using \ref osThreadJoin.
303 */
304
305 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
306 /**
307 \def osThreadUnprivileged
308 \details
309 Bitmask for a thread that runs in unprivileged mode.
310 Intended for use in \e attr_bits of \ref osThreadAttr_t type argument for \ref osThreadNew function.
311
312 In \b unprivileged processor mode, a thread:
313 - has limited access to the MSR and MRS instructions, and cannot use the CPS instruction.
314 - cannot access the system timer, NVIC, or system control block.
315 - might have restricted access to memory or peripherals.
316
317 \note Ignored on processors that only run in privileged mode.
318
319 Refer to the target processor User's Guide for details.
320 */
321
322 /**
323 \def osThreadPrivileged
324 \details
325 Bitmask for a thread that runs in privileged mode.
326 Intended for use in \e attr_bits of \ref osThreadAttr_t type argument for \ref osThreadNew function.
327
328 In \b privileged processor mode, the application software can use all the instructions and has access to all resources.
329 */
330
331 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
332 /**
333 \def osThreadZone(n)
334 \param  n MPU Protected Zone value.
335 \brief  MPU zone value in attribute bit field format.
336 \details
337
338 The preprocessor macro \b osThreadZone constructs attribute bitmask with MPU zone bits set to \a n.
339
340 <b>Code Example:</b>
341 \code
342 /* ThreadB thread attributes */
343 const osThreadAttr_t thread_B_attr = {
344   .name      = "ThreadB",       // human readable thread name
345   .attr_bits = osThreadZone(3U) // assign thread to MPU zone 3
346 };
347 \endcode
348 */
349
350
351 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
352 /**
353 \fn osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
354 \details
355 The function \b osThreadNew starts a thread function by adding it to the list of active threads and sets it to state
356 \ref ThreadStates "READY". Arguments for the thread function are passed using the parameter pointer \a *argument. When the priority of the
357 created thread function is higher than the current \ref ThreadStates "RUNNING" thread, the created thread function starts instantly and
358 becomes the new \ref ThreadStates "RUNNING" thread. Thread attributes are defined with the parameter pointer \a attr. Attributes include
359 settings for thread priority, stack size, or memory allocation.
360
361 The function can be safely called before the RTOS is
362 started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
363
364 The function \b osThreadNew returns the pointer to the thread object identifier or \token{NULL} in case of an error.
365
366 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
367
368 \b Code \b Example
369
370 Refer to the \ref thread_examples "Thread Examples" section.
371 */
372
373 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
374 /**
375 \fn const char *osThreadGetName (osThreadId_t thread_id)
376 \details
377 The function \b osThreadGetName returns the pointer to the name string of the thread identified by parameter \a thread_id or
378 \token{NULL} in case of an error.
379
380 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
381
382 <b>Code Example</b>
383 \code
384 void ThreadGetName_example (void) {
385   osThreadId_t thread_id = osThreadGetId();
386   const char* name = osThreadGetName(thread_id);
387   if (name == NULL) {
388     // Failed to get the thread name; not in a thread
389   }
390 }
391 \endcode
392 */
393
394 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
395 /**
396 \fn uint32_t osThreadGetClass (osThreadId_t thread_id);
397 \details
398 The function \b osThreadGetClass returns safety class assigned to the thread identified by parameter \a thread_id. In case of an
399 error, it returns \ref osErrorId.
400 */
401
402 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
403 /**
404 \fn uint32_t osThreadGetZone (osThreadId_t thread_id);
405 \details
406 The function \b osThreadGetZone returns the MPU Protected Zone value assigned to the thread identified by parameter \a thread_id.
407 In case of an error, it returns \ref osErrorId.
408 */
409
410 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
411 /**
412 \fn osThreadId_t osThreadGetId (void)
413 \details
414 The function \b osThreadGetId returns the thread object ID of the currently running thread or NULL in case of an error.
415
416 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
417
418 <b>Code Example</b>
419 \code
420 void ThreadGetId_example (void) {
421   osThreadId_t id;                              // id for the currently running thread
422    
423   id = osThreadGetId();
424   if (id == NULL) {
425     // Failed to get the id
426   }
427 }
428 \endcode
429 */
430 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
431 /**
432 \fn osThreadState_t osThreadGetState (osThreadId_t thread_id)
433 \details
434 The function \b osThreadGetState returns the state of the thread identified by parameter \a thread_id. In case it fails or
435 if it is called from an ISR, it will return \c osThreadError, otherwise it returns the thread state (refer to
436 \ref osThreadState_t for the list of thread states).
437  
438 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
439 */
440
441 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
442 /**
443 \fn osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority)
444 \details
445 The function \b osThreadSetPriority changes the priority of an active thread specified by the parameter \a thread_id to the
446 priority specified by the parameter \a priority. 
447
448 Possible \ref osStatus_t return values:
449     - \em osOK: the priority of the specified thread has been changed successfully.
450     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid or \a priority is incorrect.
451     - \em osErrorResource: the thread is in an invalid state.
452     - \em osErrorISR: the function \b osThreadSetPriority cannot be called from interrupt service routines.
453     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
454
455 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
456
457 <b>Code Example</b>
458 \code
459 #include "cmsis_os2.h"
460  
461 void Thread_1 (void const *arg) {               // Thread function
462   osThreadId_t id;                              // id for the currently running thread
463   osStatus_t   status;                          // status of the executed function
464  
465   :
466   id = osThreadGetId();                         // Obtain ID of current running thread
467  
468   status = osThreadSetPriority(id, osPriorityBelowNormal);  // Set thread priority
469   if (status == osOK) {
470     // Thread priority changed to BelowNormal
471   }
472   else {
473     // Failed to set the priority
474   }
475   :
476 }
477 \endcode
478 */
479 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
480 /**
481 \fn osPriority_t osThreadGetPriority (osThreadId_t thread_id)
482 \details
483 The function \b osThreadGetPriority returns the priority of an active thread specified by the parameter \a thread_id.
484
485 Possible \ref osPriority_t return values:
486     - \em priority: the priority of the specified thread.
487     - \em osPriorityError: priority cannot be determined or is illegal. It is also returned when the function is called from
488       \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
489
490 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
491
492 <b>Code Example</b>
493 \code
494 #include "cmsis_os2.h"
495  
496 void Thread_1 (void const *arg) {               // Thread function
497   osThreadId_t id;                              // id for the currently running thread
498   osPriority_t priority;                        // thread priority
499    
500   id = osThreadGetId();                         // Obtain ID of current running thread
501   priority = osThreadGetPriority(id);           // Obtain the thread priority
502 }
503 \endcode
504 */
505 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
506 /**
507 \fn osStatus_t osThreadYield (void)
508 \details
509 The function \b osThreadYield passes control to the next thread with the same priority that is in the \ref ThreadStates "READY" state. 
510 If there is no other thread with the same priority in state \ref ThreadStates "READY", then the current thread continues execution and
511 no thread switch occurs. \b osThreadYield does not set the thread to state \ref ThreadStates "BLOCKED". Thus no thread with a lower
512 priority will be scheduled even if threads in state \ref ThreadStates "READY" are available.
513
514 Possible \ref osStatus_t return values:
515     - \em osOK: control has been passed to the next thread successfully.
516     - \em osError: an unspecified error has occurred.
517     - \em osErrorISR: the function \b osThreadYield cannot be called from interrupt service routines.
518
519 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
520 \note This function <b>has no impact</b> when called when the kernel is locked, see \ref osKernelLock.
521
522 <b>Code Example</b>
523 \code
524 #include "cmsis_os2.h"
525  
526 void Thread_1 (void const *arg) {               // Thread function
527   osStatus_t status;                            // status of the executed function
528   :
529   while (1) {
530     status = osThreadYield();
531     if (status != osOK) {
532       // an error occurred
533     }
534   }
535 }
536 \endcode
537 */
538 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
539 /**
540 \fn osStatus_t osThreadSuspend (osThreadId_t thread_id)
541 \details
542 The function \b osThreadSuspend suspends the execution of the thread identified by parameter \em thread_id. The thread is put
543 into the \ref ThreadStates "BLOCKED" state (\ref osThreadBlocked). Suspending the running thread will cause a context switch to another
544 thread in \ref ThreadStates "READY" state immediately. The suspended thread is not executed until explicitly resumed with the function \ref osThreadResume. 
545
546 Threads that are already \ref ThreadStates "BLOCKED" are removed from any wait list and become ready when they are resumed.
547 Thus it is not recommended to suspend an already blocked thread.
548
549 Possible \ref osStatus_t return values:
550     - \em osOK: the thread has been suspended successfully.
551     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
552     - \em osErrorResource: the thread is in an invalid state.
553     - \em osErrorISR: the function \b osThreadSuspend cannot be called from interrupt service routines.
554     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
555
556 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
557 \note This function <b>must not</b> be called to suspend the running thread when the kernel is locked, i.e. \ref osKernelLock.
558
559 */
560
561 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
562 /**
563 \fn osStatus_t osThreadResume (osThreadId_t thread_id)
564 \details
565 The function \b osThreadResume puts the thread identified by parameter \em thread_id (which has to be in \ref ThreadStates "BLOCKED" state)
566 back to the \ref ThreadStates "READY" state. If the resumed thread has a higher priority than the running thread a context switch occurs immediately.
567
568 The thread becomes ready regardless of the reason why the thread was blocked. Thus it is not recommended to resume a thread not suspended
569 by \ref osThreadSuspend.
570
571 Functions that will put a thread into \ref ThreadStates "BLOCKED" state are:
572 \ref osEventFlagsWait and \ref osThreadFlagsWait,
573 \ref osDelay and \ref osDelayUntil,
574 \ref osMutexAcquire and \ref osSemaphoreAcquire,
575 \ref osMessageQueueGet,
576 \ref osMemoryPoolAlloc,
577 \ref osThreadJoin,
578 \ref osThreadSuspend.
579
580 Possible \ref osStatus_t return values:
581     - \em osOK: the thread has been resumed successfully.
582     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
583     - \em osErrorResource: the thread is in an invalid state.
584     - \em osErrorISR: the function \b osThreadResume cannot be called from interrupt service routines.
585     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
586
587 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
588 \note This function <b>may be</b> called when kernel is locked (\ref osKernelLock). Under this circumstances
589         a potential context switch is delayed until the kernel gets unlocked, i.e. \ref osKernelUnlock or \ref osKernelRestoreLock.
590
591 */
592
593 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
594 /**
595 \fn osStatus_t osThreadDetach (osThreadId_t thread_id)
596 \details
597 The function \b osThreadDetach changes the attribute of a thread (specified by \em thread_id) to \ref osThreadDetached.
598 Detached threads are not joinable with \ref osThreadJoin. When a detached thread is terminated, all resources are returned to
599 the system. The behavior of \ref osThreadDetach on an already detached thread is undefined.
600
601 Possible \ref osStatus_t return values:
602     - \em osOK: the attribute of the specified thread has been changed to detached successfully.
603     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
604     - \em osErrorResource: the thread is in an invalid state.
605     - \em osErrorISR: the function \b osThreadDetach cannot be called from interrupt service routines.
606     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
607
608 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
609 */
610
611 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
612 /**
613 \fn osStatus_t osThreadJoin (osThreadId_t thread_id)
614 \details
615 The function \b osThreadJoin waits for the thread specified by \em thread_id to terminate. If that thread has already
616 terminated, then \ref osThreadJoin returns immediately. The thread must be joinable. By default threads are created with the
617 attribute \ref osThreadDetached.
618
619 Possible \ref osStatus_t return values:
620     - \em osOK: if the thread has already been terminated and joined or once the thread has been terminated and the join
621       operations succeeds.
622     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
623     - \em osErrorResource: the thread is in an invalid state (ex: not joinable).
624     - \em osErrorISR: the function \b osThreadJoin cannot be called from interrupt service routines.
625     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
626
627 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines". <br>
628 \note Only one thread shall call \b osThreadJoin to join the target thread. If multiple threads try to join simultaneously with the same thread,
629        the results are undefined.
630
631 */
632
633 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
634 /**
635 \fn __NO_RETURN void osThreadExit (void)
636 \details
637
638 The function \b osThreadExit terminates the calling thread. This allows the thread to be synchronized with \ref osThreadJoin.
639
640 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
641
642 \b Code \b Example
643 \code
644 __NO_RETURN void worker (void *argument) {
645   // do something
646   osDelay(1000U);
647   osThreadExit();
648 }
649 \endcode
650 */
651
652 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
653 /**
654 \fn osStatus_t osThreadTerminate (osThreadId_t thread_id)
655 \details
656 The function \b osThreadTerminate removes the thread specified by parameter \a thread_id from the list of active threads. If
657 the thread is currently \ref ThreadStates "RUNNING", the thread terminates and the execution continues with the next \ref ThreadStates "READY" thread. If no
658 such thread exists, the function will not terminate the running thread, but return \em osErrorResource.
659
660 Possible \ref osStatus_t return values:
661     - \em osOK: the specified thread has been removed from the active thread list successfully.
662     - \em osErrorParameter: \a thread_id is \token{NULL} or invalid.
663     - \em osErrorResource: the thread is in an invalid state or no other \ref ThreadStates "READY" thread exists.
664     - \em osErrorISR: the function \b osThreadTerminate cannot be called from interrupt service routines.
665     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified thread.
666
667 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
668 \note Avoid calling the function with a \em thread_id that does not exist or has been terminated already.
669 \note \b osThreadTerminate destroys non-joinable threads and removes their thread_id from the system. Subsequent access to the
670 thread_id (for example \ref osThreadGetState) will return an \ref osThreadError. Joinable threads will not be destroyed and
671 return the status \ref osThreadTerminated until they are joined with \ref osThreadJoin.
672
673 <b>Code Example</b>
674 \code
675 #include "cmsis_os2.h"
676  
677 void Thread_1 (void *arg);                      // function prototype for Thread_1
678  
679 void ThreadTerminate_example (void) {
680   osStatus_t status;
681   osThreadId_t id;
682  
683   id = osThreadNew(Thread_1, NULL, NULL);       // create the thread
684                                                 // do something
685   status = osThreadTerminate(id);               // stop the thread
686   if (status == osOK) {
687                                                 // Thread was terminated successfully
688   }
689   else {
690                                                 // Failed to terminate a thread
691   }
692 }
693 \endcode
694 */
695
696 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
697 /**
698 \fn uint32_t osThreadGetStackSize (osThreadId_t thread_id)
699 \details
700 The function \b osThreadGetStackSize returns the stack size of the thread specified by parameter \a thread_id. In case of an
701 error, it returns \token{0}.
702
703 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
704 */
705
706 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
707 /**
708 \fn uint32_t osThreadGetStackSpace (osThreadId_t thread_id); 
709 \details
710 The function \b osThreadGetStackSpace returns the size of unused stack space for the thread specified by parameter
711 \a thread_id. Stack watermark recording during execution needs to be enabled (refer to \ref threadConfig). In case of an
712 error, it returns \token{0}.
713
714 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
715 */
716
717 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
718 /**
719 \fn uint32_t osThreadGetCount (void)
720 \details
721 The function \b osThreadGetCount returns the number of active threads or \token{0} in case of an error.
722
723 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
724 */
725
726 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
727 /**
728 \fn uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items)
729 \details
730 The function \b osThreadEnumerate returns the number of enumerated threads or \token{0} in case of an error.
731
732 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
733 */
734
735 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
736 /**
737 \fn osStatus_t osThreadFeedWatchdog (uint32_t ticks);
738 \details
739 The function \b osThreadFeedWatchdog restarts watchdog of the current running thread. If the thread watchdog is not fed again within the \a ticks interval
740 \ref osWatchdogAlarm_Handler will be called.
741
742 Possible \ref osStatus_t return values:
743     - \em osOK: the watchdog timer was restarted successfully.
744     - \em osError: cannot be executed (kernel not running).
745     - \em osErrorISR: the function \b osThreadFeedWatchdog cannot be called from interrupt service routines.
746
747 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
748
749 <b>Code Example:</b>
750 \code
751 #include "cmsis_os2.h"
752  
753 void Thread_1 (void const *arg) {          // Thread function
754   osStatus_t status;                       // Status of the executed function
755   :
756   while (1) {
757     status = osThreadFeedWatchdog(500U);   // Feed thread watchdog for 500 ticks
758     // verify status value here
759     :
760   }
761 }
762 \endcode
763 */
764
765 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
766 /**
767 \fn osStatus_t osThreadProtectPrivileged (void);
768 \details
769 The function \b osThreadProtectPrivileged disables creation of new privileged threads. After its successful execution, no new
770 threads with privilege execution mode (\ref osThreadPrivileged attribute) can be created.
771 Kernel shall be in ready state or running when \b osThreadProtectPrivileged is called.
772
773 Possible \ref osStatus_t return values:
774     - \em osOK: the creation of new privileged threads is disabled.
775     - \em osError: cannot be executed (kernel not ready).
776     - \em osErrorISR: the function \b osThreadProtectPrivileged cannot be called from interrupt service routines.
777
778 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
779
780 <b>Code Example:</b>
781 \code
782 #include "cmsis_os2.h"
783  
784 int main (void) {
785   osStatus_t status;
786   :
787   status = osKernelInitialize();        // Initialize CMSIS-RTOS2 kernel
788   // verify status value here.
789   :                                     // Create privileged threads
790   status = osThreadProtectPrivileged(); // Disable creation of new privileged threads.
791   // verify status value here.
792   :                                     // Start the kernel
793 }
794 \endcode
795 */
796
797 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
798 /**
799 \fn osStatus_t osThreadSuspendClass (uint32_t safety_class, uint32_t mode);
800 \details
801 The function \b osThreadSuspendClass suspends execution of threads based on safety class assignment. \a safety_class provides the reference safety class value, while \a mode is considered as a bitmap that additionally specifies the safety classes to be suspended.
802
803 If \ref osSafetyWithSameClass is set in \a mode than the threads with safety class value equal to \a safety_class will be suspended.
804 <br>
805 If \ref osSafetyWithLowerClass is set in \a mode than the threads with safety class value lower than \a safety_class will be suspended.
806
807 Possible \ref osStatus_t return values:
808     - \em osOK: the threads with specified safety class have been suspended successfully.
809     - \em osErrorParameter: \a safety_class is invalid.
810     - \em osErrorResource: no other \ref ThreadStates "READY" thread exists.
811     - \em osErrorISR: the function \b osThreadSuspendClass is called from interrupt other than \ref osWatchdogAlarm_Handler.
812     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class specified by \a safety_class and \a mode.
813
814 <b>Code Example:</b>
815 \code
816 #include "cmsis_os2.h"
817  
818 void SuspendNonCriticalClasses (void) {
819   osStatus_t status;
820  
821   status = osThreadSuspendClass(4U, osSafetyWithSameClass | osSafetyWithLowerClass); // Suspends threads with safety class 4 or lower
822   // verify status value here.
823 }
824 \endcode
825 */
826
827 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
828 /**
829 \fn osStatus_t osThreadResumeClass (uint32_t safety_class, uint32_t mode);
830 \details
831 The function \b osThreadResumeClass resumes execution of threads based on safety class assignment. \a safety_class provides the reference safety class value, while \a mode is considered as a bitmap that additionally specifies the safety classes to be resumed.
832
833 If \ref osSafetyWithSameClass is set in \a mode than the threads with safety class value equal to \a safety_class will be resumed.
834 <br>
835 If \ref osSafetyWithLowerClass is set in \a mode than the threads with safety class value lower than \a safety_class will be resumed.
836
837
838 Possible \ref osStatus_t return values:
839     - \em osOK: the threads with specified safety class have been resumed successfully.
840     - \em osErrorParameter: \a safety_class is invalid.
841     - \em osErrorISR: the function \b osThreadResumeClass is called from interrupt other than \ref osWatchdogAlarm_Handler.
842     - \em osErrorSafetyClass: the calling thread safety class is lower than the safety class specified by \a safety_class and \a mode.
843
844 <b>Code Example:</b>
845 \code
846 #include "cmsis_os2.h"
847  
848 void ResumeNonCriticalClasses (void) {
849   osStatus_t status;
850  
851   status = osThreadResumeClass(4U, osSafetyWithSameClass | osSafetyWithLowerClass); // Resumes threads with safety class 4 or lower
852   // verify status value here.
853 }
854 \endcode
855 */
856
857 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
858 /**
859 \fn osStatus_t osThreadTerminateZone (uint32_t zone);
860 \details
861 The function \b osThreadTerminateZone terminates execution of threads assigned to the MPU Protected Zone as given by \a zone parameter.
862
863 Possible \ref osStatus_t return values:
864     - \em osOK: the threads within the specified MPU Protected Zone have been terminated successfully.
865     - \em osErrorParameter: \a zone is invalid.
866     - \em osErrorResource: no other \ref ThreadStates "READY" thread exists.
867     - \em osErrorISR: the function \b osThreadTerminateZone is called from interrupt other than fault.
868     - \em osError: the function \b osThreadTerminateZone is called from thread.
869
870 \note \b osThreadTerminateZone destroys non-joinable threads and removes their thread IDs from the system. Subsequent access to a terminated thread via its thread ID (for example \ref osThreadGetState) will return an \ref osThreadError. Joinable threads will not be destroyed and return the status \ref osThreadTerminated until they are joined with \ref osThreadJoin.
871
872 <b>Code Example:</b>
873 \code
874 #include "cmsis_os2.h"
875  
876 void TerminateFaultedThreads (void) {  // to be called from an exception fault context
877   osStatus_t status;
878  
879   status = osThreadTerminateZone(3U); // Terminates threads assigned to MPU Protected Zone 3
880   // verify status value here.
881 }
882 \endcode
883 */
884
885 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
886 /**
887 \fn uint32_t osWatchdogAlarm_Handler (osThreadId_t thread_id);
888 \details
889 The callback function \b osWatchdogAlarm_Handler is called by the kernel when a thread watchdog expires.
890 Parameter \a thread_id identifies the thread which has the expired thread watchdog.
891 The function needs to be implemented in user application.
892
893 Return new reload value to restart the watchdog. Return \token{0} to stop the thread watchdog.
894
895 \note The callback function is called from interrupt.
896
897 \note
898 When multiple watchdogs expire in the same tick, this function is called for each thread with highest safety class first.
899
900 <b>Code Example:</b>
901 \code
902 #include "cmsis_os2.h"
903  
904 uint32_t osWatchdogAlarm_Handler (osThreadId_t thread_id) {
905   uint32_t safety_class;
906   uint32_t next_interval;
907
908   safety_class = osThreadGetClass(thread_id);
909   
910   /* Handle the watchdog depending on how safety-critical is the thread */
911   if (safety_class < ...){
912     :
913   } else {
914     :
915   }
916  
917   return next_interval;
918 }
919 \endcode
920 */
921
922 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
923 /**
924 \fn void osZoneSetup_Callback (uint32_t zone);
925 \details
926 The callback function \b osZoneSetup_Callback is called by the kernel when MPU Protected Zone changes.
927 The function shall be implemented in user application.
928
929 <b>Code Example:</b>
930 \code
931 /* Update MPU settings for newly activating Zone */
932 void osZoneSetup_Callback (uint32_t zone) {
933  
934   if (zone >= ZONES_NUM) {
935     //Issue an error for incorrect zone value
936   }
937  
938   ARM_MPU_Disable();
939   ARM_MPU_Load(mpu_table[zone], MPU_REGIONS);
940   ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
941 }
942 \endcode
943
944 \c ZONES_NUM is the total amount of zones allocated by the application.
945 For \c ARM_MPU_... functions refer to <a href="../../Core/html/group__mpu__functions.html"><b>MPU Functions</b></a> in CMSIS-Core documentation.
946 */
947
948 /// @}
949
950
951 // these struct members must stay outside the group to avoid double entries in documentation
952 /**
953
954 \var osThreadAttr_t::attr_bits
955 \details
956 The following bit masks can be used to set options:
957  - \ref osThreadDetached : create thread in a detached mode (default).
958  - \ref osThreadJoinable : create thread in \ref joinable_threads "joinable mode".  
959  - \ref osThreadUnprivileged : create thread to execute in unprivileged mode.
960  - \ref osThreadPrivileged : create thread to execute in privileged mode.  
961  - \ref osThreadZone (m) : create thread assigned to MPU zone \token{m}.
962  - \ref osSafetyClass (n) : create thread with safety class \token{n} assigned to it.
963
964 Default: \token{0} no options set. Safety class and MPU Zone are inherited from running thread. Thread privilege mode is set based on configuration \ref threadConfig_procmode.
965
966 \var osThreadAttr_t::cb_mem
967 \details
968 Pointer to a memory for the thread control block object. Refer to \ref StaticObjectMemory for more information.
969
970 Default: \token{NULL} to use \ref CMSIS_RTOS_MemoryMgmt_Automatic for the thread control block.
971
972 \var osThreadAttr_t::cb_size
973 \details
974 The size (in bytes) of memory block passed with \ref cb_mem. For RTX, the minimum value is defined with \ref osRtxThreadCbSize (higher values are permitted).
975
976 Default: \token{0} as the default is no memory provided with \ref cb_mem.
977
978 \var osThreadAttr_t::name
979 \details
980 Pointer to a constant string with a human readable name (displayed during debugging) of the thread object.
981
982 Default: \token{NULL} no name specified (debugger may display function name instead).
983
984 \var osThreadAttr_t::priority
985 \details
986 Specifies the initial thread priority with a value from #osPriority_t.
987
988 Default: \token{osPriorityNormal}.
989
990 \var osThreadAttr_t::reserved
991 \details
992 Reserved for future use.
993
994 \var osThreadAttr_t::stack_mem
995 \details
996 Pointer to a memory location for the thread stack (64-bit aligned). 
997
998 Default: \token{NULL} to allocate stack from a fixed-size memory pool using \ref ThreadStack.
999
1000 \var osThreadAttr_t::stack_size
1001 \details
1002 The size (in bytes) of the stack specified by \ref stack_mem.
1003
1004 Default: \token{0} as the default is no memory provided with \ref stack_mem.
1005
1006 \var osThreadAttr_t::tz_module
1007 \details
1008 \if (ARMv8M)
1009 TrustZone Thread Context Management Identifier to allocate context memory for threads. The RTOS kernel that runs in
1010 non-secure state calls the interface functions defined by the header file TZ_context.h. Can safely be set to zero
1011 for threads not using secure calls at all. 
1012 See <a href="../../Core/html/group__context__trustzone__functions.html">TrustZone RTOS Context Management</a>.
1013
1014 Default: \token{0} not thread context specified.
1015 \else
1016 Applicable only for devices on Armv8-M architecture. Ignored on others.
1017 \endif
1018
1019 */