From ab7a91c501fb3b7054e836a931754caae578c5f2 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 May 2023 13:09:16 +0200 Subject: Removed cco_alive(), was same as !cco_done() --- docs/ccommon_api.md | 3 +-- include/stc/algo/coroutine.h | 1 - misc/examples/cointerleave.c | 6 +++--- misc/examples/coroutines.c | 46 ++++++++++++++++++++++---------------------- misc/examples/scheduler.c | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 27df13ce..407ddac4 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -354,9 +354,8 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Obligatory label in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | -| `bool` | `cco_alive(ctx)` | Is coroutine in initial or suspended state? | -| `bool` | `cco_done(ctx)` | Is coroutine not alive? | | `bool` | `cco_suspended(ctx)` | Is coroutine in suspended state? | +| `bool` | `cco_done(ctx)` | Is coroutine done? | | | `cco_begin(ctx)` | Begin coroutine block | | | `cco_end(retval)` | End coroutine block and return retval | | | `cco_end()` | End coroutine block (return void) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 4b7bee5f..f6769162 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -63,7 +63,6 @@ enum cco_states { }; #define cco_suspended(ctx) ((ctx)->cco_state > 0) -#define cco_alive(ctx) ((ctx)->cco_state != cco_state_done) #define cco_done(ctx) ((ctx)->cco_state == cco_state_done) #define cco_begin(ctx) \ diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 64473d96..9ef7d561 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -29,13 +29,13 @@ struct Generator { bool interleaved(struct Generator* g) { cco_begin(g); - while (cco_alive(&g->x) || cco_alive(&g->y)) + while (!cco_done(&g->x) || !cco_done(&g->y)) { g->value = next_value(&g->x); - if (cco_alive(&g->x)) cco_yield(false); + if (!cco_done(&g->x)) cco_yield(false); g->value = next_value(&g->y); - if (cco_alive(&g->y)) cco_yield(false); + if (!cco_done(&g->y)) cco_yield(false); } cco_final: cco_end(true); diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 3fddf913..bbe85874 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -17,20 +17,20 @@ struct prime { int cco_state; }; -bool prime(struct prime* U) { - cco_begin(U); - if (U->result < 2) U->result = 2; - if (U->result == 2) { - if (U->count-- == 0) cco_return; - ++U->idx; +bool prime(struct prime* g) { + cco_begin(g); + if (g->result < 2) g->result = 2; + if (g->result == 2) { + if (g->count-- == 0) cco_return; + ++g->idx; cco_yield(false); } - U->result += !(U->result & 1); - for (U->pos = U->result; U->count > 0; U->pos += 2) { - if (is_prime(U->pos)) { - --U->count; - ++U->idx; - U->result = U->pos; + g->result += !(g->result & 1); + for (g->pos = g->result; g->count > 0; g->pos += 2) { + if (is_prime(g->pos)) { + --g->count; + ++g->idx; + g->result = g->pos; cco_yield(false); } } @@ -48,20 +48,20 @@ struct fibonacci { int cco_state; }; -bool fibonacci(struct fibonacci* F) { - assert(F->count < 94); +bool fibonacci(struct fibonacci* g) { + assert(g->count < 94); - cco_begin(F); - F->idx = 0; - F->result = 0; - F->b = 1; + cco_begin(g); + g->idx = 0; + g->result = 0; + g->b = 1; for (;;) { - if (F->count-- == 0) + if (g->count-- == 0) cco_return; - if (++F->idx > 1) { - int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield! - F->result = F->b; - F->b = sum; + if (++g->idx > 1) { + int64_t sum = g->result + g->b; // NB! locals only lasts until next cco_yield! + g->result = g->b; + g->b = sum; } cco_yield(false); } diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 1809f2ec..db9c2716 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -19,7 +19,7 @@ static bool schedule(Scheduler* sched) struct Task task = *Scheduler_front(sched); Scheduler_pop_front(sched); - if (cco_alive(&task)) + if (!cco_done(&task)) task.resume(&task); return !Scheduler_empty(sched); -- cgit v1.2.3