summaryrefslogtreecommitdiff
path: root/libcr/coroutine.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcr/coroutine.c')
-rw-r--r--libcr/coroutine.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c
index 7278225..129f4da 100644
--- a/libcr/coroutine.c
+++ b/libcr/coroutine.c
@@ -46,6 +46,10 @@
#error config.h must define CONFIG_COROUTINE_GDB (bool)
#endif
+/* Enforce that CONFIG_COROUTINE_NUM is greater than 1, to work around
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118212 */
+static_assert(CONFIG_COROUTINE_NUM > 1);
+
/* Implementation *************************************************************/
#if CONFIG_COROUTINE_VALGRIND
@@ -386,7 +390,7 @@ struct coroutine {
#if CONFIG_COROUTINE_VALGRIND
unsigned stack_id;
#endif
- char name[CONFIG_COROUTINE_NAME_LEN];
+ [[gnu::nonstring]] char name[CONFIG_COROUTINE_NAME_LEN];
};
/* constants ******************************************************************/
@@ -522,10 +526,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 +558,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
@@ -753,6 +761,7 @@ cid_t cr_getcid(void) {
return coroutine_running;
}
+#ifndef NDEBUG
void cr_assert_in_coroutine(void) {
assert(!cr_plat_is_in_intrhandler());
assert_cid_state(coroutine_running, state == CR_RUNNING);
@@ -761,6 +770,7 @@ void cr_assert_in_coroutine(void) {
void cr_assert_in_intrhandler(void) {
assert(cr_plat_is_in_intrhandler());
}
+#endif
/* cr_cid_info() **************************************************************/