-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_code.c
67 lines (59 loc) · 1.37 KB
/
example_code.c
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
#include "coroutines.h"
// This coroutine will return 1, 2, 3, then stop itself.
Coroutine* OneTwoThree(uint32_t, Coroutine* co)
{
CoYield(1, co);
CoYield(2, co);
CoYield(3, co);
CoReturn(co); // same as 'return co;'
}
// CounterCoroutine consumes its value and adds it to an internal running count.
// It will yield the current running count on every run.
Coroutine* CounterCoroutine(uint32_t runValue, Coroutine* co)
{
uint32_t runningCount;
runningCount = runValue;
while (1)
{
runningCount += CoYield(runningCount, co);
}
}
// FibonacciCoroutine does not consume any value.
// It will yield the next fibonacci number on every run.
Coroutine* FibonacciCoroutine(uint32_t, Coroutine* co)
{
uint32_t a, b, temp;
a = 1;
b = 0;
while(1)
{
CoYield(b, co);
temp = a + b;
b = a;
a = temp;
}
}
void Test()
{
Coroutine* gen;
Coroutine* adder;
uint32_t i, yieldValue;
gen = CoInit(&FibonacciCoroutine, 64);
adder = CoInit(&CounterCoroutine, 64);
for (i=0; i<25; i++)
{
yieldValue = CoRun(0, gen);
printf("Fibonacci number: %i\r\n", yieldValue);
printf("Sum so far: %i\r\n", CoRun(yieldValue, adder));
}
CoKill(gen);
CoKill(adder);
gen = CoInit(&OneTwoThree, 64);
while (1)
{
yieldValue = CoRun(0, gen);
if (CoHasFinished(gen)) break; // OneTwoThree coroutine can return, so first check if it returned.
printf("%i\r\n", yieldValue);
}
CoEnd(gen);
}