#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.
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 call co_thread_init(). Returns 0 in case of success, or an negative error code in case of error.
This function creates a new coroutine. func is the entry point of the coroutine. It will be called with one arg, a void *, which holds the data passed through the data parameter. If func terminates, the associated coroutine is deleted. stack is the base of the stack this coroutine will use and stacksize its size in bytes. You may pass a NULL pointer for stack in which case the memory will be allocated by co_create itself. Both, stack and stacksize are aligned to system requirements. A 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 (coroutine_t) for a new coroutine is returned, otherwise NULL.
This function deletes the given coroutine co. If the stack for this coroutine was allocated by co_create it will be freed. After a coroutine handle was passed to co_delete it is invalid and may not be used any more. It is invalid for a coroutine to delete itself with this function.
This function passes execution to the given coroutine co. The first time the coroutine is executed, its entry point func is called, and the data parameter used during the call to co_create is passed to func. The current coroutine is suspended until another one restarts it with a co_call or co_resume call. Calling oneself returns immediately.
This function passes execution back to the coroutine which either initially started this one or restarted it after a prior co_resume.
This function does the same a co_delete(co_current()) followed by a co_call would do. That is, it deletes itself and then passes execution to another coroutine co.
This function does the same a co_delete(co_current()) followed by a 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 co_resume.
This function returns the currently running coroutine.
This function returns the data associated with the
co
coroutine. The data associated with a coroutine is the
data
parameter passed to
co_create().
Sets the data associated with the co coroutine, and returns the previously associated data.
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)
Bug reports and comments to Davide Libenzi < [email protected] >.