x86-64: Don't use SSE resolvers for ISA level 3 or above
[glibc.git] / sysdeps / pthread / tst-robust1.c
blob9c562dd45b15859c42c4674adb0b7146b7b265f1
1 /* Copyright (C) 2005-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
24 static pthread_mutex_t m1;
25 static pthread_mutex_t m2;
26 static pthread_barrier_t b;
29 #ifndef LOCK
30 # define LOCK(m) pthread_mutex_lock (m)
31 #endif
34 static void *
35 tf (void *arg)
37 long int round = (long int) arg;
39 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
41 printf ("%ld: setcancelstate failed\n", round);
42 exit (1);
45 int e = LOCK (&m1);
46 if (e != 0)
48 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round, e);
49 exit (1);
52 e = LOCK (&m2);
53 if (e != 0)
55 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round, e);
56 exit (1);
59 e = pthread_barrier_wait (&b);
60 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
62 printf ("%ld: child: 1st barrier_wait failed\n", round);
63 exit (1);
66 e = pthread_barrier_wait (&b);
67 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
69 printf ("%ld: child: 2nd barrier_wait failed\n", round);
70 exit (1);
73 pthread_testcancel ();
75 printf ("%ld: testcancel returned\n", round);
76 exit (1);
80 static int
81 do_test (void)
83 #ifdef PREPARE_TMO
84 PREPARE_TMO;
85 #endif
87 pthread_mutexattr_t a;
88 if (pthread_mutexattr_init (&a) != 0)
90 puts ("mutexattr_init failed");
91 return 1;
93 if (pthread_mutexattr_setrobust (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
95 puts ("mutexattr_setrobust failed");
96 return 1;
99 #ifdef ENABLE_PI
100 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
102 puts ("pthread_mutexattr_setprotocol failed");
103 return 1;
105 else
107 int e = pthread_mutex_init (&m1, &a);
108 if (e == ENOTSUP)
110 puts ("PI robust mutexes not supported");
111 return 0;
113 else if (e != 0)
115 puts ("mutex_init m1 failed");
116 return 1;
118 pthread_mutex_destroy (&m1);
120 #endif
122 #ifndef NOT_CONSISTENT
123 if (pthread_mutex_init (&m1, &a) != 0)
125 puts ("mutex_init m1 failed");
126 return 1;
129 if (pthread_mutex_init (&m2, &a) != 0)
131 puts ("mutex_init m2 failed");
132 return 1;
134 #endif
136 if (pthread_barrier_init (&b, NULL, 2) != 0)
138 puts ("barrier_init failed");
139 return 1;
142 for (long int round = 1; round < 5; ++round)
144 #ifdef NOT_CONSISTENT
145 if (pthread_mutex_init (&m1 , &a) != 0)
147 puts ("mutex_init m1 failed");
148 return 1;
150 if (pthread_mutex_init (&m2 , &a) != 0)
152 puts ("mutex_init m2 failed");
153 return 1;
155 #endif
157 pthread_t th;
158 if (pthread_create (&th, NULL, tf, (void *) round) != 0)
160 printf ("%ld: create failed\n", round);
161 return 1;
164 int e = pthread_barrier_wait (&b);
165 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
167 printf ("%ld: parent: 1st barrier_wait failed\n", round);
168 return 1;
171 if (pthread_cancel (th) != 0)
173 printf ("%ld: cancel failed\n", round);
174 return 1;
177 e = pthread_barrier_wait (&b);
178 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
180 printf ("%ld: parent: 2nd barrier_wait failed\n", round);
181 return 1;
184 #ifndef AFTER_JOIN
185 if (round & 1)
186 #endif
188 void *res;
189 if (pthread_join (th, &res) != 0)
191 printf ("%ld: join failed\n", round);
192 return 1;
194 if (res != PTHREAD_CANCELED)
196 printf ("%ld: thread not canceled\n", round);
197 return 1;
201 e = LOCK (&m1);
202 if (e == 0)
204 printf ("%ld: parent: mutex_lock m1 succeeded\n", round);
205 return 1;
207 if (e != EOWNERDEAD)
209 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round);
210 return 1;
213 e = LOCK (&m2);
214 if (e == 0)
216 printf ("%ld: parent: mutex_lock m2 succeeded\n", round);
217 return 1;
219 if (e != EOWNERDEAD)
221 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round);
222 return 1;
225 #ifndef AFTER_JOIN
226 if ((round & 1) == 0)
228 void *res;
229 if (pthread_join (th, &res) != 0)
231 printf ("%ld: join failed\n", round);
232 return 1;
234 if (res != PTHREAD_CANCELED)
236 printf ("%ld: thread not canceled\n", round);
237 return 1;
240 #endif
242 #ifndef NOT_CONSISTENT
243 e = pthread_mutex_consistent (&m1);
244 if (e != 0)
246 printf ("%ld: mutex_consistent m1 failed with error %d\n", round, e);
247 return 1;
250 e = pthread_mutex_consistent (&m2);
251 if (e != 0)
253 printf ("%ld: mutex_consistent m2 failed with error %d\n", round, e);
254 return 1;
256 #endif
258 e = pthread_mutex_unlock (&m1);
259 if (e != 0)
261 printf ("%ld: mutex_unlock m1 failed with %d\n", round, e);
262 return 1;
265 e = pthread_mutex_unlock (&m2);
266 if (e != 0)
268 printf ("%ld: mutex_unlock m2 failed with %d\n", round, e);
269 return 1;
272 #ifdef NOT_CONSISTENT
273 e = LOCK (&m1);
274 if (e == 0)
276 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round);
277 return 1;
279 if (e != ENOTRECOVERABLE)
281 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
282 round, e);
283 return 1;
286 if (pthread_mutex_destroy (&m1) != 0)
288 puts ("mutex_destroy m1 failed");
289 return 1;
292 e = LOCK (&m2);
293 if (e == 0)
295 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round);
296 return 1;
298 if (e != ENOTRECOVERABLE)
300 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
301 round, e);
302 return 1;
305 if (pthread_mutex_destroy (&m2) != 0)
307 puts ("mutex_destroy m2 failed");
308 return 1;
310 #endif
313 #ifndef NOT_CONSISTENT
314 if (pthread_mutex_destroy (&m1) != 0)
316 puts ("mutex_destroy m1 failed");
317 return 1;
320 if (pthread_mutex_destroy (&m2) != 0)
322 puts ("mutex_destroy m2 failed");
323 return 1;
325 #endif
327 if (pthread_mutexattr_destroy (&a) != 0)
329 puts ("mutexattr_destroy failed");
330 return 1;
333 return 0;
336 #define TEST_FUNCTION do_test ()
337 #include "../test-skeleton.c"