powerpc64le: use common fmaf128 implementation
[glibc.git] / nptl / tst-mutex8.c
blob8e56ea5a3a57d967edbd2460ce900414d433e264
1 /* Copyright (C) 2003-2020 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 <https://www.gnu.org/licenses/>. */
19 /* This test checks behavior not required by POSIX. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <elf/dl-tunables.h>
28 static pthread_mutex_t *m;
29 static pthread_barrier_t b;
30 static pthread_cond_t c;
31 static bool done;
34 static void
35 cl (void *arg)
37 if (pthread_mutex_unlock (m) != 0)
39 puts ("cl: mutex_unlocked failed");
40 exit (1);
45 static void *
46 tf (void *arg)
48 if (pthread_mutex_lock (m) != 0)
50 puts ("tf: mutex_lock failed");
51 return (void *) 1l;
54 int e = pthread_barrier_wait (&b);
55 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
57 puts ("barrier_wait failed");
58 return (void *) 1l;
61 if (arg == NULL)
63 if (pthread_cond_wait (&c, m) != 0)
65 puts ("tf: cond_wait failed");
66 return (void *) 1l;
68 while (! done);
69 else
72 pthread_cleanup_push (cl, NULL);
74 if (pthread_cond_wait (&c, m) != 0)
76 puts ("tf: cond_wait failed");
77 return (void *) 1l;
80 pthread_cleanup_pop (0);
82 while (! done);
84 if (pthread_mutex_unlock (m) != 0)
86 puts ("tf: mutex_unlock failed");
87 return (void *) 1l;
90 return NULL;
94 static int
95 check_type (const char *mas, pthread_mutexattr_t *ma)
97 int e;
99 /* Check if a mutex will be elided. Lock elision can only be activated via
100 the tunables framework. By default, lock elision is disabled. */
101 bool assume_elided_mutex = false;
102 #if HAVE_TUNABLES
103 int ma_type = PTHREAD_MUTEX_TIMED_NP;
104 if (ma != NULL)
106 e = pthread_mutexattr_gettype (ma, &ma_type);
107 if (e != 0)
109 printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e);
110 return 1;
113 if (ma_type == PTHREAD_MUTEX_TIMED_NP)
115 /* This type of mutex can be elided if elision is enabled via the tunables
116 framework. Some tests below are failing if the mutex is elided.
117 Thus we only run those if we assume that the mutex won't be elided. */
118 if (TUNABLE_GET_FULL (glibc, elision, enable, int32_t, NULL) == 1)
119 assume_elided_mutex = true;
121 #endif
123 e = pthread_mutex_init (m, ma);
124 if (e != 0)
126 #ifdef ENABLE_PI
127 if (e == ENOTSUP)
129 puts ("PI mutexes unsupported");
130 return 0;
132 #endif
133 printf ("1st mutex_init failed for %s\n", mas);
134 return 1;
137 if (pthread_mutex_destroy (m) != 0)
139 printf ("immediate mutex_destroy failed for %s\n", mas);
140 return 1;
143 if (pthread_mutex_init (m, ma) != 0)
145 printf ("2nd mutex_init failed for %s\n", mas);
146 return 1;
149 if (pthread_mutex_lock (m) != 0)
151 printf ("1st mutex_lock failed for %s\n", mas);
152 return 1;
155 /* Elided mutexes don't fail destroy, thus only test this if we don't assume
156 elision. */
157 if (assume_elided_mutex == false)
159 e = pthread_mutex_destroy (m);
160 if (e == 0)
162 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas);
163 return 1;
165 if (e != EBUSY)
167 printf ("\
168 mutex_destroy of self-locked mutex did not return EBUSY %s\n",
169 mas);
170 return 1;
174 if (pthread_mutex_unlock (m) != 0)
176 printf ("1st mutex_unlock failed for %s\n", mas);
177 return 1;
180 if (pthread_mutex_trylock (m) != 0)
182 printf ("mutex_trylock failed for %s\n", mas);
183 return 1;
186 /* Elided mutexes don't fail destroy. */
187 if (assume_elided_mutex == false)
189 e = pthread_mutex_destroy (m);
190 if (e == 0)
192 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n",
193 mas);
194 return 1;
196 if (e != EBUSY)
198 printf ("\
199 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
200 mas);
201 return 1;
205 if (pthread_mutex_unlock (m) != 0)
207 printf ("2nd mutex_unlock failed for %s\n", mas);
208 return 1;
211 pthread_t th;
212 if (pthread_create (&th, NULL, tf, NULL) != 0)
214 puts ("1st create failed");
215 return 1;
217 done = false;
219 e = pthread_barrier_wait (&b);
220 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
222 puts ("1st barrier_wait failed");
223 return 1;
226 if (pthread_mutex_lock (m) != 0)
228 printf ("2nd mutex_lock failed for %s\n", mas);
229 return 1;
232 if (pthread_mutex_unlock (m) != 0)
234 printf ("3rd mutex_unlock failed for %s\n", mas);
235 return 1;
238 /* Elided mutexes don't fail destroy. */
239 if (assume_elided_mutex == false)
241 e = pthread_mutex_destroy (m);
242 if (e == 0)
244 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n",
245 mas);
246 return 1;
248 if (e != EBUSY)
250 printf ("\
251 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas);
252 return 1;
256 done = true;
257 if (pthread_cond_signal (&c) != 0)
259 puts ("cond_signal failed");
260 return 1;
263 void *r;
264 if (pthread_join (th, &r) != 0)
266 puts ("join failed");
267 return 1;
269 if (r != NULL)
271 puts ("thread didn't return NULL");
272 return 1;
275 if (pthread_mutex_destroy (m) != 0)
277 printf ("mutex_destroy after condvar-use failed for %s\n", mas);
278 return 1;
281 if (pthread_mutex_init (m, ma) != 0)
283 printf ("3rd mutex_init failed for %s\n", mas);
284 return 1;
287 if (pthread_create (&th, NULL, tf, (void *) 1) != 0)
289 puts ("2nd create failed");
290 return 1;
292 done = false;
294 e = pthread_barrier_wait (&b);
295 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
297 puts ("2nd barrier_wait failed");
298 return 1;
301 if (pthread_mutex_lock (m) != 0)
303 printf ("3rd mutex_lock failed for %s\n", mas);
304 return 1;
307 if (pthread_mutex_unlock (m) != 0)
309 printf ("4th mutex_unlock failed for %s\n", mas);
310 return 1;
313 /* Elided mutexes don't fail destroy. */
314 if (assume_elided_mutex == false)
316 e = pthread_mutex_destroy (m);
317 if (e == 0)
319 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
320 mas);
321 return 1;
323 if (e != EBUSY)
325 printf ("\
326 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
327 mas);
328 return 1;
332 if (pthread_cancel (th) != 0)
334 puts ("cond_cancel failed");
335 return 1;
338 if (pthread_join (th, &r) != 0)
340 puts ("join failed");
341 return 1;
343 if (r != PTHREAD_CANCELED)
345 puts ("thread not canceled");
346 return 1;
349 if (pthread_mutex_destroy (m) != 0)
351 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas);
352 return 1;
355 return 0;
359 static int
360 do_test (void)
362 pthread_mutex_t mm;
363 m = &mm;
365 if (pthread_barrier_init (&b, NULL, 2) != 0)
367 puts ("barrier_init failed");
368 return 1;
371 if (pthread_cond_init (&c, NULL) != 0)
373 puts ("cond_init failed");
374 return 1;
377 puts ("check normal mutex");
378 int res = check_type ("normal", NULL);
380 pthread_mutexattr_t ma;
381 if (pthread_mutexattr_init (&ma) != 0)
383 puts ("1st mutexattr_init failed");
384 return 1;
386 if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0)
388 puts ("1st mutexattr_settype failed");
389 return 1;
391 #ifdef ENABLE_PI
392 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT))
394 puts ("1st pthread_mutexattr_setprotocol failed");
395 return 1;
397 #endif
398 puts ("check recursive mutex");
399 res |= check_type ("recursive", &ma);
400 if (pthread_mutexattr_destroy (&ma) != 0)
402 puts ("1st mutexattr_destroy failed");
403 return 1;
406 if (pthread_mutexattr_init (&ma) != 0)
408 puts ("2nd mutexattr_init failed");
409 return 1;
411 if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
413 puts ("2nd mutexattr_settype failed");
414 return 1;
416 #ifdef ENABLE_PI
417 if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT))
419 puts ("2nd pthread_mutexattr_setprotocol failed");
420 return 1;
422 #endif
423 puts ("check error-checking mutex");
424 res |= check_type ("error-checking", &ma);
425 if (pthread_mutexattr_destroy (&ma) != 0)
427 puts ("2nd mutexattr_destroy failed");
428 return 1;
431 return res;
434 #define TEST_FUNCTION do_test ()
435 #include "../test-skeleton.c"