File: pcl.html

package info (click to toggle)
libpcl1 1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,696 kB
  • sloc: sh: 9,812; ansic: 875; makefile: 17
file content (440 lines) | stat: -rw-r--r-- 10,749 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PCL</TITLE>
</HEAD><BODY text="#0000FF" bgcolor="#FFFFFF" style="font-family: monospace;">
<H1>PCL</H1>
Section: Portable Coroutine Library (3)<BR>Updated: 1.12<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>

<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>

co_thread_init, co_thread_cleanup, co_create, co_call, co_resume, co_delete,
co_exit_to, co_exit, co_current, co_get_data, co_set_data - C coroutine management
<P>
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>

<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pcl.h">pcl.h</A>&gt;</B>

<B>int co_thread_init(void);</B>

<B>void co_thread_cleanup(void);</B>
<B>coroutine_t co_create(void *</B><I>func</I><B>, void *</B><I>data</I><B>, void *</B><I>stack</I><B>, int </B><I>stacksize</I><B>);</B>
<B>void co_delete(coroutine_t </B><I>co</I><B>);</B>
<B>void co_call(coroutine_t </B><I>co</I><B>);</B>
<B>void co_resume(void);</B>
<B>void co_exit_to(coroutine_t </B><I>co</I><B>);</B>
<B>void co_exit(void);</B>
<B>coroutine_t co_current(void);</B>
<B>void *co_get_data(coroutine_t </B><I>co</I><B>);</B>
<B>void *co_set_data(coroutine_t </B><I>co</I><B>, void *</B><I>data</I><B>);</B>
</PRE>

<P>
Link with
<I>-lpthread</I>

if you are using a multi-thread version of
<B>PCL</B>.


<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>

The
<B>Portable Coroutine Library (PCL)</B>

implements the low level functionality for coroutines. For a definition
of the term
<B>coroutine</B>

see
<I>The Art of Computer Programming</I> by <I>Donald E. Knuth</I>.

Coroutines are a very simple cooperative multitasking environment
where the switch from one task to another is done explicitly by a function call.
Coroutines are a lot faster than processes or threads switch, since
there is no OS kernel involvement for the operation. This document
defines an API for the low level handling of coroutines
i.e. creating and deleting coroutines and switching between them.
Higher level functionality (scheduler, etc.) is not covered.
<P>
<A NAME="lbAE">&nbsp;</A>
<H3>Functions</H3>

The following functions are defined:
<DL COMPACT>
<DT><B>int co_thread_init(void);</B>

<DD>
<P>
If the
<B>PCL</B>

library is built in multi-thread mode, and if multi threads are actually
used, this function should be called before calling any
<B>PCL</B>

function.
If the
<B>PCL</B>

library is built in multi-thread mode, but it is used only from one
thread (the main one, likely), then it is possible to avoid to call
<B>co_thread_init</B>().

Returns 0 in case of success, or an negative error code in case of error.
<P>
<DT><B>void co_thread_cleanup(void);</B>

<DD>
If the
<B>PCL</B>

library is built in multi-thread mode, and if multi threads are actually
used, this function should be called before the thread exits, or whenever
the thread decides it won't call the
<B>PCL</B>

functions anymore.
A failure in calling
<B>co_thread_cleanup</B>()

will result in resource leakage by the calling application.
<P>
<DT><B>coroutine_t co_create(void *</B><I>func</I><B>, void *</B><I>data</I><B>, void *</B><I>stack</I><B>, int </B><I>stacksize</I><B>);</B>

<DD>
<P>
This function creates a new coroutine.
<I>func</I>

is the entry point of the coroutine.  It will be called with one
arg, a
<B>void *</B>,

which holds the data passed through the
<I>data</I>

parameter. If
<I>func</I>

terminates, the associated coroutine is deleted.
<I>stack</I>

is the base of the stack this coroutine will use and
<I>stacksize</I>

its size in bytes.  You may pass a
<B>NULL</B>

