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