1 | //--------------------------------------------------------- |
---|
2 | // Multi wait test |
---|
3 | // Ensures that no deadlock from waiting/signalling conditions |
---|
4 | //--------------------------------------------------------- |
---|
5 | |
---|
6 | |
---|
7 | #include <fstream> |
---|
8 | #include <kernel> |
---|
9 | #include <monitor> |
---|
10 | #include <stdlib> |
---|
11 | #include <thread> |
---|
12 | |
---|
13 | #include <time.h> |
---|
14 | |
---|
15 | #ifdef LONG_TEST |
---|
16 | static const unsigned long N = 375_000ul; |
---|
17 | #else |
---|
18 | static const unsigned long N = 2_500ul; |
---|
19 | #endif |
---|
20 | |
---|
21 | #ifndef PREEMPTION_RATE |
---|
22 | #define PREEMPTION_RATE 10_000ul |
---|
23 | #endif |
---|
24 | |
---|
25 | unsigned int default_preemption() { |
---|
26 | return PREEMPTION_RATE; |
---|
27 | } |
---|
28 | |
---|
29 | monitor global_t {}; |
---|
30 | |
---|
31 | global_t globalA; |
---|
32 | global_t globalB; |
---|
33 | global_t globalC; |
---|
34 | |
---|
35 | condition condAB, condAC, condBC, condABC; |
---|
36 | |
---|
37 | thread Signaler {}; |
---|
38 | thread WaiterAB {}; |
---|
39 | thread WaiterAC {}; |
---|
40 | thread WaiterBC {}; |
---|
41 | thread WaiterABC{}; |
---|
42 | |
---|
43 | volatile int waiter_left; |
---|
44 | |
---|
45 | //---------------------------------------------------------------------------------------------------- |
---|
46 | // Tools |
---|
47 | void signal( condition & cond, global_t & mutex a, global_t & mutex b ) { |
---|
48 | signal( cond ); |
---|
49 | } |
---|
50 | |
---|
51 | void signal( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) { |
---|
52 | signal( cond ); |
---|
53 | } |
---|
54 | |
---|
55 | void wait( condition & cond, global_t & mutex a, global_t & mutex b ) { |
---|
56 | wait( cond ); |
---|
57 | } |
---|
58 | |
---|
59 | void wait( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) { |
---|
60 | wait( cond ); |
---|
61 | } |
---|
62 | |
---|
63 | //---------------------------------------------------------------------------------------------------- |
---|
64 | // Signaler |
---|
65 | void main( Signaler & this ) { |
---|
66 | |
---|
67 | while( waiter_left != 0 ) { |
---|
68 | unsigned action = random( 4 ); |
---|
69 | switch( action ) { |
---|
70 | case 0: |
---|
71 | signal( condABC, globalA, globalB, globalC ); |
---|
72 | break; |
---|
73 | case 1: |
---|
74 | signal( condAB , globalA, globalB ); |
---|
75 | break; |
---|
76 | case 2: |
---|
77 | signal( condBC , globalB, globalC ); |
---|
78 | break; |
---|
79 | case 3: |
---|
80 | signal( condAC , globalA, globalC ); |
---|
81 | break; |
---|
82 | default: |
---|
83 | sout | "Something went wrong" | endl; |
---|
84 | abort(); |
---|
85 | } |
---|
86 | yield(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | //---------------------------------------------------------------------------------------------------- |
---|
91 | // Waiter ABC |
---|
92 | void main( WaiterABC & this ) { |
---|
93 | for( int i = 0; i < N; i++ ) { |
---|
94 | wait( condABC, globalA, globalB, globalC ); |
---|
95 | } |
---|
96 | |
---|
97 | __sync_fetch_and_sub_4( &waiter_left, 1); |
---|
98 | } |
---|
99 | |
---|
100 | //---------------------------------------------------------------------------------------------------- |
---|
101 | // Waiter AB |
---|
102 | void main( WaiterAB & this ) { |
---|
103 | for( int i = 0; i < N; i++ ) { |
---|
104 | wait( condAB , globalA, globalB ); |
---|
105 | } |
---|
106 | |
---|
107 | __sync_fetch_and_sub_4( &waiter_left, 1); |
---|
108 | } |
---|
109 | |
---|
110 | //---------------------------------------------------------------------------------------------------- |
---|
111 | // Waiter AC |
---|
112 | void main( WaiterAC & this ) { |
---|
113 | for( int i = 0; i < N; i++ ) { |
---|
114 | wait( condAC , globalA, globalC ); |
---|
115 | } |
---|
116 | |
---|
117 | __sync_fetch_and_sub_4( &waiter_left, 1); |
---|
118 | } |
---|
119 | |
---|
120 | //---------------------------------------------------------------------------------------------------- |
---|
121 | // Waiter BC |
---|
122 | void main( WaiterBC & this ) { |
---|
123 | for( int i = 0; i < N; i++ ) { |
---|
124 | wait( condBC , globalB, globalC ); |
---|
125 | } |
---|
126 | |
---|
127 | __sync_fetch_and_sub_4( &waiter_left, 1); |
---|
128 | } |
---|
129 | |
---|
130 | //---------------------------------------------------------------------------------------------------- |
---|
131 | // Main |
---|
132 | int main(int argc, char* argv[]) { |
---|
133 | srandom( time( NULL ) ); |
---|
134 | waiter_left = 4; |
---|
135 | processor p[2]; |
---|
136 | sout | "Starting" | endl; |
---|
137 | { |
---|
138 | Signaler e; |
---|
139 | { |
---|
140 | WaiterABC a; |
---|
141 | WaiterAB b; |
---|
142 | WaiterBC c; |
---|
143 | WaiterAC d; |
---|
144 | } |
---|
145 | } |
---|
146 | sout | "Done" | endl; |
---|
147 | } |
---|