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