diff options
-rw-r--r-- | libcr/coroutine.c | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c index 7278225..05892c3 100644 --- a/libcr/coroutine.c +++ b/libcr/coroutine.c @@ -522,10 +522,24 @@ static inline void assert_cid(cid_t cid) { /* coroutine_add() ************************************************************/ +LM_NEVER_INLINE +cid_t coroutine_allocate_cid(void) { + static cid_t last_created = 0; + + size_t base = last_created; + for (size_t shift = 0; shift < CONFIG_COROUTINE_NUM; shift++) { + cid_t child = ((base + shift) % CONFIG_COROUTINE_NUM) + 1; + if (coroutine_table[child-1].state == CR_NONE) { + last_created = child; + return child; + } + } + return 0; +} + cid_t coroutine_add_with_stack_size(size_t stack_size, const char *name, cr_fn_t fn, void *args) { - static cid_t last_created = 0; cid_t parent = coroutine_running; if (parent) @@ -540,21 +554,11 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, coroutine_initialized = true; } - cid_t child; - { - size_t base = last_created; - for (size_t shift = 0; shift < CONFIG_COROUTINE_NUM; shift++) { - child = ((base + shift) % CONFIG_COROUTINE_NUM) + 1; - if (coroutine_table[child-1].state == CR_NONE) - goto found; - } + cid_t child = coroutine_allocate_cid(); + if (!child) return 0; - found: - } debugf("...child=%zu", child); - last_created = child; - if (name) strncpy(coroutine_table[child-1].name, name, sizeof(coroutine_table[child-1].name)); else |