.
[glibc/history.git] / nptl / tst-join5.c
blobdb005f5b71ebbb2c026ddbd644a40a5891ee9b2b
1 /* Copyright (C) 2003, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/syscall.h>
29 #define wait_code() \
30 do { \
31 struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 }; \
32 while (syscall (__NR_nanosleep, &ts, &ts) < 0) \
33 /* nothing */; \
34 } while (0)
37 #ifdef WAIT_IN_CHILD
38 static pthread_barrier_t b;
39 #endif
42 static void *
43 tf1 (void *arg)
45 #ifdef WAIT_IN_CHILD
46 int e = pthread_barrier_wait (&b);
47 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
49 printf ("%s: barrier_wait failed\n", __func__);
50 exit (1);
53 wait_code ();
54 #endif
56 pthread_join ((pthread_t) arg, NULL);
58 exit (42);
62 static void *
63 tf2 (void *arg)
65 #ifdef WAIT_IN_CHILD
66 int e = pthread_barrier_wait (&b);
67 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
69 printf ("%s: barrier_wait failed\n", __func__);
70 exit (1);
73 wait_code ();
74 #endif
75 pthread_join ((pthread_t) arg, NULL);
77 exit (43);
81 static int
82 do_test (void)
84 #ifdef WAIT_IN_CHILD
85 if (pthread_barrier_init (&b, NULL, 2) != 0)
87 puts ("barrier_init failed");
88 return 1;
90 #endif
92 pthread_t th;
94 int err = pthread_join (pthread_self (), NULL);
95 if (err == 0)
97 puts ("1st circular join succeeded");
98 return 1;
100 if (err != EDEADLK)
102 printf ("1st circular join %d, not EDEADLK\n", err);
103 return 1;
106 if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
108 puts ("1st create failed");
109 return 1;
112 #ifndef WAIT_IN_CHILD
113 wait_code ();
114 #endif
116 if (pthread_cancel (th) != 0)
118 puts ("cannot cancel 1st thread");
119 return 1;
122 #ifdef WAIT_IN_CHILD
123 int e = pthread_barrier_wait (&b);
124 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
126 printf ("%s: barrier_wait failed\n", __func__);
127 return 1;
129 #endif
131 void *r;
132 err = pthread_join (th, &r);
133 if (err != 0)
135 printf ("cannot join 1st thread: %d\n", err);
136 return 1;
138 if (r != PTHREAD_CANCELED)
140 puts ("1st thread not canceled");
141 return 1;
144 err = pthread_join (pthread_self (), NULL);
145 if (err == 0)
147 puts ("2nd circular join succeeded");
148 return 1;
150 if (err != EDEADLK)
152 printf ("2nd circular join %d, not EDEADLK\n", err);
153 return 1;
156 if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
158 puts ("2nd create failed");
159 return 1;
162 #ifndef WAIT_IN_CHILD
163 wait_code ();
164 #endif
166 if (pthread_cancel (th) != 0)
168 puts ("cannot cancel 2nd thread");
169 return 1;
172 #ifdef WAIT_IN_CHILD
173 e = pthread_barrier_wait (&b);
174 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
176 printf ("%s: barrier_wait failed\n", __func__);
177 return 1;
179 #endif
181 if (pthread_join (th, &r) != 0)
183 puts ("cannot join 2nd thread");
184 return 1;
186 if (r != PTHREAD_CANCELED)
188 puts ("2nd thread not canceled");
189 return 1;
192 err = pthread_join (pthread_self (), NULL);
193 if (err == 0)
195 puts ("3rd circular join succeeded");
196 return 1;
198 if (err != EDEADLK)
200 printf ("3rd circular join %d, not EDEADLK\n", err);
201 return 1;
204 return 0;
207 #define TEST_FUNCTION do_test ()
208 #include "../test-skeleton.c"