| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS |
| 22 |
|
| 23 |
#if USE_ISOC_THREADS |
| 24 |
# define TEST_ISOC_THREADS 1 |
| 25 |
#endif |
| 26 |
#if USE_POSIX_THREADS |
| 27 |
# define TEST_POSIX_THREADS 1 |
| 28 |
#endif |
| 29 |
#if USE_ISOC_AND_POSIX_THREADS |
| 30 |
# define TEST_ISOC_AND_POSIX_THREADS 1 |
| 31 |
#endif |
| 32 |
#if USE_WINDOWS_THREADS |
| 33 |
# define TEST_WINDOWS_THREADS 1 |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define ENABLE_LOCKING 1 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#define DO_TEST_LOCK 1 |
| 45 |
#define DO_TEST_RWLOCK 1 |
| 46 |
#define DO_TEST_RECURSIVE_LOCK 1 |
| 47 |
#define DO_TEST_ONCE 1 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define EXPLICIT_YIELD 1 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#define USE_VOLATILE 0 |
| 60 |
|
| 61 |
#if USE_POSIX_THREADS && HAVE_SEMAPHORE_H |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
# define USE_SEMAPHORE 1 |
| 66 |
|
| 67 |
|
| 68 |
# if defined __APPLE__ && defined __MACH__ |
| 69 |
# define USE_NAMED_SEMAPHORE 1 |
| 70 |
# else |
| 71 |
# define USE_UNNAMED_SEMAPHORE 1 |
| 72 |
# endif |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
#define ENABLE_DEBUGGING 0 |
| 77 |
|
| 78 |
|
| 79 |
#define THREAD_COUNT 10 |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define REPEAT_COUNT 50000 |
| 85 |
|
| 86 |
#include <stdint.h> |
| 87 |
#include <stdio.h> |
| 88 |
#include <stdlib.h> |
| 89 |
#include <string.h> |
| 90 |
|
| 91 |
#if !ENABLE_LOCKING |
| 92 |
# undef USE_ISOC_THREADS |
| 93 |
# undef USE_POSIX_THREADS |
| 94 |
# undef USE_ISOC_AND_POSIX_THREADS |
| 95 |
# undef USE_WINDOWS_THREADS |
| 96 |
#endif |
| 97 |
#include "glthread/lock.h" |
| 98 |
|
| 99 |
#if !ENABLE_LOCKING |
| 100 |
# if TEST_ISOC_THREADS |
| 101 |
# define USE_ISOC_THREADS 1 |
| 102 |
# endif |
| 103 |
# if TEST_POSIX_THREADS |
| 104 |
# define USE_POSIX_THREADS 1 |
| 105 |
# endif |
| 106 |
# if TEST_ISOC_AND_POSIX_THREADS |
| 107 |
# define USE_ISOC_AND_POSIX_THREADS 1 |
| 108 |
# endif |
| 109 |
# if TEST_WINDOWS_THREADS |
| 110 |
# define USE_WINDOWS_THREADS 1 |
| 111 |
# endif |
| 112 |
#endif |
| 113 |
|
| 114 |
#include "glthread/thread.h" |
| 115 |
#include "glthread/yield.h" |
| 116 |
#if USE_SEMAPHORE |
| 117 |
# include <errno.h> |
| 118 |
# include <fcntl.h> |
| 119 |
# include <semaphore.h> |
| 120 |
# include <unistd.h> |
| 121 |
#endif |
| 122 |
|
| 123 |
#if HAVE_DECL_ALARM |
| 124 |
# include <signal.h> |
| 125 |
# include <unistd.h> |
| 126 |
#endif |
| 127 |
|
| 128 |
#if ENABLE_DEBUGGING |
| 129 |
# define dbgprintf printf |
| 130 |
#else |
| 131 |
# define dbgprintf if (0) printf |
| 132 |
#endif |
| 133 |
|
| 134 |
#if EXPLICIT_YIELD |
| 135 |
# define yield() gl_thread_yield () |
| 136 |
#else |
| 137 |
# define yield() |
| 138 |
#endif |
| 139 |
|
| 140 |
#if USE_VOLATILE |
| 141 |
struct atomic_int { |
| 142 |
volatile int value; |
| 143 |
}; |
| 144 |
static void |
| 145 |
init_atomic_int (struct atomic_int *ai) |
| 146 |
{ |
| 147 |
} |
| 148 |
static int |
| 149 |
get_atomic_int_value (struct atomic_int *ai) |
| 150 |
{ |
| 151 |
return ai->value; |
| 152 |
} |
| 153 |
static void |
| 154 |
set_atomic_int_value (struct atomic_int *ai, int new_value) |
| 155 |
{ |
| 156 |
ai->value = new_value; |
| 157 |
} |
| 158 |
#elif USE_SEMAPHORE |
| 159 |
|
| 160 |
|
| 161 |
# if USE_UNNAMED_SEMAPHORE |
| 162 |
struct atomic_int { |
| 163 |
sem_t semaphore; |
| 164 |
}; |
| 165 |
#define atomic_int_semaphore(ai) (&(ai)->semaphore) |
| 166 |
static void |
| 167 |
init_atomic_int (struct atomic_int *ai) |
| 168 |
{ |
| 169 |
sem_init (&ai->semaphore, 0, 0); |
| 170 |
} |
| 171 |
# endif |
| 172 |
# if USE_NAMED_SEMAPHORE |
| 173 |
struct atomic_int { |
| 174 |
sem_t *semaphore; |
| 175 |
}; |
| 176 |
#define atomic_int_semaphore(ai) ((ai)->semaphore) |
| 177 |
static void |
| 178 |
init_atomic_int (struct atomic_int *ai) |
| 179 |
{ |
| 180 |
sem_t *s; |
| 181 |
unsigned int count; |
| 182 |
for (count = 0; ; count++) |
| 183 |
{ |
| 184 |
char name[80]; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
sprintf (name, "test-lock-%lu-%p-%u", |
| 191 |
(unsigned long) getpid (), ai, count); |
| 192 |
s = sem_open (name, O_CREAT | O_EXCL, 0600, 0); |
| 193 |
if (s == SEM_FAILED) |
| 194 |
{ |
| 195 |
if (errno == EEXIST) |
| 196 |
|
| 197 |
continue; |
| 198 |
else |
| 199 |
{ |
| 200 |
perror ("sem_open failed"); |
| 201 |
abort (); |
| 202 |
} |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
|
| 207 |
|
| 208 |
sem_unlink (name); |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
ai->semaphore = s; |
| 213 |
} |
| 214 |
# endif |
| 215 |
static int |
| 216 |
get_atomic_int_value (struct atomic_int *ai) |
| 217 |
{ |
| 218 |
if (sem_trywait (atomic_int_semaphore (ai)) == 0) |
| 219 |
{ |
| 220 |
if (sem_post (atomic_int_semaphore (ai))) |
| 221 |
abort (); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
else if (errno == EAGAIN) |
| 225 |
return 0; |
| 226 |
else |
| 227 |
abort (); |
| 228 |
} |
| 229 |
static void |
| 230 |
set_atomic_int_value (struct atomic_int *ai, int new_value) |
| 231 |
{ |
| 232 |
if (new_value == 0) |
| 233 |
|
| 234 |
return; |
| 235 |
|
| 236 |
if (sem_post (atomic_int_semaphore (ai))) |
| 237 |
abort (); |
| 238 |
} |
| 239 |
#else |
| 240 |
struct atomic_int { |
| 241 |
gl_lock_define (, lock) |
| 242 |
int value; |
| 243 |
}; |
| 244 |
static void |
| 245 |
init_atomic_int (struct atomic_int *ai) |
| 246 |
{ |
| 247 |
gl_lock_init (ai->lock); |
| 248 |
} |
| 249 |
static int |
| 250 |
get_atomic_int_value (struct atomic_int *ai) |
| 251 |
{ |
| 252 |
gl_lock_lock (ai->lock); |
| 253 |
int ret = ai->value; |
| 254 |
gl_lock_unlock (ai->lock); |
| 255 |
return ret; |
| 256 |
} |
| 257 |
static void |
| 258 |
set_atomic_int_value (struct atomic_int *ai, int new_value) |
| 259 |
{ |
| 260 |
gl_lock_lock (ai->lock); |
| 261 |
ai->value = new_value; |
| 262 |
gl_lock_unlock (ai->lock); |
| 263 |
} |
| 264 |
#endif |
| 265 |
|
| 266 |
#define ACCOUNT_COUNT 4 |
| 267 |
|
| 268 |
static int account[ACCOUNT_COUNT]; |
| 269 |
|
| 270 |
static int |
| 271 |
random_account (void) |
| 272 |
{ |
| 273 |
return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT; |
| 274 |
} |
| 275 |
|
| 276 |
static void |
| 277 |
check_accounts (void) |
| 278 |
{ |
| 279 |
int i, sum; |
| 280 |
|
| 281 |
sum = 0; |
| 282 |
for (i = 0; i < ACCOUNT_COUNT; i++) |
| 283 |
sum += account[i]; |
| 284 |
if (sum != ACCOUNT_COUNT * 1000) |
| 285 |
abort (); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
gl_lock_define_initialized(static, my_lock) |
| 296 |
|
| 297 |
static void * |
| 298 |
lock_mutator_thread (void *arg) |
| 299 |
{ |
| 300 |
int repeat; |
| 301 |
|
| 302 |
for (repeat = REPEAT_COUNT; repeat > 0; repeat--) |
| 303 |
{ |
| 304 |
int i1, i2, value; |
| 305 |
|
| 306 |
dbgprintf ("Mutator %p before lock\n", gl_thread_self_pointer ()); |
| 307 |
gl_lock_lock (my_lock); |
| 308 |
dbgprintf ("Mutator %p after lock\n", gl_thread_self_pointer ()); |
| 309 |
|
| 310 |
i1 = random_account (); |
| 311 |
i2 = random_account (); |
| 312 |
value = ((unsigned int) rand () >> 3) % 10; |
| 313 |
account[i1] += value; |
| 314 |
account[i2] -= value; |
| 315 |
|
| 316 |
dbgprintf ("Mutator %p before unlock\n", gl_thread_self_pointer ()); |
| 317 |
gl_lock_unlock (my_lock); |
| 318 |
dbgprintf ("Mutator %p after unlock\n", gl_thread_self_pointer ()); |
| 319 |
|
| 320 |
dbgprintf ("Mutator %p before check lock\n", gl_thread_self_pointer ()); |
| 321 |
gl_lock_lock (my_lock); |
| 322 |
check_accounts (); |
| 323 |
gl_lock_unlock (my_lock); |
| 324 |
dbgprintf ("Mutator %p after check unlock\n", gl_thread_self_pointer ()); |
| 325 |
|
| 326 |
yield (); |
| 327 |
} |
| 328 |
|
| 329 |
dbgprintf ("Mutator %p dying.\n", gl_thread_self_pointer ()); |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
|
| 333 |
static struct atomic_int lock_checker_done; |
| 334 |
|
| 335 |
static void * |
| 336 |
lock_checker_thread (void *arg) |
| 337 |
{ |
| 338 |
while (get_atomic_int_value (&lock_checker_done) == 0) |
| 339 |
{ |
| 340 |
dbgprintf ("Checker %p before check lock\n", gl_thread_self_pointer ()); |
| 341 |
gl_lock_lock (my_lock); |
| 342 |
check_accounts (); |
| 343 |
gl_lock_unlock (my_lock); |
| 344 |
dbgprintf ("Checker %p after check unlock\n", gl_thread_self_pointer ()); |
| 345 |
|
| 346 |
yield (); |
| 347 |
} |
| 348 |
|
| 349 |
dbgprintf ("Checker %p dying.\n", gl_thread_self_pointer ()); |
| 350 |
return NULL; |
| 351 |
} |
| 352 |
|
| 353 |
static void |
| 354 |
test_lock (void) |
| 355 |
{ |
| 356 |
int i; |
| 357 |
gl_thread_t checkerthread; |
| 358 |
gl_thread_t threads[THREAD_COUNT]; |
| 359 |
|
| 360 |
|
| 361 |
for (i = 0; i < ACCOUNT_COUNT; i++) |
| 362 |
account[i] = 1000; |
| 363 |
init_atomic_int (&lock_checker_done); |
| 364 |
set_atomic_int_value (&lock_checker_done, 0); |
| 365 |
|
| 366 |
|
| 367 |
checkerthread = gl_thread_create (lock_checker_thread, NULL); |
| 368 |
for (i = 0; i < THREAD_COUNT; i++) |
| 369 |
threads[i] = gl_thread_create (lock_mutator_thread, NULL); |
| 370 |
|
| 371 |
|
| 372 |
for (i = 0; i < THREAD_COUNT; i++) |
| 373 |
gl_thread_join (threads[i], NULL); |
| 374 |
set_atomic_int_value (&lock_checker_done, 1); |
| 375 |
gl_thread_join (checkerthread, NULL); |
| 376 |
check_accounts (); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
gl_rwlock_define_initialized(static, my_rwlock) |
| 387 |
|
| 388 |
static void * |
| 389 |
rwlock_mutator_thread (void *arg) |
| 390 |
{ |
| 391 |
int repeat; |
| 392 |
|
| 393 |
for (repeat = REPEAT_COUNT; repeat > 0; repeat--) |
| 394 |
{ |
| 395 |
int i1, i2, value; |
| 396 |
|
| 397 |
dbgprintf ("Mutator %p before wrlock\n", gl_thread_self_pointer ()); |
| 398 |
gl_rwlock_wrlock (my_rwlock); |
| 399 |
dbgprintf ("Mutator %p after wrlock\n", gl_thread_self_pointer ()); |
| 400 |
|
| 401 |
i1 = random_account (); |
| 402 |
i2 = random_account (); |
| 403 |
value = ((unsigned int) rand () >> 3) % 10; |
| 404 |
account[i1] += value; |
| 405 |
account[i2] -= value; |
| 406 |
|
| 407 |
dbgprintf ("Mutator %p before unlock\n", gl_thread_self_pointer ()); |
| 408 |
gl_rwlock_unlock (my_rwlock); |
| 409 |
dbgprintf ("Mutator %p after unlock\n", gl_thread_self_pointer ()); |
| 410 |
|
| 411 |
yield (); |
| 412 |
} |
| 413 |
|
| 414 |
dbgprintf ("Mutator %p dying.\n", gl_thread_self_pointer ()); |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
|
| 418 |
static struct atomic_int rwlock_checker_done; |
| 419 |
|
| 420 |
static void * |
| 421 |
rwlock_checker_thread (void *arg) |
| 422 |
{ |
| 423 |
while (get_atomic_int_value (&rwlock_checker_done) == 0) |
| 424 |
{ |
| 425 |
dbgprintf ("Checker %p before check rdlock\n", gl_thread_self_pointer ()); |
| 426 |
gl_rwlock_rdlock (my_rwlock); |
| 427 |
check_accounts (); |
| 428 |
gl_rwlock_unlock (my_rwlock); |
| 429 |
dbgprintf ("Checker %p after check unlock\n", gl_thread_self_pointer ()); |
| 430 |
|
| 431 |
yield (); |
| 432 |
} |
| 433 |
|
| 434 |
dbgprintf ("Checker %p dying.\n", gl_thread_self_pointer ()); |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
static void |
| 439 |
test_rwlock (void) |
| 440 |
{ |
| 441 |
int i; |
| 442 |
gl_thread_t checkerthreads[THREAD_COUNT]; |
| 443 |
gl_thread_t threads[THREAD_COUNT]; |
| 444 |
|
| 445 |
|
| 446 |
for (i = 0; i < ACCOUNT_COUNT; i++) |
| 447 |
account[i] = 1000; |
| 448 |
init_atomic_int (&rwlock_checker_done); |
| 449 |
set_atomic_int_value (&rwlock_checker_done, 0); |
| 450 |
|
| 451 |
|
| 452 |
for (i = 0; i < THREAD_COUNT; i++) |
| 453 |
checkerthreads[i] = gl_thread_create (rwlock_checker_thread, NULL); |
| 454 |
for (i = 0; i < THREAD_COUNT; i++) |
| 455 |
threads[i] = gl_thread_create (rwlock_mutator_thread, NULL); |
| 456 |
|
| 457 |
|
| 458 |
for (i = 0; i < THREAD_COUNT; i++) |
| 459 |
gl_thread_join (threads[i], NULL); |
| 460 |
set_atomic_int_value (&rwlock_checker_done, 1); |
| 461 |
for (i = 0; i < THREAD_COUNT; i++) |
| 462 |
gl_thread_join (checkerthreads[i], NULL); |
| 463 |
check_accounts (); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
gl_recursive_lock_define_initialized(static, my_reclock) |
| 474 |
|
| 475 |
static void |
| 476 |
recshuffle (void) |
| 477 |
{ |
| 478 |
int i1, i2, value; |
| 479 |
|
| 480 |
dbgprintf ("Mutator %p before lock\n", gl_thread_self_pointer ()); |
| 481 |
gl_recursive_lock_lock (my_reclock); |
| 482 |
dbgprintf ("Mutator %p after lock\n", gl_thread_self_pointer ()); |
| 483 |
|
| 484 |
i1 = random_account (); |
| 485 |
i2 = random_account (); |
| 486 |
value = ((unsigned int) rand () >> 3) % 10; |
| 487 |
account[i1] += value; |
| 488 |
account[i2] -= value; |
| 489 |
|
| 490 |
|
| 491 |
if (((unsigned int) rand () >> 3) % 2) |
| 492 |
recshuffle (); |
| 493 |
|
| 494 |
dbgprintf ("Mutator %p before unlock\n", gl_thread_self_pointer ()); |
| 495 |
gl_recursive_lock_unlock (my_reclock); |
| 496 |
dbgprintf ("Mutator %p after unlock\n", gl_thread_self_pointer ()); |
| 497 |
} |
| 498 |
|
| 499 |
static void * |
| 500 |
reclock_mutator_thread (void *arg) |
| 501 |
{ |
| 502 |
int repeat; |
| 503 |
|
| 504 |
for (repeat = REPEAT_COUNT; repeat > 0; repeat--) |
| 505 |
{ |
| 506 |
recshuffle (); |
| 507 |
|
| 508 |
dbgprintf ("Mutator %p before check lock\n", gl_thread_self_pointer ()); |
| 509 |
gl_recursive_lock_lock (my_reclock); |
| 510 |
check_accounts (); |
| 511 |
gl_recursive_lock_unlock (my_reclock); |
| 512 |
dbgprintf ("Mutator %p after check unlock\n", gl_thread_self_pointer ()); |
| 513 |
|
| 514 |
yield (); |
| 515 |
} |
| 516 |
|
| 517 |
dbgprintf ("Mutator %p dying.\n", gl_thread_self_pointer ()); |
| 518 |
return NULL; |
| 519 |
} |
| 520 |
|
| 521 |
static struct atomic_int reclock_checker_done; |
| 522 |
|
| 523 |
static void * |
| 524 |
reclock_checker_thread (void *arg) |
| 525 |
{ |
| 526 |
while (get_atomic_int_value (&reclock_checker_done) == 0) |
| 527 |
{ |
| 528 |
dbgprintf ("Checker %p before check lock\n", gl_thread_self_pointer ()); |
| 529 |
gl_recursive_lock_lock (my_reclock); |
| 530 |
check_accounts (); |
| 531 |
gl_recursive_lock_unlock (my_reclock); |
| 532 |
dbgprintf ("Checker %p after check unlock\n", gl_thread_self_pointer ()); |
| 533 |
|
| 534 |
yield (); |
| 535 |
} |
| 536 |
|
| 537 |
dbgprintf ("Checker %p dying.\n", gl_thread_self_pointer ()); |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
|
| 541 |
static void |
| 542 |
test_recursive_lock (void) |
| 543 |
{ |
| 544 |
int i; |
| 545 |
gl_thread_t checkerthread; |
| 546 |
gl_thread_t threads[THREAD_COUNT]; |
| 547 |
|
| 548 |
|
| 549 |
for (i = 0; i < ACCOUNT_COUNT; i++) |
| 550 |
account[i] = 1000; |
| 551 |
init_atomic_int (&reclock_checker_done); |
| 552 |
set_atomic_int_value (&reclock_checker_done, 0); |
| 553 |
|
| 554 |
|
| 555 |
checkerthread = gl_thread_create (reclock_checker_thread, NULL); |
| 556 |
for (i = 0; i < THREAD_COUNT; i++) |
| 557 |
threads[i] = gl_thread_create (reclock_mutator_thread, NULL); |
| 558 |
|
| 559 |
|
| 560 |
for (i = 0; i < THREAD_COUNT; i++) |
| 561 |
gl_thread_join (threads[i], NULL); |
| 562 |
set_atomic_int_value (&reclock_checker_done, 1); |
| 563 |
gl_thread_join (checkerthread, NULL); |
| 564 |
check_accounts (); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
gl_once_define(static, fresh_once) |
| 574 |
static int ready[THREAD_COUNT]; |
| 575 |
static gl_lock_t ready_lock[THREAD_COUNT]; |
| 576 |
#if ENABLE_LOCKING |
| 577 |
static gl_rwlock_t fire_signal[REPEAT_COUNT]; |
| 578 |
#else |
| 579 |
static volatile int fire_signal_state; |
| 580 |
#endif |
| 581 |
static gl_once_t once_control; |
| 582 |
static int performed; |
| 583 |
gl_lock_define_initialized(static, performed_lock) |
| 584 |
|
| 585 |
static void |
| 586 |
once_execute (void) |
| 587 |
{ |
| 588 |
gl_lock_lock (performed_lock); |
| 589 |
performed++; |
| 590 |
gl_lock_unlock (performed_lock); |
| 591 |
} |
| 592 |
|
| 593 |
static void * |
| 594 |
once_contender_thread (void *arg) |
| 595 |
{ |
| 596 |
int id = (int) (intptr_t) arg; |
| 597 |
int repeat; |
| 598 |
|
| 599 |
for (repeat = 0; repeat <= REPEAT_COUNT; repeat++) |
| 600 |
{ |
| 601 |
|
| 602 |
gl_lock_lock (ready_lock[id]); |
| 603 |
ready[id] = 1; |
| 604 |
gl_lock_unlock (ready_lock[id]); |
| 605 |
|
| 606 |
if (repeat == REPEAT_COUNT) |
| 607 |
break; |
| 608 |
|
| 609 |
dbgprintf ("Contender %p waiting for signal for round %d\n", |
| 610 |
gl_thread_self_pointer (), repeat); |
| 611 |
#if ENABLE_LOCKING |
| 612 |
|
| 613 |
gl_rwlock_rdlock (fire_signal[repeat]); |
| 614 |
|
| 615 |
gl_rwlock_unlock (fire_signal[repeat]); |
| 616 |
#else |
| 617 |
|
| 618 |
while (fire_signal_state <= repeat) |
| 619 |
yield (); |
| 620 |
#endif |
| 621 |
dbgprintf ("Contender %p got the signal for round %d\n", |
| 622 |
gl_thread_self_pointer (), repeat); |
| 623 |
|
| 624 |
|
| 625 |
gl_once (once_control, once_execute); |
| 626 |
} |
| 627 |
|
| 628 |
return NULL; |
| 629 |
} |
| 630 |
|
| 631 |
static void |
| 632 |
test_once (void) |
| 633 |
{ |
| 634 |
int i, repeat; |
| 635 |
gl_thread_t threads[THREAD_COUNT]; |
| 636 |
|
| 637 |
|
| 638 |
for (i = 0; i < THREAD_COUNT; i++) |
| 639 |
{ |
| 640 |
ready[i] = 0; |
| 641 |
gl_lock_init (ready_lock[i]); |
| 642 |
} |
| 643 |
#if ENABLE_LOCKING |
| 644 |
for (i = 0; i < REPEAT_COUNT; i++) |
| 645 |
gl_rwlock_init (fire_signal[i]); |
| 646 |
#else |
| 647 |
fire_signal_state = 0; |
| 648 |
#endif |
| 649 |
|
| 650 |
#if ENABLE_LOCKING |
| 651 |
|
| 652 |
for (i = REPEAT_COUNT-1; i >= 0; i--) |
| 653 |
gl_rwlock_wrlock (fire_signal[i]); |
| 654 |
#endif |
| 655 |
|
| 656 |
|
| 657 |
for (i = 0; i < THREAD_COUNT; i++) |
| 658 |
threads[i] = |
| 659 |
gl_thread_create (once_contender_thread, (void *) (intptr_t) i); |
| 660 |
|
| 661 |
for (repeat = 0; repeat <= REPEAT_COUNT; repeat++) |
| 662 |
{ |
| 663 |
|
| 664 |
dbgprintf ("Main thread before synchronizing for round %d\n", repeat); |
| 665 |
for (;;) |
| 666 |
{ |
| 667 |
int ready_count = 0; |
| 668 |
for (i = 0; i < THREAD_COUNT; i++) |
| 669 |
{ |
| 670 |
gl_lock_lock (ready_lock[i]); |
| 671 |
ready_count += ready[i]; |
| 672 |
gl_lock_unlock (ready_lock[i]); |
| 673 |
} |
| 674 |
if (ready_count == THREAD_COUNT) |
| 675 |
break; |
| 676 |
yield (); |
| 677 |
} |
| 678 |
dbgprintf ("Main thread after synchronizing for round %d\n", repeat); |
| 679 |
|
| 680 |
if (repeat > 0) |
| 681 |
{ |
| 682 |
|
| 683 |
|
| 684 |
if (performed != 1) |
| 685 |
abort (); |
| 686 |
} |
| 687 |
|
| 688 |
if (repeat == REPEAT_COUNT) |
| 689 |
break; |
| 690 |
|
| 691 |
|
| 692 |
memcpy (&once_control, &fresh_once, sizeof (gl_once_t)); |
| 693 |
|
| 694 |
|
| 695 |
performed = 0; |
| 696 |
|
| 697 |
|
| 698 |
for (i = 0; i < THREAD_COUNT; i++) |
| 699 |
{ |
| 700 |
gl_lock_lock (ready_lock[i]); |
| 701 |
ready[i] = 0; |
| 702 |
gl_lock_unlock (ready_lock[i]); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
dbgprintf ("Main thread giving signal for round %d\n", repeat); |
| 707 |
#if ENABLE_LOCKING |
| 708 |
gl_rwlock_unlock (fire_signal[repeat]); |
| 709 |
#else |
| 710 |
fire_signal_state = repeat + 1; |
| 711 |
#endif |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
for (i = 0; i < THREAD_COUNT; i++) |
| 716 |
gl_thread_join (threads[i], NULL); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
int |
| 723 |
main () |
| 724 |
{ |
| 725 |
#if HAVE_DECL_ALARM |
| 726 |
|
| 727 |
|
| 728 |
int alarm_value = 600; |
| 729 |
signal (SIGALRM, SIG_DFL); |
| 730 |
alarm (alarm_value); |
| 731 |
#endif |
| 732 |
|
| 733 |
#if DO_TEST_LOCK |
| 734 |
printf ("Starting test_lock ..."); fflush (stdout); |
| 735 |
test_lock (); |
| 736 |
printf (" OK\n"); fflush (stdout); |
| 737 |
#endif |
| 738 |
#if DO_TEST_RWLOCK |
| 739 |
printf ("Starting test_rwlock ..."); fflush (stdout); |
| 740 |
test_rwlock (); |
| 741 |
printf (" OK\n"); fflush (stdout); |
| 742 |
#endif |
| 743 |
#if DO_TEST_RECURSIVE_LOCK |
| 744 |
printf ("Starting test_recursive_lock ..."); fflush (stdout); |
| 745 |
test_recursive_lock (); |
| 746 |
printf (" OK\n"); fflush (stdout); |
| 747 |
#endif |
| 748 |
#if DO_TEST_ONCE |
| 749 |
printf ("Starting test_once ..."); fflush (stdout); |
| 750 |
test_once (); |
| 751 |
printf (" OK\n"); fflush (stdout); |
| 752 |
#endif |
| 753 |
|
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
#else |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
#include <stdio.h> |
| 762 |
|
| 763 |
int |
| 764 |
main () |
| 765 |
{ |
| 766 |
fputs ("Skipping test: multithreading not enabled\n", stderr); |
| 767 |
return 77; |
| 768 |
} |
| 769 |
|
| 770 |
#endif |