1/* 2 * Copyright (c) 2004, Bull S.A.. All rights reserved. 3 * Created by: Sebastien Decugis 4 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write the Free Software Foundation, Inc., 59 15 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 16 17 18 * This file is a stress test for the function pthread_cond_wait. 19 * 20 *It aims to check the following assertion: 21 * When inside the function, the thread releases the mutex 22 * before waiting for the conditionnal variable. 23 * Those two operations are atomic in the mean that 24 * no other thread can gain access to the mutex 25 * then signal (or broadcast) the condition 26 * without the blocked thread behaving as if 27 * this signal (or broadcast) had happened 28 * after it blocked on the conditionnal variable. 29 30 * The steps are: 31 * -> Create N mutex & N cond vars with different attributes 32 * -> Create N threads A, which 33 * -> lock the mutex 34 * -> create a thread B, which 35 * -> locks the mutex 36 * -> while the boolean is false, 37 * -> broadcasts the condvar 38 * -> sets an alarm timer 39 * -> waits the condition 40 * -> broadcast the condvar 41 * -> unlock the mutex 42 * -> while the boolean is false, 43 * -> set an alarm timer 44 * -> wait the condvar 45 * -> signals the condvar 46 * -> unlock the mutex 47 * -> joins the thread B 48 * -> sets the boolean True when it receives SIGUSR1 49 * -> report FAILED if the alarm timer expires (meaning one of the signal was lost). 50 * -> joins the N threads A. 51 * 52 * To test for pshared primitive, thread B can be in another process. 53 */ 54 55 56 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 57 #define _POSIX_C_SOURCE 200112L 58 59 /* We need the XSI extention for the mutex attributes 60 and the mkstemp() routine */ 61#ifndef WITHOUT_XOPEN 62 #define _XOPEN_SOURCE 600 63#endif 64/********************************************************************************************/ 65/****************************** standard includes *****************************************/ 66/********************************************************************************************/ 67 #include <pthread.h> 68 #include <stdarg.h> 69 #include <stdio.h> 70 #include <stdlib.h> 71 #include <unistd.h> 72 73 #include <errno.h> 74 #include <signal.h> 75 #include <sys/wait.h> 76 #include <sys/mman.h> 77 #include <string.h> 78 #include <time.h> 79 80/********************************************************************************************/ 81/****************************** Test framework *****************************************/ 82/********************************************************************************************/ 83 #include "testfrmw.h" 84 #include "testfrmw.c" 85 /* This header is responsible for defining the following macros: 86 * UNRESOLVED(ret, descr); 87 * where descr is a description of the error and ret is an int (error code for example) 88 * FAILED(descr); 89 * where descr is a short text saying why the test has failed. 90 * PASSED(); 91 * No parameter. 92 * 93 * Both three macros shall terminate the calling process. 94 * The testcase shall not terminate in any other maneer. 95 * 96 * The other file defines the functions 97 * void output_init() 98 * void output(char * string, ...) 99 * 100 * Those may be used to output information. 101 */ 102 103/********************************************************************************************/ 104/********************************** Configuration ******************************************/ 105/********************************************************************************************/ 106#ifndef SCALABILITY_FACTOR 107#define SCALABILITY_FACTOR 1 108#endif 109#ifndef VERBOSE 110#define VERBOSE 1 111#endif 112 113/* Number of children for each test scenario */ 114#define NCHILDREN (5) 115 116#define TIMEOUT 120 117 118#ifndef WITHOUT_ALTCLK 119#define USE_ALTCLK /* make tests with MONOTONIC CLOCK if supported */ 120#endif 121 122/********************************************************************************************/ 123/*********************************** Test case *****************************************/ 124/********************************************************************************************/ 125 126#ifdef WITHOUT_XOPEN 127/* We define those to avoid compilation errors, but they won't be used */ 128#define PTHREAD_MUTEX_DEFAULT 0 129#define PTHREAD_MUTEX_NORMAL 0 130#define PTHREAD_MUTEX_ERRORCHECK 0 131#define PTHREAD_MUTEX_RECURSIVE 0 132 133#endif 134 135struct _scenar 136{ 137 int m_type; /* Mutex type to use */ 138 int mc_pshared; /* 0: mutex and cond are process-private (default) ~ !0: Both are process-shared, if supported */ 139 int c_clock; /* 0: cond uses the default clock. ~ !0: Cond uses monotonic clock, if supported. */ 140 int fork; /* 0: Test between threads. ~ !0: Test across processes, if supported (mmap) */ 141 char * descr; /* Case description */ 142} 143scenarii[] = 144{ 145 {PTHREAD_MUTEX_DEFAULT, 0, 0, 0, "Default mutex"} 146 ,{PTHREAD_MUTEX_NORMAL, 0, 0, 0, "Normal mutex"} 147 ,{PTHREAD_MUTEX_ERRORCHECK, 0, 0, 0, "Errorcheck mutex"} 148 ,{PTHREAD_MUTEX_RECURSIVE, 0, 0, 0, "Recursive mutex"} 149 150 ,{PTHREAD_MUTEX_DEFAULT, 1, 0, 0, "PShared default mutex"} 151 ,{PTHREAD_MUTEX_NORMAL, 1, 0, 0, "Pshared normal mutex"} 152 ,{PTHREAD_MUTEX_ERRORCHECK, 1, 0, 0, "Pshared errorcheck mutex"} 153 ,{PTHREAD_MUTEX_RECURSIVE, 1, 0, 0, "Pshared recursive mutex"} 154 155 ,{PTHREAD_MUTEX_DEFAULT, 1, 0, 1, "Pshared default mutex across processes"} 156 ,{PTHREAD_MUTEX_NORMAL, 1, 0, 1, "Pshared normal mutex across processes"} 157 ,{PTHREAD_MUTEX_ERRORCHECK, 1, 0, 1, "Pshared errorcheck mutex across processes"} 158 ,{PTHREAD_MUTEX_RECURSIVE, 1, 0, 1, "Pshared recursive mutex across processes"} 159 160#ifdef USE_ALTCLK 161 ,{PTHREAD_MUTEX_DEFAULT, 1, 1, 1, "Pshared default mutex and alt clock condvar across processes"} 162 ,{PTHREAD_MUTEX_NORMAL, 1, 1, 1, "Pshared normal mutex and alt clock condvar across processes"} 163 ,{PTHREAD_MUTEX_ERRORCHECK, 1, 1, 1, "Pshared errorcheck mutex and alt clock condvar across processes"} 164 ,{PTHREAD_MUTEX_RECURSIVE, 1, 1, 1, "Pshared recursive mutex and alt clock condvar across processes"} 165 166 ,{PTHREAD_MUTEX_DEFAULT, 0, 1, 0, "Default mutex and alt clock condvar"} 167 ,{PTHREAD_MUTEX_NORMAL, 0, 1, 0, "Normal mutex and alt clock condvar"} 168 ,{PTHREAD_MUTEX_ERRORCHECK, 0, 1, 0, "Errorcheck mutex and alt clock condvar"} 169 ,{PTHREAD_MUTEX_RECURSIVE, 0, 1, 0, "Recursive mutex and alt clock condvar"} 170 171 ,{PTHREAD_MUTEX_DEFAULT, 1, 1, 0, "PShared default mutex and alt clock condvar"} 172 ,{PTHREAD_MUTEX_NORMAL, 1, 1, 0, "Pshared normal mutex and alt clock condvar"} 173 ,{PTHREAD_MUTEX_ERRORCHECK, 1, 1, 0, "Pshared errorcheck mutex and alt clock condvar"} 174 ,{PTHREAD_MUTEX_RECURSIVE, 1, 1, 0, "Pshared recursive mutex and alt clock condvar"} 175#endif 176}; 177#define NSCENAR (sizeof(scenarii)/sizeof(scenarii[0])) 178 179#define NTOT (NSCENAR * SCALABILITY_FACTOR * NCHILDREN) 180 181struct childdata 182{ 183 pthread_mutex_t mtx; 184 pthread_cond_t cnd; 185 int fork; 186 int * pBool; 187}; 188 189typedef struct 190{ 191 struct childdata cd[NTOT]; 192 int boolean; 193} testdata_t; 194 195pthread_attr_t ta; 196 197/*** 198 * The grand child function (either sub-thread or sub-process) 199 */ 200void * threaded_B (void * arg) 201{ 202 int ret; 203 struct childdata * cd = (struct childdata *)arg; 204 205 ret = pthread_mutex_lock(&(cd->mtx)); 206 if (ret != 0) { UNRESOLVED(ret, "[gchild] Unable to lock mutex"); } 207 208 while (*(cd->pBool) == 0) 209 { 210 ret = pthread_cond_broadcast(&(cd->cnd)); 211 if (ret != 0) { UNRESOLVED(ret, "[gchild] Broadcast failed"); } 212 213 alarm(TIMEOUT); // even if we are a sub-process, the main process will timeout too 214 215 ret = pthread_cond_wait(&(cd->cnd), &(cd->mtx)); 216 if (ret != 0) { UNRESOLVED(ret, "[gchild] Failed to wait the cond"); } 217 } 218 219 /* We shall broadcast again to be sure the parent is not hung */ 220 ret = pthread_cond_broadcast(&(cd->cnd)); 221 if (ret != 0) { UNRESOLVED(ret, "[gchild] Broadcast failed"); } 222 223 ret = pthread_mutex_unlock(&(cd->mtx)); 224 if (ret != 0) { UNRESOLVED(ret, "[gchild] Failed to finally release the mutex"); } 225 226 return NULL; 227} 228 229/*** 230 * The child function (always in the main process) 231 */ 232void * threaded_A (void * arg) 233{ 234 struct childdata * cd = (struct childdata *)arg; 235 int ret, status; 236 pid_t child_p=0, wrc; 237 pthread_t child_t; 238 239 ret = pthread_mutex_lock(&(cd->mtx)); 240 if (ret != 0) { UNRESOLVED(ret, "[child] Unable to lock mutex"); } 241 242 /* Create the grand child */ 243 if (cd->fork == 0) 244 { 245 ret = pthread_create(&child_t, &ta, threaded_B, arg); 246 if (ret != 0) { UNRESOLVED(ret, "[child] Failed to create a grand child thread"); } 247 } 248 else 249 { 250 child_p= fork(); 251 if (child_p == -1) { UNRESOLVED(ret, "[child] Failed to create a grand child proces"); } 252 253 if (child_p == 0) /* grand child */ 254 { 255 threaded_B(arg); 256 exit(0); 257 } 258 } 259 260 while (*(cd->pBool) == 0) 261 { 262 alarm(TIMEOUT); 263 264 ret = pthread_cond_wait(&(cd->cnd), &(cd->mtx)); 265 if (ret != 0) { UNRESOLVED(ret, "[child] Failed to wait the cond"); } 266 267 ret = pthread_cond_signal(&(cd->cnd)); 268 if (ret != 0) { UNRESOLVED(ret, "[child] Signal failed"); } 269 } 270 271 ret = pthread_mutex_unlock(&(cd->mtx)); 272 if (ret != 0) { UNRESOLVED(ret, "[gchild] Failed to finally release the mutex"); } 273 274 /* Wait for the grand child termination */ 275 if (cd->fork == 0) 276 { 277 ret = pthread_join(child_t, NULL); 278 if (ret != 0) { UNRESOLVED(ret, "[child] Failed to join a grand child thread"); } 279 } 280 else 281 { 282 wrc = waitpid(child_p, &status, 0); 283 if (wrc != child_p) 284 { 285 output("Expected pid: %i. Got %i\n", (int)child_p, (int)wrc); 286 UNRESOLVED(errno, "Waitpid failed"); 287 } 288 289 if (WIFSIGNALED(status)) 290 { 291 output("Child process killed with signal %d\n",WTERMSIG(status)); 292 UNRESOLVED( 0 , "Child process was killed"); 293 } 294 295 if (WIFEXITED(status)) 296 { 297 ret = WEXITSTATUS(status); 298 } 299 else 300 { 301 UNRESOLVED( 0, "Child process was neither killed nor exited"); 302 } 303 } 304 305 /* the end */ 306 return NULL; 307} 308 309int * pBoolean = NULL; 310 311/*** 312 * Signal handler 313 */ 314void sighdl(int sig) 315{ 316 if (sig == SIGUSR1) 317 { 318 #if VERBOSE > 1 319 output("Received the USR1 signal; stopping everything\n"); 320 #endif 321 *pBoolean = 1; 322 } 323 if (sig == SIGALRM) 324 { 325 FAILED("A wait operation timed out. A condition signaling was lost."); 326 } 327} 328 329int main(int argc, char * argv[]) 330{ 331 int ret, i, j; 332 struct sigaction sa; 333 334 pthread_mutexattr_t ma; 335 pthread_condattr_t ca; 336 clockid_t cid = CLOCK_REALTIME; 337 338 testdata_t * td; 339 testdata_t alternativ; 340 341 int do_fork; 342 long pshared, monotonic, cs, mf; 343 344 pthread_t th[NTOT]; 345 346 output_init(); 347 348 pshared = sysconf(_SC_THREAD_PROCESS_SHARED); 349 cs = sysconf(_SC_CLOCK_SELECTION); 350 monotonic = sysconf(_SC_MONOTONIC_CLOCK); 351 mf =sysconf(_SC_MAPPED_FILES); 352 353 #if VERBOSE > 0 354 output("Test starting\n"); 355 output("System abilities:\n"); 356 output(" TPS : %li\n", pshared); 357 output(" CS : %li\n", cs); 358 output(" MON : %li\n", monotonic); 359 output(" MF : %li\n", mf); 360 if ((mf < 0) || (pshared < 0)) 361 output("Process-shared attributes won't be tested\n"); 362 if ((cs < 0) || (monotonic < 0)) 363 output("Alternative clock won't be tested\n"); 364 #endif 365 366 /* We are not interested in testing the clock if we have no other clock available.. */ 367 if (monotonic < 0) 368 cs = -1; 369 370#ifndef USE_ALTCLK 371 if (cs > 0) 372 output("Implementation supports the MONOTONIC CLOCK but option is disabled in test.\n"); 373#endif 374 375/********** 376 * Allocate space for the testdata structure 377 */ 378 if (mf < 0) 379 { 380 /* Cannot mmap a file, we use an alternative method */ 381 td = &alternativ; 382 pshared = -1; /* We won't do this testing anyway */ 383 #if VERBOSE > 0 384 output("Testdata allocated in the process memory.\n"); 385 #endif 386 } 387 else 388 { 389 /* We will place the test data in a mmaped file */ 390 char filename[] = "/tmp/cond_timedwait_st1-XXXXXX"; 391 size_t sz, ps; 392 void * mmaped; 393 int fd; 394 char * tmp; 395 396 /* We now create the temp files */ 397 fd = mkstemp(filename); 398 if (fd == -1) 399 { UNRESOLVED(errno, "Temporary file could not be created"); } 400 401 /* and make sure the file will be deleted when closed */ 402 unlink(filename); 403 404 #if VERBOSE > 1 405 output("Temp file created (%s).\n", filename); 406 #endif 407 408 ps = (size_t)sysconf(_SC_PAGESIZE); 409 sz= ((sizeof(testdata_t) / ps) + 1) * ps; /* # pages needed to store the testdata */ 410 411 tmp = calloc( 1 , sz); 412 if (tmp == NULL) 413 { UNRESOLVED(errno, "Memory allocation failed"); } 414 415 /* Write the data to the file. */ 416 if (write (fd, tmp, sz) != (ssize_t) sz) 417 { UNRESOLVED(sz, "Writting to the file failed"); } 418 419 free(tmp); 420 421 /* Now we can map the file in memory */ 422 mmaped = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 423 if (mmaped == MAP_FAILED) 424 { UNRESOLVED(errno, "mmap failed"); } 425 426 td = (testdata_t *) mmaped; 427 428 /* Our datatest structure is now in shared memory */ 429 #if VERBOSE > 1 430 output("Testdata allocated in shared memory (%ib).\n", sizeof(testdata_t)); 431 #endif 432 } 433 434 /* Init the signal handler variable */ 435 pBoolean = &(td->boolean); 436 437 /* Init the structure */ 438 for ( i=0; i< NSCENAR ; i++) 439 { 440 #if VERBOSE > 1 441 output("[parent] Preparing attributes for: %s\n", scenarii[i].descr); 442 #ifdef WITHOUT_XOPEN 443 output("[parent] Mutex attributes DISABLED -> not used\n"); 444 #endif 445 #endif 446 /* set / reset everything */ 447 do_fork=0; 448 ret = pthread_mutexattr_init(&ma); 449 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object"); } 450 ret = pthread_condattr_init(&ca); 451 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to initialize the cond attribute object"); } 452 453 #ifndef WITHOUT_XOPEN 454 /* Set the mutex type */ 455 ret = pthread_mutexattr_settype(&ma, scenarii[i].m_type); 456 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set mutex type"); } 457 #if VERBOSE > 1 458 output("[parent] Mutex type : %i\n", scenarii[i].m_type); 459 #endif 460 #endif 461 462 /* Set the pshared attributes, if supported */ 463 if ((pshared > 0) && (scenarii[i].mc_pshared != 0)) 464 { 465 ret = pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED); 466 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set the mutex process-shared"); } 467 ret = pthread_condattr_setpshared(&ca, PTHREAD_PROCESS_SHARED); 468 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set the cond var process-shared"); } 469 #if VERBOSE > 1 470 output("[parent] Mutex & cond are process-shared\n"); 471 #endif 472 } 473 #if VERBOSE > 1 474 else { 475 output("[parent] Mutex & cond are process-private\n"); 476 } 477 #endif 478 479 /* Set the alternative clock, if supported */ 480 #ifdef USE_ALTCLK 481 if ((cs > 0) && (scenarii[i].c_clock != 0)) 482 { 483 ret = pthread_condattr_setclock(&ca, CLOCK_MONOTONIC); 484 if (ret != 0) { UNRESOLVED(ret, "[parent] Unable to set the monotonic clock for the cond"); } 485 #if VERBOSE > 1 486 output("[parent] Cond uses the Monotonic clock\n"); 487 #endif 488 } 489 #if VERBOSE > 1 490 else { 491 output("[parent] Cond uses the default clock\n"); 492 } 493 #endif 494 ret = pthread_condattr_getclock(&ca, &cid); 495 if (ret != 0) { UNRESOLVED(ret, "Unable to get clock from cond attr"); } 496 #endif 497 498 /* Tell whether the test will be across processes */ 499 if ((pshared > 0) && (scenarii[i].fork != 0)) 500 { 501 do_fork = 1; 502 #if VERBOSE > 1 503 output("[parent] Child will be a new process\n"); 504 #endif 505 } 506 #if VERBOSE > 1 507 else { 508 output("[parent] Child will be a new thread\n"); 509 } 510 #endif 511 512 /* Initialize all the mutex and condvars which uses those attributes */ 513 for (j=0; j < SCALABILITY_FACTOR * NCHILDREN; j++) 514 { 515 #define CD (td->cd[i+(j*NSCENAR)]) 516 CD.pBool = &(td->boolean); 517 CD.fork = do_fork; 518 CD.cid = cid; 519 520 /* initialize the condvar */ 521 ret = pthread_cond_init(&(CD.cnd), &ca); 522 if (ret != 0) { UNRESOLVED(ret, "[parent] Cond init failed"); } 523 524 /* initialize the mutex */ 525 ret = pthread_mutex_init(&(CD.mtx), &ma); 526 if (ret != 0) { UNRESOLVED(ret, "[parent] Mutex init failed"); } 527 #undef CD 528 } 529 530 ret = pthread_condattr_destroy(&ca); 531 if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the cond var attribute object"); } 532 533 ret = pthread_mutexattr_destroy(&ma); 534 if (ret != 0) { UNRESOLVED(ret, "Failed to destroy the mutex attribute object"); } 535 } 536 #if VERBOSE > 1 537 output("[parent] All condvars & mutex are ready\n"); 538 #endif 539 540 ret = pthread_attr_init(&ta); 541 if (ret != 0) { UNRESOLVED(ret, "[parent] Failed to initialize a thread attribute object"); } 542 ret = pthread_attr_setstacksize(&ta, sysconf(_SC_THREAD_STACK_MIN)); 543 if (ret != 0) { UNRESOLVED(ret, "[parent] Failed to set thread stack size"); } 544 545 546 sigemptyset (&sa.sa_mask); 547 sa.sa_flags = 0; 548 sa.sa_handler = sighdl; 549 if ((ret = sigaction (SIGUSR1, &sa, NULL))) 550 { UNRESOLVED(ret, "Unable to register signal handler"); } 551 if ((ret = sigaction (SIGALRM, &sa, NULL))) 552 { UNRESOLVED(ret, "Unable to register signal handler"); } 553 #if VERBOSE > 1 554 output("[parent] Signal handler registered\n"); 555 #endif 556 557 for (i=0; i<NTOT; i++) 558 { 559 ret = pthread_create( &th[i], &ta, threaded_A, &(td->cd[i])); 560 /* In case of failure we can exit; the child processes will die after a while */ 561 if (ret != 0) { UNRESOLVED(ret, "[Parent] Failed to create a thread"); } 562 563 #if VERBOSE > 1 564 if ((i % 10) == 0) 565 output("[parent] %i threads created...\n", i+1); 566 #endif 567 } 568 569 #if VERBOSE > 1 570 output("[parent] All %i threads are running...\n", NTOT); 571 #endif 572 573 for (i=0; i<NTOT; i++) 574 { 575 ret = pthread_join( th[i], NULL); 576 if (ret != 0) { UNRESOLVED(ret, "[Parent] Failed to join a thread"); } 577 } 578 579 /* Destroy everything */ 580 for ( i=0; i< NTOT ; i++) 581 { 582 /* destroy the condvar */ 583 ret = pthread_cond_destroy(&(td->cd[i].cnd)); 584 if (ret != 0) { UNRESOLVED(ret, "[parent] Cond destroy failed"); } 585 586 /* destroy the mutex */ 587 ret = pthread_mutex_init(&(td->cd[i].mtx), &ma); 588 if (ret != 0) { UNRESOLVED(ret, "[parent] Mutex destroy failed"); } 589 } 590 591 #if VERBOSE > 0 592 output("Test passed\n"); 593 #endif 594 595 PASSED; 596} 597 598 599