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