-
Notifications
You must be signed in to change notification settings - Fork 1
/
nested.c
65 lines (50 loc) · 1007 Bytes
/
nested.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
#include "../async.h"
#include <stdio.h>
/* task declarations */
ASYNC(upper);
ASYNC(lower);
ASYNC(numeric);
/* task definitions */
ASYNC(upper) {
static char c;
BEGIN();
for (c = 'A'; c <= 'F'; c++) {
printf("%c", c);
AWAIT(lower);
YIELD();
}
END();
}
ASYNC(lower) {
static char c;
BEGIN();
for (c = 'a'; c <= 'f'; c++) {
printf("%c", c);
YIELD();
}
END();
}
ASYNC(numeric) {
static int i;
BEGIN();
for (i = 0; i <= 9; i++) {
printf("%d", i);
YIELD();
}
END();
}
int main() {
/* numeric task will not run until after upper task
* due to short circuit evaluation
* while (AWAIT(upper)->status || AWAIT(numeric)->status) continue;
*/
task_status_t upper_status = TASK_RUNNING, numeric_status = TASK_RUNNING;
while (upper_status || numeric_status) {
if (upper_status)
upper_status = AWAIT(upper)->status;
if (numeric_status)
numeric_status = AWAIT(numeric)->status;
}
putchar('\n');
return 0;
}