--- libcoro/coro.h 2008/10/30 09:44:31 1.28 +++ libcoro/coro.h 2008/11/16 00:55:41 1.39 @@ -56,6 +56,17 @@ * 2008-04-04 New (but highly unrecommended) pthreads backend. * 2008-04-24 Reinstate CORO_LOSER (had wrong stack adjustments). * 2008-10-30 Support assembly method on x86 with and without frame pointer. + * 2008-11-03 Use a global asm statement for CORO_ASM, idea by pippijn. + * 2008-11-05 Hopefully fix misaligned stacks with CORO_ASM/SETJMP. + * 2008-11-07 rbp wasn't saved in CORO_ASM on x86_64. + * introduce coro_destroy, which is a nop except for pthreads. + * speed up CORO_PTHREAD. Do no longer leak threads either. + * coro_create now allows one to create source coro_contexts. + * do not rely on makecontext passing a void * correctly. + * try harder to get _setjmp/_longjmp. + * major code cleanup/restructuring. + * 2008-11-10 the .cfi hacks are no longer needed. + * 2008-11-16 work around a freebsd pthread bug. */ #ifndef CORO_H @@ -107,11 +118,13 @@ * -DCORO_ASM * * Handcoded assembly, known to work only on a few architectures/ABI: - * ELF Linux x86 && amd64 when gcc is used and optimisation is turned on. + * GCC + x86/IA32 and amd64/x86_64 + GNU/Linux and a few BSDs. * * -DCORO_PTHREAD * * Use the pthread API. You have to provide and -lpthread. + * This is likely the slowest backend, and it also does not support fork(), + * so avoid it at all costs. * * If you define neither of these symbols, coro.h will try to autodetect * the model. This currently works for CORO_LOSER only. For the other @@ -136,8 +149,14 @@ * uninitialised coro_context, it expects a pointer to the entry function * and the single pointer value that is given to it as argument. * - * Allocating/deallocating the stack is your own responsibility, so there is - * no coro_destroy function. + * Allocating/deallocating the stack is your own responsibility. + * + * As a special case, if coro, arg, sptr and ssize are all zero, + * then an "empty" coro_context will be created that is suitable + * as an initial source for coro_transfer. + * + * This function is not reentrant, but putting a mutex around it + * will work. */ void coro_create (coro_context *ctx, /* an uninitialised coro_context */ coro_func coro, /* the coroutine code to be executed */ @@ -149,8 +168,24 @@ * The following prototype defines the coroutine switching function. It is * usually implemented as a macro, so watch out. * -void coro_transfer(coro_context *prev, coro_context *next); + * This function is thread-safe and reentrant. + */ +#if 0 +void coro_transfer (coro_context *prev, coro_context *next); +#endif + +/* + * The following prototype defines the coroutine destroy function. It is + * usually implemented as a macro, so watch out. It also serves + * no purpose unless you want to use the CORO_PTHREAD backend, + * where it is used to clean up the thread. You are responsible + * for freeing the stack and the context itself. + * + * This function is thread-safe and reentrant. */ +#if 0 +void coro_destroy (coro_context *ctx); +#endif /* * That was it. No other user-visible functions are implemented here. @@ -186,6 +221,7 @@ }; # define coro_transfer(p,n) swapcontext (&((p)->uc), &((n)->uc)) +# define coro_destroy(ctx) (void *)(ctx) #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX @@ -193,38 +229,60 @@ # define _GNU_SOURCE /* for linux libc */ # endif +# if !CORO_LOSER +# include +# endif + +/* solaris is hopelessly borked, it expands _XOPEN_UNIX to nothing */ +# if __sun +# undef _XOPEN_UNIX +# define _XOPEN_UNIX 1 +# endif + # include struct coro_context { +#if _XOPEN_UNIX > 0 || CORO_LOSER jmp_buf env; +#else + sigjmp_buf env; +#endif }; -# if CORO_LINUX || (_XOPEN_SOURCE >= 600) -# define coro_transfer(p,n) do { if (!_setjmp ((p)->env)) _longjmp ((n)->env, 1); } while (0) +# if _XOPEN_UNIX > 0 +# define coro_transfer(p,n) do { if (! _setjmp ((p)->env )) _longjmp ((n)->env, 1); } while (0) +# elif CORO_LOSER +# define coro_transfer(p,n) do { if (! setjmp ((p)->env )) longjmp ((n)->env, 1); } while (0) # else -# define coro_transfer(p,n) do { if (!setjmp ((p)->env)) longjmp ((n)->env, 1); } while (0) +# define coro_transfer(p,n) do { if (!sigsetjmp ((p)->env, 0)) siglongjmp ((n)->env, 1); } while (0) # endif +# define coro_destroy(ctx) (void *)(ctx) + #elif CORO_ASM struct coro_context { - volatile void **sp; + void **sp; /* must be at offset 0 */ }; void __attribute__ ((__noinline__, __regparm__(2))) - coro_transfer (coro_context *prev, coro_context *next); +coro_transfer (coro_context *prev, coro_context *next); + +# define coro_destroy(ctx) (void *)(ctx) #elif CORO_PTHREAD -#include +# include extern pthread_mutex_t coro_mutex; struct coro_context { - pthread_cond_t c; + pthread_cond_t cv; + pthread_t id; }; void coro_transfer (coro_context *prev, coro_context *next); +void coro_destroy (coro_context *ctx); #endif