hurd: Fix typo
[glibc.git] / nptl / pthread_cond_destroy.c
blobfc65dca78c04436cc9d3f17be2cf6ef338061daf
1 /* Copyright (C) 2002-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <shlib-compat.h>
21 #include "pthreadP.h"
22 #include <stap-probe.h>
23 #include <atomic.h>
24 #include <futex-internal.h>
26 #include "pthread_cond_common.c"
28 /* See __pthread_cond_wait for a high-level description of the algorithm.
30 A correct program must make sure that no waiters are blocked on the condvar
31 when it is destroyed, and that there are no concurrent signals or
32 broadcasts. To wake waiters reliably, the program must signal or
33 broadcast while holding the mutex or after having held the mutex. It must
34 also ensure that no signal or broadcast are still pending to unblock
35 waiters; IOW, because waiters can wake up spuriously, the program must
36 effectively ensure that destruction happens after the execution of those
37 signal or broadcast calls.
38 Thus, we can assume that all waiters that are still accessing the condvar
39 have been woken. We wait until they have confirmed to have woken up by
40 decrementing __wrefs. */
41 int
42 __pthread_cond_destroy (pthread_cond_t *cond)
44 LIBC_PROBE (cond_destroy, 1, cond);
46 /* Set the wake request flag. We could also spin, but destruction that is
47 concurrent with still-active waiters is probably neither common nor
48 performance critical. Acquire MO to synchronize with waiters confirming
49 that they finished. */
50 unsigned int wrefs = atomic_fetch_or_acquire (&cond->__data.__wrefs, 4);
51 int private = __condvar_get_private (wrefs);
52 while (wrefs >> 3 != 0)
54 futex_wait_simple (&cond->__data.__wrefs, wrefs, private);
55 /* See above. */
56 wrefs = atomic_load_acquire (&cond->__data.__wrefs);
58 /* The memory the condvar occupies can now be reused. */
59 return 0;
61 versioned_symbol (libpthread, __pthread_cond_destroy,
62 pthread_cond_destroy, GLIBC_2_3_2);