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.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17 * This file is a stress test for the function pthread_cond_timedwait.
18 *
19 * It aims to check the following assertion:
20 * When a cancel request unblocks the thread,
21 * it must not consume any pending condition signal request.
22
23 * The steps are:
24 * -> Create a bunch of threads waiting on a condvar.
25 * -> At the same time (using a barrier) one thread is canceled and the condition is signaled.
26 * -> Test checks that the cond signaling was not lost (at least one thread must have woken cleanly).
27 * -> Then everything is cleaned up and started again.
28
29 */
30
31 /********************************************************************************************/
32 /****************************** standard includes *****************************************/
33 /********************************************************************************************/
34 #include <pthread.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include <errno.h>
41 #include <signal.h>
42 #include <string.h>
43 #include <time.h>
44
45 /********************************************************************************************/
46 /****************************** Test framework *****************************************/
47 /********************************************************************************************/
48 #include "testfrmw.h"
49 #include "testfrmw.c"
50 /* This header is responsible for defining the following macros:
51 * UNRESOLVED(ret, descr);
52 * where descr is a description of the error and ret is an int (error code for example)
53 * FAILED(descr);
54 * where descr is a short text saying why the test has failed.
55 * PASSED();
56 * No parameter.
57 *
58 * Both three macros shall terminate the calling process.
59 * The testcase shall not terminate in any other maneer.
60 *
61 * The other file defines the functions
62 * void output_init()
63 * void output(char * string, ...)
64 *
65 * Those may be used to output information.
66 */
67
68 /********************************************************************************************/
69 /********************************** Configuration ******************************************/
70 /********************************************************************************************/
71 #ifndef SCALABILITY_FACTOR
72 #define SCALABILITY_FACTOR 1
73 #endif
74 #ifndef VERBOSE
75 #define VERBOSE 1
76 #endif
77
78 /* Size of the "bunch" of threads -- the real number will be 2 more threads per scenarii */
79 #define NCHILDREN (20)
80
81 #define TIMEOUT (60)
82
83 #ifndef WITHOUT_ALTCLK
84 #define USE_ALTCLK /* make tests with MONOTONIC CLOCK if supported */
85 #endif
86
87 /********************************************************************************************/
88 /*********************************** Test case *****************************************/
89 /********************************************************************************************/
90
91 #ifdef WITHOUT_XOPEN
92 /* We define those to avoid compilation errors, but they won't be used */
93 #define PTHREAD_MUTEX_DEFAULT 0
94 #define PTHREAD_MUTEX_NORMAL 0
95 #define PTHREAD_MUTEX_ERRORCHECK 0
96 #define PTHREAD_MUTEX_RECURSIVE 0
97
98 #endif
99
100 struct _scenar {
101 int m_type; /* Mutex type to use */
102 int mc_pshared; /* 0: mutex and cond are process-private (default) ~ !0: Both are process-shared, if supported */
103 int c_clock; /* 0: cond uses the default clock. ~ !0: Cond uses monotonic clock, if supported. */
104 int fork; /* 0: Test between threads. ~ !0: Test across processes, if supported (mmap) */
105 char *descr; /* Case description */
106 } scenarii[] = {
107 {
108 PTHREAD_MUTEX_DEFAULT, 0, 0, 0, "Default mutex"}
109 , {
110 PTHREAD_MUTEX_NORMAL, 0, 0, 0, "Normal mutex"}
111 , {
112 PTHREAD_MUTEX_ERRORCHECK, 0, 0, 0, "Errorcheck mutex"}
113 , {
114 PTHREAD_MUTEX_RECURSIVE, 0, 0, 0, "Recursive mutex"}
115
116 , {
117 PTHREAD_MUTEX_DEFAULT, 1, 0, 0, "PShared default mutex"}
118 , {
119 PTHREAD_MUTEX_NORMAL, 1, 0, 0, "Pshared normal mutex"}
120 , {
121 PTHREAD_MUTEX_ERRORCHECK, 1, 0, 0, "Pshared errorcheck mutex"}
122 , {
123 PTHREAD_MUTEX_RECURSIVE, 1, 0, 0, "Pshared recursive mutex"}
124
125 , {
126 PTHREAD_MUTEX_DEFAULT, 1, 0, 1,
127 "Pshared default mutex across processes"}
128 , {
129 PTHREAD_MUTEX_NORMAL, 1, 0, 1,
130 "Pshared normal mutex across processes"}
131 , {
132 PTHREAD_MUTEX_ERRORCHECK, 1, 0, 1,
133 "Pshared errorcheck mutex across processes"}
134 , {
135 PTHREAD_MUTEX_RECURSIVE, 1, 0, 1,
136 "Pshared recursive mutex across processes"}
137
138 #ifdef USE_ALTCLK
139 , {
140 PTHREAD_MUTEX_DEFAULT, 1, 1, 1,
141 "Pshared default mutex and alt clock condvar across processes"}
142 , {
143 PTHREAD_MUTEX_NORMAL, 1, 1, 1,
144 "Pshared normal mutex and alt clock condvar across processes"}
145 , {
146 PTHREAD_MUTEX_ERRORCHECK, 1, 1, 1,
147 "Pshared errorcheck mutex and alt clock condvar across processes"}
148 , {
149 PTHREAD_MUTEX_RECURSIVE, 1, 1, 1,
150 "Pshared recursive mutex and alt clock condvar across processes"}
151
152 , {
153 PTHREAD_MUTEX_DEFAULT, 0, 1, 0,
154 "Default mutex and alt clock condvar"}
155 , {
156 PTHREAD_MUTEX_NORMAL, 0, 1, 0,
157 "Normal mutex and alt clock condvar"}
158 , {
159 PTHREAD_MUTEX_ERRORCHECK, 0, 1, 0,
160 "Errorcheck mutex and alt clock condvar"}
161 , {
162 PTHREAD_MUTEX_RECURSIVE, 0, 1, 0,
163 "Recursive mutex and alt clock condvar"}
164
165 , {
166 PTHREAD_MUTEX_DEFAULT, 1, 1, 0,
167 "PShared default mutex and alt clock condvar"}
168 , {
169 PTHREAD_MUTEX_NORMAL, 1, 1, 0,
170 "Pshared normal mutex and alt clock condvar"}
171 , {
172 PTHREAD_MUTEX_ERRORCHECK, 1, 1, 0,
173 "Pshared errorcheck mutex and alt clock condvar"}
174 , {
175 PTHREAD_MUTEX_RECURSIVE, 1, 1, 0,
176 "Pshared recursive mutex and alt clock condvar"}
177 #endif
178 };
179
180 #define NSCENAR (sizeof(scenarii)/sizeof(scenarii[0]))
181
182 /* This is the shared structure for all threads related to the same condvar */
183 struct celldata {
184 pthread_t workers[NCHILDREN * SCALABILITY_FACTOR + 2];
185 pthread_t signaler;
186
187 pthread_barrier_t bar;
188 pthread_mutex_t mtx;
189 pthread_cond_t cnd;
190 clockid_t cid;
191
192 int boolean;
193 int count;
194
195 long canceled;
196 long cancelfailed;
197 long cnttotal;
198 } cells[NSCENAR * SCALABILITY_FACTOR];
199
200 char do_it = 1;
201 pthread_attr_t ta;
202
cleanup(void * arg)203 void cleanup(void *arg)
204 {
205 int ret;
206 struct celldata *cd = (struct celldata *)arg;
207
208 /* Unlock the mutex */
209 ret = pthread_mutex_unlock(&(cd->mtx));
210 if (ret != 0) {
211 UNRESOLVED(ret, "Failed to unlock mutex in cancel handler");
212 }
213
214 }
215
worker(void * arg)216 void *worker(void *arg)
217 {
218 int ret;
219 struct celldata *cd = (struct celldata *)arg;
220 struct timespec ts;
221
222 /* lock the mutex */
223 ret = pthread_mutex_lock(&(cd->mtx));
224 if (ret != 0) {
225 UNRESOLVED(ret, "Unable to lock mutex in worker");
226 }
227
228 /* Tell the cellmaster we are ready (count++) */
229 cd->count += 1;
230
231 /* Timeout = now + TIMEOUT */
232 ret = clock_gettime(cd->cid, &ts);
233 if (ret != 0) {
234 UNRESOLVED(errno, "Gettime failed");
235 }
236 ts.tv_sec += TIMEOUT * SCALABILITY_FACTOR;
237
238 /* register cleanup handler */
239 pthread_cleanup_push(cleanup, arg);
240
241 do {
242 /* cond timedwait (while boolean == false) */
243 ret = pthread_cond_timedwait(&(cd->cnd), &(cd->mtx), &ts);
244
245 /* if timeout => failed (lost signal) */
246 if (ret == ETIMEDOUT) {
247 FAILED
248 ("Timeout occured. A condition signal was probably lost.");
249 }
250
251 if (ret != 0) {
252 UNRESOLVED(ret, "Cond timedwait failed");
253 }
254
255 } while (cd->boolean == 0);
256
257 /* broadcast the condition */
258 ret = pthread_cond_broadcast(&(cd->cnd));
259 if (ret != 0) {
260 UNRESOLVED(ret, "Broadcasting the condition failed");
261 }
262
263 /* unregister the cleanup */
264 pthread_cleanup_pop(0);
265
266 /* unlock the mutex */
267 ret = pthread_mutex_unlock(&(cd->mtx));
268 if (ret != 0) {
269 UNRESOLVED(ret, "Unable to unlock the mutex");
270 }
271
272 return NULL;
273 }
274
signaler(void * arg)275 void *signaler(void *arg)
276 {
277 int ret;
278 struct celldata *cd = (struct celldata *)arg;
279
280 /* Lock the mutex if required */
281 if (cd->boolean == -1) {
282 ret = pthread_mutex_lock(&(cd->mtx));
283 if (ret != 0) {
284 UNRESOLVED(ret, "mutex lock failed in signaler");
285 }
286 }
287
288 /* wait the barrier */
289 ret = pthread_barrier_wait(&(cd->bar));
290 if ((ret != 0) && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
291 UNRESOLVED(ret, "Barrier wait failed");
292 }
293
294 /* signal the cond */
295 ret = pthread_cond_signal(&(cd->cnd));
296 if (ret != 0) {
297 UNRESOLVED(ret, "Signaling the cond failed");
298 }
299
300 /* Unlock the mutex if required */
301 if (cd->boolean == -1) {
302 ret = pthread_mutex_unlock(&(cd->mtx));
303 if (ret != 0) {
304 UNRESOLVED(ret, "mutex unlock failed in signaler");
305 }
306 }
307
308 return NULL;
309 }
310
cellmanager(void * arg)311 void *cellmanager(void *arg)
312 {
313 int ret, i;
314 struct celldata *cd = (struct celldata *)arg;
315 struct timespec ts;
316 int randval;
317 void *w_ret;
318
319 cd->canceled = 0;
320 cd->cancelfailed = 0;
321 cd->cnttotal = 0;
322
323 /* while do_it */
324 while (do_it) {
325 /* Initialize some stuff */
326 cd->boolean = 0;
327 cd->count = 0;
328 cd->cnttotal += 1;
329
330 /* create the workers */
331 for (i = 0; i < NCHILDREN * SCALABILITY_FACTOR + 2; i++) {
332 ret =
333 pthread_create(&(cd->workers[i]), &ta, worker, arg);
334 if (ret != 0) {
335 UNRESOLVED(ret,
336 "Unable to create enough threads");
337 }
338 }
339
340 /* choose a (pseudo) random thread to cancel */
341 ret = clock_gettime(cd->cid, &ts);
342 if (ret != 0) {
343 UNRESOLVED(errno, "Failed to read clock");
344 }
345 randval =
346 (ts.tv_sec +
347 (ts.tv_nsec >> 10)) % (NCHILDREN * SCALABILITY_FACTOR + 2);
348
349 /* wait for the workers to be ready */
350 do {
351 ret = pthread_mutex_lock(&(cd->mtx));
352 if (ret != 0) {
353 UNRESOLVED(ret, "Mutex lock failed");
354 }
355
356 i = cd->count;
357
358 ret = pthread_mutex_unlock(&(cd->mtx));
359 if (ret != 0) {
360 UNRESOLVED(ret, "Mutex unlock failed");
361 }
362 } while (i < NCHILDREN * SCALABILITY_FACTOR + 2);
363
364 /* Set the boolean (1 => no lock in signaler; -1 => lock) */
365 cd->boolean = (ts.tv_sec & 1) ? -1 : 1;
366
367 /* create the signaler */
368 ret = pthread_create(&(cd->signaler), &ta, signaler, arg);
369 if (ret != 0) {
370 UNRESOLVED(ret, "Failed to create signaler thread");
371 }
372
373 /* wait the barrier */
374 ret = pthread_barrier_wait(&(cd->bar));
375 if ((ret != 0) && (ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
376 UNRESOLVED(ret, "Failed to wait for the barrier");
377 }
378
379 /* cancel the chosen thread */
380 ret = pthread_cancel(cd->workers[randval]);
381
382 /* it is possible the thread is already terminated -- so we don't stop on error */
383 if (ret != 0) {
384 #if VERBOSE > 2
385 output("%d\n", randval);
386 #endif
387 cd->cancelfailed += 1;
388 }
389
390 /* join every threads */
391 ret = pthread_join(cd->signaler, NULL);
392 if (ret != 0) {
393 UNRESOLVED(ret, "Failed to join the signaler thread");
394 }
395
396 for (i = 0; i < NCHILDREN * SCALABILITY_FACTOR + 2; i++) {
397 ret = pthread_join(cd->workers[i], &w_ret);
398 if (ret != 0) {
399 UNRESOLVED(ret, "Unable to join a worker");
400 }
401 if (w_ret == PTHREAD_CANCELED)
402 cd->canceled += 1;
403 }
404 }
405
406 return NULL;
407 }
408
sighdl(int sig)409 void sighdl(int sig)
410 {
411 /* do_it = 0 */
412 do {
413 do_it = 0;
414 }
415 while (do_it);
416 }
417
main(int argc,char * argv[])418 int main(int argc, char *argv[])
419 {
420 int ret, i, j;
421 struct sigaction sa;
422
423 pthread_mutexattr_t ma;
424 pthread_condattr_t ca;
425 clockid_t cid = CLOCK_REALTIME;
426 long canceled = 0;
427 long cancelfailed = 0;
428 long cnttotal = 0;
429
430 long pshared, monotonic, cs;
431
432 pthread_t mngrs[NSCENAR * SCALABILITY_FACTOR];
433
434 output_init();
435
436 /* check the system abilities */
437 pshared = sysconf(_SC_THREAD_PROCESS_SHARED);
438 cs = sysconf(_SC_CLOCK_SELECTION);
439 monotonic = sysconf(_SC_MONOTONIC_CLOCK);
440
441 #if VERBOSE > 0
442 output("Test starting\n");
443 output("System abilities:\n");
444 output(" TPS : %li\n", pshared);
445 output(" CS : %li\n", cs);
446 output(" MON : %li\n", monotonic);
447 if ((cs < 0) || (monotonic < 0))
448 output("Alternative clock won't be tested\n");
449 #endif
450
451 if (monotonic < 0)
452 cs = -1;
453
454 #ifndef USE_ALTCLK
455 if (cs > 0)
456 output
457 ("Implementation supports the MONOTONIC CLOCK but option is disabled in test.\n");
458 #endif
459
460 /* Initialize the celldatas according to scenarii */
461 for (i = 0; i < NSCENAR; i++) {
462 #if VERBOSE > 1
463 output("[parent] Preparing attributes for: %s\n",
464 scenarii[i].descr);
465 #ifdef WITHOUT_XOPEN
466 output("[parent] Mutex attributes DISABLED -> not used\n");
467 #endif
468 #endif
469
470 /* set / reset everything */
471 ret = pthread_mutexattr_init(&ma);
472 if (ret != 0) {
473 UNRESOLVED(ret,
474 "[parent] Unable to initialize the mutex attribute object");
475 }
476 ret = pthread_condattr_init(&ca);
477 if (ret != 0) {
478 UNRESOLVED(ret,
479 "[parent] Unable to initialize the cond attribute object");
480 }
481 #ifndef WITHOUT_XOPEN
482 /* Set the mutex type */
483 ret = pthread_mutexattr_settype(&ma, scenarii[i].m_type);
484 if (ret != 0) {
485 UNRESOLVED(ret, "[parent] Unable to set mutex type");
486 }
487 #if VERBOSE > 1
488 output("[parent] Mutex type : %i\n", scenarii[i].m_type);
489 #endif
490 #endif
491
492 /* Set the pshared attributes, if supported */
493 if ((pshared > 0) && (scenarii[i].mc_pshared != 0)) {
494 ret =
495 pthread_mutexattr_setpshared(&ma,
496 PTHREAD_PROCESS_SHARED);
497 if (ret != 0) {
498 UNRESOLVED(ret,
499 "[parent] Unable to set the mutex process-shared");
500 }
501 ret =
502 pthread_condattr_setpshared(&ca,
503 PTHREAD_PROCESS_SHARED);
504 if (ret != 0) {
505 UNRESOLVED(ret,
506 "[parent] Unable to set the cond var process-shared");
507 }
508 #if VERBOSE > 1
509 output("[parent] Mutex & cond are process-shared\n");
510 #endif
511 }
512 #if VERBOSE > 1
513 else {
514 output("[parent] Mutex & cond are process-private\n");
515 }
516 #endif
517
518 /* Set the alternative clock, if supported */
519 #ifdef USE_ALTCLK
520 if ((cs > 0) && (scenarii[i].c_clock != 0)) {
521 ret = pthread_condattr_setclock(&ca, CLOCK_MONOTONIC);
522 if (ret != 0) {
523 UNRESOLVED(ret,
524 "[parent] Unable to set the monotonic clock for the cond");
525 }
526 #if VERBOSE > 1
527 output("[parent] Cond uses the Monotonic clock\n");
528 #endif
529 }
530 #if VERBOSE > 1
531 else {
532 output("[parent] Cond uses the default clock\n");
533 }
534 #endif
535 ret = pthread_condattr_getclock(&ca, &cid);
536 if (ret != 0) {
537 UNRESOLVED(ret, "Unable to get clock from cond attr");
538 }
539 #endif
540
541 /* Initialize all the mutex and condvars which uses those attributes */
542 for (j = 0; j < SCALABILITY_FACTOR; j++) {
543 cells[i + j * NSCENAR].cid = cid;
544
545 /* initialize the condvar */
546 ret =
547 pthread_cond_init(&(cells[i + j * NSCENAR].cnd),
548 &ca);
549 if (ret != 0) {
550 UNRESOLVED(ret, "Cond init failed");
551 }
552
553 /* initialize the mutex */
554 ret =
555 pthread_mutex_init(&(cells[i + j * NSCENAR].mtx),
556 &ma);
557 if (ret != 0) {
558 UNRESOLVED(ret, "Mutex init failed");
559 }
560
561 /* initialize the barrier */
562 ret =
563 pthread_barrier_init(&(cells[i + j * NSCENAR].bar),
564 NULL, 2);
565 if (ret != 0) {
566 UNRESOLVED(ret, "Failed to init barrier");
567 }
568 }
569
570 ret = pthread_condattr_destroy(&ca);
571 if (ret != 0) {
572 UNRESOLVED(ret,
573 "Failed to destroy the cond var attribute object");
574 }
575
576 ret = pthread_mutexattr_destroy(&ma);
577 if (ret != 0) {
578 UNRESOLVED(ret,
579 "Failed to destroy the mutex attribute object");
580 }
581 }
582 #if VERBOSE > 1
583 output("[parent] All condvars & mutex are ready\n");
584 #endif
585
586 /* register the signal handler */
587 sigemptyset(&sa.sa_mask);
588 sa.sa_flags = 0;
589 sa.sa_handler = sighdl;
590 if ((ret = sigaction(SIGUSR1, &sa, NULL))) {
591 UNRESOLVED(ret, "Unable to register signal handler");
592 }
593 #if VERBOSE > 1
594 output("[parent] Signal handler registered\n");
595 #endif
596
597 /* Initialize the thread attribute object */
598 ret = pthread_attr_init(&ta);
599 if (ret != 0) {
600 UNRESOLVED(ret,
601 "[parent] Failed to initialize a thread attribute object");
602 }
603 ret = pthread_attr_setstacksize(&ta, sysconf(_SC_THREAD_STACK_MIN));
604 if (ret != 0) {
605 UNRESOLVED(ret, "[parent] Failed to set thread stack size");
606 }
607
608 /* create the NSCENAR * SCALABILITY_FACTOR manager threads */
609 for (i = 0; i < NSCENAR * SCALABILITY_FACTOR; i++) {
610 ret = pthread_create(&mngrs[i], &ta, cellmanager, &(cells[i]));
611 /* In case of failure we can exit; the child process will die after a while */
612 if (ret != 0) {
613 UNRESOLVED(ret, "[Parent] Failed to create a thread");
614 }
615 #if VERBOSE > 1
616 if ((i % 4) == 0)
617 output("[parent] %i manager threads created...\n",
618 i + 1);
619 #endif
620 }
621
622 #if VERBOSE > 1
623 output("[parent] All %i manager threads are running...\n",
624 NSCENAR * SCALABILITY_FACTOR);
625 #endif
626
627 /* join the manager threads and destroy the cells */
628 for (i = 0; i < NSCENAR * SCALABILITY_FACTOR; i++) {
629 ret = pthread_join(mngrs[i], NULL);
630 if (ret != 0) {
631 UNRESOLVED(ret, "[Parent] Failed to join a thread");
632 }
633
634 canceled += cells[i].canceled;
635 cancelfailed += cells[i].cancelfailed;
636 cnttotal += cells[i].cnttotal;
637
638 ret = pthread_barrier_destroy(&(cells[i].bar));
639 if (ret != 0) {
640 UNRESOLVED(ret, "Failed to destroy a barrier");
641 }
642
643 ret = pthread_cond_destroy(&(cells[i].cnd));
644 if (ret != 0) {
645 UNRESOLVED(ret, "Failed to destroy a cond");
646 }
647
648 ret = pthread_mutex_destroy(&(cells[i].mtx));
649 if (ret != 0) {
650 UNRESOLVED(ret, "Failed to destroy a mutex");
651 }
652 }
653
654 /* exit */
655 #if VERBOSE > 0
656 output("Test passed\n");
657 output(" Total loops : %8li\n", cnttotal);
658 #endif
659 #if VERBOSE > 1
660 output(" Failed cancel request: %8li\n", cancelfailed);
661 output(" Canceled threads : %8li\n", canceled);
662 #endif
663
664 PASSED;
665 }
666