File: pcl.3

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 (331 lines) | stat: -rw-r--r-- 9,238 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
.\"
.\"  PCL by Davide Libenzi ( Portable Coroutine Library )
.\"  Copyright (C) 2003  Davide Libenzi
.\"
.\"  This program is free software; you can redistribute it and/or modify
.\"  it under the terms of the GNU General Public License as published by
.\"  the Free Software Foundation; either version 2 of the License, or
.\"  (at your option) any later version.
.\"
.\"  This program is distributed in the hope that it will be useful,
.\"  but WITHOUT ANY WARRANTY; without even the implied warranty of
.\"  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
.\"  GNU General Public License for more details.
.\"
.\"  You should have received a copy of the GNU General Public License
.\"  along with this program; if not, write to the Free Software
.\"  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
.\"
.\"  Davide Libenzi <davidel@xmailserver.org>
.\"
.\"  Original man page source by E.Toernig <froese@gmx.de>
.\"
.na
.TH PCL 3 "1.12" "GNU" "Portable Coroutine Library"
.SH NAME
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

.SH SYNOPSIS
.nf
.B #include <pcl.h>
.sp
.BI "int co_thread_init(void);"
.sp
.BI "void co_thread_cleanup(void);"
.nl
.BI "coroutine_t co_create(void *" func ", void *" data ", void *" stack ", int " stacksize ");"
.nl
.BI "void co_delete(coroutine_t " co ");"
.nl
.BI "void co_call(coroutine_t " co ");"
.nl
.BI "void co_resume(void);"
.nl
.BI "void co_exit_to(coroutine_t " co ");"
.nl
.BI "void co_exit(void);"
.nl
.BI "coroutine_t co_current(void);"
.nl
.BI "void *co_get_data(coroutine_t " co ");"
.nl
.BI "void *co_set_data(coroutine_t " co ", void *" data ");"
.nl
.fi

Link with
.IR -lpthread
if you are using a multi-thread version of
.BR PCL .
.nl
.SH DESCRIPTION
The
.B Portable Coroutine Library (PCL)
implements the low level functionality for coroutines. For a definition
of the term
.B coroutine
see
.IR "The Art of Computer Programming" " by " "Donald E. Knuth" .
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.

.SS Functions
The following functions are defined:
.TP
.BI "int co_thread_init(void);"

If the
.B PCL
library is built in multi-thread mode, and if multi threads are actually
used, this function should be called before calling any
.B PCL
function.
If the
.B PCL
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
.BR co_thread_init ().
Returns 0 in case of success, or an negative error code in case of error.

.TP
.BI "void co_thread_cleanup(void);"
If the
.B PCL
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
functions anymore.
A failure in calling
.BR co_thread_cleanup ()
will result in resource leakage by the calling application.

.TP
.BI "coroutine_t co_create(void *" func ", void *" data ", void *" stack ", int " stacksize ");"

This function creates a new coroutine.
.I func
is the entry point of the coroutine.  It will be called with one
arg, a
.BR "void *" ,
which holds the data passed through the
.I data
parameter. If
.I func
terminates, the associated coroutine is deleted.
.I stack
is the base of the stack this coroutine will use and
.I stacksize
its size in bytes.  You may pass a
.B NULL
pointer for
.I stack
in which case the memory will be allocated by
.B co_create
itself.  Both,
.IR stack " and " stacksize
are aligned to system requirements.
A
.I stacksize
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
.RB ( "coroutine_t" )
for a new coroutine is returned, otherwise
.BR NULL .

.TP
.BI "void co_delete(coroutine_t " co ");"

This function deletes the given coroutine
.IR co .
If the stack for this coroutine was allocated by
.B co_create
it will be freed.  After a coroutine handle was passed to
.B co_delete
it is invalid and may not be used any more.
It is invalid for a coroutine to delete itself with this
function.

.TP
.BI "void co_call(coroutine_t " co ");"

This function passes execution to the given coroutine
.IR co .
The first time the coroutine is executed, its entry point
.I func
is called, and the
.I data
parameter used during the call to
.B co_create
is passed to
.IR func .
The current coroutine is suspended until another one restarts it with a
.B co_call
or
.B co_resume
call. Calling oneself returns immediately.

.TP
.BI "void co_resume(void);"

This function passes execution back to the coroutine which either
initially started this one or restarted it after a prior
.BR co_resume .

.TP
.BI "void co_exit_to(coroutine_t " co ");"

This function does the same a
.B co_delete(co_current())
followed by a
.B co_call
would do.  That is, it deletes itself and then passes execution
to another coroutine
.IR co .

.TP
.BI "void co_exit(void);"

This function does the same a
.B co_delete(co_current())
followed by a
.B co_resume
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
.BR co_resume .

.TP
.BI "coroutine_t co_current(void);"

This function returns the currently running coroutine.

.TP
.BI "void *co_get_data(coroutine_t " co ");"

This function returns the data associated with the
.I co
 coroutine. The data associated with a coroutine is the
.I data
parameter passed to
.BR co_create ().

.TP
.BI "void *co_set_data(coroutine_t " co ", void *" data ");"

Sets the
.I data
associated with the
.I co
coroutine, and returns the previously associated data.

.SS Notes
Some interactions with other parts of the system are covered here.
.TP
.B Threads
If the
.B PCL
library has been built in multi-thread mode, then it is possible to use
it in multi-thread software.
A thread should call
.BR co_thread_init ()
before using the
.B PCL
APIs, and call
.BR co_thread_cleanup ()
before exiting, or when it has done using the
.B PCL
APIs.
.br
.B WARNING:
For no reason should two different threads run the same coroutine at the
same time.

.TP
.B Signals
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.

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
.BR sigaltstack (2))
is recommended!

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)
.TP
.BR setjmp / longjmp
The use of
.BR setjmp "(2)/" longjmp (2)
is limited to jumping inside one coroutine.  Never try to jump from
one coroutine to another with
.BR longjmp (2).

.SH DIAGNOSTICS
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.
.TP
.B [PCL]: Cannot delete itself
A coroutine has called
.B co_delete
with it's own handle.
.TP
.B [PCL]: Resume to deleted coroutine
A coroutine has deleted itself with
.BR co_exit " or " co_exit_to
and the coroutine that was activated by the exit tried a
.BR co_resume .
.TP
.B [PCL]: Stale coroutine called
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.
.TP
.B [PCL]: Context switch failed
Low level error generated by the library in case a context switch
between two coroutines failes.

.SH SEE ALSO
Original
.B coroutine
library at
.BR http://www.goron.de/~froese/coro/coro.html " ."
GNU Pth library at
.BR http://www.gnu.org/software/pth/ " ."

.SH AUTHOR
Developed by Davide Libenzi <
.BR davidel@xmailserver.org " >."
.br
Ideas and man page base source taken by the coroutine library developed by
E. Toernig <
.BR froese@gmx.de " >."
.br
Also some code and ideas comes from the GNU Pth library available at
.BR http://www.gnu.org/software/pth/ " ."

.SH BUGS
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.

Bug reports and comments to Davide Libenzi <
.BR davidel@xmailserver.org " >."