1 | #include <stdio.h> |
---|
2 | #include "locks.hfa" |
---|
3 | #include <stdlib.hfa> |
---|
4 | #include <thread.hfa> |
---|
5 | |
---|
6 | const unsigned int num_times = 50000; |
---|
7 | |
---|
8 | multiple_acquisition_lock m; |
---|
9 | condition_variable( multiple_acquisition_lock ) c_m; |
---|
10 | |
---|
11 | single_acquisition_lock s; |
---|
12 | condition_variable( single_acquisition_lock ) c_s; |
---|
13 | |
---|
14 | owner_lock o; |
---|
15 | condition_variable( owner_lock ) c_o; |
---|
16 | |
---|
17 | linear_backoff_then_block_lock l; |
---|
18 | condition_variable( linear_backoff_then_block_lock ) c_l; |
---|
19 | |
---|
20 | thread T_C_M_WS1 {}; |
---|
21 | |
---|
22 | void main( T_C_M_WS1 & this ) { |
---|
23 | for (unsigned int i = 0; i < num_times; i++) { |
---|
24 | lock(m); |
---|
25 | if(empty(c_m) && i != num_times - 1) { |
---|
26 | wait(c_m,m); |
---|
27 | }else{ |
---|
28 | notify_one(c_m); |
---|
29 | } |
---|
30 | unlock(m); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | thread T_C_M_WB1 {}; |
---|
35 | |
---|
36 | void main( T_C_M_WB1 & this ) { |
---|
37 | for (unsigned int i = 0; i < num_times; i++) { |
---|
38 | lock(m); |
---|
39 | if(counter(c_m) == 3 || i == num_times - 1) { |
---|
40 | notify_all(c_m); |
---|
41 | }else{ |
---|
42 | wait(c_m,m); |
---|
43 | } |
---|
44 | unlock(m); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | thread T_C_S_WS1 {}; |
---|
49 | |
---|
50 | void main( T_C_S_WS1 & this ) { |
---|
51 | for (unsigned int i = 0; i < num_times; i++) { |
---|
52 | lock(s); |
---|
53 | if(empty(c_s) && i != num_times - 1) { |
---|
54 | wait(c_s,s); |
---|
55 | }else{ |
---|
56 | notify_one(c_s); |
---|
57 | } |
---|
58 | unlock(s); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | thread T_C_S_WB1 {}; |
---|
63 | |
---|
64 | void main( T_C_S_WB1 & this ) { |
---|
65 | for (unsigned int i = 0; i < num_times; i++) { |
---|
66 | lock(s); |
---|
67 | if(counter(c_s) == 3 || i == num_times - 1) { |
---|
68 | notify_all(c_s); |
---|
69 | }else{ |
---|
70 | wait(c_s,s); |
---|
71 | } |
---|
72 | unlock(s); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | thread T_C_L_WS1 {}; |
---|
77 | |
---|
78 | void main( T_C_L_WS1 & this ) { |
---|
79 | for (unsigned int i = 0; i < num_times; i++) { |
---|
80 | lock(l); |
---|
81 | if(empty(c_l) && i != num_times - 1) { |
---|
82 | wait(c_l,l); |
---|
83 | }else{ |
---|
84 | notify_one(c_l); |
---|
85 | } |
---|
86 | unlock(l); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | thread T_C_L_WB1 {}; |
---|
91 | |
---|
92 | void main( T_C_L_WB1 & this ) { |
---|
93 | for (unsigned int i = 0; i < num_times; i++) { |
---|
94 | lock(l); |
---|
95 | if(counter(c_l) == 3 || i == num_times - 1) { |
---|
96 | notify_all(c_l); |
---|
97 | }else{ |
---|
98 | wait(c_l,l); |
---|
99 | } |
---|
100 | unlock(l); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | thread T_C_O_WS1 {}; |
---|
105 | |
---|
106 | void main( T_C_O_WS1 & this ) { |
---|
107 | for (unsigned int i = 0; i < num_times; i++) { |
---|
108 | lock(o); |
---|
109 | if(empty(c_o) && i != num_times - 1) { |
---|
110 | wait(c_o,o); |
---|
111 | }else{ |
---|
112 | notify_one(c_o); |
---|
113 | } |
---|
114 | unlock(o); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | thread T_C_O_WB1 {}; |
---|
119 | |
---|
120 | void main( T_C_O_WB1 & this ) { |
---|
121 | for (unsigned int i = 0; i < num_times; i++) { |
---|
122 | lock(o); |
---|
123 | if(counter(c_o) == 3 || i == num_times - 1) { |
---|
124 | notify_all(c_o); |
---|
125 | }else{ |
---|
126 | wait(c_o,o); |
---|
127 | } |
---|
128 | unlock(o); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | thread T_C_M_WS2 {}; |
---|
133 | |
---|
134 | void main( T_C_M_WS2 & this ) { |
---|
135 | for (unsigned int i = 0; i < num_times; i++) { |
---|
136 | lock(m); |
---|
137 | lock(m); |
---|
138 | lock(m); |
---|
139 | if(empty(c_m) && i != num_times - 1) { |
---|
140 | wait(c_m,m); |
---|
141 | }else{ |
---|
142 | notify_one(c_m); |
---|
143 | } |
---|
144 | unlock(m); |
---|
145 | unlock(m); |
---|
146 | unlock(m); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | thread T_C_O_WS2 {}; |
---|
151 | |
---|
152 | void main( T_C_O_WS2 & this ) { |
---|
153 | for (unsigned int i = 0; i < num_times; i++) { |
---|
154 | lock(o); |
---|
155 | lock(o); |
---|
156 | lock(o); |
---|
157 | if(empty(c_o) && i != num_times - 1) { |
---|
158 | wait(c_o,o); |
---|
159 | }else{ |
---|
160 | notify_one(c_o); |
---|
161 | } |
---|
162 | unlock(o); |
---|
163 | unlock(o); |
---|
164 | unlock(o); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | thread T_C_NLW {}; |
---|
169 | |
---|
170 | void main( T_C_NLW & this ) { |
---|
171 | for (unsigned int i = 0; i < num_times; i++) { |
---|
172 | wait(c_o); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | thread T_C_NLS {}; |
---|
177 | |
---|
178 | void main( T_C_NLS & this ) { |
---|
179 | for (unsigned int i = 0; i < num_times; i++) { |
---|
180 | while (empty(c_o)) { } |
---|
181 | notify_one(c_o); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | thread T_C_S_WNF {}; |
---|
186 | |
---|
187 | void main( T_C_S_WNF & this ) { |
---|
188 | for (unsigned int i = 0; i < num_times; i++) { |
---|
189 | lock(s); |
---|
190 | if(empty(c_s) && i != num_times - 1) { |
---|
191 | wait(c_s, s, 10); |
---|
192 | }else{ |
---|
193 | if(!empty(c_s)) assert(front(c_s) == 10); |
---|
194 | notify_one(c_s); |
---|
195 | } |
---|
196 | unlock(s); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | bool done = false; |
---|
201 | |
---|
202 | thread T_C_NLWD {}; |
---|
203 | |
---|
204 | void main( T_C_NLWD & this ) { |
---|
205 | done = false; |
---|
206 | for (unsigned int i = 0; i < num_times/5; i++) { |
---|
207 | if (i % 1000 == 0) printf("Iteration: %d\n", i); |
---|
208 | wait(c_s, 1`ns); |
---|
209 | } |
---|
210 | done = true; |
---|
211 | } |
---|
212 | |
---|
213 | thread T_C_WDS {}; |
---|
214 | |
---|
215 | void main( T_C_WDS & this ) { |
---|
216 | for (unsigned int i = 0; i < num_times; i++) { |
---|
217 | while (empty(c_s) && !done) { } |
---|
218 | notify_one(c_s); |
---|
219 | sleep(1`ns); |
---|
220 | if(done) break; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | thread T_C_LWD {}; |
---|
225 | |
---|
226 | void main( T_C_LWD & this ) { |
---|
227 | done = false; |
---|
228 | for (unsigned int i = 0; i < num_times/5; i++) { |
---|
229 | if (i % 1000 == 0) printf("Iteration: %d\n", i); |
---|
230 | lock(s); |
---|
231 | wait(c_s, s, 1`ns); |
---|
232 | unlock(s); |
---|
233 | } |
---|
234 | done = true; |
---|
235 | } |
---|
236 | |
---|
237 | thread T_C_LWDS {}; |
---|
238 | |
---|
239 | void main( T_C_LWDS & this ) { |
---|
240 | for (unsigned int i = 0; i < num_times; i++) { |
---|
241 | while (empty(c_s) && !done) { } |
---|
242 | lock(s); |
---|
243 | notify_one(c_s); |
---|
244 | unlock(s); |
---|
245 | sleep(1`ns); |
---|
246 | if(done) break; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | int main() { |
---|
251 | processor p[2]; |
---|
252 | printf("Start Test 1: multi acquisition lock and condition variable single wait/notify\n"); |
---|
253 | { |
---|
254 | T_C_M_WS1 t1[2]; |
---|
255 | } |
---|
256 | printf("Done Test 1\n"); |
---|
257 | |
---|
258 | printf("Start Test 2: multi acquisition lock and condition variable 3 wait/notify all\n"); |
---|
259 | { |
---|
260 | T_C_M_WB1 t1[4]; |
---|
261 | } |
---|
262 | printf("Done Test 2\n"); |
---|
263 | |
---|
264 | printf("Start Test 3: single acquisition lock and condition variable single wait/notify\n"); |
---|
265 | { |
---|
266 | T_C_S_WS1 t1[2]; |
---|
267 | } |
---|
268 | printf("Done Test 3\n"); |
---|
269 | |
---|
270 | printf("Start Test 4: single acquisition lock and condition variable 3 wait/notify all\n"); |
---|
271 | { |
---|
272 | T_C_S_WB1 t1[4]; |
---|
273 | } |
---|
274 | printf("Done Test 4\n"); |
---|
275 | |
---|
276 | printf("Start Test 5: owner lock and condition variable single wait/notify\n"); |
---|
277 | { |
---|
278 | T_C_O_WS1 t1[2]; |
---|
279 | } |
---|
280 | printf("Done Test 5\n"); |
---|
281 | |
---|
282 | printf("Start Test 6: owner lock and condition variable 3 wait/notify all\n"); |
---|
283 | { |
---|
284 | T_C_O_WB1 t1[4]; |
---|
285 | } |
---|
286 | printf("Done Test 6\n"); |
---|
287 | |
---|
288 | printf("Start Test 7: linear backoff lock and condition variable single wait/notify\n"); |
---|
289 | { |
---|
290 | T_C_L_WS1 t1[2]; |
---|
291 | } |
---|
292 | printf("Done Test 7\n"); |
---|
293 | |
---|
294 | printf("Start Test 8: linear backoff lock and condition variable 3 wait/notify all\n"); |
---|
295 | { |
---|
296 | T_C_L_WB1 t1[4]; |
---|
297 | } |
---|
298 | printf("Done Test 8\n"); |
---|
299 | |
---|
300 | printf("Start Test 9: multi acquisiton lock and condition variable multiple acquire and wait/notify\n"); |
---|
301 | { |
---|
302 | T_C_M_WS2 t1[2]; |
---|
303 | } |
---|
304 | printf("Done Test 9\n"); |
---|
305 | |
---|
306 | printf("Start Test 10: owner lock and condition variable multiple acquire and wait/notify\n"); |
---|
307 | { |
---|
308 | T_C_O_WS2 t1[2]; |
---|
309 | } |
---|
310 | printf("Done Test 10\n"); |
---|
311 | |
---|
312 | printf("Start Test 11: no lock condition variable wait/notify\n"); |
---|
313 | { |
---|
314 | T_C_NLW t1; |
---|
315 | T_C_NLS t2; |
---|
316 | } |
---|
317 | printf("Done Test 11\n"); |
---|
318 | |
---|
319 | printf("Start Test 12: locked condition variable wait/notify with front()\n"); |
---|
320 | { |
---|
321 | T_C_S_WNF t1[2]; |
---|
322 | } |
---|
323 | printf("Done Test 12\n"); |
---|
324 | } |
---|