diff options
author | root <root> | 2008-04-04 20:07:35 +0000 |
---|---|---|
committer | root <root> | 2008-04-04 20:07:35 +0000 |
commit | e8a65d610b04d7edd7299dd578111231efbd095d (patch) | |
tree | 5169843c2e5055ed042e15470e2de939cc696643 | |
parent | e66e1f7c70a5ec4f6ff0aa04fb066c460b66be5a (diff) |
*** empty log message ***rel-4_49
-rw-r--r-- | coro.c | 67 | ||||
-rw-r--r-- | coro.h | 22 |
2 files changed, 85 insertions, 4 deletions
@@ -161,6 +161,42 @@ coro_transfer (struct coro_context *prev, struct coro_context *next) } #endif +#if CORO_PTHREAD + +struct coro_init_args { + coro_func func; + void *arg; + coro_context *self, *main; +}; + +pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void * +trampoline (void *args_) +{ + struct coro_init_args *args = (struct coro_init_args *)args_; + coro_func func = args->func; + void *arg = args->arg; + + pthread_mutex_lock (&coro_mutex); + pthread_cond_destroy (&args->self->c); + coro_transfer (args->self, args->main); + func (arg); + pthread_mutex_unlock (&coro_mutex); + + return 0; +} + +void coro_transfer(coro_context *prev, coro_context *next) +{ + pthread_cond_init (&prev->c, 0); + pthread_cond_signal (&next->c); + pthread_cond_wait (&prev->c, &coro_mutex); + pthread_cond_destroy (&prev->c); +} + +#endif + /* initialize a machine state */ void coro_create (coro_context *ctx, coro_func coro, void *arg, @@ -259,7 +295,7 @@ void coro_create (coro_context *ctx, ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize); #else -#error "microsoft libc or architecture not supported" +# error "microsoft libc or architecture not supported" #endif # elif CORO_LINUX @@ -298,8 +334,35 @@ void coro_create (coro_context *ctx, coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); +# elif CORO_PTHREAD + + pthread_t id; + pthread_attr_t attr; + coro_context nctx; + struct coro_init_args args; + static int once; + + if (!once) + { + pthread_mutex_lock (&coro_mutex); + once = 1; + } + + args.func = coro; + args.arg = arg; + args.self = ctx; + args.main = &nctx; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstack (&attr, sptr, (size_t)ssize); + pthread_create (&id, &attr, trampoline, &args); + + pthread_cond_init (&args.self->c, 0); + coro_transfer (args.main, args.self); + #else -# error unsupported architecture +# error unsupported backend #endif } @@ -53,6 +53,7 @@ * on SIGUSR2 and sigaltstack in Crossfire). * 2008-01-21 Disable CFI usage on anything but GNU/Linux. * 2008-03-02 Switched to 2-clause BSD license with GPL exception. + * 2008-04-04 New (but highly unrecommended) pthreads backend. */ #ifndef CORO_H @@ -106,6 +107,10 @@ * Handcoded assembly, known to work only on a few architectures/ABI: * ELF Linux x86 && amd64 when gcc is used and optimisation is turned on. * + * -DCORO_PTHREAD + * + * Use the pthread API. You have to provide <pthread.h> and -lpthread. + * * 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 * alternatives you should check (e.g. using autoconf) and define the @@ -153,7 +158,8 @@ void coro_transfer(coro_context *prev, coro_context *next); #if !defined(CORO_LOSER) && !defined(CORO_UCONTEXT) \ && !defined(CORO_SJLJ) && !defined(CORO_LINUX) \ - && !defined(CORO_IRIX) && !defined(CORO_ASM) + && !defined(CORO_IRIX) && !defined(CORO_ASM) \ + && !defined(CORO_PTHREAD) # if defined(WINDOWS) # define CORO_LOSER 1 /* you don't win with windoze */ # elif defined(__linux) && (defined(__x86) || defined (__amd64)) @@ -204,7 +210,19 @@ struct coro_context { }; void __attribute__ ((__noinline__, __fastcall__)) - coro_transfer(coro_context *prev, coro_context *next); + coro_transfer (coro_context *prev, coro_context *next); + +#elif CORO_PTHREAD + +#include <pthread.h> + +extern pthread_mutex_t coro_mutex; + +struct coro_context { + pthread_cond_t c; +}; + +void coro_transfer (coro_context *prev, coro_context *next); #endif |