1PCL(3)                    Portable Coroutine Library                    PCL(3)
2
3
4

NAME

6       co_thread_init, co_thread_cleanup, co_create, co_call, co_resume,
7       co_delete, co_exit_to, co_exit, co_current, co_get_data, co_set_data -
8       C coroutine management
9
10

SYNOPSIS

12       #include <pcl.h>
13
14       int co_thread_init(void);
15
16       void co_thread_cleanup(void);
17       coroutine_t co_create(void *func, void *data, void *stack, int stacksize);
18       void co_delete(coroutine_t co);
19       void co_call(coroutine_t co);
20       void co_resume(void);
21       void co_exit_to(coroutine_t co);
22       void co_exit(void);
23       coroutine_t co_current(void);
24       void *co_get_data(coroutine_t co);
25       void *co_set_data(coroutine_t co, void *data);
26
27       Link with -lpthread if you are using a multi-thread version of PCL.
28

DESCRIPTION

30       The Portable Coroutine Library (PCL) implements the low level function‐
31       ality for coroutines. For a definition of the term coroutine see The
32       Art of Computer Programming by Donald E. Knuth.  Coroutines are a very
33       simple cooperative multitasking environment where the switch from one
34       task to another is done explicitly by a function call.  Coroutines are
35       a lot faster than processes or threads switch, since there is no OS
36       kernel involvement for the operation. This document defines an API for
37       the low level handling of coroutines i.e. creating and deleting corou‐
38       tines and switching between them.  Higher level functionality (sched‐
39       uler, etc.) is not covered.
40
41
42   Functions
43       The following functions are defined:
44
45       int co_thread_init(void);
46
47              If the PCL library is built in multi-thread mode, and if multi
48              threads are actually used, this function should be called before
49              calling any PCL function.  If the PCL library is built in multi-
50              thread mode, but it is used only from one thread (the main one,
51              likely), then it is possible to avoid to call co_thread_init().
52              Returns 0 in case of success, or an negative error code in case
53              of error.
54
55
56       void co_thread_cleanup(void);
57              If the PCL library is built in multi-thread mode, and if multi
58              threads are actually used, this function should be called before
59              the thread exits, or whenever the thread decides it won't call
60              the PCL functions anymore.  A failure in calling
61              co_thread_cleanup() will result in resource leakage by the call‐
62              ing application.
63
64
65       coroutine_t co_create(void *func, void *data, void *stack, int stack‐
66       size);
67
68              This function creates a new coroutine.  func is the entry point
69              of the coroutine.  It will be called with one arg, a void *,
70              which holds the data passed through the data parameter. If func
71              terminates, the associated coroutine is deleted.  stack is the
72              base of the stack this coroutine will use and stacksize its size
73              in bytes.  You may pass a NULL pointer for stack in which case
74              the memory will be allocated by co_create itself.  Both, stack
75              and stacksize are aligned to system requirements.  A stacksize
76              of less then 4096 bytes will be rejected.  You have to make
77              sure, that the stack is large enough for your coroutine and pos‐
78              sible signal handlers (see below).  The stack will not grow!
79              (Exception: the main coroutine uses the standard system stack
80              which may still grow) On success, a handle (coroutine_t) for a
81              new coroutine is returned, otherwise NULL.
82
83
84       void co_delete(coroutine_t co);
85
86              This function deletes the given coroutine co.  If the stack for
87              this coroutine was allocated by co_create it will be freed.
88              After a coroutine handle was passed to co_delete it is invalid
89              and may not be used any more.  It is invalid for a coroutine to
90              delete itself with this function.
91
92
93       void co_call(coroutine_t co);
94
95              This function passes execution to the given coroutine co.  The
96              first time the coroutine is executed, its entry point func is
97              called, and the data parameter used during the call to co_create
98              is passed to func.  The current coroutine is suspended until
99              another one restarts it with a co_call or co_resume call. Call‐
100              ing oneself returns immediately.
101
102
103       void co_resume(void);
104
105              This function passes execution back to the coroutine which
106              either initially started this one or restarted it after a prior
107              co_resume.
108
109
110       void co_exit_to(coroutine_t co);
111
112              This function does the same a co_delete(co_current()) followed
113              by a co_call would do.  That is, it deletes itself and then
114              passes execution to another coroutine co.
115
116
117       void co_exit(void);
118
119              This function does the same a co_delete(co_current()) followed
120              by a co_resume would do.  That is, it deletes itself and then
121              passes execution back to the coroutine which either initially
122              started this one or restarted it after a prior co_resume.
123
124
125       coroutine_t co_current(void);
126
127              This function returns the currently running coroutine.
128
129
130       void *co_get_data(coroutine_t co);
131
132              This function returns the data associated with the co
133               coroutine. The data associated with a coroutine is the data
134              parameter passed to co_create().
135
136
137       void *co_set_data(coroutine_t co, void *data);
138
139              Sets the data associated with the co coroutine, and returns the
140              previously associated data.
141
142
143   Notes
144       Some interactions with other parts of the system are covered here.
145
146       Threads
147              If the PCL library has been built in multi-thread mode, then it
148              is possible to use it in multi-thread software.  A thread should
149              call co_thread_init() before using the PCL APIs, and call
150              co_thread_cleanup() before exiting, or when it has done using
151              the PCL APIs.
152              WARNING: For no reason should two different threads run the same
153              coroutine at the same time.
154
155
156       Signals
157              First, a signal handler is not defined to run in any specific
158              coroutine. The only way to leave the signal handler is by a
159              return statement.
160
161              Second, the signal handler may run with the stack of any corou‐
162              tine, even with the stack of library internal coroutines which
163              have an undefined stack size (just enough to perform a kernel
164              call).  Using and alternate stack for signal processing (see
165              sigaltstack(2)) is recommended!
166
167              Conclusion: avoid signals like a plague.  The only thing you may
168              do reliable is setting some global variables and return.  Simple
169              kernel calls may work too, but nowadays it's pretty hairy to
170              tell, which function really is a kernel call.  (Btw, all this
171              applies to normal C programs, too.  The coroutines just add one
172              more problem)
173
174       setjmp/longjmp
175              The use of setjmp(2)/longjmp(2) is limited to jumping inside one
176              coroutine.  Never try to jump from one coroutine to another with
177              longjmp(2).
178
179

