]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-join5.c
mutex: Fix robust mutex lock acquire (Bug 21778)
[thirdparty/glibc.git] / nptl / tst-join5.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2003-2017 Free Software Foundation, Inc.
e320ef46
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
e320ef46
UD
18
19#include <errno.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
22bb134c
UD
23#include <time.h>
24#include <unistd.h>
22bb134c
UD
25
26
ca0e0675
RM
27static void
28wait_code (void)
29{
30 struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 };
31 while (nanosleep (&ts, &ts) < 0)
32 ;
33}
22bb134c
UD
34
35
36#ifdef WAIT_IN_CHILD
37static pthread_barrier_t b;
38#endif
e320ef46
UD
39
40
41static void *
42tf1 (void *arg)
43{
22bb134c
UD
44#ifdef WAIT_IN_CHILD
45 int e = pthread_barrier_wait (&b);
46 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
47 {
48 printf ("%s: barrier_wait failed\n", __func__);
49 exit (1);
50 }
0ecb606c 51
22bb134c
UD
52 wait_code ();
53#endif
a334319f 54
22bb134c
UD
55 pthread_join ((pthread_t) arg, NULL);
56
57 exit (42);
e320ef46
UD
58}
59
60
61static void *
62tf2 (void *arg)
63{
22bb134c
UD
64#ifdef WAIT_IN_CHILD
65 int e = pthread_barrier_wait (&b);
66 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
67 {
68 printf ("%s: barrier_wait failed\n", __func__);
69 exit (1);
70 }
e320ef46 71
22bb134c
UD
72 wait_code ();
73#endif
74 pthread_join ((pthread_t) arg, NULL);
a334319f 75
22bb134c 76 exit (43);
e320ef46
UD
77}
78
79
80static int
81do_test (void)
82{
22bb134c
UD
83#ifdef WAIT_IN_CHILD
84 if (pthread_barrier_init (&b, NULL, 2) != 0)
85 {
86 puts ("barrier_init failed");
87 return 1;
88 }
89#endif
90
e320ef46
UD
91 pthread_t th;
92
93 int err = pthread_join (pthread_self (), NULL);
94 if (err == 0)
95 {
96 puts ("1st circular join succeeded");
22bb134c 97 return 1;
e320ef46
UD
98 }
99 if (err != EDEADLK)
100 {
101 printf ("1st circular join %d, not EDEADLK\n", err);
22bb134c 102 return 1;
e320ef46
UD
103 }
104
105 if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
106 {
107 puts ("1st create failed");
22bb134c 108 return 1;
e320ef46
UD
109 }
110
22bb134c
UD
111#ifndef WAIT_IN_CHILD
112 wait_code ();
113#endif
114
e320ef46
UD
115 if (pthread_cancel (th) != 0)
116 {
117 puts ("cannot cancel 1st thread");
22bb134c
UD
118 return 1;
119 }
120
121#ifdef WAIT_IN_CHILD
122 int e = pthread_barrier_wait (&b);
123 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
124 {
125 printf ("%s: barrier_wait failed\n", __func__);
126 return 1;
e320ef46 127 }
22bb134c 128#endif
e320ef46
UD
129
130 void *r;
131 err = pthread_join (th, &r);
132 if (err != 0)
133 {
134 printf ("cannot join 1st thread: %d\n", err);
22bb134c 135 return 1;
e320ef46
UD
136 }
137 if (r != PTHREAD_CANCELED)
138 {
139 puts ("1st thread not canceled");
22bb134c 140 return 1;
e320ef46
UD
141 }
142
143 err = pthread_join (pthread_self (), NULL);
144 if (err == 0)
145 {
146 puts ("2nd circular join succeeded");
22bb134c 147 return 1;
e320ef46
UD
148 }
149 if (err != EDEADLK)
150 {
151 printf ("2nd circular join %d, not EDEADLK\n", err);
22bb134c 152 return 1;
e320ef46
UD
153 }
154
155 if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
156 {
157 puts ("2nd create failed");
22bb134c 158 return 1;
e320ef46
UD
159 }
160
22bb134c
UD
161#ifndef WAIT_IN_CHILD
162 wait_code ();
163#endif
164
e320ef46
UD
165 if (pthread_cancel (th) != 0)
166 {
167 puts ("cannot cancel 2nd thread");
22bb134c 168 return 1;
0ecb606c 169 }
0ecb606c 170
22bb134c
UD
171#ifdef WAIT_IN_CHILD
172 e = pthread_barrier_wait (&b);
173 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
174 {
175 printf ("%s: barrier_wait failed\n", __func__);
176 return 1;
177 }
178#endif
179
e320ef46
UD
180 if (pthread_join (th, &r) != 0)
181 {
182 puts ("cannot join 2nd thread");
22bb134c 183 return 1;
e320ef46
UD
184 }
185 if (r != PTHREAD_CANCELED)
186 {
187 puts ("2nd thread not canceled");
22bb134c 188 return 1;
e320ef46
UD
189 }
190
191 err = pthread_join (pthread_self (), NULL);
192 if (err == 0)
193 {
22bb134c
UD
194 puts ("3rd circular join succeeded");
195 return 1;
e320ef46
UD
196 }
197 if (err != EDEADLK)
198 {
22bb134c
UD
199 printf ("3rd circular join %d, not EDEADLK\n", err);
200 return 1;
e320ef46
UD
201 }
202
22bb134c 203 return 0;
e320ef46
UD
204}
205
206#define TEST_FUNCTION do_test ()
207#include "../test-skeleton.c"