group__CMSIS__RTOS__ThreadMgmt.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <title>Thread Management</title>
  7. <title>CMSIS-RTOS: Thread Management</title>
  8. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  9. <link href="cmsis.css" rel="stylesheet" type="text/css" />
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <script type="text/javascript" src="printComponentTabs.js"></script>
  13. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  14. <script type="text/javascript" src="resize.js"></script>
  15. <script type="text/javascript" src="navtree.js"></script>
  16. <script type="text/javascript">
  17. $(document).ready(initResizable);
  18. $(window).load(resizeHeight);
  19. </script>
  20. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  21. <script type="text/javascript" src="search/search.js"></script>
  22. <script type="text/javascript">
  23. $(document).ready(function() { searchBox.OnSelectItem(0); });
  24. </script>
  25. </head>
  26. <body>
  27. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  28. <div id="titlearea">
  29. <table cellspacing="0" cellpadding="0">
  30. <tbody>
  31. <tr style="height: 46px;">
  32. <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  33. <td style="padding-left: 0.5em;">
  34. <div id="projectname">CMSIS-RTOS
  35. &#160;<span id="projectnumber">Version 1.03</span>
  36. </div>
  37. <div id="projectbrief">Real-Time Operating System: API and RTX Reference Implementation.</div>
  38. </td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. </div>
  43. <!-- end header part -->
  44. <div id="CMSISnav" class="tabs1">
  45. <ul class="tablist">
  46. <script type="text/javascript">
  47. <!--
  48. writeComponentTabs.call(this);
  49. //-->
  50. </script>
  51. </ul>
  52. </div>
  53. <!-- Generated by Doxygen 1.8.6 -->
  54. <script type="text/javascript">
  55. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  56. </script>
  57. <div id="navrow1" class="tabs">
  58. <ul class="tablist">
  59. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  60. <li><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
  61. <li><a href="modules.html"><span>Reference</span></a></li>
  62. <li>
  63. <div id="MSearchBox" class="MSearchBoxInactive">
  64. <span class="left">
  65. <img id="MSearchSelect" src="search/mag_sel.png"
  66. onmouseover="return searchBox.OnSearchSelectShow()"
  67. onmouseout="return searchBox.OnSearchSelectHide()"
  68. alt=""/>
  69. <input type="text" id="MSearchField" value="Search" accesskey="S"
  70. onfocus="searchBox.OnSearchFieldFocus(true)"
  71. onblur="searchBox.OnSearchFieldFocus(false)"
  72. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  73. </span><span class="right">
  74. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
  75. </span>
  76. </div>
  77. </li>
  78. </ul>
  79. </div>
  80. </div><!-- top -->
  81. <div id="side-nav" class="ui-resizable side-nav-resizable">
  82. <div id="nav-tree">
  83. <div id="nav-tree-contents">
  84. <div id="nav-sync" class="sync"></div>
  85. </div>
  86. </div>
  87. <div id="splitbar" style="-moz-user-select:none;"
  88. class="ui-resizable-handle">
  89. </div>
  90. </div>
  91. <script type="text/javascript">
  92. $(document).ready(function(){initNavTree('group__CMSIS__RTOS__ThreadMgmt.html','');});
  93. </script>
  94. <div id="doc-content">
  95. <!-- window showing the filter options -->
  96. <div id="MSearchSelectWindow"
  97. onmouseover="return searchBox.OnSearchSelectShow()"
  98. onmouseout="return searchBox.OnSearchSelectHide()"
  99. onkeydown="return searchBox.OnSearchSelectKey(event)">
  100. <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Pages</a></div>
  101. <!-- iframe showing the search results (closed by default) -->
  102. <div id="MSearchResultsWindow">
  103. <iframe src="javascript:void(0)" frameborder="0"
  104. name="MSearchResults" id="MSearchResults">
  105. </iframe>
  106. </div>
  107. <div class="header">
  108. <div class="summary">
  109. <a href="#define-members">Macros</a> &#124;
  110. <a href="#enum-members">Enumerations</a> &#124;
  111. <a href="#func-members">Functions</a> </div>
  112. <div class="headertitle">
  113. <div class="title">Thread Management<div class="ingroups"><a class="el" href="group__CMSIS__RTOS.html">CMSIS-RTOS API</a></div></div> </div>
  114. </div><!--header-->
  115. <div class="contents">
  116. <p>Define, create, and control thread functions.
  117. <a href="#details">More...</a></p>
  118. <table class="memberdecls">
  119. <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
  120. Macros</h2></td></tr>
  121. <tr class="memitem:gaee93d929beb350f16e5cc7fa602e229f"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaee93d929beb350f16e5cc7fa602e229f">osThreadDef</a>(name, priority, instances, stacksz)</td></tr>
  122. <tr class="memdesc:gaee93d929beb350f16e5cc7fa602e229f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Create a Thread Definition with function, priority, and stack requirements. <a href="#gaee93d929beb350f16e5cc7fa602e229f">More...</a><br/></td></tr>
  123. <tr class="separator:gaee93d929beb350f16e5cc7fa602e229f"><td class="memSeparator" colspan="2">&#160;</td></tr>
  124. <tr class="memitem:gaf0c7c6b5e09f8be198312144b5c9e453"><td class="memItemLeft" align="right" valign="top">#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a>(name)&#160;&#160;&#160;&amp;os_thread_def_##name</td></tr>
  125. <tr class="memdesc:gaf0c7c6b5e09f8be198312144b5c9e453"><td class="mdescLeft">&#160;</td><td class="mdescRight">Access a Thread definition. <a href="#gaf0c7c6b5e09f8be198312144b5c9e453">More...</a><br/></td></tr>
  126. <tr class="separator:gaf0c7c6b5e09f8be198312144b5c9e453"><td class="memSeparator" colspan="2">&#160;</td></tr>
  127. </table><table class="memberdecls">
  128. <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="enum-members"></a>
  129. Enumerations</h2></td></tr>
  130. <tr class="memitem:ga7f2b42f1983b9107775ec2a1c69a849a"><td class="memItemLeft" align="right" valign="top">enum &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> { <br/>
  131. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa549e79a43ff4f8b2b31afb613f5caa81">osPriorityIdle</a> = -3,
  132. <br/>
  133. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa61cb822239ac8f66dfbdc7291598a3d4">osPriorityLow</a> = -2,
  134. <br/>
  135. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa193b650117c209b4a203954542bcc3e6">osPriorityBelowNormal</a> = -1,
  136. <br/>
  137. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1">osPriorityNormal</a> = 0,
  138. <br/>
  139. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa17b36cd9cd38652c2bc6d4803990674b">osPriorityAboveNormal</a> = +1,
  140. <br/>
  141. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa914433934143a9ba767e59577c56e6c2">osPriorityHigh</a> = +2,
  142. <br/>
  143. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa1485dec3702434a1ec3cb74c7a17a4af">osPriorityRealtime</a> = +3,
  144. <br/>
  145. &#160;&#160;<a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aae35f5e2f9c64ad346822521b643bdea4">osPriorityError</a> = 0x84
  146. <br/>
  147. }</td></tr>
  148. <tr class="memdesc:ga7f2b42f1983b9107775ec2a1c69a849a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Priority used for thread control. <a href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">More...</a><br/></td></tr>
  149. <tr class="separator:ga7f2b42f1983b9107775ec2a1c69a849a"><td class="memSeparator" colspan="2">&#160;</td></tr>
  150. </table><table class="memberdecls">
  151. <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
  152. Functions</h2></td></tr>
  153. <tr class="memitem:gac59b5713cb083702dce759c73fd90dff"><td class="memItemLeft" align="right" valign="top"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (const <a class="el" href="cmsis__os_8h.html#structosThreadDef__t">osThreadDef_t</a> *thread_def, void *argument)</td></tr>
  154. <tr class="memdesc:gac59b5713cb083702dce759c73fd90dff"><td class="mdescLeft">&#160;</td><td class="mdescRight">Create a thread and add it to Active Threads and set it to state READY. <a href="#gac59b5713cb083702dce759c73fd90dff">More...</a><br/></td></tr>
  155. <tr class="separator:gac59b5713cb083702dce759c73fd90dff"><td class="memSeparator" colspan="2">&#160;</td></tr>
  156. <tr class="memitem:gab1df2a28925862ef8f9cf4e1c995c5a7"><td class="memItemLeft" align="right" valign="top"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a> (void)</td></tr>
  157. <tr class="memdesc:gab1df2a28925862ef8f9cf4e1c995c5a7"><td class="mdescLeft">&#160;</td><td class="mdescRight">Return the thread ID of the current running thread. <a href="#gab1df2a28925862ef8f9cf4e1c995c5a7">More...</a><br/></td></tr>
  158. <tr class="separator:gab1df2a28925862ef8f9cf4e1c995c5a7"><td class="memSeparator" colspan="2">&#160;</td></tr>
  159. <tr class="memitem:gaea135bb90eb853eff39e0800b91bbeab"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (<a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> thread_id)</td></tr>
  160. <tr class="memdesc:gaea135bb90eb853eff39e0800b91bbeab"><td class="mdescLeft">&#160;</td><td class="mdescRight">Terminate execution of a thread and remove it from Active Threads. <a href="#gaea135bb90eb853eff39e0800b91bbeab">More...</a><br/></td></tr>
  161. <tr class="separator:gaea135bb90eb853eff39e0800b91bbeab"><td class="memSeparator" colspan="2">&#160;</td></tr>
  162. <tr class="memitem:ga0dfb90ccf1f6e4b54b9251b12d1cbc8b"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga0dfb90ccf1f6e4b54b9251b12d1cbc8b">osThreadSetPriority</a> (<a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> thread_id, <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> priority)</td></tr>
  163. <tr class="memdesc:ga0dfb90ccf1f6e4b54b9251b12d1cbc8b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Change priority of an active thread. <a href="#ga0dfb90ccf1f6e4b54b9251b12d1cbc8b">More...</a><br/></td></tr>
  164. <tr class="separator:ga0dfb90ccf1f6e4b54b9251b12d1cbc8b"><td class="memSeparator" colspan="2">&#160;</td></tr>
  165. <tr class="memitem:ga4299d838978bc2aae5e4350754e6a4e9"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga4299d838978bc2aae5e4350754e6a4e9">osThreadGetPriority</a> (<a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> thread_id)</td></tr>
  166. <tr class="memdesc:ga4299d838978bc2aae5e4350754e6a4e9"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get current priority of an active thread. <a href="#ga4299d838978bc2aae5e4350754e6a4e9">More...</a><br/></td></tr>
  167. <tr class="separator:ga4299d838978bc2aae5e4350754e6a4e9"><td class="memSeparator" colspan="2">&#160;</td></tr>
  168. <tr class="memitem:gaf13a667493c5d629a90c13e113b99233"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf13a667493c5d629a90c13e113b99233">osThreadYield</a> (void)</td></tr>
  169. <tr class="memdesc:gaf13a667493c5d629a90c13e113b99233"><td class="mdescLeft">&#160;</td><td class="mdescRight">Pass control to next thread that is in state <b>READY</b>. <a href="#gaf13a667493c5d629a90c13e113b99233">More...</a><br/></td></tr>
  170. <tr class="separator:gaf13a667493c5d629a90c13e113b99233"><td class="memSeparator" colspan="2">&#160;</td></tr>
  171. </table>
  172. <a name="details" id="details"></a><h2 class="groupheader">Description</h2>
  173. <p>The Thread Management function group allows defining, creating, and controlling thread functions in the system. The function <b>main</b> is a special thread function that is started at system initialization and has the initial priority <em>osPriorityNormal</em>.</p>
  174. <p><a class="anchor" id="ThreadStates"></a>Threads can be in the following states:</p>
  175. <ul>
  176. <li><b>RUNNING:</b> The thread that is currently running is in the <b>RUNNING</b> state. Only one thread at a time can be in this state.</li>
  177. <li><b>READY:</b> Threads which are ready to run are in the <b>READY</b> state. Once the <b>RUNNING</b> thread has terminated or is <b>WAITING</b>, the next <b>READY</b> thread with the highest priority becomes the <b>RUNNING</b> thread.</li>
  178. <li><b>WAITING:</b> Threads that are waiting for an event to occur are in the <b>WAITING</b> state.</li>
  179. <li><b>INACTIVE:</b> Threads that are not created or terminated are in the <b>INACTIVE</b> state. These threads typically consume no system resources.</li>
  180. </ul>
  181. <div class="image">
  182. <img src="ThreadStatus.png" alt="ThreadStatus.png"/>
  183. <div class="caption">
  184. Thread State and State Transitions</div></div>
  185. <p> A CMSIS-RTOS assumes that threads are scheduled as shown in the figure <b>Thread State and State Transitions</b>. The thread states change as follows:</p>
  186. <ul>
  187. <li>A thread is created using the function <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a>. This puts the thread into the <b>READY</b> or <b>RUNNING</b> state (depending on the thread priority).</li>
  188. <li>CMSIS-RTOS is pre-emptive. The active thread with the highest priority becomes the <b>RUNNING</b> thread provided it does not wait for any event. The initial priority of a thread is defined with the <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaee93d929beb350f16e5cc7fa602e229f">osThreadDef</a> but may be changed during execution using the function <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga0dfb90ccf1f6e4b54b9251b12d1cbc8b">osThreadSetPriority</a>.</li>
  189. <li>The <b>RUNNING</b> thread transfers into the <b>WAITING</b> state when it is waiting for an event.</li>
  190. <li>Active threads can be terminated any time using the function <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a>. Threads can terminate also by just returning from the thread function. Threads that are terminated are in the <b>INACTIVE</b> state and typically do not consume any dynamic memory resources. </li>
  191. </ul>
  192. <h2 class="groupheader">Macro Definition Documentation</h2>
  193. <a class="anchor" id="gaf0c7c6b5e09f8be198312144b5c9e453"></a>
  194. <div class="memitem">
  195. <div class="memproto">
  196. <table class="memname">
  197. <tr>
  198. <td class="memname">#define osThread</td>
  199. <td>(</td>
  200. <td class="paramtype">&#160;</td>
  201. <td class="paramname">name</td><td>)</td>
  202. <td>&#160;&#160;&#160;&amp;os_thread_def_##name</td>
  203. </tr>
  204. </table>
  205. </div><div class="memdoc">
  206. <p>Access to the thread definition for the function <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a>.</p>
  207. <dl class="params"><dt>Parameters</dt><dd>
  208. <table class="params">
  209. <tr><td class="paramname">name</td><td>name of the thread definition object. </td></tr>
  210. </table>
  211. </dd>
  212. </dl>
  213. <dl class="section note"><dt>Note</dt><dd>CAN BE CHANGED: The parameter to <b>osThread</b> shall be consistent but the macro body is implementation specific in every CMSIS-RTOS. </dd></dl>
  214. </div>
  215. </div>
  216. <a class="anchor" id="gaee93d929beb350f16e5cc7fa602e229f"></a>
  217. <div class="memitem">
  218. <div class="memproto">
  219. <table class="memname">
  220. <tr>
  221. <td class="memname">#define osThreadDef</td>
  222. <td>(</td>
  223. <td class="paramtype">&#160;</td>
  224. <td class="paramname">name, </td>
  225. </tr>
  226. <tr>
  227. <td class="paramkey"></td>
  228. <td></td>
  229. <td class="paramtype">&#160;</td>
  230. <td class="paramname">priority, </td>
  231. </tr>
  232. <tr>
  233. <td class="paramkey"></td>
  234. <td></td>
  235. <td class="paramtype">&#160;</td>
  236. <td class="paramname">instances, </td>
  237. </tr>
  238. <tr>
  239. <td class="paramkey"></td>
  240. <td></td>
  241. <td class="paramtype">&#160;</td>
  242. <td class="paramname">stacksz&#160;</td>
  243. </tr>
  244. <tr>
  245. <td></td>
  246. <td>)</td>
  247. <td></td><td></td>
  248. </tr>
  249. </table>
  250. </div><div class="memdoc">
  251. <p>Define the attributes of a thread functions that can be created by the function <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> using <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a>. The argument <em>instances</em> defines the number of times that <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> can be called for the same <b>osThreadDef</b>.</p>
  252. <p><b>Code</b> <b>Example</b> </p>
  253. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  254. <div class="line"> </div>
  255. <div class="line"><span class="keywordtype">void</span> Thread (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg); <span class="comment">// function prototype for a Thread</span></div>
  256. <div class="line"><a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaee93d929beb350f16e5cc7fa602e229f">osThreadDef</a> (Thread, <a class="code" href="cmsis__os_8h.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1">osPriorityNormal</a>, 3, 0); <span class="comment">// define Thread and specify to allow three instances</span></div>
  257. <div class="line"> </div>
  258. <div class="line"><span class="keywordtype">void</span> ThreadCreate_example (<span class="keywordtype">void</span>) {</div>
  259. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id1, id2, id3;</div>
  260. <div class="line"> </div>
  261. <div class="line"> id1 = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (<a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a> (Thread), NULL); <span class="comment">// create the thread with id1</span></div>
  262. <div class="line"> id2 = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (<a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a> (Thread), NULL); <span class="comment">// create the thread with id2</span></div>
  263. <div class="line"> id3 = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (<a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a> (Thread), NULL); <span class="comment">// create the thread with id3</span></div>
  264. <div class="line"> <span class="keywordflow">if</span> (id1 == NULL) { <span class="comment">// handle thread creation for id1</span></div>
  265. <div class="line"> <span class="comment">// Failed to create the thread with id1</span></div>
  266. <div class="line"> }</div>
  267. <div class="line"> <span class="keywordflow">if</span> (id2 == NULL) { <span class="comment">// handle thread creation for id2</span></div>
  268. <div class="line"> <span class="comment">// Failed to create the thread with id2</span></div>
  269. <div class="line"> }</div>
  270. <div class="line"> <span class="keywordflow">if</span> (id3 == NULL) { <span class="comment">// handle thread creation for id3</span></div>
  271. <div class="line"> <span class="comment">// Failed to create the thread with id3</span></div>
  272. <div class="line"> }</div>
  273. <div class="line"> :</div>
  274. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (id1); <span class="comment">// stop the thread with id1</span></div>
  275. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (id2); <span class="comment">// stop the thread with id2</span></div>
  276. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (id3); <span class="comment">// stop the thread with id3</span></div>
  277. <div class="line">}</div>
  278. </div><!-- fragment --><dl class="params"><dt>Parameters</dt><dd>
  279. <table class="params">
  280. <tr><td class="paramname">name</td><td>name of the thread function. </td></tr>
  281. <tr><td class="paramname">priority</td><td>initial priority of the thread function. </td></tr>
  282. <tr><td class="paramname">instances</td><td>number of possible thread instances. </td></tr>
  283. <tr><td class="paramname">stacksz</td><td>stack size (in bytes) requirements for the thread function. </td></tr>
  284. </table>
  285. </dd>
  286. </dl>
  287. <dl class="section note"><dt>Note</dt><dd>CAN BE CHANGED: The parameters to <b>osThreadDef</b> shall be consistent but the macro body is implementation specific in every CMSIS-RTOS. </dd></dl>
  288. </div>
  289. </div>
  290. <h2 class="groupheader">Enumeration Type Documentation</h2>
  291. <a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849a"></a>
  292. <div class="memitem">
  293. <div class="memproto">
  294. <table class="memname">
  295. <tr>
  296. <td class="memname">enum <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a></td>
  297. </tr>
  298. </table>
  299. </div><div class="memdoc">
  300. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osPriority</b> shall be consistent in every CMSIS-RTOS.</dd>
  301. <dd>
  302. Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
  303. <p>The <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> value specifies the priority for a thread. The default thread priority should be <em>osPriorityNormal</em>. If a Thread is active that has a higher priority than the currently executing thread, then a thread switch occurs immediately to execute the new task.</p>
  304. <p>To prevent from a priority inversion, a CMSIS-RTOS compliant OS may optionally implement a <b>priority inheritance</b> method. A priority inversion occurs when a high priority thread is waiting for a resource or event that is controlled by a thread with a lower priority. </p>
  305. <table class="fieldtable">
  306. <tr><th colspan="2">Enumerator</th></tr><tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa549e79a43ff4f8b2b31afb613f5caa81"></a>osPriorityIdle</em>&#160;</td><td class="fielddoc">
  307. <p>priority: idle (lowest) </p>
  308. </td></tr>
  309. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa61cb822239ac8f66dfbdc7291598a3d4"></a>osPriorityLow</em>&#160;</td><td class="fielddoc">
  310. <p>priority: low </p>
  311. </td></tr>
  312. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa193b650117c209b4a203954542bcc3e6"></a>osPriorityBelowNormal</em>&#160;</td><td class="fielddoc">
  313. <p>priority: below normal </p>
  314. </td></tr>
  315. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1"></a>osPriorityNormal</em>&#160;</td><td class="fielddoc">
  316. <p>priority: normal (default) </p>
  317. </td></tr>
  318. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa17b36cd9cd38652c2bc6d4803990674b"></a>osPriorityAboveNormal</em>&#160;</td><td class="fielddoc">
  319. <p>priority: above normal </p>
  320. </td></tr>
  321. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa914433934143a9ba767e59577c56e6c2"></a>osPriorityHigh</em>&#160;</td><td class="fielddoc">
  322. <p>priority: high </p>
  323. </td></tr>
  324. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aa1485dec3702434a1ec3cb74c7a17a4af"></a>osPriorityRealtime</em>&#160;</td><td class="fielddoc">
  325. <p>priority: realtime (highest) </p>
  326. </td></tr>
  327. <tr><td class="fieldname"><em><a class="anchor" id="ga7f2b42f1983b9107775ec2a1c69a849aae35f5e2f9c64ad346822521b643bdea4"></a>osPriorityError</em>&#160;</td><td class="fielddoc">
  328. <p>system cannot determine priority or thread has illegal priority </p>
  329. </td></tr>
  330. </table>
  331. </div>
  332. </div>
  333. <h2 class="groupheader">Function Documentation</h2>
  334. <a class="anchor" id="gac59b5713cb083702dce759c73fd90dff"></a>
  335. <div class="memitem">
  336. <div class="memproto">
  337. <table class="memname">
  338. <tr>
  339. <td class="memname"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> osThreadCreate </td>
  340. <td>(</td>
  341. <td class="paramtype">const <a class="el" href="cmsis__os_8h.html#structosThreadDef__t">osThreadDef_t</a> *&#160;</td>
  342. <td class="paramname"><em>thread_def</em>, </td>
  343. </tr>
  344. <tr>
  345. <td class="paramkey"></td>
  346. <td></td>
  347. <td class="paramtype">void *&#160;</td>
  348. <td class="paramname"><em>argument</em>&#160;</td>
  349. </tr>
  350. <tr>
  351. <td></td>
  352. <td>)</td>
  353. <td></td><td></td>
  354. </tr>
  355. </table>
  356. </div><div class="memdoc">
  357. <dl class="params"><dt>Parameters</dt><dd>
  358. <table class="params">
  359. <tr><td class="paramdir">[in]</td><td class="paramname">thread_def</td><td>thread definition referenced with <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a>. </td></tr>
  360. <tr><td class="paramdir">[in]</td><td class="paramname">argument</td><td>pointer that is passed to the thread function as start argument. </td></tr>
  361. </table>
  362. </dd>
  363. </dl>
  364. <dl class="section return"><dt>Returns</dt><dd>thread ID for reference by other functions or NULL in case of error. </dd></dl>
  365. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadCreate</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  366. <p>Start a thread function by adding it to the Active Threads list and set it to state <b>READY</b>. The thread function receives the <em>argument</em> pointer as function argument when the function is started. When the priority of the created thread function is higher than the current <b>RUNNING</b> thread, the created thread function starts instantly and becomes the new <b>RUNNING</b> thread.</p>
  367. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
  368. <p><b>Code Example</b> </p>
  369. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  370. <div class="line"> </div>
  371. <div class="line"><span class="keywordtype">void</span> Thread_1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg); <span class="comment">// function prototype for Thread_1</span></div>
  372. <div class="line"><a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaee93d929beb350f16e5cc7fa602e229f">osThreadDef</a> (Thread_1, <a class="code" href="cmsis__os_8h.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1">osPriorityNormal</a>, 1, 0); <span class="comment">// define Thread_1</span></div>
  373. <div class="line"> </div>
  374. <div class="line"><span class="keywordtype">void</span> ThreadCreate_example (<span class="keywordtype">void</span>) {</div>
  375. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id;</div>
  376. <div class="line"> </div>
  377. <div class="line"> <span class="keywordtype">id</span> = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (<a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a> (Thread_1), NULL); <span class="comment">// create the thread</span></div>
  378. <div class="line"> <span class="keywordflow">if</span> (<span class="keywordtype">id</span> == NULL) { <span class="comment">// handle thread creation</span></div>
  379. <div class="line"> <span class="comment">// Failed to create a thread</span></div>
  380. <div class="line"> }</div>
  381. <div class="line"> :</div>
  382. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (<span class="keywordtype">id</span>); <span class="comment">// stop the thread</span></div>
  383. <div class="line">}</div>
  384. </div><!-- fragment -->
  385. </div>
  386. </div>
  387. <a class="anchor" id="gab1df2a28925862ef8f9cf4e1c995c5a7"></a>
  388. <div class="memitem">
  389. <div class="memproto">
  390. <table class="memname">
  391. <tr>
  392. <td class="memname"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> osThreadGetId </td>
  393. <td>(</td>
  394. <td class="paramtype">void&#160;</td>
  395. <td class="paramname"></td><td>)</td>
  396. <td></td>
  397. </tr>
  398. </table>
  399. </div><div class="memdoc">
  400. <dl class="section return"><dt>Returns</dt><dd>thread ID for reference by other functions or NULL in case of error. </dd></dl>
  401. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadGetId</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  402. <p>Get the thread ID of the current running thread.</p>
  403. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>. </dd>
  404. <dd>
  405. Must not be used inside the idle daemon. This would lead to undefined behavior.</dd></dl>
  406. <p><b>Code Example</b> </p>
  407. <div class="fragment"><div class="line"><span class="keywordtype">void</span> ThreadGetId_example (<span class="keywordtype">void</span>) {</div>
  408. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id; <span class="comment">// id for the currently running thread</span></div>
  409. <div class="line"> </div>
  410. <div class="line"> <span class="keywordtype">id</span> = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a> ();</div>
  411. <div class="line"> <span class="keywordflow">if</span> (<span class="keywordtype">id</span> == NULL) {</div>
  412. <div class="line"> <span class="comment">// Failed to get the id; not in a thread</span></div>
  413. <div class="line"> }</div>
  414. <div class="line">}</div>
  415. </div><!-- fragment -->
  416. </div>
  417. </div>
  418. <a class="anchor" id="ga4299d838978bc2aae5e4350754e6a4e9"></a>
  419. <div class="memitem">
  420. <div class="memproto">
  421. <table class="memname">
  422. <tr>
  423. <td class="memname"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> osThreadGetPriority </td>
  424. <td>(</td>
  425. <td class="paramtype"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a>&#160;</td>
  426. <td class="paramname"><em>thread_id</em></td><td>)</td>
  427. <td></td>
  428. </tr>
  429. </table>
  430. </div><div class="memdoc">
  431. <dl class="params"><dt>Parameters</dt><dd>
  432. <table class="params">
  433. <tr><td class="paramdir">[in]</td><td class="paramname">thread_id</td><td>thread ID obtained by <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> or <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a>. </td></tr>
  434. </table>
  435. </dd>
  436. </dl>
  437. <dl class="section return"><dt>Returns</dt><dd>current priority value of the thread function. </dd></dl>
  438. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadGetPriority</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  439. <p>Get the priority of an active thread. In case of a failure the value <b>osPriorityError</b> is returned.</p>
  440. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
  441. <p><b>Code Example</b> </p>
  442. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  443. <div class="line"> </div>
  444. <div class="line"><span class="keywordtype">void</span> Thread_1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg) { <span class="comment">// Thread function</span></div>
  445. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id; <span class="comment">// id for the currently running thread</span></div>
  446. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> priority; <span class="comment">// thread priority</span></div>
  447. <div class="line"> </div>
  448. <div class="line"> <span class="keywordtype">id</span> = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a> (); <span class="comment">// Obtain ID of current running thread</span></div>
  449. <div class="line"> </div>
  450. <div class="line"> <span class="keywordflow">if</span> (<span class="keywordtype">id</span> != NULL) {</div>
  451. <div class="line"> priority = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#ga4299d838978bc2aae5e4350754e6a4e9">osThreadGetPriority</a> (<span class="keywordtype">id</span>);</div>
  452. <div class="line"> }</div>
  453. <div class="line"> <span class="keywordflow">else</span> {</div>
  454. <div class="line"> <span class="comment">// Failed to get the id</span></div>
  455. <div class="line"> }</div>
  456. <div class="line">}</div>
  457. </div><!-- fragment -->
  458. </div>
  459. </div>
  460. <a class="anchor" id="ga0dfb90ccf1f6e4b54b9251b12d1cbc8b"></a>
  461. <div class="memitem">
  462. <div class="memproto">
  463. <table class="memname">
  464. <tr>
  465. <td class="memname"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> osThreadSetPriority </td>
  466. <td>(</td>
  467. <td class="paramtype"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a>&#160;</td>
  468. <td class="paramname"><em>thread_id</em>, </td>
  469. </tr>
  470. <tr>
  471. <td class="paramkey"></td>
  472. <td></td>
  473. <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a>&#160;</td>
  474. <td class="paramname"><em>priority</em>&#160;</td>
  475. </tr>
  476. <tr>
  477. <td></td>
  478. <td>)</td>
  479. <td></td><td></td>
  480. </tr>
  481. </table>
  482. </div><div class="memdoc">
  483. <dl class="params"><dt>Parameters</dt><dd>
  484. <table class="params">
  485. <tr><td class="paramdir">[in]</td><td class="paramname">thread_id</td><td>thread ID obtained by <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> or <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a>. </td></tr>
  486. <tr><td class="paramdir">[in]</td><td class="paramname">priority</td><td>new priority value for the thread function. </td></tr>
  487. </table>
  488. </dd>
  489. </dl>
  490. <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function. </dd></dl>
  491. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadSetPriority</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  492. <p>Change the priority of an active thread.</p>
  493. <p><b><a class="el" href="group__CMSIS__RTOS__Status.html">Status and Error Codes</a></b><br/>
  494. </p>
  495. <ul>
  496. <li><em>osOK:</em> the priority of the specified thread has been successfully changed.</li>
  497. <li><em>osErrorParameter:</em> thread_id is incorrect.</li>
  498. <li><em>osErrorValue:</em> incorrect priority value.</li>
  499. <li><em>osErrorResource:</em> thread_id refers to a thread that is not an active thread.</li>
  500. <li><em>osErrorISR:</em> <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ga0dfb90ccf1f6e4b54b9251b12d1cbc8b">osThreadSetPriority</a> cannot be called from interrupt service routines.</li>
  501. </ul>
  502. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
  503. <p><b>Code Example</b> </p>
  504. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  505. <div class="line"> </div>
  506. <div class="line"><span class="keywordtype">void</span> Thread_1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg) { <span class="comment">// Thread function</span></div>
  507. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id; <span class="comment">// id for the currently running thread</span></div>
  508. <div class="line"> <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a">osPriority</a> pr; <span class="comment">// thread priority</span></div>
  509. <div class="line"> <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> status; <span class="comment">// status of the executed function</span></div>
  510. <div class="line"> </div>
  511. <div class="line"> : </div>
  512. <div class="line"> <span class="keywordtype">id</span> = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a> (); <span class="comment">// Obtain ID of current running thread</span></div>
  513. <div class="line"> </div>
  514. <div class="line"> <span class="keywordflow">if</span> (<span class="keywordtype">id</span> != NULL) {</div>
  515. <div class="line"> status = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#ga0dfb90ccf1f6e4b54b9251b12d1cbc8b">osThreadSetPriority</a> (<span class="keywordtype">id</span>, <a class="code" href="cmsis__os_8h.html#ga7f2b42f1983b9107775ec2a1c69a849aa193b650117c209b4a203954542bcc3e6">osPriorityBelowNormal</a>);</div>
  516. <div class="line"> <span class="keywordflow">if</span> (status == <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99ea9e1c9e2550bb4de8969a935acffc968f">osOK</a>) {</div>
  517. <div class="line"> <span class="comment">// Thread priority changed to BelowNormal</span></div>
  518. <div class="line"> }</div>
  519. <div class="line"> <span class="keywordflow">else</span> {</div>
  520. <div class="line"> <span class="comment">// Failed to set the priority</span></div>
  521. <div class="line"> }</div>
  522. <div class="line"> }</div>
  523. <div class="line"> <span class="keywordflow">else</span> {</div>
  524. <div class="line"> <span class="comment">// Failed to get the id</span></div>
  525. <div class="line"> }</div>
  526. <div class="line"> : </div>
  527. <div class="line">}</div>
  528. </div><!-- fragment -->
  529. </div>
  530. </div>
  531. <a class="anchor" id="gaea135bb90eb853eff39e0800b91bbeab"></a>
  532. <div class="memitem">
  533. <div class="memproto">
  534. <table class="memname">
  535. <tr>
  536. <td class="memname"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> osThreadTerminate </td>
  537. <td>(</td>
  538. <td class="paramtype"><a class="el" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a>&#160;</td>
  539. <td class="paramname"><em>thread_id</em></td><td>)</td>
  540. <td></td>
  541. </tr>
  542. </table>
  543. </div><div class="memdoc">
  544. <dl class="params"><dt>Parameters</dt><dd>
  545. <table class="params">
  546. <tr><td class="paramdir">[in]</td><td class="paramname">thread_id</td><td>thread ID obtained by <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> or <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a>. </td></tr>
  547. </table>
  548. </dd>
  549. </dl>
  550. <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function. </dd></dl>
  551. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadTerminate</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  552. <p>Remove the thread function from the active thread list. If the thread is currently RUNNING the execution will stop.</p>
  553. <dl class="section note"><dt>Note</dt><dd>In case that <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> terminates the currently running task, the function never returns and other threads that are in the READY state are started.</dd></dl>
  554. <p><b><a class="el" href="group__CMSIS__RTOS__Status.html">Status and Error Codes</a></b><br/>
  555. </p>
  556. <ul>
  557. <li><em>osOK:</em> the specified thread has been successfully terminated.</li>
  558. <li><em>osErrorParameter:</em> thread_id is incorrect.</li>
  559. <li><em>osErrorResource:</em> thread_id refers to a thread that is not an active thread.</li>
  560. <li><em>osErrorISR:</em> <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> cannot be called from interrupt service routines.</li>
  561. </ul>
  562. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>. </dd>
  563. <dd>
  564. Avoid calling the function with a <em>thread_id</em> that does not exist or has been terminated already.</dd></dl>
  565. <p><b>Code Example</b> </p>
  566. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  567. <div class="line"> </div>
  568. <div class="line"><span class="keywordtype">void</span> Thread_1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg); <span class="comment">// function prototype for Thread_1</span></div>
  569. <div class="line"><a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaee93d929beb350f16e5cc7fa602e229f">osThreadDef</a> (Thread_1, <a class="code" href="cmsis__os_8h.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1">osPriorityNormal</a>, 1, 0); <span class="comment">// define Thread_1</span></div>
  570. <div class="line"></div>
  571. <div class="line"><span class="keywordtype">void</span> ThreadTerminate_example (<span class="keywordtype">void</span>) {</div>
  572. <div class="line"> <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> status;</div>
  573. <div class="line"> <a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f">osThreadId</a> id;</div>
  574. <div class="line"> </div>
  575. <div class="line"> <span class="keywordtype">id</span> = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> (<a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453">osThread</a> (Thread_1), NULL); <span class="comment">// create the thread</span></div>
  576. <div class="line"> : </div>
  577. <div class="line"> status = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaea135bb90eb853eff39e0800b91bbeab">osThreadTerminate</a> (<span class="keywordtype">id</span>); <span class="comment">// stop the thread</span></div>
  578. <div class="line"> <span class="keywordflow">if</span> (status == <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99ea9e1c9e2550bb4de8969a935acffc968f">osOK</a>) {</div>
  579. <div class="line"> <span class="comment">// Thread was terminated successfully</span></div>
  580. <div class="line"> }</div>
  581. <div class="line"> <span class="keywordflow">else</span> {</div>
  582. <div class="line"> <span class="comment">// Failed to terminate a thread</span></div>
  583. <div class="line"> }</div>
  584. <div class="line">}</div>
  585. </div><!-- fragment -->
  586. </div>
  587. </div>
  588. <a class="anchor" id="gaf13a667493c5d629a90c13e113b99233"></a>
  589. <div class="memitem">
  590. <div class="memproto">
  591. <table class="memname">
  592. <tr>
  593. <td class="memname"><a class="el" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> osThreadYield </td>
  594. <td>(</td>
  595. <td class="paramtype">void&#160;</td>
  596. <td class="paramname"></td><td>)</td>
  597. <td></td>
  598. </tr>
  599. </table>
  600. </div><div class="memdoc">
  601. <dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function. </dd></dl>
  602. <dl class="section note"><dt>Note</dt><dd>MUST REMAIN UNCHANGED: <b>osThreadYield</b> shall be consistent in every CMSIS-RTOS.</dd></dl>
  603. <p>Pass control to the next thread that is in state <b>READY</b>. If there is no other thread in the state <b>READY</b>, the current thread continues execution and no thread switching occurs.</p>
  604. <p><b><a class="el" href="group__CMSIS__RTOS__Status.html">Status and Error Codes</a></b><br/>
  605. </p>
  606. <ul>
  607. <li><em>osOK:</em> the function has been correctly executed.</li>
  608. <li><em>osErrorISR:</em> <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf13a667493c5d629a90c13e113b99233">osThreadYield</a> cannot be called from interrupt service routines.</li>
  609. </ul>
  610. <dl class="section note"><dt>Note</dt><dd>Cannot be called from <a class="el" href="functionOverview.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
  611. <p><b>Code Example</b> </p>
  612. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
  613. <div class="line"> </div>
  614. <div class="line"><span class="keywordtype">void</span> Thread_1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *arg) { <span class="comment">// Thread function</span></div>
  615. <div class="line"> <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99e">osStatus</a> status; <span class="comment">// status of the executed function</span></div>
  616. <div class="line"> :</div>
  617. <div class="line"> <span class="keywordflow">while</span> (1) {</div>
  618. <div class="line"> status = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaf13a667493c5d629a90c13e113b99233">osThreadYield</a>(); <span class="comment">// </span></div>
  619. <div class="line"> <span class="keywordflow">if</span> (status != <a class="code" href="group__CMSIS__RTOS__Status.html#gae2e091fefc4c767117727bd5aba4d99ea9e1c9e2550bb4de8969a935acffc968f">osOK</a>) {</div>
  620. <div class="line"> <span class="comment">// thread switch not occurred, not in a thread function</span></div>
  621. <div class="line"> }</div>
  622. <div class="line"> }</div>
  623. <div class="line">}</div>
  624. </div><!-- fragment -->
  625. </div>
  626. </div>
  627. </div><!-- contents -->
  628. </div><!-- doc-content -->
  629. <!-- start footer part -->
  630. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  631. <ul>
  632. <li class="footer">Generated on Thu Apr 9 2020 15:49:56 for CMSIS-RTOS Version 1.03 by Arm Ltd. All rights reserved.
  633. <!--
  634. <a href="http://www.doxygen.org/index.html">
  635. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.6
  636. -->
  637. </li>
  638. </ul>
  639. </div>
  640. </body>
  641. </html>