diff options
Diffstat (limited to 'libcr/coroutine.c')
-rw-r--r-- | libcr/coroutine.c | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c index 0920f23..fd44acc 100644 --- a/libcr/coroutine.c +++ b/libcr/coroutine.c @@ -214,7 +214,7 @@ static void call_with_stack(void *stack, cr_fn_t fn, void *args) { static const uint8_t stack_pattern[] = {0x1e, 0x15, 0x16, 0x0a, 0xcc, 0x52, 0x7e, 0xb7}; #endif -static void inline assert_cid(cid_t cid) { +static inline void assert_cid(cid_t cid) { assert(cid > 0); assert(cid <= CONFIG_COROUTINE_NUM); #if CONFIG_COROUTINE_PROTECT_STACK @@ -233,13 +233,14 @@ static void inline assert_cid(cid_t cid) { assert(coroutine_table[(cid)-1].state opstate); \ } while (0) +cid_t coroutine_add(cr_fn_t fn, void *args) { + return coroutine_add_with_stack_size(CONFIG_COROUTINE_DEFAULT_STACK_SIZE, fn, args); +} + cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args) { static cid_t last_created = 0; cid_t parent = coroutine_running; - if (!stack_size) - stack_size = CONFIG_COROUTINE_DEFAULT_STACK_SIZE; - if (parent) assert_cid_state(parent, == CR_RUNNING); assert(stack_size); @@ -295,19 +296,19 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args) { void coroutine_main(void) { debugf("coroutine_main()\n"); - bool ran; + bool ran = false; for (coroutine_running = 1;; coroutine_running = (coroutine_running%CONFIG_COROUTINE_NUM)+1) { if (coroutine_running == 1) ran = false; struct coroutine *cr = &coroutine_table[coroutine_running-1]; if (cr->state == CR_RUNNABLE) { debugf("running cid=%zu...\n", coroutine_running); - ran = true; cr->state = CR_RUNNING; if (!setjmp(coroutine_main_env)) { /* point=b */ longjmp(cr->env, 1); /* jump to point=c */ assert(false); /* should cr_exit() instead of returning */ } + ran = true; assert_cid_state(coroutine_running, != CR_RUNNING); if (cr->state == CR_NONE) { #if CONFIG_COROUTINE_MEASURE_STACK @@ -386,8 +387,10 @@ void cr_unpause_from_sighandler(cid_t cid) { switch (coroutine_table[cid-1].state) { case CR_RUNNING: coroutine_table[cid-1].sig_unpause = true; + break; case CR_PAUSED: coroutine_table[cid-1].state = CR_RUNNABLE; + break; default: assert(false); } |