pointer for
<I>stack</I>

in which case the memory will be allocated by
<B>co_create</B>

itself.  Both,
<I>stack</I> and <I>stacksize</I>

are aligned to system requirements.
A
<I>stacksize</I>

of less then 4096 bytes will be rejected.
You have to make sure, that the stack is large enough for your
coroutine and possible signal handlers (see below).  The stack
will not grow!  (Exception: the main coroutine uses the standard
system stack which may still grow) On success, a handle
(<B>coroutine_t</B>)

for a new coroutine is returned, otherwise
<B>NULL</B>.

<P>
<DT><B>void co_delete(coroutine_t </B><I>co</I><B>);</B>

<DD>
<P>
This function deletes the given coroutine
<I>co</I>.

If the stack for this coroutine was allocated by
<B>co_create</B>

it will be freed.  After a coroutine handle was passed to
<B>co_delete</B>

it is invalid and may not be used any more.
It is invalid for a coroutine to delete itself with this
function.
<P>
<DT><B>void co_call(coroutine_t </B><I>co</I><B>);</B>

<DD>
<P>
This function passes execution to the given coroutine
<I>co</I>.

The first time the coroutine is executed, its entry point
<I>func</I>

is called, and the
<I>data</I>

parameter used during the call to
<B>co_create</B>

is passed to
<I>func</I>.

The current coroutine is suspended until another one restarts it with a
<B>co_call</B>

or
<B>co_resume</B>

call. Calling oneself returns immediately.
<P>
<DT><B>void co_resume(void);</B>

<DD>
<P>
This function passes execution back to the coroutine which either
initially started this one or restarted it after a prior
<B>co_resume</B>.

<P>
<DT><B>void co_exit_to(coroutine_t </B><I>co</I><B>);</B>

<DD>
<P>
This function does the same a
<B>co_delete(co_current())</B>

followed by a
<B>co_call</B>

would do.  That is, it deletes itself and then passes execution
to another coroutine
<I>co</I>.

<P>
<DT><B>void co_exit(void);</B>

<DD>
<P>
This function does the same a
<B>co_delete(co_current())</B>

followed by a
<B>co_resume</B>

would do.  That is, it deletes itself and then passes execution
back to the coroutine which either initially started this one or
restarted it after a prior
<B>co_resume</B>.

<P>
<DT><B>coroutine_t co_current(void);</B>

<DD>
<P>
This function returns the currently running coroutine.
<P>
<DT><B>void *co_get_data(coroutine_t </B><I>co</I><B>);</B>

<DD>
<P>
This function returns the data associated with the
<I>co</I>

<BR>&nbsp;coroutine.&nbsp;The&nbsp;data&nbsp;associated&nbsp;with&nbsp;a&nbsp;coroutine&nbsp;is&nbsp;the
<I>data</I>

parameter passed to
<B>co_create</B>().

<P>
<DT><B>void *co_set_data(coroutine_t </B><I>co</I><B>, void *</B><I>data</I><B>);</B>

<DD>
<P>
Sets the
<I>data</I>

associated with the
<I>co</I>

coroutine, and returns the previously associated data.
<P>
</DL>
<A NAME="lbAF">&nbsp;</A>
<H3>Notes</H3>

Some interactions with other parts of the system are covered here.
<DL COMPACT>
<DT><B>Threads</B>

<DD>
If the
<B>PCL</B>

library has been built in multi-thread mode, then it is possible to use
it in multi-thread software.
A thread should call
<B>co_thread_init</B>()

before using the
<B>PCL</B>

APIs, and call
<B>co_thread_cleanup</B>()

before exiting, or when it has done using the
<B>PCL</B>

APIs.
<BR>

<B>WARNING:</B>

For no reason should two different threads run the same coroutine at the
same time.
<P>
<DT><B>Signals</B>

