📄 a00021.html
字号:
<a name="l00109"></a>00109 <span class="comment">}</span><a name="l00110"></a>00110 <span class="comment"></span><a name="l00111"></a>00111 <span class="comment">PT_THREAD(driver_thread(struct pt *pt))</span><a name="l00112"></a>00112 <span class="comment">{</span><a name="l00113"></a>00113 <span class="comment"> static struct pt pt_producer, pt_consumer;</span><a name="l00114"></a>00114 <span class="comment"></span><a name="l00115"></a>00115 <span class="comment"> PT_BEGIN(pt);</span><a name="l00116"></a>00116 <span class="comment"> </span><a name="l00117"></a>00117 <span class="comment"> PT_SEM_INIT(&empty, 0);</span><a name="l00118"></a>00118 <span class="comment"> PT_SEM_INIT(&full, BUFSIZE);</span><a name="l00119"></a>00119 <span class="comment"> PT_SEM_INIT(&mutex, 1);</span><a name="l00120"></a>00120 <span class="comment"></span><a name="l00121"></a>00121 <span class="comment"> PT_INIT(&pt_producer);</span><a name="l00122"></a>00122 <span class="comment"> PT_INIT(&pt_consumer);</span><a name="l00123"></a>00123 <span class="comment"></span><a name="l00124"></a>00124 <span class="comment"> PT_WAIT_THREAD(pt, producer(&pt_producer) &</span><a name="l00125"></a>00125 <span class="comment"> consumer(&pt_consumer));</span><a name="l00126"></a>00126 <span class="comment"></span><a name="l00127"></a>00127 <span class="comment"> PT_END(pt);</span><a name="l00128"></a>00128 <span class="comment">}</span><a name="l00129"></a>00129 <span class="comment"> \endcode</span><a name="l00130"></a>00130 <span class="comment"> *</span><a name="l00131"></a>00131 <span class="comment"> * The program uses three protothreads: one protothread that</span><a name="l00132"></a>00132 <span class="comment"> * implements the consumer, one thread that implements the producer,</span><a name="l00133"></a>00133 <span class="comment"> * and one protothread that drives the two other protothreads. The</span><a name="l00134"></a>00134 <span class="comment"> * program uses three semaphores: "full", "empty" and "mutex". The</span><a name="l00135"></a>00135 <span class="comment"> * "mutex" semaphore is used to provide mutual exclusion for the</span><a name="l00136"></a>00136 <span class="comment"> * buffer, the "empty" semaphore is used to block the consumer is the</span><a name="l00137"></a>00137 <span class="comment"> * buffer is empty, and the "full" semaphore is used to block the</span><a name="l00138"></a>00138 <span class="comment"> * producer is the buffer is full.</span><a name="l00139"></a>00139 <span class="comment"> *</span><a name="l00140"></a>00140 <span class="comment"> * The "driver_thread" holds two protothread state variables,</span><a name="l00141"></a>00141 <span class="comment"> * "pt_producer" and "pt_consumer". It is important to note that both</span><a name="l00142"></a>00142 <span class="comment"> * these variables are declared as <i>static</i>. If the static</span><a name="l00143"></a>00143 <span class="comment"> * keyword is not used, both variables are stored on the stack. Since</span><a name="l00144"></a>00144 <span class="comment"> * protothreads do not store the stack, these variables may be</span><a name="l00145"></a>00145 <span class="comment"> * overwritten during a protothread wait operation. Similarly, both</span><a name="l00146"></a>00146 <span class="comment"> * the "consumer" and "producer" protothreads declare their local</span><a name="l00147"></a>00147 <span class="comment"> * variables as static, to avoid them being stored on the stack.</span><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"> */</span><a name="l00151"></a>00151 <span class="comment"></span><a name="l00152"></a>00152 <span class="comment">/**</span><a name="l00153"></a>00153 <span class="comment"> * \file</span><a name="l00154"></a>00154 <span class="comment"> * Couting semaphores implemented on protothreads</span><a name="l00155"></a>00155 <span class="comment"> * \author</span><a name="l00156"></a>00156 <span class="comment"> * Adam Dunkels <adam@sics.se></span><a name="l00157"></a>00157 <span class="comment"> *</span><a name="l00158"></a>00158 <span class="comment"> */</span><a name="l00159"></a>00159 <a name="l00160"></a>00160 <span class="preprocessor">#ifndef __PT_SEM_H__</span><a name="l00161"></a>00161 <span class="preprocessor"></span><span class="preprocessor">#define __PT_SEM_H__</span><a name="l00162"></a>00162 <span class="preprocessor"></span><a name="l00163"></a>00163 <span class="preprocessor">#include "<a class="code" href="a00013.html">pt.h</a>"</span><a name="l00164"></a>00164 <a name="l00165"></a><a class="code" href="a00006.html">00165</a> <span class="keyword">struct </span><a class="code" href="a00006.html">pt_sem</a> {<a name="l00166"></a><a class="code" href="a00006.html#6f341120f42d5fd9f329ff1119594743">00166</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="a00006.html#6f341120f42d5fd9f329ff1119594743">count</a>;<a name="l00167"></a>00167 };<a name="l00168"></a>00168 <span class="comment"></span><a name="l00169"></a>00169 <span class="comment">/**</span><a name="l00170"></a>00170 <span class="comment"> * Initialize a semaphore</span><a name="l00171"></a>00171 <span class="comment"> *</span><a name="l00172"></a>00172 <span class="comment"> * This macro initializes a semaphore with a value for the</span><a name="l00173"></a>00173 <span class="comment"> * counter. Internally, the semaphores use an "unsigned int" to</span><a name="l00174"></a>00174 <span class="comment"> * represent the counter, and therefore the "count" argument should be</span><a name="l00175"></a>00175 <span class="comment"> * within range of an unsigned int.</span><a name="l00176"></a>00176 <span class="comment"> *</span><a name="l00177"></a>00177 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span><a name="l00178"></a>00178 <span class="comment"> * representing the semaphore</span><a name="l00179"></a>00179 <span class="comment"> *</span><a name="l00180"></a>00180 <span class="comment"> * \param c (unsigned int) The initial count of the semaphore.</span><a name="l00181"></a>00181 <span class="comment"> * \hideinitializer</span><a name="l00182"></a>00182 <span class="comment"> */</span><a name="l00183"></a><a class="code" href="a00016.html#gd7089c5dc86f12019f0361d82a75b04b">00183</a> <span class="preprocessor">#define PT_SEM_INIT(s, c) (s)->count = c</span><a name="l00184"></a>00184 <span class="preprocessor"></span><span class="comment"></span><a name="l00185"></a>00185 <span class="comment">/**</span><a name="l00186"></a>00186 <span class="comment"> * Wait for a semaphore</span><a name="l00187"></a>00187 <span class="comment"> *</span><a name="l00188"></a>00188 <span class="comment"> * This macro carries out the "wait" operation on the semaphore. The</span><a name="l00189"></a>00189 <span class="comment"> * wait operation causes the protothread to block while the counter is</span><a name="l00190"></a>00190 <span class="comment"> * zero. When the counter reaches a value larger than zero, the</span><a name="l00191"></a>00191 <span class="comment"> * protothread will continue.</span><a name="l00192"></a>00192 <span class="comment"> *</span><a name="l00193"></a>00193 <span class="comment"> * \param pt (struct pt *) A pointer to the protothread (struct pt) in</span><a name="l00194"></a>00194 <span class="comment"> * which the operation is executed.</span><a name="l00195"></a>00195 <span class="comment"> *</span><a name="l00196"></a>00196 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span><a name="l00197"></a>00197 <span class="comment"> * representing the semaphore</span><a name="l00198"></a>00198 <span class="comment"> *</span><a name="l00199"></a>00199 <span class="comment"> * \hideinitializer</span><a name="l00200"></a>00200 <span class="comment"> */</span><a name="l00201"></a><a class="code" href="a00016.html#g386ff87a52a840512906f2940e229e2e">00201</a> <span class="preprocessor">#define PT_SEM_WAIT(pt, s) \</span><a name="l00202"></a>00202 <span class="preprocessor"> do { \</span><a name="l00203"></a>00203 <span class="preprocessor"> PT_WAIT_UNTIL(pt, (s)->count > 0); \</span><a name="l00204"></a>00204 <span class="preprocessor"> --(s)->count; \</span><a name="l00205"></a>00205 <span class="preprocessor"> } while(0)</span><a name="l00206"></a>00206 <span class="preprocessor"></span><span class="comment"></span><a name="l00207"></a>00207 <span class="comment">/**</span><a name="l00208"></a>00208 <span class="comment"> * Signal a semaphore</span><a name="l00209"></a>00209 <span class="comment"> *</span><a name="l00210"></a>00210 <span class="comment"> * This macro carries out the "signal" operation on the semaphore. The</span><a name="l00211"></a>00211 <span class="comment"> * signal operation increments the counter inside the semaphore, which</span><a name="l00212"></a>00212 <span class="comment"> * eventually will cause waiting protothreads to continue executing.</span><a name="l00213"></a>00213 <span class="comment"> *</span><a name="l00214"></a>00214 <span class="comment"> * \param pt (struct pt *) A pointer to the protothread (struct pt) in</span><a name="l00215"></a>00215 <span class="comment"> * which the operation is executed.</span><a name="l00216"></a>00216 <span class="comment"> *</span><a name="l00217"></a>00217 <span class="comment"> * \param s (struct pt_sem *) A pointer to the pt_sem struct</span><a name="l00218"></a>00218 <span class="comment"> * representing the semaphore</span><a name="l00219"></a>00219 <span class="comment"> *</span><a name="l00220"></a>00220 <span class="comment"> * \hideinitializer</span><a name="l00221"></a>00221 <span class="comment"> */</span><a name="l00222"></a><a class="code" href="a00016.html#g1eaaf4d9d75e24582acc6440d7085f19">00222</a> <span class="preprocessor">#define PT_SEM_SIGNAL(pt, s) ++(s)->count</span><a name="l00223"></a>00223 <span class="preprocessor"></span><a name="l00224"></a>00224 <span class="preprocessor">#endif </span><span class="comment">/* __PT_SEM_H__ */</span><a name="l00225"></a>00225 <span class="comment"></span><a name="l00226"></a>00226 <span class="comment">/** @} */</span><span class="comment"></span><a name="l00227"></a>00227 <span class="comment">/** @} */</span><a name="l00228"></a>00228 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Oct 2 10:06:29 2006 for The Protothreads Library 1.4 by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -