diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-27 22:27:01 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-27 22:27:01 -0600 |
commit | 7ec97df3ee8edfd102fe573eaa61cf4e5c6284cb (patch) | |
tree | e696f30da5645cdd3cb09971d9544622e9943bcf /libcr | |
parent | fa357459f88bb8f0170d1a68df66e7d068d59996 (diff) |
wip fixes
Diffstat (limited to 'libcr')
-rw-r--r-- | libcr/coroutine.c | 15 | ||||
-rw-r--r-- | libcr/include/libcr/coroutine.h | 2 |
2 files changed, 10 insertions, 7 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); } diff --git a/libcr/include/libcr/coroutine.h b/libcr/include/libcr/coroutine.h index 5c2d608..47467bd 100644 --- a/libcr/include/libcr/coroutine.h +++ b/libcr/include/libcr/coroutine.h @@ -90,7 +90,7 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args); * Like coroutine_add_with_stack_size(), but uses a default stack size so * you don't need to think about it. */ -#define coroutine_add(fn, args) coroutine_add_with_stack_size(0, fn, args) +cid_t coroutine_add(cr_fn_t fn, void *args); /** * The main scheduler loop. |