aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_cond_broadcast/2-3.c
blob: 926077f0d23e8145726713c1762eb14dd1800293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * Copyright (c) 2004, Bull S.A..  All rights reserved.
 * Created by: Sebastien Decugis

 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 * This file tests the following assertion:
 *
 * The pthread_cond_broadcast function unblocks all the threads blocked on the
 * conditional variable.

 * The steps are:
 *  -> Create N threads which will wait on a condition variable
 *  -> broadcast the condition
 *  -> Every child checks that it owns the mutex (when possible)
 *
 */

#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/wait.h>

#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"

#define UNRESOLVED_KILLALL(error, text) {			\
	if (td->fork) {						\
		int _nch;					\
		for (_nch = 0; _nch < children.nb; _nch++)	\
			kill(children.ch[_nch].p, SIGKILL);	\
	}							\
	UNRESOLVED(error, text);				\
	}
#define FAILED_KILLALL(text, Tchild) {				\
	if (td->fork) {						\
		int _nch;					\
		for (_nch = 0; _nch < children.nb; _nch++)	\
			kill(children.ch[_nch].p, SIGKILL);	\
	}							\
	FAILED(text);						\
	}

#ifndef VERBOSE
#define VERBOSE 1
#endif

#define NCHILDREN (20)

#define TIMEOUT  (120)

#ifndef WITHOUT_ALTCLK
#define USE_ALTCLK		/* make tests with MONOTONIC CLOCK if supported */
#endif

#ifdef WITHOUT_XOPEN
/* We define those to avoid compilation errors, but they won't be used */
#define PTHREAD_MUTEX_DEFAULT 0
#define PTHREAD_MUTEX_NORMAL 0
#define PTHREAD_MUTEX_ERRORCHECK 0
#define PTHREAD_MUTEX_RECURSIVE 0

#endif

struct _scenar {
	int m_type;
	int mc_pshared;
	int c_clock;
	int fork;
	char *descr;
} scenarii[] = {
	{
	PTHREAD_MUTEX_DEFAULT, 0, 0, 0, "Default mutex"}, {
	PTHREAD_MUTEX_NORMAL, 0, 0, 0, "Normal mutex"}, {
	PTHREAD_MUTEX_ERRORCHECK, 0, 0, 0, "Errorcheck mutex"}, {
	PTHREAD_MUTEX_RECURSIVE, 0, 0, 0, "Recursive mutex"}, {
	PTHREAD_MUTEX_DEFAULT, 1, 0, 0, "PShared default mutex"}, {
	PTHREAD_MUTEX_NORMAL, 1, 0, 0, "Pshared normal mutex"}, {
	PTHREAD_MUTEX_ERRORCHECK, 1, 0, 0, "Pshared errorcheck mutex"}, {
	PTHREAD_MUTEX_RECURSIVE, 1, 0, 0, "Pshared recursive mutex"}, {
	PTHREAD_MUTEX_DEFAULT, 1, 0, 1,
		    "Pshared default mutex across processes"}, {
	PTHREAD_MUTEX_NORMAL, 1, 0, 1, "Pshared normal mutex across processes"},
	{
	PTHREAD_MUTEX_ERRORCHECK, 1, 0, 1,
		    "Pshared errorcheck mutex across processes"}, {
	PTHREAD_MUTEX_RECURSIVE, 1, 0, 1,
		    "Pshared recursive mutex across processes"},
#ifdef USE_ALTCLK
	{
	PTHREAD_MUTEX_DEFAULT, 1, 1, 1,
		    "Pshared default mutex and alt clock condvar across processes"},
	{
	PTHREAD_MUTEX_NORMAL, 1, 1, 1,
		    "Pshared normal mutex and alt clock condvar across processes"},
	{
	PTHREAD_MUTEX_ERRORCHECK, 1, 1, 1,
		    "Pshared errorcheck mutex and alt clock condvar across processes"},
	{
	PTHREAD_MUTEX_RECURSIVE, 1, 1, 1,
		    "Pshared recursive mutex and alt clock condvar across processes"},
	{
	PTHREAD_MUTEX_DEFAULT, 0, 1, 0, "Default mutex and alt clock condvar"},
	{
	PTHREAD_MUTEX_NORMAL, 0, 1, 0, "Normal mutex and alt clock condvar"},
	{
	PTHREAD_MUTEX_ERRORCHECK, 0, 1, 0,
		    "Errorcheck mutex and alt clock condvar"}, {
	PTHREAD_MUTEX_RECURSIVE, 0, 1, 0,
		    "Recursive mutex and alt clock condvar"}, {
	PTHREAD_MUTEX_DEFAULT, 1, 1, 0,
		    "PShared default mutex and alt clock condvar"}, {
	PTHREAD_MUTEX_NORMAL, 1, 1, 0,
		    "Pshared normal mutex and alt clock condvar"}, {
	PTHREAD_MUTEX_ERRORCHECK, 1, 1, 0,
		    "Pshared errorcheck mutex and alt clock condvar"}, {
	PTHREAD_MUTEX_RECURSIVE, 1, 1, 0,
		    "Pshared recursive mutex and alt clock condvar"},
#endif
};

