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. */
26 #include <elf/dl-tunables.h>
28 static pthread_mutex_t
*m
;
29 static pthread_barrier_t b
;
30 static pthread_cond_t c
;
37 if (pthread_mutex_unlock (m
) != 0)
39 puts ("cl: mutex_unlocked failed");
48 if (pthread_mutex_lock (m
) != 0)
50 puts ("tf: mutex_lock failed");
54 int e
= pthread_barrier_wait (&b
);
55 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
57 puts ("barrier_wait failed");
63 if (pthread_cond_wait (&c
, m
) != 0)
65 puts ("tf: cond_wait failed");
72 pthread_cleanup_push (cl
, NULL
);
74 if (pthread_cond_wait (&c
, m
) != 0)
76 puts ("tf: cond_wait failed");
80 pthread_cleanup_pop (0);
84 if (pthread_mutex_unlock (m
) != 0)
86 puts ("tf: mutex_unlock failed");
95 check_type (const char *mas
, pthread_mutexattr_t
*ma
)
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;
103 int ma_type
= PTHREAD_MUTEX_TIMED_NP
;
106 e
= pthread_mutexattr_gettype (ma
, &ma_type
);
109 printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e
);
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;
123 e
= pthread_mutex_init (m
, ma
);
129 puts ("PI mutexes unsupported");
133 printf ("1st mutex_init failed for %s\n", mas
);
137 if (pthread_mutex_destroy (m
) != 0)
139 printf ("immediate mutex_destroy failed for %s\n", mas
);
143 if (pthread_mutex_init (m
, ma
) != 0)
145 printf ("2nd mutex_init failed for %s\n", mas
);
149 if (pthread_mutex_lock (m
) != 0)
151 printf ("1st mutex_lock failed for %s\n", mas
);
155 /* Elided mutexes don't fail destroy, thus only test this if we don't assume
157 if (assume_elided_mutex
== false)
159 e
= pthread_mutex_destroy (m
);
162 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas
);
168 mutex_destroy of self-locked mutex did not return EBUSY %s\n",
174 if (pthread_mutex_unlock (m
) != 0)
176 printf ("1st mutex_unlock failed for %s\n", mas
);
180 if (pthread_mutex_trylock (m
) != 0)
182 printf ("mutex_trylock failed for %s\n", mas
);
186 /* Elided mutexes don't fail destroy. */
187 if (assume_elided_mutex
== false)
189 e
= pthread_mutex_destroy (m
);
192 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n",
199 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
205 if (pthread_mutex_unlock (m
) != 0)
207 printf ("2nd mutex_unlock failed for %s\n", mas
);
212 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
214 puts ("1st create failed");
219 e
= pthread_barrier_wait (&b
);
220 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
222 puts ("1st barrier_wait failed");
226 if (pthread_mutex_lock (m
) != 0)
228 printf ("2nd mutex_lock failed for %s\n", mas
);
232 if (pthread_mutex_unlock (m
) != 0)
234 printf ("3rd mutex_unlock failed for %s\n", mas
);
238 /* Elided mutexes don't fail destroy. */
239 if (assume_elided_mutex
== false)
241 e
= pthread_mutex_destroy (m
);
244 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n",
251 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas
);
257 if (pthread_cond_signal (&c
) != 0)
259 puts ("cond_signal failed");
264 if (pthread_join (th
, &r
) != 0)
266 puts ("join failed");
271 puts ("thread didn't return NULL");
275 if (pthread_mutex_destroy (m
) != 0)
277 printf ("mutex_destroy after condvar-use failed for %s\n", mas
);
281 if (pthread_mutex_init (m
, ma
) != 0)
283 printf ("3rd mutex_init failed for %s\n", mas
);
287 if (pthread_create (&th
, NULL
, tf
, (void *) 1) != 0)
289 puts ("2nd create failed");
294 e
= pthread_barrier_wait (&b
);
295 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
297 puts ("2nd barrier_wait failed");
301 if (pthread_mutex_lock (m
) != 0)
303 printf ("3rd mutex_lock failed for %s\n", mas
);
307 if (pthread_mutex_unlock (m
) != 0)
309 printf ("4th mutex_unlock failed for %s\n", mas
);
313 /* Elided mutexes don't fail destroy. */
314 if (assume_elided_mutex
== false)
316 e
= pthread_mutex_destroy (m
);
319 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
326 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
332 if (pthread_cancel (th
) != 0)
334 puts ("cond_cancel failed");
338 if (pthread_join (th
, &r
) != 0)
340 puts ("join failed");
343 if (r
!= PTHREAD_CANCELED
)
345 puts ("thread not canceled");
349 if (pthread_mutex_destroy (m
) != 0)
351 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas
);
365 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
367 puts ("barrier_init failed");
371 if (pthread_cond_init (&c
, NULL
) != 0)
373 puts ("cond_init failed");
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");
386 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_RECURSIVE
) != 0)
388 puts ("1st mutexattr_settype failed");
392 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
394 puts ("1st pthread_mutexattr_setprotocol failed");
398 puts ("check recursive mutex");
399 res
|= check_type ("recursive", &ma
);
400 if (pthread_mutexattr_destroy (&ma
) != 0)
402 puts ("1st mutexattr_destroy failed");
406 if (pthread_mutexattr_init (&ma
) != 0)
408 puts ("2nd mutexattr_init failed");
411 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_ERRORCHECK
) != 0)
413 puts ("2nd mutexattr_settype failed");
417 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
419 puts ("2nd pthread_mutexattr_setprotocol failed");
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");
434 #define TEST_FUNCTION do_test ()
435 #include "../test-skeleton.c"