]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/threads.cc
License change.
[polintos/scott/priv.git] / kernel / tests / threads.cc
1 // tests/threads.cc
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15 #include <kern/types.h>
16 #include <kern/libc.h>
17 #include <kern/time.h>
18 #include <kern/thread.h>
19
20 void wake(Time::KTimerEntry *entry)
21 {
22         Threads::Blocker *b = static_cast<Threads::Blocker *>(entry->data);
23         b->wake();
24 }
25
26 void clearflag(Time::KTimerEntry *entry)
27 {
28         int *flag = static_cast<int *>(entry->data);
29         *flag = 0;
30 }
31
32 void threadb(void *arg)
33 {
34         Time::KTimerEntry timer(Time::monotonic_timers);
35         Threads::ThreadBlocker tb(curthread);
36         
37         for (;;) {
38                 volatile int flag = 1;
39                 Time::Time now;
40                 Time::monotonic_clock.get_time(&now);
41                 
42                 now.seconds += 2;
43                 
44                 timer.func = clearflag;
45                 timer.data = (void *)(&flag);
46                 timer.arm(now);
47                 
48                 while (flag)
49                         printf("B");
50
51                 now.seconds += 2;
52
53                 tb.blocked = true;
54                 timer.func = wake;
55                 timer.data = &tb;
56                 timer.arm(now);
57                 
58                 curthread->block(&tb);
59         }
60 }
61
62 extern System::Mem::Mappable Mem::physmem;
63
64 void threada(void *arg)
65 {
66         Time::KTimerEntry timer(Time::monotonic_timers);
67         Threads::ThreadBlocker tb(curthread);
68         
69         Threads::sched.new_thread(threadb, NULL, "thread b")->wake();
70
71         for (;;) {
72                 volatile int flag = 1;
73                 Time::Time now;
74                 Time::monotonic_clock.get_time(&now);
75                 
76                 now.seconds++;
77                 
78                 timer.func = clearflag;
79                 timer.data = (void *)(&flag);
80                 timer.arm(now);
81                 
82                 while (flag)
83                         printf("A");
84
85                 now.seconds++;
86
87                 tb.blocked = true;
88                 timer.func = wake;
89                 timer.data = &tb;
90                 timer.arm(now);
91                 
92                 curthread->block(&tb);
93         }
94 }
95
96 void run_test()
97 {
98         Threads::sched.new_thread(threada, NULL, "thread a")->wake();
99 }