⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a00018.html

📁 Protothreads are extremely lightweight stackless threads designed for severely memory constrained s
💻 HTML
📖 第 1 页 / 共 2 页
字号:
00115 <span class="comment">  PT_BEGIN(pt);</span>00116 <span class="comment">  </span>00117 <span class="comment">  PT_SEM_INIT(&amp;empty, 0);</span>00118 <span class="comment">  PT_SEM_INIT(&amp;full, BUFSIZE);</span>00119 <span class="comment">  PT_SEM_INIT(&amp;mutex, 1);</span>00120 <span class="comment"></span>00121 <span class="comment">  PT_INIT(&amp;pt_producer);</span>00122 <span class="comment">  PT_INIT(&amp;pt_consumer);</span>00123 <span class="comment"></span>00124 <span class="comment">  PT_WAIT_THREAD(pt, producer(&amp;pt_producer) &amp;</span>00125 <span class="comment">                     consumer(&amp;pt_consumer));</span>00126 <span class="comment"></span>00127 <span class="comment">  PT_END(pt);</span>00128 <span class="comment">}</span>00129 <span class="comment"> \endcode</span>00130 <span class="comment"> *</span>00131 <span class="comment"> * The program uses three protothreads: one protothread that</span>00132 <span class="comment"> * implements the consumer, one thread that implements the producer,</span>00133 <span class="comment"> * and one protothread that drives the two other protothreads. The</span>00134 <span class="comment"> * program uses three semaphores: "full", "empty" and "mutex". The</span>00135 <span class="comment"> * "mutex" semaphore is used to provide mutual exclusion for the</span>00136 <span class="comment"> * buffer, the "empty" semaphore is used to block the consumer is the</span>00137 <span class="comment"> * buffer is empty, and the "full" semaphore is used to block the</span>00138 <span class="comment"> * producer is the buffer is full.</span>00139 <span class="comment"> *</span>00140 <span class="comment"> * The "driver_thread" holds two protothread state variables,</span>00141 <span class="comment"> * "pt_producer" and "pt_consumer". It is important to note that both</span>00142 <span class="comment"> * these variables are declared as &lt;i&gt;static&lt;/i&gt;. If the static</span>00143 <span class="comment"> * keyword is not used, both variables are stored on the stack. Since</span>00144 <span class="comment"> * protothreads do not store the stack, these variables may be</span>00145 <span class="comment"> * overwritten during a protothread wait operation. Similarly, both</span>00146 <span class="comment"> * the "consumer" and "producer" protothreads declare their local</span>00147 <span class="comment"> * variables as static, to avoid them being stored on the stack.</span>00148 <span class="comment"> * </span>00149 <span class="comment"> *</span>00150 <span class="comment"> */</span>00151    <span class="comment"></span>00152 <span class="comment">/**</span>00153 <span class="comment"> * \file</span>00154 <span class="comment"> * Couting semaphores implemented on protothreads</span>00155 <span class="comment"> * \author</span>00156 <span class="comment"> * Adam Dunkels &lt;adam@sics.se&gt;</span>00157 <span class="comment"> *</span>00158 <span class="comment"> */</span>00159 00160 <span class="preprocessor">#ifndef __PT_SEM_H__</span>00161 <span class="preprocessor"></span><span class="preprocessor">#define __PT_SEM_H__</span>00162 <span class="preprocessor"></span>00163 <span class="preprocessor">#include "<a class="code" href="a00011.html">pt.h</a>"</span>00164 00165 <span class="keyword">struct </span>pt_sem {00166   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> count;00167 };00168 <span class="comment"></span>00169 <span class="comment">/**</span>00170 <span class="comment"> * Initialize a semaphore</span>00171 <span class="comment"> *</span>00172 <span class="comment"> * This macro initializes a semaphore with a value for the</span>00173 <span class="comment"> * counter. Internally, the semaphores use an "unsigned int" to</span>00174 <span class="comment"> * represent the counter, and therefore the "count" argument should be</span>00175 <span class="comment"> * within range of an unsigned int.</span>00176 <span class="comment"> *</span>00177 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span>00178 <span class="comment"> * representing the semaphore</span>00179 <span class="comment"> *</span>00180 <span class="comment"> * \param c (unsigned int) The initial count of the semaphore.</span>00181 <span class="comment"> * \hideinitializer</span>00182 <span class="comment"> */</span><a name="l00183"></a><a class="code" href="a00013.html#ga0">00183</a> <span class="preprocessor">#define PT_SEM_INIT(s, c) (s)-&gt;count = c</span>00184 <span class="preprocessor"></span><span class="comment"></span>00185 <span class="comment">/**</span>00186 <span class="comment"> * Wait for a semaphore</span>00187 <span class="comment"> *</span>00188 <span class="comment"> * This macro carries out the "wait" operation on the semaphore. The</span>00189 <span class="comment"> * wait operation causes the protothread to block while the counter is</span>00190 <span class="comment"> * zero. When the counter reaches a value larger than zero, the</span>00191 <span class="comment"> * protothread will continue.</span>00192 <span class="comment"> *</span>00193 <span class="comment"> * \param pt (struct pt *) A pointer to the protothread (struct pt) in</span>00194 <span class="comment"> * which the operation is executed.</span>00195 <span class="comment"> *</span>00196 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span>00197 <span class="comment"> * representing the semaphore</span>00198 <span class="comment"> *</span>00199 <span class="comment"> * \hideinitializer</span>00200 <span class="comment"> */</span><a name="l00201"></a><a class="code" href="a00013.html#ga1">00201</a> <span class="preprocessor">#define PT_SEM_WAIT(pt, s)      \</span>00202 <span class="preprocessor">  do {                                          \</span>00203 <span class="preprocessor">    PT_WAIT_UNTIL(pt, (s)-&gt;count &gt; 0);          \</span>00204 <span class="preprocessor">    --(s)-&gt;count;                               \</span>00205 <span class="preprocessor">  } while(0)</span>00206 <span class="preprocessor"></span><span class="comment"></span>00207 <span class="comment">/**</span>00208 <span class="comment"> * Signal a semaphore</span>00209 <span class="comment"> *</span>00210 <span class="comment"> * This macro carries out the "signal" operation on the semaphore. The</span>00211 <span class="comment"> * signal operation increments the counter inside the semaphore, which</span>00212 <span class="comment"> * eventually will cause waiting protothreads to continue executing.</span>00213 <span class="comment"> *</span>00214 <span class="comment"> * \param pt (struct pt *) A pointer to the protothread (struct pt) in</span>00215 <span class="comment"> * which the operation is executed.</span>00216 <span class="comment"> *</span>00217 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span>00218 <span class="comment"> * representing the semaphore</span>00219 <span class="comment"> *</span>00220 <span class="comment"> * \hideinitializer</span>00221 <span class="comment"> */</span><a name="l00222"></a><a class="code" href="a00013.html#ga2">00222</a> <span class="preprocessor">#define PT_SEM_SIGNAL(pt, s) ++(s)-&gt;count</span>00223 <span class="preprocessor"></span>00224 <span class="preprocessor">#endif </span><span class="comment">/* __PT_SEM_H__ */</span>00225 <span class="comment"></span>00226 <span class="comment">/** @} */</span><span class="comment"></span>00227 <span class="comment">/** @} */</span>00228    </pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Feb 24 11:39:16 2005 for The Protothreads Library 1.0 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -