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