diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-29 10:56:18 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-29 10:56:18 -0600 |
commit | 7b34cb7741c683dc623ece652032f1bf09d34140 (patch) | |
tree | e131e4973f47bcfc93131c9eed7c78d4c614a4be /libcr | |
parent | f54d6bfcc488b644926b962c230f8f12d0d65646 (diff) |
wip fixes
Diffstat (limited to 'libcr')
-rw-r--r-- | libcr/coroutine.c | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c index d221ca7..79ba894 100644 --- a/libcr/coroutine.c +++ b/libcr/coroutine.c @@ -126,7 +126,7 @@ enum coroutine_state { CR_INITIALIZING, /* running, before cr_begin() */ CR_RUNNING, /* running, after cr_begin() */ CR_RUNNABLE, /* not running, but runnable */ - CR_PAUSED, /* not running, and not runnable */ + CR_PAUSED, /* not running, and not runnable */ }; struct coroutine { @@ -139,6 +139,14 @@ struct coroutine { /* constants ******************************************************************/ +const char *coroutine_state_strs[] = { + [CR_NONE] = "CR_NONE", + [CR_INITIALIZING] = "CR_INITIALIZING", + [CR_RUNNING] = "CR_RUNNING", + [CR_RUNNABLE] = "CR_RUNNABLE", + [CR_PAUSED] = "CR_PAUSED", +}; + #define STACK_ALIGNMENT ({ __attribute__((aligned)) void fn(void) {}; __alignof__(fn); }) #if CONFIG_COROUTINE_MEASURE_STACK || CONFIG_COROUTINE_PROTECT_STACK @@ -174,12 +182,36 @@ static cid_t coroutine_running = 0; /* utility functions **********************************************************/ +#define errorf(...) fprintf(stderr, "error: " __VA_ARGS__) +#define infof(...) printf("info: " __VA_ARGS__) #if CONFIG_COROUTINE_DEBUG # define debugf(...) printf("dbg: " __VA_ARGS__) #else # define debugf(...) #endif +#ifdef __GLIBC__ +# define assertf(expr, ...) \ + ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \ + if (expr) \ + ; /* empty */ \ + else { \ + errorf("assertion: " __VA_ARGS__); \ + __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \ + } \ + })) +#else +# define assertf(expr, ...) assert(expr) +#endif + +/** Return `n` rounded up to the nearest multiple of `d` */ +#define round_up(n, d) ( ( ((n)+(d)-1) / (d) ) * (d) ) + +static inline const char* coroutine_state_str(enum coroutine_state state) { + assert(state < (sizeof(coroutine_state_strs)/sizeof(coroutine_state_strs[0]))); + return coroutine_state_strs[state]; +} + static inline void assert_cid(cid_t cid) { assert(cid > 0); assert(cid <= CONFIG_COROUTINE_NUM); @@ -199,10 +231,6 @@ static inline void assert_cid(cid_t cid) { assert(coroutine_table[(cid)-1].state opstate); \ } while (0) - -/* Return `n` rounded up to the nearest multiple of `d` */ -#define round_up(n, d) ( ( ((n)+(d)-1) / (d) ) * (d) ) - /* call_with_stack() **********************************************************/ static void call_with_stack(void *stack, cr_fn_t fn, void *args) { @@ -351,7 +379,7 @@ void coroutine_main(void) { for (;;) { cid_t next = next_coroutine(); if (!next) { - fprintf(stderr, "error: no coroutines\n"); + errorf("no coroutines\n"); return; } if (!setjmp(coroutine_main_env)) { /* point=b */ @@ -363,7 +391,7 @@ void coroutine_main(void) { #if CONFIG_COROUTINE_MEASURE_STACK struct stack_stats sizes; measure_stack(coroutine_running, &sizes); - printf("info: cid=%zu: exited having used %zu B stack space\n", coroutine_running, sizes.max); + infof("cid=%zu: exited having used %zu B stack space\n", coroutine_running, sizes.max); #endif free(coroutine_table[coroutine_running-1].stack); coroutine_table[coroutine_running-1] = (struct coroutine){0}; @@ -381,7 +409,10 @@ void cr_begin(void) { } static inline void _cr_transition(enum coroutine_state state) { - debugf("cid=%zu: transition %i->%i\n", coroutine_running, coroutine_table[coroutine_running-1].state, state); + debugf("cid=%zu: transition %s->%s\n", + coroutine_running, + coroutine_state_str(coroutine_table[coroutine_running-1].state), + coroutine_state_str(state)); coroutine_table[coroutine_running-1].state = state; @@ -438,6 +469,7 @@ void cr_unpause(cid_t cid) { void cr_unpause_from_sighandler(cid_t cid) { assert_cid(cid); + debugf("cr_unpause_from_sighandler(%zu)\n", cid); switch (coroutine_table[cid-1].state) { case CR_RUNNING: @@ -447,7 +479,8 @@ void cr_unpause_from_sighandler(cid_t cid) { coroutine_table[cid-1].state = CR_RUNNABLE; break; default: - assert(false); + assertf(false, "cid=%zu state=%s\n", + cid, coroutine_state_str(coroutine_table[cid-1].state)); } } |