xref: /nrf52832-nimble/rt-thread/components/net/uip/doc/html/a00142.html (revision 104654410c56c573564690304ae786df310c91fc)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3<title>uIP 1.0: Protothreads</title>
4<link href="doxygen.css" rel="stylesheet" type="text/css">
5<link href="tabs.css" rel="stylesheet" type="text/css">
6</head><body>
7<!-- Generated by Doxygen 1.4.6 -->
8<div class="tabs">
9  <ul>
10    <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
11    <li><a href="modules.html"><span>Modules</span></a></li>
12    <li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
13    <li><a href="files.html"><span>Files</span></a></li>
14    <li><a href="examples.html"><span>Examples</span></a></li>
15  </ul></div>
16<h1>Protothreads</h1><hr><a name="_details"></a><h2>Detailed Description</h2>
17Protothreads are a type of lightweight stackless threads designed for severly memory constrained systems such as deeply embedded systems or sensor network nodes.
18<p>
19Protothreads provides linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an RTOS.<p>
20Protothreads are a extremely lightweight, stackless type of threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. The purpose of protothreads is to implement sequential flow of control without complex state machines or full multi-threading. Protothreads provides conditional blocking inside C functions.<p>
21The advantage of protothreads over a purely event-driven approach is that protothreads provides a sequential code structure that allows for blocking functions. In purely event-driven systems, blocking must be implemented by manually breaking the function into two pieces - one for the piece of code before the blocking call and one for the code after the blocking call. This makes it hard to use control structures such as if() conditionals and while() loops.<p>
22The advantage of protothreads over ordinary threads is that a protothread do not require a separate stack. In memory constrained systems, the overhead of allocating multiple stacks can consume large amounts of the available memory. In contrast, each protothread only requires between two and twelve bytes of state, depending on the architecture.<p>
23<dl compact><dt><b>Note:</b></dt><dd>Because protothreads do not save the stack context across a blocking call, <b>local variables are not preserved when the protothread blocks</b>. This means that local variables should be used with utmost care - <b>if in doubt, do not use local variables inside a protothread!</b></dd></dl>
24Main features:<p>
25<ul>
26<li>No machine specific code - the protothreads library is pure C</li></ul>
27<p>
28<ul>
29<li>Does not use error-prone functions such as longjmp()</li></ul>
30<p>
31<ul>
32<li>Very small RAM overhead - only two bytes per protothread</li></ul>
33<p>
34<ul>
35<li>Can be used with or without an OS</li></ul>
36<p>
37<ul>
38<li>Provides blocking wait without full multi-threading or stack-switching</li></ul>
39<p>
40Examples applications:<p>
41<ul>
42<li>Memory constrained systems</li></ul>
43<p>
44<ul>
45<li>Event-driven protocol stacks</li></ul>
46<p>
47<ul>
48<li>Deeply embedded systems</li></ul>
49<p>
50<ul>
51<li>Sensor network nodes</li></ul>
52<p>
53The protothreads API consists of four basic operations: initialization: <a class="el" href="a00142.html#ge6bae7dc0225468c8a5ac269df549892">PT_INIT()</a>, execution: <a class="el" href="a00142.html#g2ffbb9e554e08a343ae2f9de4bedfdfc">PT_BEGIN()</a>, conditional blocking: <a class="el" href="a00142.html#g99e43010ec61327164466aa2d902de45">PT_WAIT_UNTIL()</a> and exit: <a class="el" href="a00142.html#g7b04a0035bef29d905496c23bae066d2">PT_END()</a>. On top of these, two convenience functions are built: reversed condition blocking: <a class="el" href="a00142.html#gad14bbbf092b90aa0a5a4f9169504a8d">PT_WAIT_WHILE()</a> and protothread blocking: <a class="el" href="a00142.html#g2f8f70c30b9ee08a103fbd69a4365c4c">PT_WAIT_THREAD()</a>.<p>
54<dl compact><dt><b>See also:</b></dt><dd><a class="el" href="a00142.html">Protothreads API documentation</a></dd></dl>
55The protothreads library is released under a BSD-style license that allows for both non-commercial and commercial usage. The only requirement is that credit is given.<h2><a class="anchor" name="authors">
56Authors</a></h2>
57The protothreads library was written by Adam Dunkels &lt;<a href="mailto:[email protected]">[email protected]</a>&gt; with support from Oliver Schmidt &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;.<h2><a class="anchor" name="pt-desc">
58Protothreads</a></h2>
59Protothreads are a extremely lightweight, stackless threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. The purpose of protothreads is to implement sequential flow of control without using complex state machines or full multi-threading. Protothreads provides conditional blocking inside a C function.<p>
60In memory constrained systems, such as deeply embedded systems, traditional multi-threading may have a too large memory overhead. In traditional multi-threading, each thread requires its own stack, that typically is over-provisioned. The stacks may use large parts of the available memory.<p>
61The main advantage of protothreads over ordinary threads is that protothreads are very lightweight: a protothread does not require its own stack. Rather, all protothreads run on the same stack and context switching is done by stack rewinding. This is advantageous in memory constrained systems, where a stack for a thread might use a large part of the available memory. A protothread only requires only two bytes of memory per protothread. Moreover, protothreads are implemented in pure C and do not require any machine-specific assembler code.<p>
62A protothread runs within a single C function and cannot span over other functions. A protothread may call normal C functions, but cannot block inside a called function. Blocking inside nested function calls is instead made by spawning a separate protothread for each potentially blocking function. The advantage of this approach is that blocking is explicit: the programmer knows exactly which functions that block that which functions the never blocks.<p>
63Protothreads are similar to asymmetric co-routines. The main difference is that co-routines uses a separate stack for each co-routine, whereas protothreads are stackless. The most similar mechanism to protothreads are Python generators. These are also stackless constructs, but have a different purpose. Protothreads provides blocking contexts inside a C function, whereas Python generators provide multiple exit points from a generator function.<h2><a class="anchor" name="pt-autovars">
64Local variables</a></h2>
65<dl compact><dt><b>Note:</b></dt><dd>Because protothreads do not save the stack context across a blocking call, local variables are not preserved when the protothread blocks. This means that local variables should be used with utmost care - if in doubt, do not use local variables inside a protothread!</dd></dl>
66<h2><a class="anchor" name="pt-scheduling">
67Scheduling</a></h2>
68A protothread is driven by repeated calls to the function in which the protothread is running. Each time the function is called, the protothread will run until it blocks or exits. Thus the scheduling of protothreads is done by the application that uses protothreads.<h2><a class="anchor" name="pt-impl">
69Implementation</a></h2>
70Protothreads are implemented using <a class="el" href="a00155.html">local continuations</a>. A local continuation represents the current state of execution at a particular place in the program, but does not provide any call history or local variables. A local continuation can be set in a specific function to capture the state of the function. After a local continuation has been set can be resumed in order to restore the state of the function at the point where the local continuation was set.<p>
71Local continuations can be implemented in a variety of ways:<p>
72<ol type=1>
73<li>by using machine specific assembler code,</li><li>by using standard C constructs, or</li><li>by using compiler extensions.</li></ol>
74<p>
75The first way works by saving and restoring the processor state, except for stack pointers, and requires between 16 and 32 bytes of memory per protothread. The exact amount of memory required depends on the architecture.<p>
76The standard C implementation requires only two bytes of state per protothread and utilizes the C switch() statement in a non-obvious way that is similar to Duff's device. This implementation does, however, impose a slight restriction to the code that uses protothreads in that the code cannot use switch() statements itself.<p>
77Certain compilers has C extensions that can be used to implement protothreads. GCC supports label pointers that can be used for this purpose. With this implementation, protothreads require 4 bytes of RAM per protothread.
78<p>
79<table border="0" cellpadding="0" cellspacing="0">
80<tr><td></td></tr>
81<tr><td colspan="2"><br><h2>Files</h2></td></tr>
82<tr><td class="memItemLeft" nowrap align="right" valign="top">file &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00128.html">pt.h</a></td></tr>
83
84<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Protothreads implementation. <br></td></tr>
85
86<p>
87<tr><td colspan="2"><br><h2>Modules</h2></td></tr>
88<tr><td class="memItemLeft" nowrap align="right" valign="top">&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00155.html">Local continuations</a></td></tr>
89
90<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Local continuations form the basis for implementing protothreads. <br></td></tr>
91
92<p>
93<tr><td colspan="2"><br><h2>Data Structures</h2></td></tr>
94<tr><td class="memItemLeft" nowrap align="right" valign="top">struct &nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00084.html">pt</a></td></tr>
95
96<tr><td colspan="2"><br><h2>Initialization</h2></td></tr>
97<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#ge6bae7dc0225468c8a5ac269df549892">PT_INIT</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
98
99<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Initialize a protothread.  <a href="#ge6bae7dc0225468c8a5ac269df549892"></a><br></td></tr>
100<tr><td colspan="2"><br><h2>Declaration and definition</h2></td></tr>
101<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g3d4c8bd4aada659eb34f5d2ffd3e7901">PT_THREAD</a>(name_args)</td></tr>
102
103<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Declaration of a protothread.  <a href="#g3d4c8bd4aada659eb34f5d2ffd3e7901"></a><br></td></tr>
104<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g2ffbb9e554e08a343ae2f9de4bedfdfc">PT_BEGIN</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
105
106<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Declare the start of a protothread inside the C function implementing the protothread.  <a href="#g2ffbb9e554e08a343ae2f9de4bedfdfc"></a><br></td></tr>
107<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g7b04a0035bef29d905496c23bae066d2">PT_END</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
108
109<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Declare the end of a protothread.  <a href="#g7b04a0035bef29d905496c23bae066d2"></a><br></td></tr>
110<tr><td colspan="2"><br><h2>Blocked wait</h2></td></tr>
111<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g99e43010ec61327164466aa2d902de45">PT_WAIT_UNTIL</a>(<a class="el" href="a00084.html">pt</a>, condition)</td></tr>
112
113<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Block and wait until condition is true.  <a href="#g99e43010ec61327164466aa2d902de45"></a><br></td></tr>
114<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#gad14bbbf092b90aa0a5a4f9169504a8d">PT_WAIT_WHILE</a>(<a class="el" href="a00084.html">pt</a>, cond)</td></tr>
115
116<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Block and wait while condition is true.  <a href="#gad14bbbf092b90aa0a5a4f9169504a8d"></a><br></td></tr>
117<tr><td colspan="2"><br><h2>Hierarchical protothreads</h2></td></tr>
118<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g2f8f70c30b9ee08a103fbd69a4365c4c">PT_WAIT_THREAD</a>(<a class="el" href="a00084.html">pt</a>, thread)</td></tr>
119
120<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Block and wait until a child protothread completes.  <a href="#g2f8f70c30b9ee08a103fbd69a4365c4c"></a><br></td></tr>
121<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g9e97a0b4d5cc7764d8e19758f5da53ae">PT_SPAWN</a>(<a class="el" href="a00084.html">pt</a>, child, thread)</td></tr>
122
123<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Spawn a child protothread and wait until it exits.  <a href="#g9e97a0b4d5cc7764d8e19758f5da53ae"></a><br></td></tr>
124<tr><td colspan="2"><br><h2>Exiting and restarting</h2></td></tr>
125<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#gcd3ac045f0a4ae63412e3b3d8780e8ab">PT_RESTART</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
126
127<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Restart the protothread.  <a href="#gcd3ac045f0a4ae63412e3b3d8780e8ab"></a><br></td></tr>
128<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g905451249dca72ce0385bf2a9ff178ee">PT_EXIT</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
129
130<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Exit the protothread.  <a href="#g905451249dca72ce0385bf2a9ff178ee"></a><br></td></tr>
131<tr><td colspan="2"><br><h2>Calling a protothread</h2></td></tr>
132<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#gfa82b860a64b67d25ab3abc21811896f">PT_SCHEDULE</a>(f)</td></tr>
133
134<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Schedule a protothread.  <a href="#gfa82b860a64b67d25ab3abc21811896f"></a><br></td></tr>
135<tr><td colspan="2"><br><h2>Yielding from a protothread</h2></td></tr>
136<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g155cba6121323726d02c00284428fed6">PT_YIELD</a>(<a class="el" href="a00084.html">pt</a>)</td></tr>
137
138<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Yield from the current protothread.  <a href="#g155cba6121323726d02c00284428fed6"></a><br></td></tr>
139<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#ge3c821e3a388615528efda9d23c7d115">PT_YIELD_UNTIL</a>(<a class="el" href="a00084.html">pt</a>, cond)</td></tr>
140
141<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Yield from the protothread until a condition occurs.  <a href="#ge3c821e3a388615528efda9d23c7d115"></a><br></td></tr>
142<tr><td colspan="2"><br><h2>Defines</h2></td></tr>
143<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="g7b5319b5b65761a845fcd1500fde4cdc"></a><!-- doxytag: member="pt::PT_WAITING" ref="g7b5319b5b65761a845fcd1500fde4cdc" args="" -->
144#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g7b5319b5b65761a845fcd1500fde4cdc">PT_WAITING</a>&nbsp;&nbsp;&nbsp;0</td></tr>
145
146<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="gcfae9053e5c107a1aed6b228c917d2ea"></a><!-- doxytag: member="pt::PT_EXITED" ref="gcfae9053e5c107a1aed6b228c917d2ea" args="" -->
147#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#gcfae9053e5c107a1aed6b228c917d2ea">PT_EXITED</a>&nbsp;&nbsp;&nbsp;1</td></tr>
148
149<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="g9ff1e8936a8a26bff54c05f8a989b93b"></a><!-- doxytag: member="pt::PT_ENDED" ref="g9ff1e8936a8a26bff54c05f8a989b93b" args="" -->
150#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#g9ff1e8936a8a26bff54c05f8a989b93b">PT_ENDED</a>&nbsp;&nbsp;&nbsp;2</td></tr>
151
152<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="ge469332907e0617d72d5e2dd4297119d"></a><!-- doxytag: member="pt::PT_YIELDED" ref="ge469332907e0617d72d5e2dd4297119d" args="" -->
153#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="a00142.html#ge469332907e0617d72d5e2dd4297119d">PT_YIELDED</a>&nbsp;&nbsp;&nbsp;3</td></tr>
154
155</table>
156<hr><h2>Define Documentation</h2>
157<a class="anchor" name="g2ffbb9e554e08a343ae2f9de4bedfdfc"></a><!-- doxytag: member="pt.h::PT_BEGIN" ref="g2ffbb9e554e08a343ae2f9de4bedfdfc" args="(pt)" --><p>
158<table class="mdTable" cellpadding="2" cellspacing="0">
159  <tr>
160    <td class="mdRow">
161      <table cellpadding="0" cellspacing="0" border="0">
162        <tr>
163          <td class="md" nowrap valign="top">#define PT_BEGIN          </td>
164          <td class="md" valign="top">(&nbsp;</td>
165          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
166          <td class="mdname1" valign="top" nowrap>          </td>
167          <td class="md" valign="top">&nbsp;)&nbsp;</td>
168          <td class="md" nowrap></td>
169        </tr>
170      </table>
171    </td>
172  </tr>
173</table>
174<table cellspacing="5" cellpadding="0" border="0">
175  <tr>
176    <td>
177      &nbsp;
178    </td>
179    <td>
180
181<p>
182Declare the start of a protothread inside the C function implementing the protothread.
183<p>
184This macro is used to declare the starting point of a protothread. It should be placed at the start of the function in which the protothread runs. All C statements above the <a class="el" href="a00142.html#g2ffbb9e554e08a343ae2f9de4bedfdfc">PT_BEGIN()</a> invokation will be executed each time the protothread is scheduled.<p>
185<dl compact><dt><b>Parameters:</b></dt><dd>
186  <table border="0" cellspacing="2" cellpadding="0">
187    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
188  </table>
189</dl>
190<dl compact><dt><b>Examples: </b></dt><dd>
191<a class="el" href="a00048.html#a17">dhcpc.c</a>.</dl>
192<p>
193Definition at line <a class="el" href="a00194.html#l00115">115</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
194  </tr>
195</table>
196<a class="anchor" name="g7b04a0035bef29d905496c23bae066d2"></a><!-- doxytag: member="pt.h::PT_END" ref="g7b04a0035bef29d905496c23bae066d2" args="(pt)" --><p>
197<table class="mdTable" cellpadding="2" cellspacing="0">
198  <tr>
199    <td class="mdRow">
200      <table cellpadding="0" cellspacing="0" border="0">
201        <tr>
202          <td class="md" nowrap valign="top">#define PT_END          </td>
203          <td class="md" valign="top">(&nbsp;</td>
204          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
205          <td class="mdname1" valign="top" nowrap>          </td>
206          <td class="md" valign="top">&nbsp;)&nbsp;</td>
207          <td class="md" nowrap></td>
208        </tr>
209      </table>
210    </td>
211  </tr>
212</table>
213<table cellspacing="5" cellpadding="0" border="0">
214  <tr>
215    <td>
216      &nbsp;
217    </td>
218    <td>
219
220<p>
221Declare the end of a protothread.
222<p>
223This macro is used for declaring that a protothread ends. It must always be used together with a matching <a class="el" href="a00142.html#g2ffbb9e554e08a343ae2f9de4bedfdfc">PT_BEGIN()</a> macro.<p>
224<dl compact><dt><b>Parameters:</b></dt><dd>
225  <table border="0" cellspacing="2" cellpadding="0">
226    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
227  </table>
228</dl>
229<dl compact><dt><b>Examples: </b></dt><dd>
230<a class="el" href="a00048.html#a34">dhcpc.c</a>.</dl>
231<p>
232Definition at line <a class="el" href="a00194.html#l00127">127</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
233  </tr>
234</table>
235<a class="anchor" name="g905451249dca72ce0385bf2a9ff178ee"></a><!-- doxytag: member="pt.h::PT_EXIT" ref="g905451249dca72ce0385bf2a9ff178ee" args="(pt)" --><p>
236<table class="mdTable" cellpadding="2" cellspacing="0">
237  <tr>
238    <td class="mdRow">
239      <table cellpadding="0" cellspacing="0" border="0">
240        <tr>
241          <td class="md" nowrap valign="top">#define PT_EXIT          </td>
242          <td class="md" valign="top">(&nbsp;</td>
243          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
244          <td class="mdname1" valign="top" nowrap>          </td>
245          <td class="md" valign="top">&nbsp;)&nbsp;</td>
246          <td class="md" nowrap></td>
247        </tr>
248      </table>
249    </td>
250  </tr>
251</table>
252<table cellspacing="5" cellpadding="0" border="0">
253  <tr>
254    <td>
255      &nbsp;
256    </td>
257    <td>
258
259<p>
260Exit the protothread.
261<p>
262This macro causes the protothread to exit. If the protothread was spawned by another protothread, the parent protothread will become unblocked and can continue to run.<p>
263<dl compact><dt><b>Parameters:</b></dt><dd>
264  <table border="0" cellspacing="2" cellpadding="0">
265    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
266  </table>
267</dl>
268
269<p>
270Definition at line <a class="el" href="a00194.html#l00246">246</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
271  </tr>
272</table>
273<a class="anchor" name="ge6bae7dc0225468c8a5ac269df549892"></a><!-- doxytag: member="pt.h::PT_INIT" ref="ge6bae7dc0225468c8a5ac269df549892" args="(pt)" --><p>
274<table class="mdTable" cellpadding="2" cellspacing="0">
275  <tr>
276    <td class="mdRow">
277      <table cellpadding="0" cellspacing="0" border="0">
278        <tr>
279          <td class="md" nowrap valign="top">#define PT_INIT          </td>
280          <td class="md" valign="top">(&nbsp;</td>
281          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
282          <td class="mdname1" valign="top" nowrap>          </td>
283          <td class="md" valign="top">&nbsp;)&nbsp;</td>
284          <td class="md" nowrap></td>
285        </tr>
286      </table>
287    </td>
288  </tr>
289</table>
290<table cellspacing="5" cellpadding="0" border="0">
291  <tr>
292    <td>
293      &nbsp;
294    </td>
295    <td>
296
297<p>
298Initialize a protothread.
299<p>
300Initializes a protothread. Initialization must be done prior to starting to execute the protothread.<p>
301<dl compact><dt><b>Parameters:</b></dt><dd>
302  <table border="0" cellspacing="2" cellpadding="0">
303    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure.</td></tr>
304  </table>
305</dl>
306<dl compact><dt><b>See also:</b></dt><dd><a class="el" href="a00142.html#g9e97a0b4d5cc7764d8e19758f5da53ae">PT_SPAWN()</a> </dd></dl>
307<dl compact><dt><b>Examples: </b></dt><dd>
308<a class="el" href="a00048.html#a41">dhcpc.c</a>.</dl>
309<p>
310Definition at line <a class="el" href="a00194.html#l00080">80</a> of file <a class="el" href="a00194.html">pt.h</a>.
311<p>
312Referenced by <a class="el" href="a00184.html#l00298">httpd_appcall()</a>.    </td>
313  </tr>
314</table>
315<a class="anchor" name="gcd3ac045f0a4ae63412e3b3d8780e8ab"></a><!-- doxytag: member="pt.h::PT_RESTART" ref="gcd3ac045f0a4ae63412e3b3d8780e8ab" args="(pt)" --><p>
316<table class="mdTable" cellpadding="2" cellspacing="0">
317  <tr>
318    <td class="mdRow">
319      <table cellpadding="0" cellspacing="0" border="0">
320        <tr>
321          <td class="md" nowrap valign="top">#define PT_RESTART          </td>
322          <td class="md" valign="top">(&nbsp;</td>
323          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
324          <td class="mdname1" valign="top" nowrap>          </td>
325          <td class="md" valign="top">&nbsp;)&nbsp;</td>
326          <td class="md" nowrap></td>
327        </tr>
328      </table>
329    </td>
330  </tr>
331</table>
332<table cellspacing="5" cellpadding="0" border="0">
333  <tr>
334    <td>
335      &nbsp;
336    </td>
337    <td>
338
339<p>
340Restart the protothread.
341<p>
342This macro will block and cause the running protothread to restart its execution at the place of the <a class="el" href="a00142.html#g2ffbb9e554e08a343ae2f9de4bedfdfc">PT_BEGIN()</a> call.<p>
343<dl compact><dt><b>Parameters:</b></dt><dd>
344  <table border="0" cellspacing="2" cellpadding="0">
345    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
346  </table>
347</dl>
348<dl compact><dt><b>Examples: </b></dt><dd>
349<a class="el" href="a00048.html#a27">dhcpc.c</a>.</dl>
350<p>
351Definition at line <a class="el" href="a00194.html#l00229">229</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
352  </tr>
353</table>
354<a class="anchor" name="gfa82b860a64b67d25ab3abc21811896f"></a><!-- doxytag: member="pt.h::PT_SCHEDULE" ref="gfa82b860a64b67d25ab3abc21811896f" args="(f)" --><p>
355<table class="mdTable" cellpadding="2" cellspacing="0">
356  <tr>
357    <td class="mdRow">
358      <table cellpadding="0" cellspacing="0" border="0">
359        <tr>
360          <td class="md" nowrap valign="top">#define PT_SCHEDULE          </td>
361          <td class="md" valign="top">(&nbsp;</td>
362          <td class="md" nowrap valign="top">f&nbsp;</td>
363          <td class="mdname1" valign="top" nowrap>          </td>
364          <td class="md" valign="top">&nbsp;)&nbsp;</td>
365          <td class="md" nowrap></td>
366        </tr>
367      </table>
368    </td>
369  </tr>
370</table>
371<table cellspacing="5" cellpadding="0" border="0">
372  <tr>
373    <td>
374      &nbsp;
375    </td>
376    <td>
377
378<p>
379Schedule a protothread.
380<p>
381This function shedules a protothread. The return value of the function is non-zero if the protothread is running or zero if the protothread has exited.<p>
382<dl compact><dt><b>Parameters:</b></dt><dd>
383  <table border="0" cellspacing="2" cellpadding="0">
384    <tr><td valign="top"></td><td valign="top"><em>f</em>&nbsp;</td><td>The call to the C function implementing the protothread to be scheduled </td></tr>
385  </table>
386</dl>
387
388<p>
389Definition at line <a class="el" href="a00194.html#l00271">271</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
390  </tr>
391</table>
392<a class="anchor" name="g9e97a0b4d5cc7764d8e19758f5da53ae"></a><!-- doxytag: member="pt.h::PT_SPAWN" ref="g9e97a0b4d5cc7764d8e19758f5da53ae" args="(pt, child, thread)" --><p>
393<table class="mdTable" cellpadding="2" cellspacing="0">
394  <tr>
395    <td class="mdRow">
396      <table cellpadding="0" cellspacing="0" border="0">
397        <tr>
398          <td class="md" nowrap valign="top">#define PT_SPAWN          </td>
399          <td class="md" valign="top">(&nbsp;</td>
400          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>,         <tr>
401          <td class="md" nowrap align="right"></td>
402          <td class="md"></td>
403          <td class="md" nowrap>child,         <tr>
404          <td class="md" nowrap align="right"></td>
405          <td class="md"></td>
406          <td class="md" nowrap>thread&nbsp;</td>
407          <td class="mdname1" valign="top" nowrap>          </td>
408          <td class="md" valign="top">&nbsp;)&nbsp;</td>
409          <td class="md" nowrap></td>
410        </tr>
411      </table>
412    </td>
413  </tr>
414</table>
415<table cellspacing="5" cellpadding="0" border="0">
416  <tr>
417    <td>
418      &nbsp;
419    </td>
420    <td>
421
422<p>
423Spawn a child protothread and wait until it exits.
424<p>
425This macro spawns a child protothread and waits until it exits. The macro can only be used within a protothread.<p>
426<dl compact><dt><b>Parameters:</b></dt><dd>
427  <table border="0" cellspacing="2" cellpadding="0">
428    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
429    <tr><td valign="top"></td><td valign="top"><em>child</em>&nbsp;</td><td>A pointer to the child protothread's control structure. </td></tr>
430    <tr><td valign="top"></td><td valign="top"><em>thread</em>&nbsp;</td><td>The child protothread with arguments </td></tr>
431  </table>
432</dl>
433
434<p>
435Definition at line <a class="el" href="a00194.html#l00206">206</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
436  </tr>
437</table>
438<a class="anchor" name="g3d4c8bd4aada659eb34f5d2ffd3e7901"></a><!-- doxytag: member="pt.h::PT_THREAD" ref="g3d4c8bd4aada659eb34f5d2ffd3e7901" args="(name_args)" --><p>
439<table class="mdTable" cellpadding="2" cellspacing="0">
440  <tr>
441    <td class="mdRow">
442      <table cellpadding="0" cellspacing="0" border="0">
443        <tr>
444          <td class="md" nowrap valign="top">#define PT_THREAD          </td>
445          <td class="md" valign="top">(&nbsp;</td>
446          <td class="md" nowrap valign="top">name_args&nbsp;</td>
447          <td class="mdname1" valign="top" nowrap>          </td>
448          <td class="md" valign="top">&nbsp;)&nbsp;</td>
449          <td class="md" nowrap></td>
450        </tr>
451      </table>
452    </td>
453  </tr>
454</table>
455<table cellspacing="5" cellpadding="0" border="0">
456  <tr>
457    <td>
458      &nbsp;
459    </td>
460    <td>
461
462<p>
463Declaration of a protothread.
464<p>
465This macro is used to declare a protothread. All protothreads must be declared with this macro.<p>
466<dl compact><dt><b>Parameters:</b></dt><dd>
467  <table border="0" cellspacing="2" cellpadding="0">
468    <tr><td valign="top"></td><td valign="top"><em>name_args</em>&nbsp;</td><td>The name and arguments of the C function implementing the protothread. </td></tr>
469  </table>
470</dl>
471<dl compact><dt><b>Examples: </b></dt><dd>
472<a class="el" href="a00048.html#a16">dhcpc.c</a>, and <a class="el" href="a00038.html#a165">smtp.c</a>.</dl>
473<p>
474Definition at line <a class="el" href="a00194.html#l00100">100</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
475  </tr>
476</table>
477<a class="anchor" name="g2f8f70c30b9ee08a103fbd69a4365c4c"></a><!-- doxytag: member="pt.h::PT_WAIT_THREAD" ref="g2f8f70c30b9ee08a103fbd69a4365c4c" args="(pt, thread)" --><p>
478<table class="mdTable" cellpadding="2" cellspacing="0">
479  <tr>
480    <td class="mdRow">
481      <table cellpadding="0" cellspacing="0" border="0">
482        <tr>
483          <td class="md" nowrap valign="top">#define PT_WAIT_THREAD          </td>
484          <td class="md" valign="top">(&nbsp;</td>
485          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>,         <tr>
486          <td class="md" nowrap align="right"></td>
487          <td class="md"></td>
488          <td class="md" nowrap>thread&nbsp;</td>
489          <td class="mdname1" valign="top" nowrap>          </td>
490          <td class="md" valign="top">&nbsp;)&nbsp;</td>
491          <td class="md" nowrap></td>
492        </tr>
493      </table>
494    </td>
495  </tr>
496</table>
497<table cellspacing="5" cellpadding="0" border="0">
498  <tr>
499    <td>
500      &nbsp;
501    </td>
502    <td>
503
504<p>
505Block and wait until a child protothread completes.
506<p>
507This macro schedules a child protothread. The current protothread will block until the child protothread completes.<p>
508<dl compact><dt><b>Note:</b></dt><dd>The child protothread must be manually initialized with the <a class="el" href="a00142.html#ge6bae7dc0225468c8a5ac269df549892">PT_INIT()</a> function before this function is used.</dd></dl>
509<dl compact><dt><b>Parameters:</b></dt><dd>
510  <table border="0" cellspacing="2" cellpadding="0">
511    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
512    <tr><td valign="top"></td><td valign="top"><em>thread</em>&nbsp;</td><td>The child protothread with arguments</td></tr>
513  </table>
514</dl>
515<dl compact><dt><b>See also:</b></dt><dd><a class="el" href="a00142.html#g9e97a0b4d5cc7764d8e19758f5da53ae">PT_SPAWN()</a> </dd></dl>
516
517<p>
518Definition at line <a class="el" href="a00194.html#l00192">192</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
519  </tr>
520</table>
521<a class="anchor" name="g99e43010ec61327164466aa2d902de45"></a><!-- doxytag: member="pt.h::PT_WAIT_UNTIL" ref="g99e43010ec61327164466aa2d902de45" args="(pt, condition)" --><p>
522<table class="mdTable" cellpadding="2" cellspacing="0">
523  <tr>
524    <td class="mdRow">
525      <table cellpadding="0" cellspacing="0" border="0">
526        <tr>
527          <td class="md" nowrap valign="top">#define PT_WAIT_UNTIL          </td>
528          <td class="md" valign="top">(&nbsp;</td>
529          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>,         <tr>
530          <td class="md" nowrap align="right"></td>
531          <td class="md"></td>
532          <td class="md" nowrap>condition&nbsp;</td>
533          <td class="mdname1" valign="top" nowrap>          </td>
534          <td class="md" valign="top">&nbsp;)&nbsp;</td>
535          <td class="md" nowrap></td>
536        </tr>
537      </table>
538    </td>
539  </tr>
540</table>
541<table cellspacing="5" cellpadding="0" border="0">
542  <tr>
543    <td>
544      &nbsp;
545    </td>
546    <td>
547
548<p>
549Block and wait until condition is true.
550<p>
551This macro blocks the protothread until the specified condition is true.<p>
552<dl compact><dt><b>Parameters:</b></dt><dd>
553  <table border="0" cellspacing="2" cellpadding="0">
554    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
555    <tr><td valign="top"></td><td valign="top"><em>condition</em>&nbsp;</td><td>The condition. </td></tr>
556  </table>
557</dl>
558<dl compact><dt><b>Examples: </b></dt><dd>
559<a class="el" href="a00048.html#a24">dhcpc.c</a>.</dl>
560<p>
561Definition at line <a class="el" href="a00194.html#l00148">148</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
562  </tr>
563</table>
564<a class="anchor" name="gad14bbbf092b90aa0a5a4f9169504a8d"></a><!-- doxytag: member="pt.h::PT_WAIT_WHILE" ref="gad14bbbf092b90aa0a5a4f9169504a8d" args="(pt, cond)" --><p>
565<table class="mdTable" cellpadding="2" cellspacing="0">
566  <tr>
567    <td class="mdRow">
568      <table cellpadding="0" cellspacing="0" border="0">
569        <tr>
570          <td class="md" nowrap valign="top">#define PT_WAIT_WHILE          </td>
571          <td class="md" valign="top">(&nbsp;</td>
572          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>,         <tr>
573          <td class="md" nowrap align="right"></td>
574          <td class="md"></td>
575          <td class="md" nowrap>cond&nbsp;</td>
576          <td class="mdname1" valign="top" nowrap>          </td>
577          <td class="md" valign="top">&nbsp;)&nbsp;</td>
578          <td class="md" nowrap></td>
579        </tr>
580      </table>
581    </td>
582  </tr>
583</table>
584<table cellspacing="5" cellpadding="0" border="0">
585  <tr>
586    <td>
587      &nbsp;
588    </td>
589    <td>
590
591<p>
592Block and wait while condition is true.
593<p>
594This function blocks and waits while condition is true. See <a class="el" href="a00142.html#g99e43010ec61327164466aa2d902de45">PT_WAIT_UNTIL()</a>.<p>
595<dl compact><dt><b>Parameters:</b></dt><dd>
596  <table border="0" cellspacing="2" cellpadding="0">
597    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
598    <tr><td valign="top"></td><td valign="top"><em>cond</em>&nbsp;</td><td>The condition. </td></tr>
599  </table>
600</dl>
601
602<p>
603Definition at line <a class="el" href="a00194.html#l00167">167</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
604  </tr>
605</table>
606<a class="anchor" name="g155cba6121323726d02c00284428fed6"></a><!-- doxytag: member="pt.h::PT_YIELD" ref="g155cba6121323726d02c00284428fed6" args="(pt)" --><p>
607<table class="mdTable" cellpadding="2" cellspacing="0">
608  <tr>
609    <td class="mdRow">
610      <table cellpadding="0" cellspacing="0" border="0">
611        <tr>
612          <td class="md" nowrap valign="top">#define PT_YIELD          </td>
613          <td class="md" valign="top">(&nbsp;</td>
614          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>&nbsp;</td>
615          <td class="mdname1" valign="top" nowrap>          </td>
616          <td class="md" valign="top">&nbsp;)&nbsp;</td>
617          <td class="md" nowrap></td>
618        </tr>
619      </table>
620    </td>
621  </tr>
622</table>
623<table cellspacing="5" cellpadding="0" border="0">
624  <tr>
625    <td>
626      &nbsp;
627    </td>
628    <td>
629
630<p>
631Yield from the current protothread.
632<p>
633This function will yield the protothread, thereby allowing other processing to take place in the system.<p>
634<dl compact><dt><b>Parameters:</b></dt><dd>
635  <table border="0" cellspacing="2" cellpadding="0">
636    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
637  </table>
638</dl>
639<dl compact><dt><b>Examples: </b></dt><dd>
640<a class="el" href="a00048.html#a33">dhcpc.c</a>.</dl>
641<p>
642Definition at line <a class="el" href="a00194.html#l00290">290</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
643  </tr>
644</table>
645<a class="anchor" name="ge3c821e3a388615528efda9d23c7d115"></a><!-- doxytag: member="pt.h::PT_YIELD_UNTIL" ref="ge3c821e3a388615528efda9d23c7d115" args="(pt, cond)" --><p>
646<table class="mdTable" cellpadding="2" cellspacing="0">
647  <tr>
648    <td class="mdRow">
649      <table cellpadding="0" cellspacing="0" border="0">
650        <tr>
651          <td class="md" nowrap valign="top">#define PT_YIELD_UNTIL          </td>
652          <td class="md" valign="top">(&nbsp;</td>
653          <td class="md" nowrap valign="top"><a class="el" href="a00084.html">pt</a>,         <tr>
654          <td class="md" nowrap align="right"></td>
655          <td class="md"></td>
656          <td class="md" nowrap>cond&nbsp;</td>
657          <td class="mdname1" valign="top" nowrap>          </td>
658          <td class="md" valign="top">&nbsp;)&nbsp;</td>
659          <td class="md" nowrap></td>
660        </tr>
661      </table>
662    </td>
663  </tr>
664</table>
665<table cellspacing="5" cellpadding="0" border="0">
666  <tr>
667    <td>
668      &nbsp;
669    </td>
670    <td>
671
672<p>
673Yield from the protothread until a condition occurs.
674<p>
675<dl compact><dt><b>Parameters:</b></dt><dd>
676  <table border="0" cellspacing="2" cellpadding="0">
677    <tr><td valign="top"></td><td valign="top"><em>pt</em>&nbsp;</td><td>A pointer to the protothread control structure. </td></tr>
678    <tr><td valign="top"></td><td valign="top"><em>cond</em>&nbsp;</td><td>The condition.</td></tr>
679  </table>
680</dl>
681This function will yield the protothread, until the specified condition evaluates to true.
682<p>
683Definition at line <a class="el" href="a00194.html#l00310">310</a> of file <a class="el" href="a00194.html">pt.h</a>.    </td>
684  </tr>
685</table>
686<hr size="1"><address style="align: right;"><small>Generated on Mon Jun 12 10:23:02 2006 for uIP 1.0 by&nbsp;
687<a href="http://www.doxygen.org/index.html">
688<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
689</body>
690</html>
691