gnuk/ChibiOS_2.0.8/docs/html/chthreads_8c_source.html

567 lines
72 KiB
HTML
Raw Normal View History

2010-08-10 03:11:02 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>ChibiOS/RT: chthreads.c Source File</title>
<link href="custom.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<table style="text-align: center; width: 100%;" border="0"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 80px;"><img alt="ChibiOS/RT Logo" src="logo_small.png"></td>
<td><big><big>ChibiOS/RT</big></big><br><br>Architecture - Reference Manual - Guides</td>
<td style="width: 80px;"></td>
</tr>
</tbody>
</table>
<hr size="1">
2010-11-22 05:53:37 +00:00
<!-- Generated by Doxygen 1.7.1 -->
2010-08-10 03:11:02 +00:00
<div class="navigation" id="top">
<div class="tabs">
2010-11-22 05:53:37 +00:00
<ul class="tablist">
2010-08-10 03:11:02 +00:00
<li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
2010-11-22 05:53:37 +00:00
<div class="tabs2">
<ul class="tablist">
2010-08-10 03:11:02 +00:00
<li><a href="files.html"><span>File&nbsp;List</span></a></li>
<li><a href="globals.html"><span>Globals</span></a></li>
</ul>
</div>
2010-11-22 05:53:37 +00:00
<div class="header">
<div class="headertitle">
<h1>chthreads.c</h1> </div>
</div>
<div class="contents">
<a href="chthreads_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
2010-08-10 03:11:02 +00:00
<a name="l00002"></a>00002 <span class="comment"> ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.</span>
<a name="l00003"></a>00003 <span class="comment"></span>
<a name="l00004"></a>00004 <span class="comment"> This file is part of ChibiOS/RT.</span>
<a name="l00005"></a>00005 <span class="comment"></span>
<a name="l00006"></a>00006 <span class="comment"> ChibiOS/RT is free software; you can redistribute it and/or modify</span>
<a name="l00007"></a>00007 <span class="comment"> it under the terms of the GNU General Public License as published by</span>
<a name="l00008"></a>00008 <span class="comment"> the Free Software Foundation; either version 3 of the License, or</span>
<a name="l00009"></a>00009 <span class="comment"> (at your option) any later version.</span>
<a name="l00010"></a>00010 <span class="comment"></span>
<a name="l00011"></a>00011 <span class="comment"> ChibiOS/RT is distributed in the hope that it will be useful,</span>
<a name="l00012"></a>00012 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00013"></a>00013 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<a name="l00014"></a>00014 <span class="comment"> GNU General Public License for more details.</span>
<a name="l00015"></a>00015 <span class="comment"></span>
<a name="l00016"></a>00016 <span class="comment"> You should have received a copy of the GNU General Public License</span>
<a name="l00017"></a>00017 <span class="comment"> along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<a name="l00018"></a>00018 <span class="comment"></span>
<a name="l00019"></a>00019 <span class="comment"> ---</span>
<a name="l00020"></a>00020 <span class="comment"></span>
<a name="l00021"></a>00021 <span class="comment"> A special exception to the GPL can be applied should you wish to distribute</span>
<a name="l00022"></a>00022 <span class="comment"> a combined work that includes ChibiOS/RT, without being obliged to provide</span>
<a name="l00023"></a>00023 <span class="comment"> the source code for any proprietary components. See the file exception.txt</span>
<a name="l00024"></a>00024 <span class="comment"> for full details of how and when the exception can be applied.</span>
<a name="l00025"></a>00025 <span class="comment">*/</span>
<a name="l00026"></a>00026 <span class="comment"></span>
<a name="l00027"></a>00027 <span class="comment">/**</span>
<a name="l00028"></a>00028 <span class="comment"> * @file chthreads.c</span>
<a name="l00029"></a>00029 <span class="comment"> * @brief Threads code.</span>
<a name="l00030"></a>00030 <span class="comment"> *</span>
<a name="l00031"></a>00031 <span class="comment"> * @addtogroup threads</span>
<a name="l00032"></a>00032 <span class="comment"> * @details Threads related APIs and services.</span>
<a name="l00033"></a>00033 <span class="comment"> *</span>
<a name="l00034"></a>00034 <span class="comment"> * &lt;h2&gt;Operation mode&lt;/h2&gt;</span>
<a name="l00035"></a>00035 <span class="comment"> * A thread is an abstraction of an independent instructions flow.</span>
<a name="l00036"></a>00036 <span class="comment"> * In ChibiOS/RT a thread is represented by a &quot;C&quot; function owning</span>
<a name="l00037"></a>00037 <span class="comment"> * a processor context, state informations and a dedicated stack</span>
<a name="l00038"></a>00038 <span class="comment"> * area. In this scenario static variables are shared among all</span>
<a name="l00039"></a>00039 <span class="comment"> * threads while automatic variables are local to the thread.&lt;br&gt;</span>
<a name="l00040"></a>00040 <span class="comment"> * Operations defined for threads:</span>
<a name="l00041"></a>00041 <span class="comment"> * - &lt;b&gt;Init&lt;/b&gt;, a thread is prepared and put in the suspended</span>
<a name="l00042"></a>00042 <span class="comment"> * state.</span>
<a name="l00043"></a>00043 <span class="comment"> * - &lt;b&gt;Create&lt;/b&gt;, a thread is started on the specified thread</span>
<a name="l00044"></a>00044 <span class="comment"> * function. This operation is available in multiple variants,</span>
<a name="l00045"></a>00045 <span class="comment"> * both static and dynamic.</span>
<a name="l00046"></a>00046 <span class="comment"> * - &lt;b&gt;Exit&lt;/b&gt;, a thread terminates by returning from its top</span>
<a name="l00047"></a>00047 <span class="comment"> * level function or invoking a specific API, the thread can</span>
<a name="l00048"></a>00048 <span class="comment"> * return a value that can be retrieved by other threads.</span>
<a name="l00049"></a>00049 <span class="comment"> * - &lt;b&gt;Wait&lt;/b&gt;, a thread waits for the termination of another</span>
<a name="l00050"></a>00050 <span class="comment"> * thread and retrieves its return value.</span>
<a name="l00051"></a>00051 <span class="comment"> * - &lt;b&gt;Resume&lt;/b&gt;, a thread created in suspended state is started.</span>
<a name="l00052"></a>00052 <span class="comment"> * - &lt;b&gt;Sleep&lt;/b&gt;, the execution of a thread is suspended for the</span>
<a name="l00053"></a>00053 <span class="comment"> * specified amount of time or the specified future absolute time</span>
<a name="l00054"></a>00054 <span class="comment"> * is reached.</span>
<a name="l00055"></a>00055 <span class="comment"> * - &lt;b&gt;SetPriority&lt;/b&gt;, a thread changes its own priority level.</span>
<a name="l00056"></a>00056 <span class="comment"> * - &lt;b&gt;Yield&lt;/b&gt;, a thread voluntarily renounces to its time slot.</span>
<a name="l00057"></a>00057 <span class="comment"> * .</span>
<a name="l00058"></a>00058 <span class="comment"> * The threads subsystem is implicitly included in kernel however</span>
<a name="l00059"></a>00059 <span class="comment"> * some of its part may be excluded by disabling them in @p chconf.h,</span>
<a name="l00060"></a>00060 <span class="comment"> * see the @p CH_USE_WAITEXIT and @p CH_USE_DYNAMIC configuration</span>
<a name="l00061"></a>00061 <span class="comment"> * options.</span>
<a name="l00062"></a>00062 <span class="comment"> * @{</span>
<a name="l00063"></a>00063 <span class="comment"> */</span>
<a name="l00064"></a>00064
<a name="l00065"></a>00065 <span class="preprocessor">#include &quot;<a class="code" href="ch_8h.html" title="ChibiOS/RT main include file.">ch.h</a>&quot;</span>
<a name="l00066"></a>00066 <span class="comment"></span>
<a name="l00067"></a>00067 <span class="comment">/**</span>
<a name="l00068"></a>00068 <span class="comment"> * @brief Initializes a thread structure.</span>
<a name="l00069"></a>00069 <span class="comment"> *</span>
<a name="l00070"></a>00070 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00071"></a>00071 <span class="comment"> * @param[in] prio the priority level for the new thread</span>
<a name="l00072"></a>00072 <span class="comment"> * @return The same thread pointer passed as parameter.</span>
<a name="l00073"></a>00073 <span class="comment"> */</span>
<a name="l00074"></a><a class="code" href="group__threads.html#ga4f6ae58bb3cedfc03aacde6d49e49d35">00074</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#ga4f6ae58bb3cedfc03aacde6d49e49d35" title="Initializes a thread structure.">init_thread</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp, <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> prio) {
<a name="l00075"></a>00075
2010-11-22 05:53:37 +00:00
<a name="l00076"></a>00076 tp-&gt;<a class="code" href="struct_thread.html#ae60cc22956f4c19965cb94045333aa9c" title="Thread priority.">p_prio</a> = prio;
<a name="l00077"></a>00077 tp-&gt;<a class="code" href="struct_thread.html#a3c3e9d6abccb62c3a5fab6ef043a391f" title="Current thread state.">p_state</a> = THD_STATE_SUSPENDED;
<a name="l00078"></a>00078 tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> = THD_MEM_MODE_STATIC;
<a name="l00079"></a>00079 <span class="preprocessor">#if CH_USE_MUTEXES</span>
<a name="l00080"></a>00080 <span class="preprocessor"></span> tp-&gt;<a class="code" href="struct_thread.html#a38de11dd7f8cdcfbc45b2c467f400fe8" title="Thread&amp;#39;s own, non-inherited, priority.">p_realprio</a> = prio;
<a name="l00081"></a>00081 tp-&gt;<a class="code" href="struct_thread.html#a3ae80b4b3de4c7a23c542ca251e1f017" title="List of the mutexes owned by this thread.">p_mtxlist</a> = NULL;
<a name="l00082"></a>00082 <span class="preprocessor">#endif</span>
<a name="l00083"></a>00083 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_EVENTS</span>
<a name="l00084"></a>00084 <span class="preprocessor"></span> tp-&gt;<a class="code" href="struct_thread.html#aa571d2963bd8100fa210715dc5669522" title="Pending events mask.">p_epending</a> = 0;
<a name="l00085"></a>00085 <span class="preprocessor">#endif</span>
<a name="l00086"></a>00086 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_NESTED_LOCKS</span>
<a name="l00087"></a>00087 <span class="preprocessor"></span> tp-&gt;p_locks = 0;
<a name="l00088"></a>00088 <span class="preprocessor">#endif</span>
<a name="l00089"></a>00089 <span class="preprocessor"></span><span class="preprocessor">#if CH_DBG_THREADS_PROFILING</span>
<a name="l00090"></a>00090 <span class="preprocessor"></span> tp-&gt;<a class="code" href="struct_thread.html#a723aab9bf515e789c3d9ea81f9dbd6a0" title="Thread consumed time in ticks.">p_time</a> = 0;
<a name="l00091"></a>00091 <span class="preprocessor">#endif</span>
<a name="l00092"></a>00092 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_DYNAMIC</span>
<a name="l00093"></a>00093 <span class="preprocessor"></span> tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a> = 1;
2010-08-10 03:11:02 +00:00
<a name="l00094"></a>00094 <span class="preprocessor">#endif</span>
<a name="l00095"></a>00095 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_WAITEXIT</span>
<a name="l00096"></a>00096 <span class="preprocessor"></span> <a class="code" href="group__internals.html#gaaef72a9005ecd65e7b5c22e95ad82388" title="Threads list initialization.">list_init</a>(&amp;tp-&gt;<a class="code" href="struct_thread.html#ad1088bc1f9970cc102dc28000134e9ab" title="Termination waiting list.">p_waiting</a>);
<a name="l00097"></a>00097 <span class="preprocessor">#endif</span>
<a name="l00098"></a>00098 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_MESSAGES</span>
<a name="l00099"></a>00099 <span class="preprocessor"></span> <a class="code" href="group__internals.html#ga6d19d0f3ff1fa599b6d9783dec180451" title="Threads queue initialization.">queue_init</a>(&amp;tp-&gt;<a class="code" href="struct_thread.html#a282cb59bdbe00fcdf9741c47b735fa1f" title="Messages queue.">p_msgqueue</a>);
<a name="l00100"></a>00100 <span class="preprocessor">#endif</span>
2010-11-22 05:53:37 +00:00
<a name="l00101"></a>00101 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_REGISTRY</span>
<a name="l00102"></a>00102 <span class="preprocessor"></span> <a class="code" href="group__registry.html#ga365d4805b1329b068d9211b680f43388" title="Adds a thread to the registry list.">REG_INSERT</a>(tp);
2010-08-10 03:11:02 +00:00
<a name="l00103"></a>00103 <span class="preprocessor">#endif</span>
2010-11-22 05:53:37 +00:00
<a name="l00104"></a>00104 <span class="preprocessor"></span><span class="preprocessor">#if defined(THREAD_EXT_INIT)</span>
<a name="l00105"></a>00105 <span class="preprocessor"></span> <a class="code" href="group__config.html#ga758e6ac94f0ad132cd747325719e4d2f" title="Threads initialization hook.">THREAD_EXT_INIT</a>(tp);
<a name="l00106"></a>00106 <span class="preprocessor">#endif</span>
<a name="l00107"></a>00107 <span class="preprocessor"></span> <span class="keywordflow">return</span> tp;
<a name="l00108"></a>00108 }
<a name="l00109"></a>00109
<a name="l00110"></a>00110 <span class="preprocessor">#if CH_DBG_FILL_THREADS</span>
<a name="l00111"></a>00111 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keywordtype">void</span> memfill(<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *startp, <a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *endp, <a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> v) {
<a name="l00112"></a>00112
<a name="l00113"></a>00113 <span class="keywordflow">while</span> (startp &lt; endp)
<a name="l00114"></a>00114 *startp++ = v;
<a name="l00115"></a>00115 }
<a name="l00116"></a>00116 <span class="preprocessor">#endif</span>
<a name="l00117"></a>00117 <span class="preprocessor"></span><span class="comment"></span>
<a name="l00118"></a>00118 <span class="comment">/**</span>
<a name="l00119"></a>00119 <span class="comment"> * @brief Creates a new thread into a static memory area.</span>
<a name="l00120"></a>00120 <span class="comment"> * @details The new thread is initialized but not inserted in the ready list,</span>
<a name="l00121"></a>00121 <span class="comment"> * the initial state is @p THD_STATE_SUSPENDED.</span>
<a name="l00122"></a>00122 <span class="comment"> * @note A thread can terminate by calling @p chThdExit() or by simply</span>
<a name="l00123"></a>00123 <span class="comment"> * returning from its main function.</span>
<a name="l00124"></a>00124 <span class="comment"> * @note Threads created using this function do not obey to the</span>
<a name="l00125"></a>00125 <span class="comment"> * @p CH_DBG_FILL_THREADS debug option because it would keep</span>
<a name="l00126"></a>00126 <span class="comment"> * the kernel locked for too much time.</span>
<a name="l00127"></a>00127 <span class="comment"> *</span>
<a name="l00128"></a>00128 <span class="comment"> * @param[out] wsp pointer to a working area dedicated to the thread stack</span>
<a name="l00129"></a>00129 <span class="comment"> * @param[in] size size of the working area</span>
<a name="l00130"></a>00130 <span class="comment"> * @param[in] prio the priority level for the new thread</span>
<a name="l00131"></a>00131 <span class="comment"> * @param[in] pf the thread function</span>
<a name="l00132"></a>00132 <span class="comment"> * @param[in] arg an argument passed to the thread function. It can be</span>
<a name="l00133"></a>00133 <span class="comment"> * @p NULL.</span>
<a name="l00134"></a>00134 <span class="comment"> * @return The pointer to the @p Thread structure allocated for</span>
<a name="l00135"></a>00135 <span class="comment"> * the thread into the working space area.</span>
<a name="l00136"></a>00136 <span class="comment"> */</span>
<a name="l00137"></a><a class="code" href="group__threads.html#gac6de13d4044b32d3ae9e2066b5d4c030">00137</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#gac6de13d4044b32d3ae9e2066b5d4c030" title="Creates a new thread into a static memory area.">chThdCreateI</a>(<span class="keywordtype">void</span> *wsp, <span class="keywordtype">size_t</span> size,
<a name="l00138"></a>00138 <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> prio, <a class="code" href="group__threads.html#gabfe59aa7d2ed1b8e57a2ce17bcbc1189" title="Thread function.">tfunc_t</a> pf, <span class="keywordtype">void</span> *arg) {
<a name="l00139"></a>00139 <span class="comment">/* Thread structure is layed out in the lower part of the thread workspace */</span>
<a name="l00140"></a>00140 <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp = wsp;
<a name="l00141"></a>00141
<a name="l00142"></a>00142 <a class="code" href="group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title="Function parameter check.">chDbgCheck</a>((wsp != NULL) &amp;&amp; (size &gt;= <a class="code" href="group__core.html#gaf7a83c1c8bde96b77299c36dc598d33d" title="Computes the thread working area global size.">THD_WA_SIZE</a>(0)) &amp;&amp;
<a name="l00143"></a>00143 (prio &lt;= <a class="code" href="group__scheduler.html#ga0621c307e30868052b802fbc821297e8" title="Highest user priority.">HIGHPRIO</a>) &amp;&amp; (pf != NULL),
<a name="l00144"></a>00144 <span class="stringliteral">&quot;chThdCreateI&quot;</span>);
<a name="l00145"></a>00145 <a class="code" href="group__core.html#gaf5777aa68e4c4786b577953c7237a95d" title="Platform dependent part of the chThdInit() API.">SETUP_CONTEXT</a>(wsp, size, pf, arg);
<a name="l00146"></a>00146 <span class="keywordflow">return</span> <a class="code" href="group__threads.html#ga4f6ae58bb3cedfc03aacde6d49e49d35" title="Initializes a thread structure.">init_thread</a>(tp, prio);
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148 <span class="comment"></span>
<a name="l00149"></a>00149 <span class="comment">/**</span>
<a name="l00150"></a>00150 <span class="comment"> * @brief Creates a new thread into a static memory area.</span>
<a name="l00151"></a>00151 <span class="comment"> * @note A thread can terminate by calling @p chThdExit() or by simply</span>
<a name="l00152"></a>00152 <span class="comment"> * returning from its main function.</span>
<a name="l00153"></a>00153 <span class="comment"> *</span>
<a name="l00154"></a>00154 <span class="comment"> * @param[out] wsp pointer to a working area dedicated to the thread stack</span>
<a name="l00155"></a>00155 <span class="comment"> * @param[in] size size of the working area</span>
<a name="l00156"></a>00156 <span class="comment"> * @param[in] prio the priority level for the new thread</span>
<a name="l00157"></a>00157 <span class="comment"> * @param[in] pf the thread function</span>
<a name="l00158"></a>00158 <span class="comment"> * @param[in] arg an argument passed to the thread function. It can be</span>
<a name="l00159"></a>00159 <span class="comment"> * @p NULL.</span>
<a name="l00160"></a>00160 <span class="comment"> * @return The pointer to the @p Thread structure allocated for</span>
<a name="l00161"></a>00161 <span class="comment"> * the thread into the working space area.</span>
<a name="l00162"></a>00162 <span class="comment"> */</span>
<a name="l00163"></a><a class="code" href="group__threads.html#ga048e588238dd336c6059133b3d0c3435">00163</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#ga048e588238dd336c6059133b3d0c3435" title="Creates a new thread into a static memory area.">chThdCreateStatic</a>(<span class="keywordtype">void</span> *wsp, <span class="keywordtype">size_t</span> size,
<a name="l00164"></a>00164 <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> prio, <a class="code" href="group__threads.html#gabfe59aa7d2ed1b8e57a2ce17bcbc1189" title="Thread function.">tfunc_t</a> pf, <span class="keywordtype">void</span> *arg) {
<a name="l00165"></a>00165 <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp;
<a name="l00166"></a>00166
<a name="l00167"></a>00167 <span class="preprocessor">#if CH_DBG_FILL_THREADS</span>
<a name="l00168"></a>00168 <span class="preprocessor"></span> memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp, (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>), <a class="code" href="group__debug.html#gad8d58eb160d991692ecc3bd4e04e0cbd" title="Fill value for thread area in debug mode.">THREAD_FILL_VALUE</a>);
<a name="l00169"></a>00169 memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>),
<a name="l00170"></a>00170 (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + size, <a class="code" href="group__debug.html#ga1379fe3590594def45a6a6c7a4faa41b" title="Fill value for thread stack area in debug mode.">STACK_FILL_VALUE</a>);
<a name="l00171"></a>00171 <span class="preprocessor">#endif</span>
<a name="l00172"></a>00172 <span class="preprocessor"></span> <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00173"></a>00173 <a class="code" href="group__scheduler.html#gae47a8819a21ef2d521124e76bfb37c95" title="Wakes up a thread.">chSchWakeupS</a>(tp = <a class="code" href="group__threads.html#gac6de13d4044b32d3ae9e2066b5d4c030" title="Creates a new thread into a static memory area.">chThdCreateI</a>(wsp, size, prio, pf, arg), <a class="code" href="group__scheduler.html#gae1c86078318e11c2fa57ee3c2ec69f61" title="Default thread wakeup low level message.">RDY_OK</a>);
<a name="l00174"></a>00174 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00175"></a>00175 <span class="keywordflow">return</span> tp;
<a name="l00176"></a>00176 }
<a name="l00177"></a>00177
<a name="l00178"></a>00178 <span class="preprocessor">#if CH_USE_DYNAMIC &amp;&amp; CH_USE_HEAP</span>
<a name="l00179"></a>00179 <span class="preprocessor"></span><span class="comment">/**</span>
<a name="l00180"></a>00180 <span class="comment"> * @brief Creates a new thread allocating the memory from the heap.</span>
<a name="l00181"></a>00181 <span class="comment"> * @note A thread can terminate by calling @p chThdExit() or by simply</span>
<a name="l00182"></a>00182 <span class="comment"> * returning from its main function.</span>
<a name="l00183"></a>00183 <span class="comment"> * @note The memory allocated for the thread is not released when the thread</span>
<a name="l00184"></a>00184 <span class="comment"> * terminates but when a @p chThdWait() is performed.</span>
<a name="l00185"></a>00185 <span class="comment"> * @note The function is available only if the @p CH_USE_DYNAMIC,</span>
<a name="l00186"></a>00186 <span class="comment"> * @p CH_USE_HEAP and @p CH_USE_WAITEXIT options are enabled</span>
<a name="l00187"></a>00187 <span class="comment"> * in @p chconf.h.</span>
<a name="l00188"></a>00188 <span class="comment"> *</span>
<a name="l00189"></a>00189 <span class="comment"> * @param[in] heapp heap from which allocate the memory or @p NULL for the</span>
<a name="l00190"></a>00190 <span class="comment"> * default heap</span>
<a name="l00191"></a>00191 <span class="comment"> * @param[in] size size of the working area to be allocated</span>
<a name="l00192"></a>00192 <span class="comment"> * @param[in] prio the priority level for the new thread</span>
<a name="l00193"></a>00193 <span class="comment"> * @param[in] pf the thread function</span>
<a name="l00194"></a>00194 <span class="comment"> * @param[in] arg an argument passed to the thread function. It can be</span>
<a name="l00195"></a>00195 <span class="comment"> * @p NULL.</span>
<a name="l00196"></a>00196 <span class="comment"> * @return The pointer to the @p Thread structure allocated for</span>
<a name="l00197"></a>00197 <span class="comment"> * the thread into the working space area.</span>
<a name="l00198"></a>00198 <span class="comment"> * @retval NULL if the memory cannot be allocated.</span>
<a name="l00199"></a>00199 <span class="comment"> */</span>
<a name="l00200"></a><a class="code" href="group__threads.html#ga50b84e3e82a4e09c1066e1d422e4c780">00200</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#ga50b84e3e82a4e09c1066e1d422e4c780" title="Creates a new thread allocating the memory from the heap.">chThdCreateFromHeap</a>(<a class="code" href="structmemory__heap.html" title="Structure describing a memory heap.">MemoryHeap</a> *heapp, <span class="keywordtype">size_t</span> size,
<a name="l00201"></a>00201 <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> prio, <a class="code" href="group__threads.html#gabfe59aa7d2ed1b8e57a2ce17bcbc1189" title="Thread function.">tfunc_t</a> pf, <span class="keywordtype">void</span> *arg) {
<a name="l00202"></a>00202 <span class="keywordtype">void</span> *wsp;
<a name="l00203"></a>00203 <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp;
<a name="l00204"></a>00204
<a name="l00205"></a>00205 wsp = <a class="code" href="group__heaps.html#ga3d364eec0bef8b1986f63b3288ebbd5f" title="Allocates a block of memory from the heap by using the first-fit algorithm.">chHeapAlloc</a>(heapp, size);
<a name="l00206"></a>00206 <span class="keywordflow">if</span> (wsp == NULL)
<a name="l00207"></a>00207 <span class="keywordflow">return</span> NULL;
<a name="l00208"></a>00208
<a name="l00209"></a>00209 <span class="preprocessor">#if CH_DBG_FILL_THREADS</span>
<a name="l00210"></a>00210 <span class="preprocessor"></span> memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp, (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>), <a class="code" href="group__debug.html#gad8d58eb160d991692ecc3bd4e04e0cbd" title="Fill value for thread area in debug mode.">THREAD_FILL_VALUE</a>);
<a name="l00211"></a>00211 memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>),
<a name="l00212"></a>00212 (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + size, <a class="code" href="group__debug.html#ga1379fe3590594def45a6a6c7a4faa41b" title="Fill value for thread stack area in debug mode.">STACK_FILL_VALUE</a>);
<a name="l00213"></a>00213 <span class="preprocessor">#endif</span>
<a name="l00214"></a>00214 <span class="preprocessor"></span>
<a name="l00215"></a>00215 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00216"></a>00216 tp = <a class="code" href="group__threads.html#gac6de13d4044b32d3ae9e2066b5d4c030" title="Creates a new thread into a static memory area.">chThdCreateI</a>(wsp, size, prio, pf, arg);
<a name="l00217"></a>00217 tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> = THD_MEM_MODE_HEAP;
<a name="l00218"></a>00218 <a class="code" href="group__scheduler.html#gae47a8819a21ef2d521124e76bfb37c95" title="Wakes up a thread.">chSchWakeupS</a>(tp, <a class="code" href="group__scheduler.html#gae1c86078318e11c2fa57ee3c2ec69f61" title="Default thread wakeup low level message.">RDY_OK</a>);
<a name="l00219"></a>00219 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00220"></a>00220 <span class="keywordflow">return</span> tp;
<a name="l00221"></a>00221 }
<a name="l00222"></a>00222 <span class="preprocessor">#endif </span><span class="comment">/* CH_USE_DYNAMIC &amp;&amp; CH_USE_HEAP */</span>
<a name="l00223"></a>00223
<a name="l00224"></a>00224 <span class="preprocessor">#if CH_USE_DYNAMIC &amp;&amp; CH_USE_MEMPOOLS</span>
<a name="l00225"></a>00225 <span class="preprocessor"></span><span class="comment">/**</span>
<a name="l00226"></a>00226 <span class="comment"> * @brief Creates a new thread allocating the memory from the specified</span>
<a name="l00227"></a>00227 <span class="comment"> * memory pool.</span>
<a name="l00228"></a>00228 <span class="comment"> * @note A thread can terminate by calling @p chThdExit() or by simply</span>
<a name="l00229"></a>00229 <span class="comment"> * returning from its main function.</span>
<a name="l00230"></a>00230 <span class="comment"> * @note The memory allocated for the thread is not released when the thread</span>
<a name="l00231"></a>00231 <span class="comment"> * terminates but when a @p chThdWait() is performed.</span>
<a name="l00232"></a>00232 <span class="comment"> * @note The function is available only if the @p CH_USE_DYNAMIC,</span>
<a name="l00233"></a>00233 <span class="comment"> * @p CH_USE_MEMPOOLS and @p CH_USE_WAITEXIT options are enabled</span>
<a name="l00234"></a>00234 <span class="comment"> * in @p chconf.h.</span>
<a name="l00235"></a>00235 <span class="comment"> *</span>
<a name="l00236"></a>00236 <span class="comment"> * @param[in] mp pointer to the memory pool object</span>
<a name="l00237"></a>00237 <span class="comment"> * @param[in] prio the priority level for the new thread</span>
<a name="l00238"></a>00238 <span class="comment"> * @param[in] pf the thread function</span>
<a name="l00239"></a>00239 <span class="comment"> * @param[in] arg an argument passed to the thread function. It can be</span>
<a name="l00240"></a>00240 <span class="comment"> * @p NULL.</span>
<a name="l00241"></a>00241 <span class="comment"> * @return The pointer to the @p Thread structure allocated for</span>
<a name="l00242"></a>00242 <span class="comment"> * the thread into the working space area.</span>
<a name="l00243"></a>00243 <span class="comment"> * @retval NULL if the memory pool is empty.</span>
<a name="l00244"></a>00244 <span class="comment"> */</span>
<a name="l00245"></a><a class="code" href="group__threads.html#gad3ccc1a6f054c543258ed2fe97ed2f58">00245</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#gad3ccc1a6f054c543258ed2fe97ed2f58" title="Creates a new thread allocating the memory from the specified memory pool.">chThdCreateFromMemoryPool</a>(<a class="code" href="struct_memory_pool.html" title="Memory pool descriptor.">MemoryPool</a> *mp, <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> prio,
<a name="l00246"></a>00246 <a class="code" href="group__threads.html#gabfe59aa7d2ed1b8e57a2ce17bcbc1189" title="Thread function.">tfunc_t</a> pf, <span class="keywordtype">void</span> *arg) {
<a name="l00247"></a>00247 <span class="keywordtype">void</span> *wsp;
<a name="l00248"></a>00248 <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp;
<a name="l00249"></a>00249
<a name="l00250"></a>00250 <a class="code" href="group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title="Function parameter check.">chDbgCheck</a>(mp != NULL, <span class="stringliteral">&quot;chThdCreateFromMemoryPool&quot;</span>);
<a name="l00251"></a>00251
<a name="l00252"></a>00252 wsp = <a class="code" href="group__pools.html#gab86190d70893fe708c75570af6720bd5" title="Allocates an object from a memory pool.">chPoolAlloc</a>(mp);
<a name="l00253"></a>00253 <span class="keywordflow">if</span> (wsp == NULL)
<a name="l00254"></a>00254 <span class="keywordflow">return</span> NULL;
<a name="l00255"></a>00255
<a name="l00256"></a>00256 <span class="preprocessor">#if CH_DBG_FILL_THREADS</span>
<a name="l00257"></a>00257 <span class="preprocessor"></span> memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp, (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>), <a class="code" href="group__debug.html#gad8d58eb160d991692ecc3bd4e04e0cbd" title="Fill value for thread area in debug mode.">THREAD_FILL_VALUE</a>);
<a name="l00258"></a>00258 memfill((<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + <span class="keyword">sizeof</span>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a>),
<a name="l00259"></a>00259 (<a class="code" href="group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5">uint8_t</a> *)wsp + mp-&gt;<a class="code" href="struct_memory_pool.html#a962ca1e05036d54213b3a0eeaebb0ec6" title="Memory pool objects size.">mp_object_size</a>, <a class="code" href="group__debug.html#ga1379fe3590594def45a6a6c7a4faa41b" title="Fill value for thread stack area in debug mode.">STACK_FILL_VALUE</a>);
<a name="l00260"></a>00260 <span class="preprocessor">#endif</span>
<a name="l00261"></a>00261 <span class="preprocessor"></span>
<a name="l00262"></a>00262 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00263"></a>00263 tp = <a class="code" href="group__threads.html#gac6de13d4044b32d3ae9e2066b5d4c030" title="Creates a new thread into a static memory area.">chThdCreateI</a>(wsp, mp-&gt;<a class="code" href="struct_memory_pool.html#a962ca1e05036d54213b3a0eeaebb0ec6" title="Memory pool objects size.">mp_object_size</a>, prio, pf, arg);
<a name="l00264"></a>00264 tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> = THD_MEM_MODE_MEMPOOL;
<a name="l00265"></a>00265 tp-&gt;<a class="code" href="struct_thread.html#ae0e65912a0c02df5477a3a524cd41d97" title="Memory Pool where the thread workspace is returned.">p_mpool</a> = mp;
<a name="l00266"></a>00266 <a class="code" href="group__scheduler.html#gae47a8819a21ef2d521124e76bfb37c95" title="Wakes up a thread.">chSchWakeupS</a>(tp, <a class="code" href="group__scheduler.html#gae1c86078318e11c2fa57ee3c2ec69f61" title="Default thread wakeup low level message.">RDY_OK</a>);
<a name="l00267"></a>00267 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00268"></a>00268 <span class="keywordflow">return</span> tp;
<a name="l00269"></a>00269 }
<a name="l00270"></a>00270 <span class="preprocessor">#endif </span><span class="comment">/* CH_USE_DYNAMIC &amp;&amp; CH_USE_MEMPOOLS */</span>
<a name="l00271"></a>00271 <span class="comment"></span>
<a name="l00272"></a>00272 <span class="comment">/**</span>
<a name="l00273"></a>00273 <span class="comment"> * @brief Changes the running thread priority level then reschedules if</span>
<a name="l00274"></a>00274 <span class="comment"> * necessary.</span>
<a name="l00275"></a>00275 <span class="comment"> * @note The function returns the real thread priority regardless of the</span>
<a name="l00276"></a>00276 <span class="comment"> * current priority that could be higher than the real priority</span>
<a name="l00277"></a>00277 <span class="comment"> * because the priority inheritance mechanism.</span>
<a name="l00278"></a>00278 <span class="comment"> *</span>
<a name="l00279"></a>00279 <span class="comment"> * @param[in] newprio the new priority level of the running thread</span>
<a name="l00280"></a>00280 <span class="comment"> * @return The old priority level.</span>
<a name="l00281"></a>00281 <span class="comment"> */</span>
<a name="l00282"></a><a class="code" href="group__threads.html#ga5a5d9388c64e4c1a3aec129d2831eefe">00282</a> <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> <a class="code" href="group__threads.html#ga5a5d9388c64e4c1a3aec129d2831eefe" title="Changes the running thread priority level then reschedules if necessary.">chThdSetPriority</a>(<a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> newprio) {
<a name="l00283"></a>00283 <a class="code" href="group__types.html#ga5f2488ba73e5969cbc0f7033735374ee" title="Priority, use the fastest unsigned type.">tprio_t</a> oldprio;
2010-08-10 03:11:02 +00:00
<a name="l00284"></a>00284
2010-11-22 05:53:37 +00:00
<a name="l00285"></a>00285 <a class="code" href="group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title="Function parameter check.">chDbgCheck</a>((newprio &gt;= <a class="code" href="group__scheduler.html#ga38aff897bc7950077d5985b3d4bcf4d5" title="Lowest user priority.">LOWPRIO</a>) &amp;&amp; (newprio &lt;= <a class="code" href="group__scheduler.html#ga0621c307e30868052b802fbc821297e8" title="Highest user priority.">HIGHPRIO</a>),
<a name="l00286"></a>00286 <span class="stringliteral">&quot;chThdSetPriority&quot;</span>);
<a name="l00287"></a>00287
<a name="l00288"></a>00288 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00289"></a>00289 <span class="preprocessor">#if CH_USE_MUTEXES</span>
<a name="l00290"></a>00290 <span class="preprocessor"></span> oldprio = <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_realprio;
<a name="l00291"></a>00291 <span class="keywordflow">if</span> ((<a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_prio == <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_realprio) || (newprio &gt; <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_prio))
<a name="l00292"></a>00292 <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_prio = newprio;
<a name="l00293"></a>00293 <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_realprio = newprio;
<a name="l00294"></a>00294 <span class="preprocessor">#else</span>
<a name="l00295"></a>00295 <span class="preprocessor"></span> oldprio = <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_prio;
<a name="l00296"></a>00296 <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>-&gt;p_prio = newprio;
<a name="l00297"></a>00297 <span class="preprocessor">#endif</span>
<a name="l00298"></a>00298 <span class="preprocessor"></span> <a class="code" href="group__scheduler.html#ga4e38b4bee3d2330f6a0f1cdb7660af20" title="Performs a reschedule if a higher priority thread is runnable.">chSchRescheduleS</a>();
<a name="l00299"></a>00299 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00300"></a>00300 <span class="keywordflow">return</span> oldprio;
<a name="l00301"></a>00301 }
<a name="l00302"></a>00302 <span class="comment"></span>
<a name="l00303"></a>00303 <span class="comment">/**</span>
<a name="l00304"></a>00304 <span class="comment"> * @brief Resumes a suspended thread.</span>
<a name="l00305"></a>00305 <span class="comment"> * @note Use this function to resume threads created with @p chThdInit().</span>
<a name="l00306"></a>00306 <span class="comment"> *</span>
<a name="l00307"></a>00307 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00308"></a>00308 <span class="comment"> * @return The pointer to the thread.</span>
<a name="l00309"></a>00309 <span class="comment"> */</span>
<a name="l00310"></a><a class="code" href="group__threads.html#ga2c1ce2be73cd177af7e577b282232a7d">00310</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#ga2c1ce2be73cd177af7e577b282232a7d" title="Resumes a suspended thread.">chThdResume</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp) {
<a name="l00311"></a>00311
<a name="l00312"></a>00312 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00313"></a>00313 <a class="code" href="group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title="Condition assertion.">chDbgAssert</a>(tp-&gt;<a class="code" href="struct_thread.html#a3c3e9d6abccb62c3a5fab6ef043a391f" title="Current thread state.">p_state</a> == <a class="code" href="group__threads.html#gace00089d9d68ba83bb866396332c15d8" title="Thread state: Thread created in suspended state.">THD_STATE_SUSPENDED</a>,
<a name="l00314"></a>00314 <span class="stringliteral">&quot;chThdResume(), #1&quot;</span>,
<a name="l00315"></a>00315 <span class="stringliteral">&quot;thread not in THD_STATE_SUSPENDED state&quot;</span>);
<a name="l00316"></a>00316 <a class="code" href="group__scheduler.html#gae47a8819a21ef2d521124e76bfb37c95" title="Wakes up a thread.">chSchWakeupS</a>(tp, <a class="code" href="group__scheduler.html#gae1c86078318e11c2fa57ee3c2ec69f61" title="Default thread wakeup low level message.">RDY_OK</a>);
<a name="l00317"></a>00317 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00318"></a>00318 <span class="keywordflow">return</span> tp;
<a name="l00319"></a>00319 }
<a name="l00320"></a>00320 <span class="comment"></span>
<a name="l00321"></a>00321 <span class="comment">/**</span>
<a name="l00322"></a>00322 <span class="comment"> * @brief Requests a thread termination.</span>
<a name="l00323"></a>00323 <span class="comment"> * @note The thread is not terminated but a termination request is added to</span>
<a name="l00324"></a>00324 <span class="comment"> * its @p p_flags field. The thread can read this status by</span>
<a name="l00325"></a>00325 <span class="comment"> * invoking @p chThdShouldTerminate() and then terminate cleanly.</span>
<a name="l00326"></a>00326 <span class="comment"> *</span>
<a name="l00327"></a>00327 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00328"></a>00328 <span class="comment"> */</span>
<a name="l00329"></a><a class="code" href="group__threads.html#ga7ea7371fa41598ebd523622587ce3f22">00329</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#ga7ea7371fa41598ebd523622587ce3f22" title="Requests a thread termination.">chThdTerminate</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp) {
<a name="l00330"></a>00330
<a name="l00331"></a>00331 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00332"></a>00332 tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> |= THD_TERMINATE;
<a name="l00333"></a>00333 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335 <span class="comment"></span>
<a name="l00336"></a>00336 <span class="comment">/**</span>
<a name="l00337"></a>00337 <span class="comment"> * @brief Suspends the invoking thread for the specified time.</span>
<a name="l00338"></a>00338 <span class="comment"> *</span>
<a name="l00339"></a>00339 <span class="comment"> * @param[in] time the delay in system ticks, the special values are</span>
<a name="l00340"></a>00340 <span class="comment"> * handled as follow:</span>
<a name="l00341"></a>00341 <span class="comment"> * - @a TIME_INFINITE the thread enters an infinite sleep</span>
<a name="l00342"></a>00342 <span class="comment"> * state.</span>
<a name="l00343"></a>00343 <span class="comment"> * - @a TIME_IMMEDIATE this value is accepted but</span>
<a name="l00344"></a>00344 <span class="comment"> * interpreted as a normal time specification not as an</span>
<a name="l00345"></a>00345 <span class="comment"> * immediate timeout specification.</span>
<a name="l00346"></a>00346 <span class="comment"> * .</span>
<a name="l00347"></a>00347 <span class="comment"> */</span>
<a name="l00348"></a><a class="code" href="group__threads.html#ga8b10a49e2ec522a5ddecabdc453afcab">00348</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#ga8b10a49e2ec522a5ddecabdc453afcab" title="Suspends the invoking thread for the specified time.">chThdSleep</a>(<a class="code" href="group__types.html#gae3e32a98d431a02106616da3071832dd" title="System Time, recommended fastest unsigned.">systime_t</a> time) {
<a name="l00349"></a>00349
<a name="l00350"></a>00350 <a class="code" href="group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title="Function parameter check.">chDbgCheck</a>(time != <a class="code" href="group__scheduler.html#gae1568cf3090c973c3bc47f86ae2f5afb" title="Infinite time specification for all the syscalls with a timeout specification.">TIME_INFINITE</a>, <span class="stringliteral">&quot;chThdSleep&quot;</span>);
<a name="l00351"></a>00351
<a name="l00352"></a>00352 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00353"></a>00353 <a class="code" href="group__threads.html#ga851dc0c4ac72885bf09feebdef4673ac" title="Suspends the invoking thread for the specified time.">chThdSleepS</a>(time);
<a name="l00354"></a>00354 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00355"></a>00355 }
<a name="l00356"></a>00356 <span class="comment"></span>
<a name="l00357"></a>00357 <span class="comment">/**</span>
<a name="l00358"></a>00358 <span class="comment"> * @brief Suspends the invoking thread until the system time arrives to the</span>
<a name="l00359"></a>00359 <span class="comment"> * specified value.</span>
<a name="l00360"></a>00360 <span class="comment"> *</span>
<a name="l00361"></a>00361 <span class="comment"> * @param[in] time absolute system time</span>
<a name="l00362"></a>00362 <span class="comment"> */</span>
<a name="l00363"></a><a class="code" href="group__threads.html#ga9e6e2f8d46d415e8f0862daf0be1d984">00363</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#ga9e6e2f8d46d415e8f0862daf0be1d984" title="Suspends the invoking thread until the system time arrives to the specified value.">chThdSleepUntil</a>(<a class="code" href="group__types.html#gae3e32a98d431a02106616da3071832dd" title="System Time, recommended fastest unsigned.">systime_t</a> time) {
2010-08-10 03:11:02 +00:00
<a name="l00364"></a>00364
<a name="l00365"></a>00365 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
2010-11-22 05:53:37 +00:00
<a name="l00366"></a>00366 <span class="keywordflow">if</span> ((time -= <a class="code" href="group__time.html#ga137c8f67c450f34416a786d169be90e2" title="Current system time.">chTimeNow</a>()) &gt; 0)
<a name="l00367"></a>00367 <a class="code" href="group__threads.html#ga851dc0c4ac72885bf09feebdef4673ac" title="Suspends the invoking thread for the specified time.">chThdSleepS</a>(time);
<a name="l00368"></a>00368 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00369"></a>00369 }
<a name="l00370"></a>00370 <span class="comment"></span>
<a name="l00371"></a>00371 <span class="comment">/**</span>
<a name="l00372"></a>00372 <span class="comment"> * @brief Yields the time slot.</span>
<a name="l00373"></a>00373 <span class="comment"> * @details Yields the CPU control to the next thread in the ready list with</span>
<a name="l00374"></a>00374 <span class="comment"> * equal priority, if any.</span>
<a name="l00375"></a>00375 <span class="comment"> */</span>
<a name="l00376"></a><a class="code" href="group__threads.html#gacb0a8f22a8358487eaebab3c84b1c4a1">00376</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#gacb0a8f22a8358487eaebab3c84b1c4a1" title="Yields the time slot.">chThdYield</a>(<span class="keywordtype">void</span>) {
2010-08-10 03:11:02 +00:00
<a name="l00377"></a>00377
2010-11-22 05:53:37 +00:00
<a name="l00378"></a>00378 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00379"></a>00379 <a class="code" href="group__scheduler.html#ga8fd763c1a004a010fc13575578edefbd" title="Yields the time slot.">chSchDoYieldS</a>();
<a name="l00380"></a>00380 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00381"></a>00381 }
<a name="l00382"></a>00382 <span class="comment"></span>
<a name="l00383"></a>00383 <span class="comment">/**</span>
<a name="l00384"></a>00384 <span class="comment"> * @brief Terminates the current thread by specifying an exit status code.</span>
<a name="l00385"></a>00385 <span class="comment"> *</span>
<a name="l00386"></a>00386 <span class="comment"> * @param[in] msg thread exit code. The code can be retrieved by using</span>
<a name="l00387"></a>00387 <span class="comment"> * @p chThdWait().</span>
<a name="l00388"></a>00388 <span class="comment"> */</span>
<a name="l00389"></a><a class="code" href="group__threads.html#ga24ab3a3a4d70214ee360867a1c3c75ac">00389</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#ga24ab3a3a4d70214ee360867a1c3c75ac" title="Terminates the current thread by specifying an exit status code.">chThdExit</a>(<a class="code" href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3" title="Message, use signed pointer equivalent.">msg_t</a> msg) {
<a name="l00390"></a>00390 <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp = currp;
<a name="l00391"></a>00391
<a name="l00392"></a>00392 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00393"></a>00393 tp-&gt;<a class="code" href="struct_thread.html#a72cd0424b6098ca241fac1f74d21b56a" title="State-specific fields.">p_u</a>.<a class="code" href="struct_thread.html#addbf4614b204bdc2a3480c434cf7baaf" title="Thread exit code.">exitcode</a> = msg;
<a name="l00394"></a>00394 <span class="preprocessor">#if defined(THREAD_EXT_EXIT)</span>
<a name="l00395"></a>00395 <span class="preprocessor"></span> <a class="code" href="group__config.html#ga5137c8f67dd4fa75e99824a95a3fa746" title="Threads finalization hook.">THREAD_EXT_EXIT</a>(tp);
<a name="l00396"></a>00396 <span class="preprocessor">#endif</span>
<a name="l00397"></a>00397 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_WAITEXIT</span>
<a name="l00398"></a>00398 <span class="preprocessor"></span> <span class="keywordflow">while</span> (<a class="code" href="group__internals.html#ga8f6bea56a53acc44758fe61c98d68a4e" title="Evaluates to TRUE if the specified threads queue or list is not empty.">notempty</a>(&amp;tp-&gt;<a class="code" href="struct_thread.html#ad1088bc1f9970cc102dc28000134e9ab" title="Termination waiting list.">p_waiting</a>))
<a name="l00399"></a>00399 <a class="code" href="group__scheduler.html#ga535d58cfb3436c8d4e4586c31c062298" title="Inserts a thread in the Ready List.">chSchReadyI</a>(<a class="code" href="group__internals.html#gad9aa523aebdaeefa3cc4a311bc210e8a" title="Pops a Thread from the top of a stack list and returns it.">list_remove</a>(&amp;tp-&gt;<a class="code" href="struct_thread.html#ad1088bc1f9970cc102dc28000134e9ab" title="Termination waiting list.">p_waiting</a>));
<a name="l00400"></a>00400 <span class="preprocessor">#endif</span>
<a name="l00401"></a>00401 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_REGISTRY</span>
2010-11-30 04:54:43 +00:00
<a name="l00402"></a>00402 <span class="preprocessor"></span> <span class="comment">/* Static threads are immediately removed from the registry because</span>
<a name="l00403"></a>00403 <span class="comment"> there is no memory to recover.*/</span>
<a name="l00404"></a>00404 <span class="keywordflow">if</span> ((tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> &amp; <a class="code" href="group__threads.html#ga03fc66bb40f45829816cd4e8353fead2" title="Thread memory mode mask.">THD_MEM_MODE_MASK</a>) == <a class="code" href="group__threads.html#ga222e36a338ac4048b1da998f112771ea" title="Thread memory mode: static.">THD_MEM_MODE_STATIC</a>)
<a name="l00405"></a>00405 <a class="code" href="group__registry.html#ga472c6eb9a86e5963f452aa78719c92da" title="Removes a thread from the registry list.">REG_REMOVE</a>(tp);
<a name="l00406"></a>00406 <span class="preprocessor">#endif</span>
<a name="l00407"></a>00407 <span class="preprocessor"></span> <a class="code" href="group__scheduler.html#ga97c1c514b755a1e71caf2f19c1ccf986" title="Puts the current thread to sleep into the specified state.">chSchGoSleepS</a>(<a class="code" href="group__threads.html#ga10583a3a3fb4c62693bb08796f8d13ae" title="Thread state: After termination.">THD_STATE_FINAL</a>);
<a name="l00408"></a>00408 }
<a name="l00409"></a>00409
<a name="l00410"></a>00410 <span class="preprocessor">#if CH_USE_DYNAMIC || defined(__DOXYGEN__)</span>
<a name="l00411"></a>00411 <span class="preprocessor"></span><span class="comment">/**</span>
<a name="l00412"></a>00412 <span class="comment"> * @brief Adds a reference to a thread object.</span>
<a name="l00413"></a>00413 <span class="comment"> *</span>
<a name="l00414"></a>00414 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00415"></a>00415 <span class="comment"> * @return The same thread pointer passed as parameter</span>
<a name="l00416"></a>00416 <span class="comment"> * representing the new reference.</span>
<a name="l00417"></a>00417 <span class="comment"> */</span>
<a name="l00418"></a><a class="code" href="group__threads.html#gabefc5734beaf29ae0bcd52cd41744930">00418</a> <a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *<a class="code" href="group__threads.html#gabefc5734beaf29ae0bcd52cd41744930" title="Adds a reference to a thread object.">chThdAddRef</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp) {
<a name="l00419"></a>00419
<a name="l00420"></a>00420 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00421"></a>00421 <a class="code" href="group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title="Condition assertion.">chDbgAssert</a>(tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a> &lt; 255, <span class="stringliteral">&quot;chThdAddRef(), #1&quot;</span>, <span class="stringliteral">&quot;too many references&quot;</span>);
<a name="l00422"></a>00422 tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a>++;
<a name="l00423"></a>00423 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00424"></a>00424 <span class="keywordflow">return</span> tp;
<a name="l00425"></a>00425 }
<a name="l00426"></a>00426 <span class="comment"></span>
<a name="l00427"></a>00427 <span class="comment">/**</span>
<a name="l00428"></a>00428 <span class="comment"> * @brief Releases a reference to a thread object.</span>
<a name="l00429"></a>00429 <span class="comment"> * @details If the references counter reaches zero &lt;b&gt;and&lt;/b&gt; the thread</span>
<a name="l00430"></a>00430 <span class="comment"> * is in the @p THD_STATE_FINAL state then the thread&#39;s memory is</span>
<a name="l00431"></a>00431 <span class="comment"> * returned to the proper allocator.</span>
<a name="l00432"></a>00432 <span class="comment"> * @note Static threads are not affected.</span>
<a name="l00433"></a>00433 <span class="comment"> *</span>
<a name="l00434"></a>00434 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00435"></a>00435 <span class="comment"> */</span>
<a name="l00436"></a><a class="code" href="group__threads.html#gaa22f80040cf192015be4607f31a0ac80">00436</a> <span class="keywordtype">void</span> <a class="code" href="group__threads.html#gaa22f80040cf192015be4607f31a0ac80" title="Releases a reference to a thread object.">chThdRelease</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp) {
<a name="l00437"></a>00437 <a class="code" href="group__types.html#ga205898ea166481281fec650f2a7e85a3" title="Thread references counter, uint8_t is ok.">trefs_t</a> refs;
<a name="l00438"></a>00438
<a name="l00439"></a>00439 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00440"></a>00440 <a class="code" href="group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title="Condition assertion.">chDbgAssert</a>(tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a> &gt; 0, <span class="stringliteral">&quot;chThdRelease(), #1&quot;</span>, <span class="stringliteral">&quot;not referenced&quot;</span>);
<a name="l00441"></a>00441 refs = --tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a>;
<a name="l00442"></a>00442 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00443"></a>00443
<a name="l00444"></a>00444 <span class="comment">/* If the references counter reaches zero and the thread is in its</span>
<a name="l00445"></a>00445 <span class="comment"> terminated state then the memory can be returned to the proper</span>
<a name="l00446"></a>00446 <span class="comment"> allocator. Of course static threads are not affected.*/</span>
<a name="l00447"></a>00447 <span class="keywordflow">if</span> ((refs == 0) &amp;&amp; (tp-&gt;<a class="code" href="struct_thread.html#a3c3e9d6abccb62c3a5fab6ef043a391f" title="Current thread state.">p_state</a> == <a class="code" href="group__threads.html#ga10583a3a3fb4c62693bb08796f8d13ae" title="Thread state: After termination.">THD_STATE_FINAL</a>)) {
<a name="l00448"></a>00448 <span class="keywordflow">switch</span> (tp-&gt;<a class="code" href="struct_thread.html#a1e044ddc7521288d6fb473756a214d23" title="Various thread flags.">p_flags</a> &amp; <a class="code" href="group__threads.html#ga03fc66bb40f45829816cd4e8353fead2" title="Thread memory mode mask.">THD_MEM_MODE_MASK</a>) {
<a name="l00449"></a>00449 <span class="preprocessor">#if CH_USE_HEAP</span>
<a name="l00450"></a>00450 <span class="preprocessor"></span> <span class="keywordflow">case</span> <a class="code" href="group__threads.html#ga706ca53f21d35623480e8e5a239c3fcb" title="Thread memory mode: heap.">THD_MEM_MODE_HEAP</a>:
<a name="l00451"></a>00451 <span class="preprocessor">#if CH_USE_REGISTRY</span>
<a name="l00452"></a>00452 <span class="preprocessor"></span> <a class="code" href="group__registry.html#ga472c6eb9a86e5963f452aa78719c92da" title="Removes a thread from the registry list.">REG_REMOVE</a>(tp);
<a name="l00453"></a>00453 <span class="preprocessor">#endif</span>
<a name="l00454"></a>00454 <span class="preprocessor"></span> <a class="code" href="group__heaps.html#ga0fc553df0138f02e0b233be91e413c79" title="Frees a previously allocated memory block.">chHeapFree</a>(tp);
<a name="l00455"></a>00455 <span class="keywordflow">break</span>;
<a name="l00456"></a>00456 <span class="preprocessor">#endif</span>
<a name="l00457"></a>00457 <span class="preprocessor"></span><span class="preprocessor">#if CH_USE_MEMPOOLS</span>
<a name="l00458"></a>00458 <span class="preprocessor"></span> <span class="keywordflow">case</span> <a class="code" href="group__threads.html#ga70a7c2fa69d7ca90e6f191c2ebd8cc6f" title="Thread memory mode: pool.">THD_MEM_MODE_MEMPOOL</a>:
<a name="l00459"></a>00459 <span class="preprocessor">#if CH_USE_REGISTRY</span>
<a name="l00460"></a>00460 <span class="preprocessor"></span> <a class="code" href="group__registry.html#ga472c6eb9a86e5963f452aa78719c92da" title="Removes a thread from the registry list.">REG_REMOVE</a>(tp);
<a name="l00461"></a>00461 <span class="preprocessor">#endif</span>
<a name="l00462"></a>00462 <span class="preprocessor"></span> <a class="code" href="group__pools.html#ga978f8d038e8e981bf9e5a104b81c6cb6" title="Releases (or adds) an object into (to) a memory pool.">chPoolFree</a>(tp-&gt;<a class="code" href="struct_thread.html#ae0e65912a0c02df5477a3a524cd41d97" title="Memory Pool where the thread workspace is returned.">p_mpool</a>, tp);
<a name="l00463"></a>00463 <span class="keywordflow">break</span>;
<a name="l00464"></a>00464 <span class="preprocessor">#endif</span>
<a name="l00465"></a>00465 <span class="preprocessor"></span> }
<a name="l00466"></a>00466 }
<a name="l00467"></a>00467 }
<a name="l00468"></a>00468 <span class="preprocessor">#endif </span><span class="comment">/* CH_USE_DYNAMIC */</span>
<a name="l00469"></a>00469
<a name="l00470"></a>00470 <span class="preprocessor">#if CH_USE_WAITEXIT || defined(__DOXYGEN__)</span>
<a name="l00471"></a>00471 <span class="preprocessor"></span><span class="comment">/**</span>
<a name="l00472"></a>00472 <span class="comment"> * @brief Blocks the execution of the invoking thread until the specified</span>
<a name="l00473"></a>00473 <span class="comment"> * thread terminates then the exit code is returned.</span>
<a name="l00474"></a>00474 <span class="comment"> * @details This function waits for the specified thread to terminate then</span>
<a name="l00475"></a>00475 <span class="comment"> * decrements its reference counter, if the counter reaches zero then</span>
<a name="l00476"></a>00476 <span class="comment"> * the thread working area is returned to the proper allocator.&lt;br&gt;</span>
<a name="l00477"></a>00477 <span class="comment"> * The memory used by the exited thread is handled in different ways</span>
<a name="l00478"></a>00478 <span class="comment"> * depending on the API that spawned the thread:</span>
<a name="l00479"></a>00479 <span class="comment"> * - If the thread was spawned by @p chThdCreateStatic() or by</span>
<a name="l00480"></a>00480 <span class="comment"> * @p chThdInit() then nothing happens and the thread working area</span>
<a name="l00481"></a>00481 <span class="comment"> * is not released or modified in any way. This is the default,</span>
<a name="l00482"></a>00482 <span class="comment"> * totally static, behavior.</span>
<a name="l00483"></a>00483 <span class="comment"> * - If the thread was spawned by @p chThdCreateFromHeap() then</span>
<a name="l00484"></a>00484 <span class="comment"> * the working area is returned to the system heap.</span>
<a name="l00485"></a>00485 <span class="comment"> * - If the thread was spawned by @p chThdCreateFromMemoryPool()</span>
<a name="l00486"></a>00486 <span class="comment"> * then the working area is returned to the owning memory pool.</span>
<a name="l00487"></a>00487 <span class="comment"> * .</span>
<a name="l00488"></a>00488 <span class="comment"> * Please read the @ref article_lifecycle article for more details.</span>
<a name="l00489"></a>00489 <span class="comment"> * @note After invoking @p chThdWait() the thread pointer becomes invalid</span>
<a name="l00490"></a>00490 <span class="comment"> * and must not be used as parameter for further system calls.</span>
<a name="l00491"></a>00491 <span class="comment"> * @note The function is available only if the @p CH_USE_WAITEXIT</span>
<a name="l00492"></a>00492 <span class="comment"> * option is enabled in @p chconf.h.</span>
<a name="l00493"></a>00493 <span class="comment"> * @note If @p CH_USE_DYNAMIC is not specified this function just waits for</span>
<a name="l00494"></a>00494 <span class="comment"> * the thread termination, no memory allocators are involved.</span>
<a name="l00495"></a>00495 <span class="comment"> *</span>
<a name="l00496"></a>00496 <span class="comment"> * @param[in] tp pointer to the thread</span>
<a name="l00497"></a>00497 <span class="comment"> * @return The exit code from the terminated thread.</span>
<a name="l00498"></a>00498 <span class="comment"> */</span>
<a name="l00499"></a><a class="code" href="group__threads.html#ga94a5727ecdbe0f738dfc8e1ede1c3409">00499</a> <a class="code" href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3" title="Message, use signed pointer equivalent.">msg_t</a> <a class="code" href="group__threads.html#ga94a5727ecdbe0f738dfc8e1ede1c3409" title="Blocks the execution of the invoking thread until the specified thread terminates then the exit code ...">chThdWait</a>(<a class="code" href="struct_thread.html" title="Structure representing a thread.">Thread</a> *tp) {
<a name="l00500"></a>00500 <a class="code" href="group__types.html#ga35bcb0c321cd7bc45bf1a11fa17ebdd3" title="Message, use signed pointer equivalent.">msg_t</a> msg;
<a name="l00501"></a>00501
<a name="l00502"></a>00502 <a class="code" href="group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title="Function parameter check.">chDbgCheck</a>(tp != NULL, <span class="stringliteral">&quot;chThdWait&quot;</span>);
<a name="l00503"></a>00503
<a name="l00504"></a>00504 <a class="code" href="group__system.html#ga9f6573c0763d1e4e97c63c62edad6e42" title="Enters the kernel lock mode.">chSysLock</a>();
<a name="l00505"></a>00505 <a class="code" href="group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title="Condition assertion.">chDbgAssert</a>(tp != <a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>, <span class="stringliteral">&quot;chThdWait(), #1&quot;</span>, <span class="stringliteral">&quot;waiting self&quot;</span>);
<a name="l00506"></a>00506 <span class="preprocessor">#if CH_USE_DYNAMIC</span>
<a name="l00507"></a>00507 <span class="preprocessor"></span> <a class="code" href="group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title="Condition assertion.">chDbgAssert</a>(tp-&gt;<a class="code" href="struct_thread.html#af86d1da435197d113bc42fe8139a21c5" title="References to this thread.">p_refs</a> &gt; 0, <span class="stringliteral">&quot;chThdWait(), #2&quot;</span>, <span class="stringliteral">&quot;not referenced&quot;</span>);
<a name="l00508"></a>00508 <span class="preprocessor">#endif</span>
<a name="l00509"></a>00509 <span class="preprocessor"></span> <span class="keywordflow">if</span> (tp-&gt;<a class="code" href="struct_thread.html#a3c3e9d6abccb62c3a5fab6ef043a391f" title="Current thread state.">p_state</a> != <a class="code" href="group__threads.html#ga10583a3a3fb4c62693bb08796f8d13ae" title="Thread state: After termination.">THD_STATE_FINAL</a>) {
<a name="l00510"></a>00510 <a class="code" href="group__internals.html#ga5818ce2d363d36402baece614b06e41b" title="Pushes a Thread on top of a stack list.">list_insert</a>(<a class="code" href="group__scheduler.html#ga1a5fddde56420e8accd0c09c1abb32a7" title="Current thread pointer access macro.">currp</a>, &amp;tp-&gt;<a class="code" href="struct_thread.html#ad1088bc1f9970cc102dc28000134e9ab" title="Termination waiting list.">p_waiting</a>);
<a name="l00511"></a>00511 <a class="code" href="group__scheduler.html#ga97c1c514b755a1e71caf2f19c1ccf986" title="Puts the current thread to sleep into the specified state.">chSchGoSleepS</a>(<a class="code" href="group__threads.html#gaf726245829b74db3a77a5f4bcc17bf67" title="Thread state: Waiting in chThdWait().">THD_STATE_WTEXIT</a>);
<a name="l00512"></a>00512 }
<a name="l00513"></a>00513 msg = tp-&gt;<a class="code" href="struct_thread.html#a72cd0424b6098ca241fac1f74d21b56a" title="State-specific fields.">p_u</a>.<a class="code" href="struct_thread.html#addbf4614b204bdc2a3480c434cf7baaf" title="Thread exit code.">exitcode</a>;
<a name="l00514"></a>00514 <a class="code" href="group__system.html#ga5a257fa58a09815eb64a45e2dfbdc22e" title="Leaves the kernel lock mode.">chSysUnlock</a>();
<a name="l00515"></a>00515 <span class="preprocessor">#if CH_USE_DYNAMIC</span>
<a name="l00516"></a>00516 <span class="preprocessor"></span> <a class="code" href="group__threads.html#gaa22f80040cf192015be4607f31a0ac80" title="Releases a reference to a thread object.">chThdRelease</a>(tp);
<a name="l00517"></a>00517 <span class="preprocessor">#endif</span>
<a name="l00518"></a>00518 <span class="preprocessor"></span> <span class="keywordflow">return</span> msg;
<a name="l00519"></a>00519 }
<a name="l00520"></a>00520 <span class="preprocessor">#endif </span><span class="comment">/* CH_USE_WAITEXIT */</span>
<a name="l00521"></a>00521 <span class="comment"></span>
<a name="l00522"></a>00522 <span class="comment">/** @} */</span>
2010-08-10 03:11:02 +00:00
</pre></div></div>
2010-11-22 05:53:37 +00:00
</div>
2010-08-10 03:11:02 +00:00
<hr size="1"><address style="text-align: right;"><small>
2010-11-30 04:54:43 +00:00
Generated on Sun Nov 28 2010 14:09:48 for ChibiOS/RT by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.7.1</small></address>
2010-08-10 03:11:02 +00:00
</body>
</html>