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