diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-25 08:39:44 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-25 08:39:44 -0700 |
commit | 92de40d33e085040e4b9936ae1ddebf152dff102 (patch) | |
tree | 7f1156acb9a3af4e720319ba17120176cdf28a2e /libcr/coroutine.c | |
parent | 839c3c83993b2b8d9d6be20407afdf59be23e701 (diff) |
libcr: Return if all coroutines exit
Diffstat (limited to 'libcr/coroutine.c')
-rw-r--r-- | libcr/coroutine.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c index 76d21f3..18b2b80 100644 --- a/libcr/coroutine.c +++ b/libcr/coroutine.c @@ -434,6 +434,7 @@ static struct { cid_t buf[NEXT_POWER_OF_2(CONFIG_COROUTINE_NUM)]; } coroutine_ringbuf = {0}; static cid_t coroutine_running = 0; +static size_t coroutine_cnt = 0; /* utility functions **********************************************************/ @@ -527,6 +528,7 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, coroutine_running = child; coroutine_table[child-1].state = CR_INITIALIZING; + coroutine_cnt++; if (!cr_plat_setjmp(&coroutine_add_env)) { /* point=a */ void *stack_base = coroutine_table[child-1].stack #if CR_PLAT_STACK_GROWS_DOWNWARD @@ -562,13 +564,13 @@ cid_t coroutine_add(const char *name, cr_fn_t fn, void *args) { /* coroutine_main() ***********************************************************/ -[[noreturn]] void coroutine_main(void) { +void coroutine_main(void) { debugf("coroutine_main()"); bool saved = cr_save_and_disable_interrupts(); assert(saved); assert(!cr_is_in_intrhandler()); coroutine_running = 0; - for (;;) { + while (coroutine_cnt) { cid_t next; while ( !((next = coroutine_ringbuf_pop())) ) { /* No coroutines are runnable, wait for an interrupt @@ -589,7 +591,9 @@ cid_t coroutine_add(const char *name, cr_fn_t fn, void *args) { #endif free(coroutine_table[coroutine_running-1].stack); coroutine_table[coroutine_running-1] = (struct coroutine){0}; + coroutine_cnt--; } + cr_restore_interrupts(saved); } /* cr_*() *********************************************************************/ |