]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/mutex.cc
Switch to a simple X11-style license.
[polintos/scott/priv.git] / kernel / tests / mutex.cc
1 // tests/mutex.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 #include <util/lock.h>
28
29 Lock::Lock testlock;
30
31 void threadb(void *arg)
32 {
33         testlock.lock();
34         
35         while (true) {
36                 Time::Time now;
37                 Time::monotonic_clock.get_time(&now);
38                 
39                 while (true) {
40                         printf("B");
41                 
42                         Time::Time now2;
43                         Time::monotonic_clock.get_time(&now2);
44
45                         if (now.seconds != now2.seconds)
46                                 break;
47                 }
48                 
49                 testlock.unlock();
50                 testlock.lock();
51         }
52 }
53
54 void threada(void *arg)
55 {
56         Threads::sched.new_thread(threadb, NULL, "thread b")->wake();
57
58         testlock.lock();
59         
60         while (true) {
61                 Time::Time now;
62                 Time::monotonic_clock.get_time(&now);
63                 
64                 while (true) {
65                         printf("A");
66                 
67                         Time::Time now2;
68                         Time::monotonic_clock.get_time(&now2);
69
70                         if (now.seconds != now2.seconds)
71                                 break;
72                 }
73                 
74                 testlock.unlock();
75                 testlock.lock();
76         }
77 }
78
79
80 void run_test()
81 {
82         Threads::sched.new_thread(threada, NULL, "thread a")->wake();
83 }