#define NSCENAR (sizeof(scenarii) / sizeof(scenarii[0]))

struct testdata {
	int count;
	pthread_cond_t cnd;
	pthread_mutex_t mtx;
	int predicate;
	clockid_t cid;
	int mtype;
	char fork;
} *td;

struct {
	union {
		pthread_t t;
		pid_t p;
	} ch[NCHILDREN];
	int nb;
} children;

static void *child(void *arg)
{
	int ret = 0;
	int timed;
	struct timespec ts;

	/* lock the mutex */
	ret = pthread_mutex_lock(&td->mtx);
	if (ret != 0)
		UNRESOLVED(ret, "Failed to lock mutex in child");

	/* increment count */
	td->count++;
	timed = td->count & 1;

	if (timed) {
		ret = clock_gettime(td->cid, &ts);
		if (ret != 0)
			UNRESOLVED(errno, "Unable to read clock");
		ts.tv_sec += TIMEOUT;
	}

	do {
		/* Wait while the predicate is false */
		if (timed)
			ret = pthread_cond_timedwait(&td->cnd, &td->mtx, &ts);
		else
			ret = pthread_cond_wait(&td->cnd, &td->mtx);
#if VERBOSE > 5
		output("[child] Wokenup timed=%i, Predicate=%i, ret=%i\n",
		       timed, td->predicate, ret);
#endif
	} while ((ret == 0) && (td->predicate == 0));
	if (ret == ETIMEDOUT) {
		FAILED("Timeout occured. This means a cond signal was lost "
		       "-- or parent died");
	}
	if (ret != 0)
		UNRESOLVED(ret, "Failed to wait for the cond");

	/* Check that we are owning the mutex */
#ifdef WITHOUT_XOPEN
	ret = pthread_mutex_trylock(&(td->mtx));
	if (ret == 0)
		FAILED("The mutex was not owned after return from "
		       "condition waiting");
#else
	if (td->mtype == PTHREAD_MUTEX_RECURSIVE) {
		ret = pthread_mutex_trylock(&(td->mtx));
		if (ret != 0)
			FAILED("Unable to relock recursive mutex: not owning?");
		ret = pthread_mutex_unlock(&(td->mtx));
		if (ret != 0)
			UNRESOLVED(ret, "Failed to unlock the mutex");
	}
	if (td->mtype == PTHREAD_MUTEX_ERRORCHECK) {
		ret = pthread_mutex_lock(&(td->mtx));
		if (ret == 0)
			FAILED("Was able to lock errorcheck mutex: "
			       "the mutex was not acquired once already?");
	}
#endif

	/* unlock the mutex */
	ret = pthread_mutex_unlock(&td->mtx);
	if (ret != 0)
		UNRESOLVED(ret, "Failed to unlock the mutex.");

	return NULL;
}

static void *timer(void *arg)
{
	unsigned int to = TIMEOUT;
	do {
		to = sleep(to);
	} while (to > 0);
	FAILED_KILLALL("Operation timed out. A signal was lost.", pchildren);
	return NULL;
}

