-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschtest.c
57 lines (50 loc) · 1.19 KB
/
schtest.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
#include <stdio.h>
#include "defs.h"
struct context sch_context;
struct context foo_context;
struct context bar_context;
struct context baz_context;
#define STACK_DEPTH 512
uint64 foo_stack[STACK_DEPTH];
uint64 bar_stack[STACK_DEPTH];
uint64 baz_stack[STACK_DEPTH];
void foo() {
uint64 c = 0;
for (;;) {
printf("foo : %lu\n", c);
swtch(&foo_context, &sch_context);
c += 1;
}
}
void bar() {
uint64 c = 0;
for (;;) {
printf("bar : %lu\n", c);
swtch(&bar_context, &sch_context);
c += 2;
}
}
void baz() {
uint64 c = 0;
for (;;) {
printf("baz : %lu\n", c);
swtch(&baz_context, &sch_context);
c += 3;
}
}
int main() {
// setting up initial contexts
foo_context.ra = (uint64)foo;
foo_context.sp = (uint64)(foo_stack + STACK_DEPTH);
bar_context.ra = (uint64)bar;
bar_context.sp = (uint64)(bar_stack + STACK_DEPTH);
baz_context.ra = (uint64)baz;
baz_context.sp = (uint64)(baz_stack + STACK_DEPTH);
// start scheduling
for (;;) {
swtch(&sch_context, &foo_context);
swtch(&sch_context, &bar_context);
swtch(&sch_context, &baz_context);
}
return 0;
}