summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/coroutines.c
blob: 89b428eead526398bdb6be317600e18039aed788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stc/algo/coroutine.h>
#include <stdio.h>
#include <stdint.h>

// Demonstrate to call another coroutine from a coroutine:
// First create a 2D iterator, then call fibonacci sequence:

struct iterate {
    int max_x, max_y;
    int cco_state;
    int x, y;
};

bool iterate(struct iterate* I) {
    cco_begin(I);
        for (I->x = 0; I->x < I->max_x; I->x++)
            for (I->y = 0; I->y < I->max_y; I->y++)
                cco_yield(true);
        cco_final:
    cco_end(false);
}

// Use coroutine to create a fibonacci sequence generator:

struct fibonacci {
    int n; 
    int cco_state;
    int64_t a, b, idx;
};

int64_t fibonacci(struct fibonacci* F) {
    assert (F->n < 95);

    cco_begin(F);
        F->a = 0;
        F->b = 1;
        for (F->idx = 0; F->idx < F->n; F->idx++) {
            cco_yield(F->a);
            int64_t sum = F->a + F->b; // NB! locals only lasts until next cco_yield!
            F->a = F->b;
            F->b = sum;
        }
        cco_final:
    cco_end(-1);
}

// Combine

struct combine {
    struct iterate it;
    struct fibonacci fib;
    int cco_state;
};

bool combine(struct combine* C) {
    cco_begin(C);
        cco_yield_coroutine(&C->it, iterate(&C->it), true);
        cco_yield_coroutine(&C->fib, fibonacci(&C->fib), true);
        // May reuse the C->it context; state has been reset to 0.
        C->it.max_x = 2; C->it.max_y = 2;
        cco_yield_coroutine(&C->it, iterate(&C->it), true);
        cco_final: puts("final");
    cco_end(false);
}

int main(void) {
    struct combine comb = {.it={3, 3}, .fib={14}};
    while (combine(&comb))
        printf("Iter=(%d, %d). Fib=%lld\n", comb.it.x, comb.it.y, (long long)comb.fib.a);
}