diff options
| author | Tyge Løvset <[email protected]> | 2023-02-28 11:01:17 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-28 11:01:17 +0100 |
| commit | 158fbcd56a9684423f70e9579bdf2271f8c90b9f (patch) | |
| tree | 0ee8b745dbec75d8101f34e7119a9e51b09906a3 /misc/examples/coroutines.c | |
| parent | d3c93c2cbb9ffe83a162d32b1021eb24ec703a9c (diff) | |
| download | STC-modified-158fbcd56a9684423f70e9579bdf2271f8c90b9f.tar.gz STC-modified-158fbcd56a9684423f70e9579bdf2271f8c90b9f.zip | |
Final cleanups on corotines.
Diffstat (limited to 'misc/examples/coroutines.c')
| -rw-r--r-- | misc/examples/coroutines.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 577ef8c1..2c9e6d5c 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -52,15 +52,18 @@ bool fibonacci(struct fibonacci* F) { assert(F->count < 94); cco_begin(F); - F->idx = 1; + F->idx = 0; F->result = 0; F->b = 1; - for (;; F->idx++) { - if (F->count-- == 0) cco_return; + for (;;) { + if (F->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; + } cco_yield(true); - int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield! - F->result = F->b; - F->b = sum; } cco_final: printf("final fib\n"); @@ -69,29 +72,29 @@ bool fibonacci(struct fibonacci* F) { // Combine -struct combine { +struct combined { struct prime prm; struct fibonacci fib; int cco_state; }; -bool combine(struct combine* C) { +bool combined(struct combined* C) { cco_begin(C); cco_yield(prime(&C->prm), &C->prm, true); cco_yield(fibonacci(&C->fib), &C->fib, true); // Reuse the C->prm context and extend the count: - C->prm.count = 20; + C->prm.count = 8; C->prm.result += 2; cco_yield(prime(&C->prm), &C->prm, true); - cco_final: puts("final"); + cco_final: puts("final comb"); cco_end(false); } int main(void) { - struct combine comb = {.prm={.count=10}, .fib={14}}; + struct combined comb = {.prm={.count=8}, .fib={14}}; if (true) - while (combine(&comb)) + while (combined(&comb)) printf("Prime(%d)=%lld, Fib(%d)=%lld\n", comb.prm.idx, (long long)comb.prm.result, comb.fib.idx, (long long)comb.fib.result); |