int main(void)
{
	int ret;

	pthread_mutexattr_t ma;
	pthread_condattr_t ca;

	int scenar;
	long pshared, monotonic, cs, mf;

	int child_count;

	pid_t pid;
	int status;

	pthread_t t_timer;

	pthread_attr_t ta;

	struct testdata alternativ;

	output_init();

	/* check the system abilities */
	pshared = sysconf(_SC_THREAD_PROCESS_SHARED);
	cs = sysconf(_SC_CLOCK_SELECTION);
	monotonic = sysconf(_SC_MONOTONIC_CLOCK);
	mf = sysconf(_SC_MAPPED_FILES);

#if VERBOSE > 0
	output("Test starting\n");
	output("System abilities:\n");
	output(" TPS : %li\n", pshared);
	output(" CS  : %li\n", cs);
	output(" MON : %li\n", monotonic);
	output(" MF  : %li\n", mf);
	if ((mf < 0) || (pshared < 0))
		output("Process-shared attributes won't be tested\n");
	if ((cs < 0) || (monotonic < 0))
		output("Alternative clock won't be tested\n");
#endif

	if (monotonic < 0)
		cs = -1;

#ifndef USE_ALTCLK
	if (cs > 0)
		output("Implementation supports the MONOTONIC CLOCK "
		       "but option is disabled in test.\n");
#endif

	if (mf < 0) {
		/* Cannot mmap a file, we use an alternative method */
		td = &alternativ;
		pshared = -1;	/* We won't do this testing anyway */
#if VERBOSE > 0
		output("Testdata allocated in the process memory.\n");
#endif
	} else {
		char filename[] = "/tmp/cond_broadcast-XXXXXX";
		size_t sz, ps;
		void *mmaped;
		int fd;
		char *tmp;

		fd = mkstemp(filename);
		if (fd == -1)
			UNRESOLVED(errno,
				   "Temporary file could not be created");

		unlink(filename);

#if VERBOSE > 1
		output("Temp file created (%s).\n", filename);
#endif

		ps = (size_t) sysconf(_SC_PAGESIZE);
		sz = ((sizeof(struct testdata) / ps) + 1) * ps;

		tmp = calloc(1, sz);
		if (tmp == NULL)
			UNRESOLVED(errno, "Memory allocation failed");

		if (write(fd, tmp, sz) != (ssize_t) sz)
			UNRESOLVED(sz, "Writting to the file failed");

		free(tmp);

		mmaped =
		    mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
		if (mmaped == MAP_FAILED)
			UNRESOLVED(errno, "mmap failed");

		td = (struct testdata *)mmaped;

		/* Our datatest structure is now in shared memory */
#if VERBOSE > 1
		output("Testdata allocated in shared memory (%ib).\n",
		       sizeof(struct testdata));
#endif
	}

	ret = pthread_attr_init(&ta);
	if (ret != 0)
		UNRESOLVED(ret, "[parent] Failed to initialize a thread "
			   "attribute object");
	ret = pthread_attr_setstacksize(&ta, sysconf(_SC_THREAD_STACK_MIN));
	if (ret != 0)
		UNRESOLVED(ret, "[parent] Failed to set thread stack size");

	/* Do the test for each test scenario */
	for (scenar = 0; scenar < NSCENAR; scenar++) {
		/* set / reset everything */
		td->fork = 0;
		ret = pthread_mutexattr_init(&ma);
		if (ret != 0)
			UNRESOLVED(ret, "[parent] Unable to initialize the "
				   "mutex attribute object");
		ret = pthread_condattr_init(&ca);
		if (ret != 0)
			UNRESOLVED(ret, "[parent] Unable to initialize the "
				   "cond attribute object");

#ifndef WITHOUT_XOPEN
		/* Set the mutex type */
		ret = pthread_mutexattr_settype(&ma, scenarii[scenar].m_type);
		if (ret != 0)
			UNRESOLVED(ret, "[parent] Unable to set mutex type");
#endif

		td->mtype = scenarii[scenar].m_type;

		/* Set the pshared attributes, if supported */
		if ((pshared > 0) && (scenarii[scenar].mc_pshared != 0)) {
			ret = pthread_mutexattr_setpshared(&ma,
							   PTHREAD_PROCESS_SHARED);
			if (ret != 0)
				UNRESOLVED(ret, "[parent] Unable to set "
					   "the mutex process-shared");
			ret = pthread_condattr_setpshared(&ca,
							  PTHREAD_PROCESS_SHARED);
			if (ret != 0)
				UNRESOLVED(ret, "[parent] Unable to set "
					   "the cond var process-shared");
		}

		/* Set the alternative clock, if supported */
#ifdef USE_ALTCLK
		if ((cs > 0) && (scenarii[scenar].c_clock != 0)) {
			ret = pthread_condattr_setclock(&ca, CLOCK_MONOTONIC);
			if (ret != 0)
				UNRESOLVED(ret, "[parent] Unable to set the "
					   "monotonic clock for the cond");
		}
		ret = pthread_condattr_getclock(&ca, &td->cid);
		if (ret != 0)
			UNRESOLVED(ret, "Unable to get clock from cond attr");
#else
		td->cid = CLOCK_REALTIME;
#endif

		/* Tell whether the test will be across processes */
		if ((pshared > 0) && (scenarii[scenar].fork != 0))
			td->fork = 1;

		ret = pthread_cond_init(&td->cnd, &ca);
		if (ret != 0)
			UNRESOLVED(ret, "Cond init failed");

		ret = pthread_mutex_init(&td->mtx, &ma);
		if (ret != 0)
			UNRESOLVED(ret, "Mutex init failed");

		ret = pthread_condattr_destroy(&ca);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to destroy the cond var "
				   "attribute object");

		ret = pthread_mutexattr_destroy(&ma);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to destroy the mutex "
				   "attribute object");

