]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_cond_destroy.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / pthread_cond_destroy.c
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749 18
73f7c32c 19#include <errno.h>
bf293afe 20#include <shlib-compat.h>
76a50749 21#include "pthreadP.h"
5acf7263 22#include <stap-probe.h>
ed19993b
TR
23#include <atomic.h>
24#include <futex-internal.h>
25
26#include "pthread_cond_common.c"
27
28/* See __pthread_cond_wait for a high-level description of the algorithm.
29
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. */
76a50749 41int
9d46370c 42__pthread_cond_destroy (pthread_cond_t *cond)
76a50749 43{
5acf7263
RM
44 LIBC_PROBE (cond_destroy, 1, cond);
45
ed19993b
TR
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)
73f7c32c 53 {
ed19993b
TR
54 futex_wait_simple (&cond->__data.__wrefs, wrefs, private);
55 /* See above. */
56 wrefs = atomic_load_acquire (&cond->__data.__wrefs);
73f7c32c 57 }
ed19993b 58 /* The memory the condvar occupies can now be reused. */
76a50749
UD
59 return 0;
60}
bf293afe
UD
61versioned_symbol (libpthread, __pthread_cond_destroy,
62 pthread_cond_destroy, GLIBC_2_3_2);