string: Hook up the default implementation on test-memchr
[glibc.git] / nptl / pthread_cond_destroy.c
blob98c12c831b961b7ea8916abc636d1defff414b4e
1 /* Copyright (C) 2002-2023 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 <shlib-compat.h>
20 #include "pthreadP.h"
21 #include <stap-probe.h>
22 #include <atomic.h>
23 #include <futex-internal.h>
25 #include "pthread_cond_common.c"
27 /* See __pthread_cond_wait for a high-level description of the algorithm.
29 A correct program must make sure that no waiters are blocked on the condvar
30 when it is destroyed, and that there are no concurrent signals or
31 broadcasts. To wake waiters reliably, the program must signal or
32 broadcast while holding the mutex or after having held the mutex. It must
33 also ensure that no signal or broadcast are still pending to unblock
34 waiters; IOW, because waiters can wake up spuriously, the program must
35 effectively ensure that destruction happens after the execution of those
36 signal or broadcast calls.
37 Thus, we can assume that all waiters that are still accessing the condvar
38 have been woken. We wait until they have confirmed to have woken up by
39 decrementing __wrefs. */
40 int
41 __pthread_cond_destroy (pthread_cond_t *cond)
43 LIBC_PROBE (cond_destroy, 1, cond);
45 /* Set the wake request flag. We could also spin, but destruction that is
46 concurrent with still-active waiters is probably neither common nor
47 performance critical. Acquire MO to synchronize with waiters confirming
48 that they finished. */
49 unsigned int wrefs = atomic_fetch_or_acquire (&cond->__data.__wrefs, 4);
50 int private = __condvar_get_private (wrefs);
51 while (wrefs >> 3 != 0)
53 futex_wait_simple (&cond->__data.__wrefs, wrefs, private);
54 /* See above. */
55 wrefs = atomic_load_acquire (&cond->__data.__wrefs);
57 /* The memory the condvar occupies can now be reused. */
58 return 0;
60 libc_hidden_def (__pthread_cond_destroy)
61 versioned_symbol (libc, __pthread_cond_destroy,
62 pthread_cond_destroy, GLIBC_2_3_2);