forked from cahirwpz/mimiker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
57 lines (40 loc) · 1.27 KB
/
thread.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 <stdc.h>
#include <thread.h>
static thread_t *td0, *td1, *td2;
static void demo_thread_1() {
int times = 3;
kprintf("Thread '%s' started.\n", thread_self()->td_name);
do {
thread_switch_to(td2);
kprintf("Thread '%s' running.\n", thread_self()->td_name);
} while (--times);
thread_switch_to(td0);
}
static void demo_thread_2() {
int times = 3;
kprintf("Thread '%s' started.\n", thread_self()->td_name);
do {
thread_switch_to(td1);
kprintf("Thread '%s' running.\n", thread_self()->td_name);
} while (--times);
panic("This line need not be reached!");
}
int main(int argc, char **argv, char **envp) {
kprintf("Thread '%s' started.\n", thread_self()->td_name);
kprintf("argc = %d\n", argc);
kprintf("argv = %p\n", argv);
kprintf("argp = %p\n", envp);
td0 = thread_self();
td1 = thread_create("first", demo_thread_1, NULL);
td2 = thread_create("second", demo_thread_2, NULL);
thread_switch_to(td1);
kprintf("Thread '%s' running.\n", thread_self()->td_name);
assert(td0 == thread_get_by_tid(td0->td_tid));
assert(td1 == thread_get_by_tid(td1->td_tid));
assert(td2 == thread_get_by_tid(td2->td_tid));
assert(NULL == thread_get_by_tid(1234));
thread_dump_all();
thread_delete(td2);
thread_delete(td1);
return 0;
}