<DD>
First, a signal handler is not defined to run in any specific
coroutine. The only way to leave the signal handler is
by a return statement.
<P>
Second, the signal handler may run with the stack of any coroutine,
even with the stack of library internal coroutines which have an
undefined stack size (just enough to perform a kernel call).
Using and alternate stack for signal processing (see
<B><A HREF="/cgi-bin/man/man2html?2+sigaltstack">sigaltstack</A></B>(2))

is recommended!
<P>
Conclusion: avoid signals like a plague.  The only thing you may
do reliable is setting some global variables and return.
Simple kernel calls may work too, but nowadays it's pretty hairy
to tell, which function really is a kernel call.
(Btw, all this applies to normal C programs, too.  The coroutines
just add one more problem)
<DT><B>setjmp</B>/<B>longjmp</B>

<DD>
The use of
<B><A HREF="/cgi-bin/man/man2html?2+setjmp">setjmp</A></B>(2)/<B><A HREF="/cgi-bin/man/man2html?2+longjmp">longjmp</A></B>(2)

is limited to jumping inside one coroutine.  Never try to jump from
one coroutine to another with
<B><A HREF="/cgi-bin/man/man2html?2+longjmp">longjmp</A></B>(2).

<P>
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>DIAGNOSTICS</H2>

Some fatal errors are caught by the library.  If one occurs,
a short message is written to file descriptor 2 (stderr) and
a segmentation violation is generated.
<DL COMPACT>
<DT><B>[PCL]: Cannot delete itself</B>

<DD>
A coroutine has called
<B>co_delete</B>

with it's own handle.
<DT><B>[PCL]: Resume to deleted coroutine</B>

<DD>
A coroutine has deleted itself with
<B>co_exit</B> or <B>co_exit_to</B>

and the coroutine that was activated by the exit tried a
<B>co_resume</B>.

<DT><B>[PCL]: Stale coroutine called</B>

<DD>
Someone tried to active a coroutine that has already been
deleted.  This error is only detected, if the stack of the
deleted coroutine is still resident in memory.
<DT><B>[PCL]: Context switch failed</B>

<DD>
Low level error generated by the library in case a context switch
between two coroutines failes.
<P>
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>SEE ALSO</H2>

Original
<B>coroutine</B>

library at
<B><A HREF="http://www.goron.de/~froese/coro/coro.html">http://www.goron.de/~froese/coro/coro.html</A></B> .

GNU Pth library at
<B><A HREF="http://www.gnu.org/software/pth/">http://www.gnu.org/software/pth/</A></B> .

<P>
<A NAME="lbAI">&nbsp;</A>
<H2>AUTHOR</H2>

Developed by Davide Libenzi &lt;
<B><A HREF="mailto:davidel@xmailserver.org">davidel@xmailserver.org</A></B> &gt;.

<BR>

Ideas and man page base source taken by the coroutine library developed by
E. Toernig &lt;
<B><A HREF="mailto:froese@gmx.de">froese@gmx.de</A></B> &gt;.

<BR>

Also some code and ideas comes from the GNU Pth library available at
<B><A HREF="http://www.gnu.org/software/pth/">http://www.gnu.org/software/pth/</A></B> .

<P>
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>

There are no known bugs.  But, this library is still
in development even if it results very stable and pretty much ready for
production use.
<P>
Bug reports and comments to Davide Libenzi &lt;
<B><A HREF="mailto:davidel@xmailserver.org">davidel@xmailserver.org</A></B> &gt;.

<P>
<P>

<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT><A HREF="#lbAB">NAME</A><DD>
<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT><A HREF="#lbAE">Functions</A><DD>
<DT><A HREF="#lbAF">Notes</A><DD>
</DL>
<DT><A HREF="#lbAG">DIAGNOSTICS</A><DD>
<DT><A HREF="#lbAH">SEE ALSO</A><DD>
<DT><A HREF="#lbAI">AUTHOR</A><DD>
<DT><A HREF="#lbAJ">BUGS</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 03:12:40 GMT, November 20, 2010
</BODY>
</HTML>