| [b9255d0] | 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 | 
 | 
|---|
| [22b7579] | 17 | fast_lock f;
 | 
|---|
 | 18 | condition_variable( fast_lock ) c_f;
 | 
|---|
 | 19 | 
 | 
|---|
| [5a46e09] | 20 | linear_backoff_then_block_lock l;
 | 
|---|
 | 21 | condition_variable( linear_backoff_then_block_lock ) c_l;
 | 
|---|
 | 22 | 
 | 
|---|
| [b9255d0] | 23 | thread T_C_M_WS1 {};
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | void 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 | 
 | 
|---|
 | 37 | thread T_C_M_WB1 {};
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | void 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 | 
 | 
|---|
 | 51 | thread T_C_S_WS1 {};
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | void 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 | 
 | 
|---|
 | 65 | thread T_C_S_WB1 {};
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | void 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 | 
 | 
|---|
| [22b7579] | 79 | thread T_C_F_WS1 {};
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | void 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 | 
 | 
|---|
 | 93 | thread T_C_F_WB1 {};
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | void 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 | 
 | 
|---|
| [5a46e09] | 107 | thread T_C_L_WS1 {};
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 | void 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 | 
 | 
|---|
 | 121 | thread T_C_L_WB1 {};
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 | void 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 | 
 | 
|---|
| [b9255d0] | 135 | thread T_C_O_WS1 {};
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | void 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 | 
 | 
|---|
 | 149 | thread T_C_O_WB1 {};
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 | void 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 | 
 | 
|---|
 | 163 | thread T_C_M_WS2 {};
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 | void 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 | 
 | 
|---|
 | 181 | thread T_C_O_WS2 {};
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 | void 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 | 
 | 
|---|
 | 199 | thread T_C_NLW {};
 | 
|---|
 | 200 | 
 | 
|---|
 | 201 | void main( T_C_NLW & this ) {
 | 
|---|
 | 202 |         for (unsigned int i = 0; i < num_times; i++) {
 | 
|---|
 | 203 |                 wait(c_o);
 | 
|---|
 | 204 |         }
 | 
|---|
 | 205 | }
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 | thread T_C_NLS {};
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | void 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 | 
 | 
|---|
 | 216 | thread T_C_S_WNF {};
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 | void 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 | 
 | 
|---|
 | 231 | bool done = false;
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 | thread T_C_NLWD {};
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 | void main( T_C_NLWD & this ) {
 | 
|---|
 | 236 |         done = false;
 | 
|---|
 | 237 |         for (unsigned int i = 0; i < num_times/5; i++) {
 | 
|---|
| [68a867ee] | 238 |                 if (i % 1000 == 0) printf("Iteration: %d\n", i);
 | 
|---|
| [b9255d0] | 239 |                 wait(c_s, 1`ns);
 | 
|---|
 | 240 |         }
 | 
|---|
 | 241 |         done = true;
 | 
|---|
 | 242 | }
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 | thread T_C_WDS {};
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 | void 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 | 
 | 
|---|
 | 255 | thread T_C_LWD {};
 | 
|---|
 | 256 | 
 | 
|---|
 | 257 | void main( T_C_LWD & this ) {
 | 
|---|
 | 258 |         done = false;
 | 
|---|
 | 259 |         for (unsigned int i = 0; i < num_times/5; i++) {
 | 
|---|
| [68a867ee] | 260 |                 if (i % 1000 == 0) printf("Iteration: %d\n", i);
 | 
|---|
| [b9255d0] | 261 |                 lock(s);
 | 
|---|
 | 262 |                 wait(c_s, s, 1`ns);
 | 
|---|
 | 263 |                 unlock(s);
 | 
|---|
 | 264 |         }
 | 
|---|
 | 265 |         done = true;
 | 
|---|
 | 266 | }
 | 
|---|
 | 267 | 
 | 
|---|
 | 268 | thread T_C_LWDS {};
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 | void 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 | 
 | 
|---|
 | 281 | int 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 | 
 | 
|---|
| [22b7579] | 319 |         printf("Start Test 7: fast lock and condition variable single wait/notify\n");
 | 
|---|
| [b9255d0] | 320 |         {
 | 
|---|
| [22b7579] | 321 |                 T_C_F_WS1 t1[2];
 | 
|---|
| [b9255d0] | 322 |         }
 | 
|---|
 | 323 |         printf("Done Test 7\n");
 | 
|---|
 | 324 | 
 | 
|---|
| [22b7579] | 325 |         printf("Start Test 8: fast lock and condition variable 3 wait/notify all\n");
 | 
|---|
| [b9255d0] | 326 |         {
 | 
|---|
| [22b7579] | 327 |                 T_C_F_WB1 t1[4];
 | 
|---|
| [b9255d0] | 328 |         }
 | 
|---|
 | 329 |         printf("Done Test 8\n");
 | 
|---|
 | 330 | 
 | 
|---|
| [5a46e09] | 331 |         printf("Start Test 9: linear backoff lock and condition variable single wait/notify\n");
 | 
|---|
| [22b7579] | 332 |         {
 | 
|---|
| [5a46e09] | 333 |                 T_C_L_WS1 t1[2];
 | 
|---|
| [22b7579] | 334 |         }
 | 
|---|
 | 335 |         printf("Done Test 9\n");
 | 
|---|
 | 336 | 
 | 
|---|
| [5a46e09] | 337 |         printf("Start Test 10: linear backoff lock and condition variable 3 wait/notify all\n");
 | 
|---|
| [22b7579] | 338 |         {
 | 
|---|
| [5a46e09] | 339 |                 T_C_L_WB1 t1[4];
 | 
|---|
| [22b7579] | 340 |         }
 | 
|---|
 | 341 |         printf("Done Test 10\n");
 | 
|---|
 | 342 | 
 | 
|---|
| [5a46e09] | 343 |         printf("Start Test 11: multi acquisiton lock and condition variable multiple acquire and wait/notify\n");
 | 
|---|
| [b9255d0] | 344 |         {
 | 
|---|
| [5a46e09] | 345 |                 T_C_M_WS2 t1[2];
 | 
|---|
| [b9255d0] | 346 |         }
 | 
|---|
| [22b7579] | 347 |         printf("Done Test 11\n");
 | 
|---|
| [b9255d0] | 348 | 
 | 
|---|
| [5a46e09] | 349 |         printf("Start Test 12: owner lock and condition variable multiple acquire and wait/notify\n");
 | 
|---|
| [b9255d0] | 350 |         {
 | 
|---|
| [5a46e09] | 351 |                 T_C_O_WS2 t1[2];
 | 
|---|
| [b9255d0] | 352 |         }
 | 
|---|
| [22b7579] | 353 |         printf("Done Test 12\n");
 | 
|---|
| [b9255d0] | 354 | 
 | 
|---|
| [5a46e09] | 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");
 | 
|---|
| [b9255d0] | 367 | }
 | 
|---|