diff options
| author | Tyge Løvset <[email protected]> | 2023-05-03 16:45:37 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-05-03 16:45:37 +0200 |
| commit | 6b23e35287f26dad63abd755c5f365b443e025a3 (patch) | |
| tree | c13c80d539efa1edbf7bc7d829f0e3d53e07e3b5 | |
| parent | e4efe2f9cc87e70e981ee75ec5c4d6db4cb60c49 (diff) | |
| download | STC-modified-6b23e35287f26dad63abd755c5f365b443e025a3.tar.gz STC-modified-6b23e35287f26dad63abd755c5f365b443e025a3.zip | |
Reverted from cco_await_with(promise, ret) to cco_await(promise, ret).
| -rw-r--r-- | include/stc/algo/coroutine.h | 15 | ||||
| -rw-r--r-- | misc/examples/coread.c | 14 | ||||
| -rw-r--r-- | misc/examples/coroutines.c | 26 |
3 files changed, 30 insertions, 25 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index ae9e4464..89dd27f0 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -57,11 +57,12 @@ int main(void) { */ #include <stc/ccommon.h> -enum cco_states { +enum { cco_state_final = -1, cco_state_done = -2, }; +#define cco_initial(ctx) ((ctx)->cco_state == 0) #define cco_suspended(ctx) ((ctx)->cco_state > 0) #define cco_done(ctx) ((ctx)->cco_state == cco_state_done) @@ -82,8 +83,9 @@ enum cco_states { case __LINE__:; \ } while (0) -#define cco_await(promise) cco_await_with(promise, ) -#define cco_await_with(promise, retval) \ +#define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) +#define cco_await_1(promise) cco_await_2(promise, ) +#define cco_await_2(promise, retval) \ do { \ *_state = __LINE__; \ case __LINE__: if (!(promise)) return retval; \ @@ -119,10 +121,11 @@ typedef struct { * This macro carries out the "wait" operation on the semaphore, * and causes the "thread" to block while the counter is zero. */ -#define cco_await_sem(sem) cco_await_sem_with(sem, ) -#define cco_await_sem_with(sem, retval) \ +#define cco_await_sem(...) c_MACRO_OVERLOAD(cco_await_sem, __VA_ARGS__) +#define cco_await_sem_1(sem) cco_await_sem_2(sem, ) +#define cco_await_sem_2(sem, retval) \ do { \ - cco_await_with((sem)->count > 0, retval); \ + cco_await_2((sem)->count > 0, retval); \ --(sem)->count; \ } while (0) diff --git a/misc/examples/coread.c b/misc/examples/coread.c index d5385a87..38447c44 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -11,19 +11,19 @@ struct file_read { cstr line; }; -bool file_read(struct file_read* U) +bool file_read(struct file_read* g) { - cco_begin(U) - U->fp = fopen(U->filename, "r"); - U->line = cstr_init(); + cco_begin(g) + g->fp = fopen(g->filename, "r"); + g->line = cstr_init(); - while (cstr_getline(&U->line, U->fp)) + while (cstr_getline(&g->line, g->fp)) cco_yield(false); cco_final: // this label is required. printf("finish\n"); - cstr_drop(&U->line); - fclose(U->fp); + cstr_drop(&g->line); + fclose(g->fp); cco_end(true); } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 00cedd84..a5db3291 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -36,7 +36,7 @@ bool prime(struct prime* g) { } } cco_final: - printf("final prm\n"); + printf("final prm\n"); cco_end(true); } @@ -60,14 +60,15 @@ bool fibonacci(struct fibonacci* g) { if (g->count-- == 0) cco_return; if (++g->idx > 1) { - llong sum = g->result + g->b; // NB! locals lasts only until next cco_yield/cco_await! + // NB! locals lasts only until next cco_yield/cco_await! + llong sum = g->result + g->b; g->result = g->b; g->b = sum; } cco_yield(false); } cco_final: - printf("final fib\n"); + printf("final fib\n"); cco_end(true); } @@ -80,17 +81,18 @@ struct combined { }; -bool combined(struct combined* C) { - cco_begin(C); - cco_await_with(prime(&C->prm), false); - cco_await_with(fibonacci(&C->fib), false); +bool combined(struct combined* g) { + cco_begin(g); + cco_await(prime(&g->prm), false); + cco_await(fibonacci(&g->fib), false); - // Reuse the C->prm context and extend the count: - C->prm.count = 8, C->prm.result += 2; - cco_reset(&C->prm); - cco_await_with(prime(&C->prm), false); + // Reuse the g->prm context and extend the count: + g->prm.count = 8, g->prm.result += 2; + cco_reset(&g->prm); + cco_await(prime(&g->prm), false); - cco_final: puts("final comb"); + cco_final: + puts("final combined"); cco_end(true); } |
