]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/mutex.cc
Initial checkin from Perforce.
[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 conditions:
11 // 
12 //     * Redistributions of source code must retain the above copyright notice,
13 //       this list of conditions and the following disclaimers.
14 // 
15 //     * Redistributions in binary form must reproduce the above copyright notice,
16 //       this list of conditions and the following disclaimers in the
17 //       documentation and/or other materials provided with the distribution.
18 // 
19 //     * The names of the Software's authors and/or contributors
20 //       may not be used to endorse or promote products derived from
21 //       this Software without specific prior written permission.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
29 // SOFTWARE.
30
31 #include <kern/types.h>
32 #include <kern/libc.h>
33 #include <kern/time.h>
34 #include <kern/thread.h>
35 #include <util/lock.h>
36
37 Lock::Lock testlock;
38
39 void threadb(void *arg)
40 {
41         testlock.lock();
42         
43         while (true) {
44                 Time::Time now;
45                 Time::monotonic_clock.get_time(&now);
46                 
47                 while (true) {
48                         printf("B");
49                 
50                         Time::Time now2;
51                         Time::monotonic_clock.get_time(&now2);
52
53                         if (now.seconds != now2.seconds)
54                                 break;
55                 }
56                 
57                 testlock.unlock();
58                 testlock.lock();
59         }
60 }
61
62 void threada(void *arg)
63 {
64         Threads::sched.new_thread(threadb, NULL, "thread b")->wake();
65
66         testlock.lock();
67         
68         while (true) {
69                 Time::Time now;
70                 Time::monotonic_clock.get_time(&now);
71                 
72                 while (true) {
73                         printf("A");
74                 
75                         Time::Time now2;
76                         Time::monotonic_clock.get_time(&now2);
77
78                         if (now.seconds != now2.seconds)
79                                 break;
80                 }
81                 
82                 testlock.unlock();
83                 testlock.lock();
84         }
85 }
86
87
88 void run_test()
89 {
90         Threads::sched.new_thread(threada, NULL, "thread a")->wake();
91 }