Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / tst-join5.c
blob7f95bd1ea576ef0687e6e2a8b30cfe11b9379dae
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <sys/syscall.h>
28 #define wait_code() \
29 do { \
30 struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 }; \
31 while (syscall (__NR_nanosleep, &ts, &ts) < 0) \
32 /* nothing */; \
33 } while (0)
36 #ifdef WAIT_IN_CHILD
37 static pthread_barrier_t b;
38 #endif
41 static void *
42 tf1 (void *arg)
44 #ifdef WAIT_IN_CHILD
45 int e = pthread_barrier_wait (&b);
46 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
48 printf ("%s: barrier_wait failed\n", __func__);
49 exit (1);
52 wait_code ();
53 #endif
55 pthread_join ((pthread_t) arg, NULL);
57 exit (42);
61 static void *
62 tf2 (void *arg)
64 #ifdef WAIT_IN_CHILD
65 int e = pthread_barrier_wait (&b);
66 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
68 printf ("%s: barrier_wait failed\n", __func__);
69 exit (1);
72 wait_code ();
73 #endif
74 pthread_join ((pthread_t) arg, NULL);
76 exit (43);
80 static int
81 do_test (void)
83 #ifdef WAIT_IN_CHILD
84 if (pthread_barrier_init (&b, NULL, 2) != 0)
86 puts ("barrier_init failed");
87 return 1;
89 #endif
91 pthread_t th;
93 int err = pthread_join (pthread_self (), NULL);
94 if (err == 0)
96 puts ("1st circular join succeeded");
97 return 1;
99 if (err != EDEADLK)
101 printf ("1st circular join %d, not EDEADLK\n", err);
102 return 1;
105 if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
107 puts ("1st create failed");
108 return 1;
111 #ifndef WAIT_IN_CHILD
112 wait_code ();
113 #endif
115 if (pthread_cancel (th) != 0)
117 puts ("cannot cancel 1st thread");
118 return 1;
121 #ifdef WAIT_IN_CHILD
122 int e = pthread_barrier_wait (&b);
123 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
125 printf ("%s: barrier_wait failed\n", __func__);
126 return 1;
128 #endif
130 void *r;
131 err = pthread_join (th, &r);
132 if (err != 0)
134 printf ("cannot join 1st thread: %d\n", err);
135 return 1;
137 if (r != PTHREAD_CANCELED)
139 puts ("1st thread not canceled");
140 return 1;
143 err = pthread_join (pthread_self (), NULL);
144 if (err == 0)
146 puts ("2nd circular join succeeded");
147 return 1;
149 if (err != EDEADLK)
151 printf ("2nd circular join %d, not EDEADLK\n", err);
152 return 1;
155 if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
157 puts ("2nd create failed");
158 return 1;
161 #ifndef WAIT_IN_CHILD
162 wait_code ();
163 #endif
165 if (pthread_cancel (th) != 0)
167 puts ("cannot cancel 2nd thread");
168 return 1;
171 #ifdef WAIT_IN_CHILD
172 e = pthread_barrier_wait (&b);
173 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
175 printf ("%s: barrier_wait failed\n", __func__);
176 return 1;
178 #endif
180 if (pthread_join (th, &r) != 0)
182 puts ("cannot join 2nd thread");
183 return 1;
185 if (r != PTHREAD_CANCELED)
187 puts ("2nd thread not canceled");
188 return 1;
191 err = pthread_join (pthread_self (), NULL);
192 if (err == 0)
194 puts ("3rd circular join succeeded");
195 return 1;
197 if (err != EDEADLK)
199 printf ("3rd circular join %d, not EDEADLK\n", err);
200 return 1;
203 return 0;
206 #define TEST_FUNCTION do_test ()
207 #include "../test-skeleton.c"