diff options
| author | Tyge Løvset <[email protected]> | 2023-02-24 15:27:14 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-24 15:27:14 +0100 |
| commit | dde5e3465c4198841fdba32dfaf2220c87bc6a94 (patch) | |
| tree | 4b20c999218534d6693bf0b4eb80703572c676ff /misc/examples | |
| parent | 560b1f4e4bf61ca01c27f1895b5138f6004eb717 (diff) | |
| download | STC-modified-dde5e3465c4198841fdba32dfaf2220c87bc6a94.tar.gz STC-modified-dde5e3465c4198841fdba32dfaf2220c87bc6a94.zip | |
Update ccoro.h and added cosub.c example demoing a corotine calling another coroutine.
Diffstat (limited to 'misc/examples')
| -rw-r--r-- | misc/examples/cofib.c | 43 | ||||
| -rw-r--r-- | misc/examples/cosub.c | 65 |
2 files changed, 65 insertions, 43 deletions
diff --git a/misc/examples/cofib.c b/misc/examples/cofib.c deleted file mode 100644 index b58775ae..00000000 --- a/misc/examples/cofib.c +++ /dev/null @@ -1,43 +0,0 @@ -#include <stc/algo/ccoro.h> -#include <stdio.h> -#include <stdint.h> - -// Use coroutine to create a fibonacci sequence generator: - -typedef long long llong; -struct fibonacci { - int n; - int ccoro_state; - llong a, b, idx; -}; - -llong fibonacci(struct fibonacci* U) { - assert (U->n < 95); - - ccoro_execute(U, - U->a = 0; - U->b = 1; - for (U->idx = 0; U->idx < U->n; U->idx++) { - ccoro_yield (U->a); - llong sum = U->a + U->b; // NB! locals only lasts until next ccoro_yield! - U->a = U->b; - U->b = sum; - } - ccoro_final: - ); - - return -1; -} - - -int main(void) { - printf("Fibonacci numbers:\n"); - struct fibonacci fib = {.n = 14}; - - for (;;) { - llong x = fibonacci(&fib); - if (!ccoro_alive(&fib)) break; - printf(" %lld", x); - } - puts(""); -} diff --git a/misc/examples/cosub.c b/misc/examples/cosub.c new file mode 100644 index 00000000..0aee7aee --- /dev/null +++ b/misc/examples/cosub.c @@ -0,0 +1,65 @@ +#include <stc/algo/ccoro.h> +#include <stdio.h> +#include <stdint.h> + +// Use coroutine to create a fibonacci sequence generator: + +typedef long long intll_t; + +struct fibonacci { + int n; + int ccoro_state; + intll_t a, b, idx; +}; + +intll_t fibonacci(struct fibonacci* F) { + assert (F->n < 95); + + ccoro_execute(F, + F->a = 0; + F->b = 1; + for (F->idx = 0; F->idx < F->n; F->idx++) { + ccoro_yield (F->a); + intll_t sum = F->a + F->b; // NB! locals only lasts until next ccoro_yield! + F->a = F->b; + F->b = sum; + } + ccoro_final: + ); + + return -1; +} + +// Demonstrate to call another coroutine from a coroutine: +// Create a 2D iterator, and call fibonacci sequence when x,y = 1,1: + +struct iterate { + int max_x, max_y; + int ccoro_state; + int x, y; +}; + +bool iterate(struct iterate* I, struct fibonacci* F) { + ccoro_execute(I, + for (I->x = 0; I->x < I->max_x; I->x++) + for (I->y = 0; I->y < I->max_y; I->y++) + if (I->x == 1 && I->y == 1) + ccoro_yield_call (F, fibonacci(F), true); + else + ccoro_yield (true); + ccoro_finish: + ); + return false; +} + + +int main(void) { + printf("Fibonacci numbers:\n"); + struct fibonacci fib = {.n = 14}; + struct iterate iter = {3, 3}; + + while (iterate(&iter, &fib)) + printf("%d %d. Fib: %lld\n", iter.x, iter.y, fib.a); + + puts(""); +} |
