-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathschtest.c
More file actions
59 lines (51 loc) · 1.18 KB
/
schtest.c
File metadata and controls
59 lines (51 loc) · 1.18 KB
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
/*
* swtest
* Simple examples for understanding the usage of xv6 swtch
* Copyright (C) 2017 Takuo Watanabe
*/
#include <stdio.h>
#include <stdlib.h>
#include "context.h"
#include "swtch.h"
struct context *sch_ctx, *foo_ctx, *bar_ctx, *baz_ctx;
void foo(int c) {
while (1) {
printf("foo : %d\n", c);
c += 1;
swtch(&foo_ctx, sch_ctx);
}
}
void bar(int c) {
while (1) {
printf("bar : %d\n", c);
swtch(&bar_ctx, sch_ctx);
c += 2;
}
}
void baz(int c) {
while (1) {
printf("baz : %d\n", c);
swtch(&baz_ctx, sch_ctx);
c += 3;
printf("baz : %d\n", c);
swtch(&baz_ctx, sch_ctx);
c += 3;
}
}
void scheduler() {
while (1) {
swtch(&sch_ctx, foo_ctx);
swtch(&sch_ctx, bar_ctx);
swtch(&sch_ctx, baz_ctx);
}
}
int main() {
uint *foo_stack = valloc(STACK_SIZE);
uint *bar_stack = valloc(STACK_SIZE);
uint *baz_stack = valloc(STACK_SIZE);
foo_ctx = new_context(foo_stack + STACK_DEPTH, foo, 0);
bar_ctx = new_context(bar_stack + STACK_DEPTH, bar, 0);
baz_ctx = new_context(baz_stack + STACK_DEPTH, baz, 0);
scheduler();
return 0;
}