PCL(3)
π PCL(3) π β Portable Coroutine Library
okay bestie, section 3, it's the Portable Coroutine Library, and no cap it is so much cuter than it sounds. coroutines!! cooperative multitasking!! let's go β¨
πΈ 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 β literally all the C coroutine management you could ever need π«Ά
β¨ SYNOPSIS β¨
#include <pcl.h>
int co_thread_init(void);
void co_thread_cleanup(void);
coroutine_t co_create(void *func, void *data, void *stack, int stacksize);
void co_delete(coroutine_t co);
void co_call(coroutine_t co);
void co_resume(void);
void co_exit_to(coroutine_t co);
void co_exit(void);
coroutine_t co_current(void);
void *co_get_data(coroutine_t co);
void *co_set_data(coroutine_t co, void *data);
Link with -lpthread if you are using a multi-thread version of PCL. π
π¦ DESCRIPTION π¦
So the Portable Coroutine Library (PCL, hi) implements the low level functionality for coroutines. and if you're like "bestie what even is a coroutine", the definition lives in The Art of Computer Programming by Donald E. Knuth. iconic reference, we love him π
Coroutines are a very simple cooperative multitasking environment where the switch from one task to another is done explicitly by a function call. no scheduler jumping in uninvited, no vibes-based preemption, you're the one calling the shots. and the way they switch a lot faster than processes or threads switch, since there is no OS kernel involvement for the operation?? main character energy fr fr π₯
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. that's someone else's problem, not ours π
π Functions π
The following functions are defined:
int co_thread_init(void);β If the PCL library is built in multi-thread mode, and if multi threads are actually used, this function should be called before calling any PCL function. If the 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 callco_thread_init(). Returns0in case of success, or an negative error code in case of error. (yes it says "an negative", we don't fix typos here, we respect the lore π)void co_thread_cleanup(void);β If the 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 PCL functions anymore. A failure in callingco_thread_cleanup()will result in resource leakage by the calling application. so like. please call it. don't leak. it's not a vibe πcoroutine_t co_create(void *func, void *data, void *stack, int stacksize);β This function creates a new coroutine, born fresh and ready to slay.funcis the entry point of the coroutine. It will be called with one arg, avoid *, which holds the data passed through thedataparameter. Iffuncterminates, the associated coroutine is deleted.stackis the base of the stack this coroutine will use andstacksizeits size in bytes. You may pass aNULLpointer forstackin which case the memory will be allocated byco_createitself. Both,stackandstacksizeare aligned to system requirements. Astacksizeof less then4096bytes 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 (coroutine_t) for a new coroutine is returned, otherwiseNULL. π§void co_delete(coroutine_t co);β This function deletes the given coroutineco. If the stack for this coroutine was allocated byco_createit will be freed. After a coroutine handle was passed toco_deleteit is invalid and may not be used any more. It is invalid for a coroutine to delete itself with this function. π« (no self-deletion, that's a whole thing, see DIAGNOSTICS below)void co_call(coroutine_t co);β This function passes execution to the given coroutineco. The first time the coroutine is executed, its entry pointfuncis called, and thedataparameter used during the call toco_createis passed tofunc. The current coroutine is suspended until another one restarts it with aco_callorco_resumecall. Calling oneself returns immediately. (so like, calling yourself is fine, it just does nothing. self-care πΈ)void co_resume(void);β This function passes execution back to the coroutine which either initially started this one or restarted it after a priorco_resume. it's the "okay going back to whoever woke me up" button π«Άvoid co_exit_to(coroutine_t co);β This function does the same aco_delete(co_current())followed by aco_callwould do. That is, it deletes itself and then passes execution to another coroutineco. bye bestie, off to someone else β¨void co_exit(void);β This function does the same aco_delete(co_current())followed by aco_resumewould 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 priorco_resume. π¦coroutine_t co_current(void);β This function returns the currently running coroutine. it's giving "who am i right now" πvoid *co_get_data(coroutine_t co);β This function returns the data associated with thecocoroutine. The data associated with a coroutine is thedataparameter passed toco_create().void *co_set_data(coroutine_t co, void *data);β Sets the data associated with thecocoroutine, and returns the previously associated data. (it hands you back the old one, how polite π)
π Notes π
Some interactions with other parts of the system are covered here. and ngl this is the part where you sit up straight.
π Threads
If the PCL library has been built in multi-thread mode, then it is possible to use it in multi-thread software. A thread should call co_thread_init() before using the PCL APIs, and call co_thread_cleanup() before exiting, or when it has done using the PCL APIs.
WARNING: For no reason should two different threads run the same coroutine at the same time. like. for NO reason. zero. none. don't do it π¨
π 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 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. π)
π setjmp/longjmp
The use of setjmp(2)/longjmp(2) is limited to jumping inside one coroutine. Never try to jump from one coroutine to another with longjmp(2). that's not a jump bestie that's a crime π
π₯ 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. dramatic? yes. on purpose? also yes.
[PCL]: Cannot delete itselfβ A coroutine has calledco_deletewith it's own handle.[PCL]: Resume to deleted coroutineβ A coroutine has deleted itself withco_exitorco_exit_toand the coroutine that was activated by the exit tried aco_resume.[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.[PCL]: Context switch failedβ Low level error generated by the library in case a context switch between two coroutines failes.
β SEE ALSO β
- Original coroutine library at http://www.goron.de/~froese/coro/coro.html .
- GNU Pth library at http://www.gnu.org/software/pth/ .
π AUTHOR π
Developed by Davide Libenzi < davidel@xmailserver.org >.
Ideas and man page base source taken by the coroutine library developed by E. Toernig < froese@gmx.de >.
Also some code and ideas comes from the GNU Pth library available at http://www.gnu.org/software/pth/ .
π 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. (no known bugs AND still in development?? that's the dream combo honestly β¨)
Bug reports and comments to Davide Libenzi < davidel@xmailserver.org >.
and that's the whole page bestie!! this was GNU, version 1.12, living in section PCL(3) π go forth and coroutine responsibly, mwah π«Άππ¦
π¦ where this page comes from
- debian bookworm: libpcl1-dev 1.12-2 Β· GPL-2.0-or-later
- ubuntu jammy: libpcl1-dev 1.12-2 Β· GPL-2.0-or-later
- Copyright (C) 2003 Davide Libenzi
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms