1 /* $OpenBSD: blocked_join.c,v 1.2 2012/08/22 22:51:27 fgsch Exp $ */
2 /*
3 * Federico G. Schwindt <fgsch@openbsd.org>, 2012. Public Domain.
4 */
5
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <pthread.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include "test.h"
13
14 void *
deadlock_detector(void * arg)15 deadlock_detector(void *arg)
16 {
17 sleep(10);
18 PANIC("deadlock detected");
19 }
20
21 void *
joiner(void * arg)22 joiner(void *arg)
23 {
24 pthread_t mainthread = *(pthread_t *)arg;
25 ASSERT(pthread_join(mainthread, NULL) == EDEADLK);
26 return (NULL);
27 }
28
29 int
main(int argc,char ** argv)30 main(int argc, char **argv)
31 {
32 pthread_t d, t, self = pthread_self();
33 pid_t pid;
34
35 switch ((pid = fork())) {
36 case -1:
37 PANIC("cannot fork");
38 /* NOTREACHED */
39
40 case 0:
41 /* child */
42 break;
43
44 default:
45 CHECKe(waitpid(pid, NULL, 0));
46 _exit(0);
47 /* NOTREACHED */
48 }
49
50 CHECKr(pthread_create(&d, NULL, deadlock_detector, NULL));
51 CHECKr(pthread_create(&t, NULL, joiner, &self));
52 CHECKr(pthread_join(t, NULL));
53 SUCCEED;
54 }
55