summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/stc/algo/coroutine.h6
-rw-r--r--misc/examples/coread.c5
2 files changed, 8 insertions, 3 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h
index c3f36ac2..78dc80c6 100644
--- a/include/stc/algo/coroutine.h
+++ b/include/stc/algo/coroutine.h
@@ -76,9 +76,13 @@ enum {
*_state = cco_state_done; \
return ret
+#define cco(co) \
+ for (int *_state = &(co)->cco_state, _once=1; _once; *_state = cco_state_done, _once=0) \
+ _begin: switch (*_state) case 0:
+
#define cco_yield(ret) \
do { \
- *_state = __LINE__; return ret; \
+ *_state = __LINE__; return ret; goto _begin; \
case __LINE__:; \
} while (0)
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index e60fb31c..1976231f 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -13,7 +13,7 @@ struct file_read {
void file_read(struct file_read* g)
{
- cco_begin(g)
+ cco(g) {
g->fp = fopen(g->filename, "r");
g->line = cstr_init();
@@ -23,7 +23,8 @@ void file_read(struct file_read* g)
printf("finish\n");
cstr_drop(&g->line);
fclose(g->fp);
- cco_end();
+ }
+ return;
}
int main(void)