summaryrefslogtreecommitdiffstats
path: root/regress/lib/libpthread/blocked_join/blocked_join.c
blob: 20cf748d4c349c686b52407e44125382395dc6f2 (plain) (blame)
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
/*	$OpenBSD: blocked_join.c,v 1.2 2012/08/22 22:51:27 fgsch Exp $	*/
/*
 * Federico G. Schwindt <fgsch@openbsd.org>, 2012. Public Domain.
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "test.h"

void *
deadlock_detector(void *arg)
{
	sleep(10);
	PANIC("deadlock detected");
}

void *
joiner(void *arg)
{
	pthread_t mainthread = *(pthread_t *)arg;
	ASSERT(pthread_join(mainthread, NULL) == EDEADLK);
	return (NULL);
}

int
main(int argc, char **argv)
{
	pthread_t d, t, self = pthread_self();
	pid_t pid;

	switch ((pid = fork())) {
	case -1:
		PANIC("cannot fork");
		/* NOTREACHED */

	case 0:
		/* child */
		break;

	default:
		CHECKe(waitpid(pid, NULL, 0));
		_exit(0);
		/* NOTREACHED */
	}

        CHECKr(pthread_create(&d, NULL, deadlock_detector, NULL));
	CHECKr(pthread_create(&t, NULL, joiner, &self));
	CHECKr(pthread_join(t, NULL));
	SUCCEED;
}