]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/threads.cc
Switch to a simple X11-style license.
[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 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
21 // SOFTWARE.
22
23 #include <kern/types.h>
24 #include <kern/libc.h>
25 #include <kern/time.h>
26 #include <kern/thread.h>
27
28 void wake(Time::KTimerEntry *entry)
29 {
30         Threads::Blocker *b = static_cast<Threads::Blocker *>(entry->data);
31         b->wake();
32 }
33
34 void clearflag(Time::KTimerEntry *entry)
35 {
36         int *flag = static_cast<int *>(entry->data);
37         *flag = 0;
38 }
39
40 void threadb(void *arg)
41 {
42         Time::KTimerEntry timer(Time::monotonic_timers);
43         Threads::ThreadBlocker tb(curthread);
44         
45         for (;;) {
46                 volatile int flag = 1;
47                 Time::Time now;
48                 Time::monotonic_clock.get_time(&now);
49                 
50                 now.seconds += 2;
51                 
52                 timer.func = clearflag;
53                 timer.data = (void *)(&flag);
54                 timer.arm(now);
55                 
56                 while (flag)
57                         printf("B");
58
59                 now.seconds += 2;
60
61                 tb.blocked = true;
62                 timer.func = wake;
63                 timer.data = &tb;
64                 timer.arm(now);
65                 
66                 curthread->block(&tb);
67         }
68 }
69
70 extern System::Mem::Mappable Mem::physmem;
71
72 void threada(void *arg)
73 {
74         Time::KTimerEntry timer(Time::monotonic_timers);
75         Threads::ThreadBlocker tb(curthread);
76         
77         Threads::sched.new_thread(threadb, NULL, "thread b")->wake();
78
79         for (;;) {
80                 volatile int flag = 1;
81                 Time::Time now;
82                 Time::monotonic_clock.get_time(&now);
83                 
84                 now.seconds++;
85                 
86                 timer.func = clearflag;
87                 timer.data = (void *)(&flag);
88                 timer.arm(now);
89                 
90                 while (flag)
91                         printf("A");
92
93                 now.seconds++;
94
95                 tb.blocked = true;
96                 timer.func = wake;
97                 timer.data = &tb;
98                 timer.arm(now);
99                 
100                 curthread->block(&tb);
101         }
102 }
103
104 void run_test()
105 {
106         Threads::sched.new_thread(threada, NULL, "thread a")->wake();
107 }