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