1 /* Copyright (C) 2003-2022 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 /* This test checks behavior not required by POSIX. */
25 #include <elf/dl-tunables.h>
27 static pthread_mutex_t
*m
;
28 static pthread_barrier_t b
;
29 static pthread_cond_t c
;
36 if (pthread_mutex_unlock (m
) != 0)
38 puts ("cl: mutex_unlocked failed");
47 if (pthread_mutex_lock (m
) != 0)
49 puts ("tf: mutex_lock failed");
53 int e
= pthread_barrier_wait (&b
);
54 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
56 puts ("barrier_wait failed");
62 if (pthread_cond_wait (&c
, m
) != 0)
64 puts ("tf: cond_wait failed");
71 pthread_cleanup_push (cl
, NULL
);
73 if (pthread_cond_wait (&c
, m
) != 0)
75 puts ("tf: cond_wait failed");
79 pthread_cleanup_pop (0);
83 if (pthread_mutex_unlock (m
) != 0)
85 puts ("tf: mutex_unlock failed");
94 check_type (const char *mas
, pthread_mutexattr_t
*ma
)
98 /* Check if a mutex will be elided. Lock elision can only be activated via
99 the tunables framework. By default, lock elision is disabled. */
100 bool assume_elided_mutex
= false;
102 int ma_type
= PTHREAD_MUTEX_TIMED_NP
;
105 e
= pthread_mutexattr_gettype (ma
, &ma_type
);
108 printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e
);
112 if (ma_type
== PTHREAD_MUTEX_TIMED_NP
)
114 /* This type of mutex can be elided if elision is enabled via the tunables
115 framework. Some tests below are failing if the mutex is elided.
116 Thus we only run those if we assume that the mutex won't be elided. */
117 if (TUNABLE_GET_FULL (glibc
, elision
, enable
, int32_t, NULL
) == 1)
118 assume_elided_mutex
= true;
122 e
= pthread_mutex_init (m
, ma
);
128 puts ("PI mutexes unsupported");
132 printf ("1st mutex_init failed for %s\n", mas
);
136 if (pthread_mutex_destroy (m
) != 0)
138 printf ("immediate mutex_destroy failed for %s\n", mas
);
142 if (pthread_mutex_init (m
, ma
) != 0)
144 printf ("2nd mutex_init failed for %s\n", mas
);
148 if (pthread_mutex_lock (m
) != 0)
150 printf ("1st mutex_lock failed for %s\n", mas
);
154 /* Elided mutexes don't fail destroy, thus only test this if we don't assume
156 if (assume_elided_mutex
== false)
158 e
= pthread_mutex_destroy (m
);
161 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas
);
167 mutex_destroy of self-locked mutex did not return EBUSY %s\n",
173 if (pthread_mutex_unlock (m
) != 0)
175 printf ("1st mutex_unlock failed for %s\n", mas
);
179 if (pthread_mutex_trylock (m
) != 0)
181 printf ("mutex_trylock failed for %s\n", mas
);
185 /* Elided mutexes don't fail destroy. */
186 if (assume_elided_mutex
== false)
188 e
= pthread_mutex_destroy (m
);
191 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n",
198 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
204 if (pthread_mutex_unlock (m
) != 0)
206 printf ("2nd mutex_unlock failed for %s\n", mas
);
211 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
213 puts ("1st create failed");
218 e
= pthread_barrier_wait (&b
);
219 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
221 puts ("1st barrier_wait failed");
225 if (pthread_mutex_lock (m
) != 0)
227 printf ("2nd mutex_lock failed for %s\n", mas
);
231 if (pthread_mutex_unlock (m
) != 0)
233 printf ("3rd mutex_unlock failed for %s\n", mas
);
237 /* Elided mutexes don't fail destroy. */
238 if (assume_elided_mutex
== false)
240 e
= pthread_mutex_destroy (m
);
243 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n",
250 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas
);
256 if (pthread_cond_signal (&c
) != 0)
258 puts ("cond_signal failed");
263 if (pthread_join (th
, &r
) != 0)
265 puts ("join failed");
270 puts ("thread didn't return NULL");
274 if (pthread_mutex_destroy (m
) != 0)
276 printf ("mutex_destroy after condvar-use failed for %s\n", mas
);
280 if (pthread_mutex_init (m
, ma
) != 0)
282 printf ("3rd mutex_init failed for %s\n", mas
);
286 if (pthread_create (&th
, NULL
, tf
, (void *) 1) != 0)
288 puts ("2nd create failed");
293 e
= pthread_barrier_wait (&b
);
294 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
296 puts ("2nd barrier_wait failed");
300 if (pthread_mutex_lock (m
) != 0)
302 printf ("3rd mutex_lock failed for %s\n", mas
);
306 if (pthread_mutex_unlock (m
) != 0)
308 printf ("4th mutex_unlock failed for %s\n", mas
);
312 /* Elided mutexes don't fail destroy. */
313 if (assume_elided_mutex
== false)
315 e
= pthread_mutex_destroy (m
);
318 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
325 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
331 if (pthread_cancel (th
) != 0)
333 puts ("cond_cancel failed");
337 if (pthread_join (th
, &r
) != 0)
339 puts ("join failed");
342 if (r
!= PTHREAD_CANCELED
)
344 puts ("thread not canceled");
348 if (pthread_mutex_destroy (m
) != 0)
350 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas
);
364 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
366 puts ("barrier_init failed");
370 if (pthread_cond_init (&c
, NULL
) != 0)
372 puts ("cond_init failed");
376 puts ("check normal mutex");
377 int res
= check_type ("normal", NULL
);
379 pthread_mutexattr_t ma
;
380 if (pthread_mutexattr_init (&ma
) != 0)
382 puts ("1st mutexattr_init failed");
385 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_RECURSIVE
) != 0)
387 puts ("1st mutexattr_settype failed");
391 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
393 puts ("1st pthread_mutexattr_setprotocol failed");
397 puts ("check recursive mutex");
398 res
|= check_type ("recursive", &ma
);
399 if (pthread_mutexattr_destroy (&ma
) != 0)
401 puts ("1st mutexattr_destroy failed");
405 if (pthread_mutexattr_init (&ma
) != 0)
407 puts ("2nd mutexattr_init failed");
410 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_ERRORCHECK
) != 0)
412 puts ("2nd mutexattr_settype failed");
416 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
418 puts ("2nd pthread_mutexattr_setprotocol failed");
422 puts ("check error-checking mutex");
423 res
|= check_type ("error-checking", &ma
);
424 if (pthread_mutexattr_destroy (&ma
) != 0)
426 puts ("2nd mutexattr_destroy failed");
433 #define TEST_FUNCTION do_test ()
434 #include "../test-skeleton.c"