]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_cond_broadcast.c
Further harden glibc malloc metadata against 1-byte overflows.
[thirdparty/glibc.git] / nptl / pthread_cond_broadcast.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2003-2017 Free Software Foundation, Inc.
a88c9263
UD
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
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
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a88c9263
UD
18
19#include <endian.h>
20#include <errno.h>
21#include <sysdep.h>
ed19993b 22#include <futex-internal.h>
a88c9263
UD
23#include <pthread.h>
24#include <pthreadP.h>
5acf7263 25#include <stap-probe.h>
ed19993b 26#include <atomic.h>
a88c9263
UD
27
28#include <shlib-compat.h>
29
ed19993b 30#include "pthread_cond_common.c"
a88c9263 31
ed19993b
TR
32
33/* We do the following steps from __pthread_cond_signal in one critical
34 section: (1) signal all waiters in G1, (2) close G1 so that it can become
35 the new G2 and make G2 the new G1, and (3) signal all waiters in the new
36 G1. We don't need to do all these steps if there are no waiters in G1
37 and/or G2. See __pthread_cond_signal for further details. */
a88c9263 38int
9d46370c 39__pthread_cond_broadcast (pthread_cond_t *cond)
a88c9263 40{
5acf7263
RM
41 LIBC_PROBE (cond_broadcast, 1, cond);
42
ed19993b
TR
43 unsigned int wrefs = atomic_load_relaxed (&cond->__data.__wrefs);
44 if (wrefs >> 3 == 0)
45 return 0;
46 int private = __condvar_get_private (wrefs);
47
48 __condvar_acquire_lock (cond, private);
a88c9263 49
ed19993b
TR
50 unsigned long long int wseq = __condvar_load_wseq_relaxed (cond);
51 unsigned int g2 = wseq & 1;
52 unsigned int g1 = g2 ^ 1;
53 wseq >>= 1;
54 bool do_futex_wake = false;
55
56 /* Step (1): signal all waiters remaining in G1. */
57 if (cond->__data.__g_size[g1] != 0)
a88c9263 58 {
ed19993b
TR
59 /* Add as many signals as the remaining size of the group. */
60 atomic_fetch_add_relaxed (cond->__data.__g_signals + g1,
61 cond->__data.__g_size[g1] << 1);
62 cond->__data.__g_size[g1] = 0;
63
64 /* We need to wake G1 waiters before we quiesce G1 below. */
65 /* TODO Only set it if there are indeed futex waiters. We could
66 also try to move this out of the critical section in cases when
67 G2 is empty (and we don't need to quiesce). */
68 futex_wake (cond->__data.__g_signals + g1, INT_MAX, private);
a88c9263
UD
69 }
70
ed19993b
TR
71 /* G1 is complete. Step (2) is next unless there are no waiters in G2, in
72 which case we can stop. */
73 if (__condvar_quiesce_and_switch_g1 (cond, wseq, &g1, private))
74 {
75 /* Step (3): Send signals to all waiters in the old G2 / new G1. */
76 atomic_fetch_add_relaxed (cond->__data.__g_signals + g1,
77 cond->__data.__g_size[g1] << 1);
78 cond->__data.__g_size[g1] = 0;
79 /* TODO Only set it if there are indeed futex waiters. */
80 do_futex_wake = true;
81 }
82
83 __condvar_release_lock (cond, private);
84
85 if (do_futex_wake)
86 futex_wake (cond->__data.__g_signals + g1, INT_MAX, private);
a88c9263
UD
87
88 return 0;
89}
90
91versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
92 GLIBC_2_3_2);