DIAGNOSTICS

181       Some fatal errors are caught by the library.  If one occurs, a short
182       message is written to file descriptor 2 (stderr) and a segmentation
183       violation is generated.
184
185       [PCL]: Cannot delete itself
186              A coroutine has called co_delete with it's own handle.
187
188       [PCL]: Resume to deleted coroutine
189              A coroutine has deleted itself with co_exit or co_exit_to and
190              the coroutine that was activated by the exit tried a co_resume.
191
192       [PCL]: Stale coroutine called
193              Someone tried to active a coroutine that has already been
194              deleted.  This error is only detected, if the stack of the
195              deleted coroutine is still resident in memory.
196
197       [PCL]: Context switch failed
198              Low level error generated by the library in case a context
199              switch between two coroutines failes.
200
201

SEE ALSO

203       Original coroutine library at
204       http://www.goron.de/~froese/coro/coro.html .  GNU Pth library at
205       http://www.gnu.org/software/pth/ .
206
207

AUTHOR

209       Developed by Davide Libenzi < davidel@xmailserver.org >.
210       Ideas and man page base source taken by the coroutine library developed
211       by E. Toernig < froese@gmx.de >.
212       Also some code and ideas comes from the GNU Pth library available at
213       http://www.gnu.org/software/pth/ .
214
215

BUGS

217       There are no known bugs.  But, this library is still in development
218       even if it results very stable and pretty much ready for production
219       use.
220
221       Bug reports and comments to Davide Libenzi < davidel@xmailserver.org >.
222
223
224
225
226GNU                                  1.12                               PCL(3)
Impressum