]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/cmsis_os2_Thread.txt
Return values updated.
[cmsis] / CMSIS / DoxyGen / RTOS2 / src / cmsis_os2_Thread.txt
1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 //  ==== Thread Management ====
3 /** 
4 \addtogroup CMSIS_RTOS_ThreadMgmt Thread Management
5 \ingroup CMSIS_RTOS CMSIS_RTOSv2
6 \brief Define, create, and control thread functions.
7 \details 
8 The Thread Management function group allows defining, creating, and controlling thread functions in the system.
9
10 \note Thread management functions cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
11
12 \anchor ThreadStates
13 Thread states
14 -------------
15 Threads can be in the following states:
16
17  - \b RUNNING: The thread that is currently running is in the \ref ThreadStates "RUNNING" state. Only one thread at a time can be in this
18    state.
19  - \b READY: Threads which are ready to run are in the \ref ThreadStates "READY" state. Once the \ref ThreadStates "RUNNING" thread has terminated, or is
20    \ref ThreadStates "BLOCKED", the next \ref ThreadStates "READY" thread with the highest priority becomes the \ref ThreadStates "RUNNING" thread.
21  - \b BLOCKED: Threads that are blocked either delayed, waiting for an event to occur or suspended are in the \ref ThreadStates "BLOCKED"
22    state.
23  - \b TERMINATED: When \ref osThreadTerminate is called, threads are \ref ThreadStates "TERMINATED" with resources not yet released. 
24  - \b INACTIVE: Threads that are not created or have been terminated with all resources released are in the \ref ThreadStates "INACTIVE" state.
25  
26 \image html "ThreadStatus.png" "Thread State and State Transitions"
27
28
29 A CMSIS-RTOS assumes that threads are scheduled as shown in the figure <b>Thread State and State Transitions</b>. The thread
30 states change as follows:
31  - A thread is created using the function \ref osThreadNew. This puts the thread into the \ref ThreadStates "READY" or \ref ThreadStates "RUNNING" state
32    (depending on the thread priority).
33  - CMSIS-RTOS is pre-emptive. The active thread with the highest priority becomes the \ref ThreadStates "RUNNING" thread provided it does not
34    wait for any event. The initial priority of a thread is defined with the \ref osThreadAttr_t but may be changed during
35    execution using the function \ref osThreadSetPriority.
36  - The \ref ThreadStates "RUNNING" thread transfers into the \ref ThreadStates "BLOCKED" state when it is delayed, waiting for an event or suspended.
37  - Active threads can be terminated any time using the function \ref osThreadTerminate. Threads can terminate also by just
38    returning from the thread function. Threads that are terminated are in the \ref ThreadStates "INACTIVE" state and typically do not consume
39    any dynamic memory resources. 
40
41 \note 
42 Refer to \ref threadConfig for RTX5 configuration options.
43
44 \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 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 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 <b>Example 6 - Joinable threads</b>
172  
173 In this example a master thread creates four threads with the \ref osThreadJoinable attribute. These will do some work and
174 return using the \ref osThreadExit call after finished. \ref osThreadJoin is used to synchronize the thread termination. 
175
176
177 \code 
178 __NO_RETURN void worker (void *argument) {     
179     ; // work a lot on data[] 
180     osDelay(1000);       
181     osThreadExit();
182 }
183  
184 __NO_RETURN void thread1 (void *argument) {
185   osThreadAttr_t worker_attr;
186   osThreadId_t worker_ids[4];
187   uint8_t data[4][10];
188
189   memset(&worker_attr, 0, sizeof(worker_attr));
190   worker_attr.attr_bits = osThreadJoinable;
191   
192   worker_ids[0] = osThreadNew(worker, &data[0][0], &worker_attr);    
193   worker_ids[1] = osThreadNew(worker, &data[1][0], &worker_attr);    
194   worker_ids[2] = osThreadNew(worker, &data[2][0], &worker_attr);    
195   worker_ids[3] = osThreadNew(worker, &data[3][0], &worker_attr);    
196  
197   osThreadJoin(worker_ids[0]);
198   osThreadJoin(worker_ids[1]);
199   osThreadJoin(worker_ids[2]);
200   osThreadJoin(worker_ids[3]);
201  
202   osThreadExit(); 
203 }
204 \endcode
205    
206 @{
207 */
208 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
209 /**
210 \enum osThreadState_t
211 \details
212 State of a thread as retrieved by \ref osThreadGetState. In case \b osThreadGetState fails or if it is called from an ISR, it
213 will return \c osThreadError, otherwise it returns the thread state.
214
215 \var osThreadState_t::osThreadInactive
216 \details The thread is created but not actively used, or has been terminated.
217
218 \var osThreadState_t::osThreadReady
219 \details The thread is ready for execution but not currently running.
220
221 \var osThreadState_t::osThreadRunning
222 \details The thread is currently running.
223
224 \var osThreadState_t::osThreadBlocked
225 \details The thread is currently blocked (delayed, waiting for an event or suspended).
226
227 \var osThreadState_t::osThreadTerminated
228 \details The thread is terminated and all its resources are freed.
229
230 \var osThreadState_t::osThreadError
231 \details The thread thread has raised an error condition and cannot be scheduled.
232 */
233
234 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
235 /**
236 \enum osPriority_t
237 \details
238
239 The \b osPriority_t value specifies the priority for a thread. The default thread priority should be \a osPriorityNormal.
240 If an active thread becomes ready that has a higher priority than the currently running thread then a thread switch occurs
241 immediately. The system continues executing the thread with the higher priority.
242
243 To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a <b>priority inheritance</b>
244 method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a
245 thread with a lower priority. Thus causing the high priority thread potentially beeing blocked forever by another thread
246 with lower priority. To come over this issue the low priority thread controlling the resource should be treated as having
247 the higher priority until it releases the resource.
248
249 \note Priority inheritance only applies to mutexes.
250
251 \var osPriority_t::osPriorityIdle
252 \details This lowest priority should not be used for any other thread. 
253
254 \var osPriority_t::osPriorityISR 
255 \details This highest priority might be used by the RTOS implementation but must not be used for any user thread.
256 */
257
258 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
259 /**
260 \typedef void (*osThreadFunc_t) (void *argument)
261 \details Entry function for threads. Setting up a new thread (\ref osThreadNew) will start execution with a call into this
262 entry function. The optional argument can be used to hand over arbitrary user data to the thread, i.e. to identify the thread
263 or for runtime parameters.
264
265 \param[in] argument Arbitrary user data set on \ref osThreadNew.
266 */
267
268 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
269 /**
270 \typedef osThreadId_t
271 \details Returned by:
272 - \ref osThreadNew
273 - \ref osThreadGetId
274 - \ref osThreadEnumerate
275 - \ref osMutexGetOwner
276 */
277
278 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
279 /**
280 \struct osThreadAttr_t
281 \details
282 Attributes to configure a thread.
283
284 Refer to \ref CMSIS_RTOS_MemoryMgmt for details about usage of
285  - osThreadAttr_t::cb_mem
286  - osThreadAttr_t::cb_size
287  - osThreadAttr_t::stack_mem
288  - osThreadAttr_t::stack_size
289 */
290
291 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
292 /**
293 \def osThreadJoinable
294 \details
295 A thread in this state can be joined using \ref osThreadJoin.
296 */
297
298 /**
299 \def osThreadDetached
300 \details
301 A thread in this state cannot be joined using \ref osThreadJoin.
302 */
303
304 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
305 /**
306 \fn osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
307 \details
308 The function \b osThreadNew starts a thread function by adding it to the list of active threads and sets it to state
309 \ref ThreadStates "READY". Arguments for the thread function are passed using the parameter pointer \a *argument. When the priority of the
310 created thread function is higher than the current \ref ThreadStates "RUNNING" thread, the created thread function starts instantly and
311 becomes the new \ref ThreadStates "RUNNING" thread. Thread attributes are defined with the parameter pointer \a attr. Attributes include
312 settings for thread priority, stack size, or memory allocation.
313
314 The function can be safely called before the RTOS is
315 started (call to \ref osKernelStart), but not before it is initialized (call to \ref osKernelInitialize).
316
317 The function \b osThreadNew returns the pointer to the thread object identifier or \token{NULL} in case of an error.
318
319 Possible \ref osThreadId_t return values:
320 - pointer to the thread object, in case of success
321 - \em NULL, if the priority level is outside the permitted range: \em osPriorityIdle ... \em osPriorityISR.
322 - \em NULL, if the \em stack_mem is not \em NULL but:
323   - \em stack_mem is not a multiple of 8 bytes
324   - \em stack_size equals 0
325 - \em NULL, if \em stack_size is lower smaller than 72 bytes
326 - \em NULL, if \em stack_size is not zero and not a multiple of 8
327 - \em NULL, if the control block (attr->cb_mem) is not large enough to hold a thread control block
328 - \em NULL, if the control block (attr->cb_mem) is not 4 byte aligned  
329 - \em NULL, if the maximum number of threads have been created already
330 - \em NULL, if the thread function \em func is \em NULL
331 - \em 'osErrorISR', if IRQ is masked
332 - \em 'osErrorISR', in IRQ mode. 
333
334 \note Cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
335
336 \b Code \b Example
337
338 Refer to the \ref thread_examples "Thread Examples" section.
339 */
340
341 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
342 /**
343 \fn const char *osThreadGetName (osThreadId_t thread_id)
344 \details
345 The function \b osThreadGetName returns the pointer to the name string of the thread identified by parameter \a thread_id or
346 \token{NULL} in case of an error.
347
348 Possible return values:
349 - pointer to the name string, in case of success
350 - \em NULL, if no name has been defined
351 - \em NULL, if a thread specified by \a thread_id does not exist
352 - \em NULL, if thread_id is \em NULL.
353
354
355 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
356
357 <b>Code Example</b>
358 \code
359 void ThreadGetName_example (void)  {
360   osThreadId_t thread_id = osThreadGetId ();
361   const char* name = osThreadGetName (thread_id);
362   if (name == NULL) {
363     // Failed to get the thread name; not in a thread
364   }
365 }
366 \endcode
367 */
368
369 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
370 /**
371 \fn osThreadId_t osThreadGetId (void)
372 \details
373 The function \b osThreadGetId returns the thread object ID of the currently running thread or NULL in case of an error.
374
375 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
376
377 <b>Code Example</b>
378 \code
379 void ThreadGetId_example (void)  {
380   osThreadId_t id;                                           // id for the currently running thread
381    
382   id = osThreadGetId ();
383   if (id == NULL) {
384     // Failed to get the id; not in a thread
385   }
386 }
387 \endcode
388 */
389 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
390 /**
391 \fn osThreadState_t osThreadGetState (osThreadId_t thread_id)
392 \details
393 The function \b osThreadGetState returns the state of the thread identified by parameter \a thread_id. In case it fails or
394 if it is called from an ISR, it will return \c osThreadError, otherwise it returns the thread state (refer to
395 \ref osThreadState_t for the list of thread states).
396
397 Possible \ref osThreadState_t return values:
398 - the state of the thread specified by \a thread_id, in case of success
399 - \em 'osThreadError', if the thread specified by \a thread_id is \em NULL
400
401 - \em 'osThreadError', if if a thread specified by \a thread_id does not exist
402 - \em 'osThreadError', if IRQ is masked
403
404 - \em 'osThreadError', if in IRQ mode.
405
406 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
407 */
408
409 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
410 /**
411 \fn osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority)
412 \details
413 The function \b osThreadSetPriority changes the priority of an active thread specified by the parameter \a thread_id to the
414 priority specified by the parameter \a priority. 
415
416 Possible \ref osStatus_t return values:
417 - \em 'osOK', if the priority of thread specified by \a thread_id  has been changed successfully
418 - \em 'osErrorParameter', if the thread specified by \a thread_id is \em NULL
419 - \em 'osErrorParameter', if a thread specified by \a thread_id does not exist
420 - \em 'osErrorParameter', if \em osPriorityIdle < \em priority or \em priority > \em osPriorityISR 
421 - \em 'osErrorResource', if the thread is in state \em 'osThreadInactive' or \em 'osThreadTerminated'
422 - \em 'osErrorISR', if IRQ is masked
423 - \em 'osErrorISR', if in IRQ mode.
424
425 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
426
427 <b>Code Example</b>
428 \code
429 #include "cmsis_os2.h"
430  
431 void Thread_1 (void const *arg)  {                          // Thread function
432   osThreadId_t id;                                          // id for the currently running thread
433   osStatus_t   status;                                      // status of the executed function
434    
435   :  
436   id = osThreadGetId ();                                    // Obtain ID of current running thread
437    
438   status = osThreadSetPriority (id, osPriorityBelowNormal); // Set thread priority
439   if (status == osOK)  {
440     // Thread priority changed to BelowNormal
441   }
442   else {
443     // Failed to set the priority
444   }
445   :  
446 }
447 \endcode
448 */
449 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
450 /**
451 \fn osPriority_t osThreadGetPriority (osThreadId_t thread_id)
452 \details
453 The function \b osThreadGetPriority returns the priority of an active thread specified by the parameter \a thread_id.
454
455 Possible \ref osPriority_t return values:
456 - the priority of the thread specified by \em thread_id, in case of success
457 - \em 'osPriorityError', if the threads specified by \em thread_id is in state \em 'osRtxObjectInactive' or \em 'osRtxObjectTerminated'
458 - \em 'osPriorityError', if a thread specified by \em thread_id does not exist
459 - \em 'osPriorityError', if the thread is \em NULL.
460
461 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
462
463 <b>Code Example</b>
464 \code
465 #include "cmsis_os2.h"
466  
467 void Thread_1 (void const *arg)  {                         // Thread function
468   osThreadId_t id;                                         // id for the currently running thread
469   osPriority_t priority;                                   // thread priority
470    
471   id = osThreadGetId ();                                   // Obtain ID of current running thread
472   priority = osThreadGetPriority (id);                     // Obtain the thread priority
473 }
474 \endcode
475 */
476 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
477 /**
478 \fn osStatus_t osThreadYield (void)
479 \details
480 The function \b osThreadYield passes control to the next thread with the same priority that is in the \ref ThreadStates "READY" state. 
481 If there is no other thread with the same priority in state \ref ThreadStates "READY", then the current thread continues execution and
482 no thread switch occurs. \b osThreadYield does not set the thread to state \ref ThreadStates "BLOCKED". Thus no thread with a lower
483 priority will be scheduled even if threads in state \ref ThreadStates "READY" are available.
484
485 Possible \ref osStatus_t return values:
486 - \em 'osOK', if control has been passed to the next thread successfully
487 - \em 'osErrorISR', if IRQ is masked
488 - \em 'osErrorISR', if in IRQ mode.
489
490
491 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
492 \note This function <b>has no impact</b> when called when the kernel is locked, see \ref osKernelLock.
493
494 <b>Code Example</b>
495 \code
496 #include "cmsis_os2.h"
497  
498 void Thread_1 (void const *arg)  {                         // Thread function
499   osStatus_t   status;                                     // status of the executed function
500   :
501   while (1)  {
502     status = osThreadYield();                              // 
503     if (status != osOK)  {
504       // an error occurred
505     }
506   }
507 }
508 \endcode
509 */
510 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
511 /**
512 \fn osStatus_t osThreadSuspend (osThreadId_t thread_id)
513 \details
514 The function \b osThreadSuspend suspends the execution of the thread identified by parameter \em thread_id. The thread is put
515 into the \ref ThreadStates "BLOCKED" state (\ref osThreadBlocked). Suspending the running thread will cause a context switch to another
516 thread in \ref ThreadStates "READY" state immediately. The suspended thread is not executed until explicitly resumed with the function \ref osThreadResume. 
517
518 Threads that are already \ref ThreadStates "BLOCKED" are removed from any wait list and become ready when they are resumed.
519 Thus it is not recommended to suspend an already blocked thread.
520
521 Possible \ref osStatus_t return values:
522  - \em 'osOK', if the thread has been suspended successfully
523  - \em 'osErrorParameter', if a thread specified by \a thread_id does not exist
524  - \em 'osErrorParameter', if the thread specified by \a thread_id is NULL
525
526  - \em 'osErrorResource', if no thread is in state \em 'osRtxThreadReady' or the kernel is not running
527
528  - \em 'osErrorResource', the the thread specified by \a thread_id is in state \em 'osRtxThreadInactive'
529
530  - \em 'osErrorResource', the the thread specified by \a thread_id is in state \em 'osRtxThreadTerminated'
531
532  - \em 'osErrorISR', if IRQ is masked
533
534  - \em 'osErrorISR', if in IRQ mode.
535
536
537 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
538 \note This function <b>must not</b> be called to suspend the running thread when the kernel is locked, i.e. \ref osKernelLock.
539
540 */
541
542 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
543 /**
544 \fn osStatus_t osThreadResume (osThreadId_t thread_id)
545 \details
546 The function \b osThreadResume puts the thread identified by parameter \em thread_id (which has to be in \ref ThreadStates "BLOCKED" state)
547 back to the \ref ThreadStates "READY" state. If the resumed thread has a higher priority than the running thread a context switch occurs immediately.
548
549 The thread becomes ready regardless of the reason why the thread was blocked. Thus it is not recommended to resume a thread not suspended
550 by \ref osThreadSuspend.
551
552 Functions that will put a thread into \ref ThreadStates "BLOCKED" state are:
553 \ref osEventFlagsWait and \ref osThreadFlagsWait,
554 \ref osDelay and \ref osDelayUntil,
555 \ref osMutexAcquire and \ref osSemaphoreAcquire,
556 \ref osMessageQueueGet,
557 \ref osMemoryPoolAlloc,
558 \ref osThreadJoin,
559 \ref osThreadSuspend.
560
561 Possible \ref osStatus_t return values:
562  - \em 'osOK', if the thread specified by \a thread_id has been resumed successfully
563  - \em 'osErrorParameter', if a thread specified by \a thread_id does not exist
564  - \em 'osErrorParameter', if the thread specified by \a thread_id is \em NULL
565  - \em 'osErrorResource', if the state of the thread specified by \a thread_id is not 'Blocked'.
566  - \em 'osErrorISR', if IRQ is masked
567  - \em 'osErrorISR', in IRQ mode.
568
569 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
570 \note This function <b>may be</b> called when kernel is locked (\ref osKernelLock). Under this circumstances
571         a potential context switch is delayed until the kernel gets unlocked, i.e. \ref osKernelUnlock or \ref osKernelRestoreLock.
572
573 */
574
575 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
576 /**
577 \fn osStatus_t osThreadDetach (osThreadId_t thread_id)
578 \details
579 The function \b osThreadDetach changes the attribute of a thread (specified by \em thread_id) to \ref osThreadDetached.
580 Detached threads are not joinable with \ref osThreadJoin. When a detached thread is terminated, all resources are returned to
581 the system. The behavior of \ref osThreadDetach on an already detached thread is undefined.
582
583 Possible \ref osStatus_t return values:
584  - \em 'osOK', if the attribute of the thread specified by \a thread_id has been changed to detached successfully.
585  - \em 'osErrorParameter', if \em thread_id is \em NULL
586  - \em 'osErrorParameter', if a thread specified by \a thread_id does not exist
587  - \em 'osErrorResource', if the thread is in state \em 'osRtxThreadInactive'
588  - \em 'osErrorResource', if the thread is marked as \em 'osThreadDetached'
589  - \em 'osErrorISR', if IRQ is masked
590  - \em 'osErrorISR', if in IRQ mode.  
591
592
593 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
594 */
595
596 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
597 /**
598 \fn osStatus_t osThreadJoin (osThreadId_t thread_id)
599 \details
600 The function \b osThreadJoin waits for the thread specified by \em thread_id to terminate. If that thread has already
601 terminated, then \ref osThreadJoin returns immediately. The thread must be joinable. By default threads are created with the
602 attribute \ref osThreadDetached.
603
604 Possible \ref osStatus_t return values:
605    - \em 'osOK', in case of success
606    - \em 'osErrorParameter', if the thread is \em NULL
607    - \em 'osErrorParameter', if a thread specified by \a thread_id does not exist
608    - \em 'osErrorResource', if the thread specified by \a thread_id is marked as not joinable
609
610    - \em 'osErrorResource', if the state of the thread is \em 'osRtxThreadInactive' or \em 'osRtxThreadRunning'
611    - \em 'osErrorResource', if the kernel is not running
612    - \em 'osErrorISR', if IRQ is masked
613
614    - \em 'osErrorISR', if in IRQ mode.
615
616 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
617 */
618
619 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
620 /**
621 \fn __NO_RETURN void osThreadExit (void)
622 \details
623
624 The function \b osThreadExit terminates the calling thread. This allows the thread to be synchronized with \ref osThreadJoin.
625
626 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
627
628 \b Code \b Example
629 \code
630 __NO_RETURN void worker (void *argument) {
631   // do something
632   osDelay(1000);
633   osThreadExit();
634 }
635 \endcode
636 */
637
638 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
639 /**
640 \fn osStatus_t osThreadTerminate (osThreadId_t thread_id)
641 \details
642 The function \b osThreadTerminate removes the thread specified by parameter \a thread_id from the list of active threads. If
643 the thread is currently \ref ThreadStates "RUNNING", the thread terminates and the execution continues with the next \ref ThreadStates "READY" thread. If no
644 such thread exists, the function will not terminate the running thread, but return \em osErrorResource.
645
646 Possible \ref osStatus_t return values:
647 - \em 'osOK', if the thread specified by \a thread_id has been removed from the active thread list successfully
648 - \em 'osErrorParameter', if the thread specified by \a thread_id does not exist
649 - \em 'osErrorParameter', if a thread with \em thread_id is \em NULL
650 - \em 'osErrorResource', if the thread is in state \em 'osRtxThreadInactive' or \em 'osRtxThreadTerminated'
651 - \em 'osErrorResource', if no thread is in state \em 'osRtxThreadReady' that can run next
652 - \em 'osErrorResource', if the kernel is not running
653 - \em 'osErrorISR', if IRQ is masked
654
655 - \em 'osErrorISR', if in IRQ mode.
656
657
658 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
659 \note Avoid calling the function with a \em thread_id that does not exist or has been terminated already.
660
661 <b>Code Example</b>
662 \code
663 #include "cmsis_os2.h"
664  
665 void Thread_1 (void *arg);                             // function prototype for Thread_1
666  
667 void ThreadTerminate_example (void) {
668   osStatus_t status;
669   osThreadId_t id;
670  
671   id = osThreadNew (Thread_1, NULL, NULL);             // create the thread
672                                                        // do something
673   status = osThreadTerminate (id);                     // stop the thread
674   if (status == osOK) {
675                                                        // Thread was terminated successfully
676     }
677   else {
678                                                        // Failed to terminate a thread
679     }
680 }
681 \endcode
682 */
683
684 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
685 /**
686 \fn uint32_t osThreadGetStackSize (osThreadId_t thread_id)
687 \details
688 The function \b osThreadGetStackSize returns the stack size of the thread specified by parameter \a thread_id. In case of an
689 error, it returns \token{0}.
690 /*
691 New definition
692 */
693 Possible return values:
694 - the stack size of the thread
695 - \em 0, if a thread specified by \a thread_id does not exist
696 - \em 0, if the thread specified by \em thread_id is \em NULL
697 - \em 0, if the thread is in state \em 'osRtxObjectInactive'
698 - \em 0, if IRQ is masked
699 - \em 0, if in IRQ mode.
700
701
702 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
703 */
704
705 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
706 /**
707 \fn uint32_t osThreadGetStackSpace (osThreadId_t thread_id); 
708 \details
709 The function \b osThreadGetStackSpace returns the size of unused stack space for the thread specified by parameter
710 \a thread_id. Stack watermark recording during execution needs to be enabled (refer to \ref threadConfig). In case of an
711 error, it returns \token{0}.
712
713 Possible return values:
714 - the size of unused stack space, in case of success
715 - \em 0, if a thread specified by \a thread_id does not exist
716 - \em 0, if the thread with \em thread_id is \em NULL
717 - \em 0, if the thread is in state \em 'osRtxObjectInactive'
718 - \em 0, if stack watermark is not enabled
719 - \em 0, if IRQ is masked
720 - \em 0, if in IRQ mode
721
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 osThreadGetCount (void)
729 \details
730 The function \b osThreadGetCount returns the number of active 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 uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items)
738 \details
739 The function \b osThreadEnumerate returns the number of enumerated threads or \token{0} in case of an error.
740
741 Possible return values:
742 - number of enumerated threads, in case of success
743 - \em 0, if \em thread_array is \em NULL
744 - \em 0, if \em array_items = 0
745 - \em 0, if IRQ is masked
746 - \em 0, if in IRQ mode.
747
748 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
749 */
750
751 /// @}
752
753
754 // these struct members must stay outside the group to avoid double entries in documentation
755 /**
756 \var osThreadAttr_t::attr_bits
757 \details
758 The following predefined bit masks can be assigned to set options for a thread object.
759
760 | Bit Mask                | Description                                             |
761 | :---------------------- | :------------------------------------------------------ |
762 | \ref osThreadDetached   | Create thread in a detached mode (default).             |
763 | \ref osThreadJoinable   | Create thread in joinable mode, see \ref osThreadJoin.  |
764
765
766 \var osThreadAttr_t::cb_mem
767 \details
768 Pointer to a memory location for the thread control block object. This can optionally be used for custom memory management systems.\n
769 Default: \token{NULL} (uses kernel memory management).
770
771 \var osThreadAttr_t::cb_size
772 \details
773 The size of the memory block passed with \ref cb_mem. Must be the size of a thread control block object or larger.
774
775 \var osThreadAttr_t::name
776 \details
777 Pointer to a string with a human readable name of the thread object.\n
778 Default: \token{NULL}.
779
780 \var osThreadAttr_t::priority
781 \details
782 Specifies the initial thread priority with a value from #osPriority_t.\n
783 Default: \token{osPriorityNormal}
784
785 \var osThreadAttr_t::reserved
786 \details
787 Reserved for future use (set to '0').\n
788 Default: \token{0}.
789
790 \var osThreadAttr_t::stack_mem
791 \details
792 Pointer to a memory location for the thread stack, \b must be 64-bit aligned. This can optionally be used for custom memory
793 management systems.\n
794 Default: \token{NULL} (uses kernel memory management).
795
796 \var osThreadAttr_t::tz_module
797 \details
798 TrustZone Thread Context Management Identifier to allocate context memory for threads. The RTOS kernel that runs in
799 non-secure state calls the interface functions defined by the header file TZ_context.h. Can safely be set to zero
800 for threads not using secure calls at all. 
801 See <a href="../../Core/html/group__context__trustzone__functions.html">TrustZone RTOS Context Management</a>.
802
803 \var osThreadAttr_t::stack_size
804 \details
805 The size of the stack specified by \ref stack_mem in Bytes.
806 */