universally apply our cflags (no vsx, no altivec..)
[glibc.git] / fbtl / tst-join5.c
1 /* Copyright (C) 2003-2013 Free Software Foundation, Inc.
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
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
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>
26
27 #if !defined(__NR_nanosleep) && defined(SYS_nanosleep)
28 # define __NR_nanosleep SYS_nanosleep
29 #endif
30
31 #define wait_code() \
32 do { \
33 struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 }; \
34 while (syscall (__NR_nanosleep, &ts, &ts) < 0) \
35 /* nothing */; \
36 } while (0)
37
38
39 #ifdef WAIT_IN_CHILD
40 static pthread_barrier_t b;
41 #endif
42
43
44 static void *
45 tf1 (void *arg)
46 {
47 #ifdef WAIT_IN_CHILD
48 int e = pthread_barrier_wait (&b);
49 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
50 {
51 printf ("%s: barrier_wait failed\n", __func__);
52 exit (1);
53 }
54
55 wait_code ();
56 #endif
57
58 pthread_join ((pthread_t) arg, NULL);
59
60 exit (42);
61 }
62
63
64 static void *
65 tf2 (void *arg)
66 {
67 #ifdef WAIT_IN_CHILD
68 int e = pthread_barrier_wait (&b);
69 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
70 {
71 printf ("%s: barrier_wait failed\n", __func__);
72 exit (1);
73 }
74
75 wait_code ();
76 #endif
77 pthread_join ((pthread_t) arg, NULL);
78
79 exit (43);
80 }
81
82
83 static int
84 do_test (void)
85 {
86 #ifdef WAIT_IN_CHILD
87 if (pthread_barrier_init (&b, NULL, 2) != 0)
88 {
89 puts ("barrier_init failed");
90 return 1;
91 }
92 #endif
93
94 pthread_t th;
95
96 int err = pthread_join (pthread_self (), NULL);
97 if (err == 0)
98 {
99 puts ("1st circular join succeeded");
100 return 1;
101 }
102 if (err != EDEADLK)
103 {
104 printf ("1st circular join %d, not EDEADLK\n", err);
105 return 1;
106 }
107
108 if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
109 {
110 puts ("1st create failed");
111 return 1;
112 }
113
114 #ifndef WAIT_IN_CHILD
115 wait_code ();
116 #endif
117
118 if (pthread_cancel (th) != 0)
119 {
120 puts ("cannot cancel 1st thread");
121 return 1;
122 }
123
124 #ifdef WAIT_IN_CHILD
125 int e = pthread_barrier_wait (&b);
126 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
127 {
128 printf ("%s: barrier_wait failed\n", __func__);
129 return 1;
130 }
131 #endif
132
133 void *r;
134 err = pthread_join (th, &r);
135 if (err != 0)
136 {
137 printf ("cannot join 1st thread: %d\n", err);
138 return 1;
139 }
140 if (r != PTHREAD_CANCELED)
141 {
142 puts ("1st thread not canceled");
143 return 1;
144 }
145
146 err = pthread_join (pthread_self (), NULL);
147 if (err == 0)
148 {
149 puts ("2nd circular join succeeded");
150 return 1;
151 }
152 if (err != EDEADLK)
153 {
154 printf ("2nd circular join %d, not EDEADLK\n", err);
155 return 1;
156 }
157
158 if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
159 {
160 puts ("2nd create failed");
161 return 1;
162 }
163
164 #ifndef WAIT_IN_CHILD
165 wait_code ();
166 #endif
167
168 if (pthread_cancel (th) != 0)
169 {
170 puts ("cannot cancel 2nd thread");
171 return 1;
172 }
173
174 #ifdef WAIT_IN_CHILD
175 e = pthread_barrier_wait (&b);
176 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
177 {
178 printf ("%s: barrier_wait failed\n", __func__);
179 return 1;
180 }
181 #endif
182
183 if (pthread_join (th, &r) != 0)
184 {
185 puts ("cannot join 2nd thread");
186 return 1;
187 }
188 if (r != PTHREAD_CANCELED)
189 {
190 puts ("2nd thread not canceled");
191 return 1;
192 }
193
194 err = pthread_join (pthread_self (), NULL);
195 if (err == 0)
196 {
197 puts ("3rd circular join succeeded");
198 return 1;
199 }
200 if (err != EDEADLK)
201 {
202 printf ("3rd circular join %d, not EDEADLK\n", err);
203 return 1;
204 }
205
206 return 0;
207 }
208
209 #define TEST_FUNCTION do_test ()
210 #include "../test-skeleton.c"