#if VERBOSE > 2
		output("[parent] Starting test %s\n", scenarii[scenar].descr);
#endif

		td->count = 0;

		/* Create all the children */
		for (children.nb = 0; children.nb < NCHILDREN; children.nb++) {
			if (td->fork == 0) {
				ret =
				    pthread_create(&
						   (children.ch[children.nb].t),
						   &ta, child, NULL);
				if (ret != 0)
					UNRESOLVED(ret, "Failed to create "
						   "enough threads");
			} else {
				children.ch[children.nb].p = fork();
				if (children.ch[children.nb].p == 0) {
					child(NULL);
					exit(0);
				}
				if (children.ch[children.nb].p == -1) {
					children.nb--;
					UNRESOLVED_KILLALL(errno, "Failed to "
							   "create enough processes");
				}
			}
		}
#if VERBOSE > 4
		output("[parent] Created %i children\n", NCHILDREN);
#endif

		/* Make sure all children are waiting */
		ret = pthread_mutex_lock(&td->mtx);
		if (ret != 0)
			UNRESOLVED_KILLALL(ret, "Failed to lock mutex");
		child_count = td->count;
		while (child_count < NCHILDREN) {
			ret = pthread_mutex_unlock(&td->mtx);
			if (ret != 0)
				UNRESOLVED_KILLALL(ret,
						   "Failed to unlock mutex");
			sched_yield();
			ret = pthread_mutex_lock(&td->mtx);
			if (ret != 0)
				UNRESOLVED_KILLALL(ret, "Failed to lock mutex");
			child_count = td->count;
		}

#if VERBOSE > 4
		output("[parent] All children are waiting\n");
#endif

		ret = pthread_create(&t_timer, NULL, timer, NULL);
		if (ret != 0)
			UNRESOLVED_KILLALL(ret,
					   "Unable to create timer thread");

		/* Wakeup the children */
		td->predicate = 1;
		ret = pthread_cond_broadcast(&td->cnd);
		if (ret != 0)
			UNRESOLVED_KILLALL(ret, "Failed to broadcast "
					   "the condition.");

#if VERBOSE > 4
		output("[parent] Condition was signaled\n");
#endif

		ret = pthread_mutex_unlock(&td->mtx);
		if (ret != 0)
			UNRESOLVED_KILLALL(ret, "Failed to unlock mutex");

#if VERBOSE > 4
		output("[parent] Joining the children\n");
#endif

		/* join the children */
		for (--children.nb; children.nb >= 0; children.nb--) {
			if (td->fork == 0) {
				ret = pthread_join(children.ch[children.nb].t,
						   NULL);
				if (ret != 0)
					UNRESOLVED(ret,
						   "Failed to join a child thread");
			} else {
				pid = waitpid(children.ch[children.nb].p,
					      &status, 0);
				if (pid != children.ch[children.nb].p) {
					ret = errno;
					output("Waitpid failed (expected: %i, "
					       "got: %i)\n",
					       children.ch[children.nb].p, pid);
					UNRESOLVED_KILLALL(ret,
							   "Waitpid failed");
				}
				if (WIFEXITED(status)) {
					if (ret != PTS_FAIL)
						ret |= WEXITSTATUS(status);
				}
			}
		}
		if (ret != 0) {
			output_fini();
			exit(ret);
		}
#if VERBOSE > 4
		output("[parent] All children terminated\n");
#endif

		ret = pthread_cancel(t_timer);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to cancel the timeout handler");

		ret = pthread_join(t_timer, NULL);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to join the timeout handler");

		ret = pthread_cond_destroy(&td->cnd);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to destroy the condvar");

		ret = pthread_mutex_destroy(&td->mtx);
		if (ret != 0)
			UNRESOLVED(ret, "Failed to destroy the mutex");

	}

	ret = pthread_attr_destroy(&ta);
	if (ret != 0)
		UNRESOLVED(ret, "Final thread attr destroy failed");

	PASSED;
}