source: tests/unified_locking/locks.cfa@ 6ff08d8

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 6ff08d8 was 5a46e09, checked in by caparsons <caparson@…>, 4 years ago

Added Martins SpinCondLock as linear_backoff_then_block lock

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