1/* Copyright (C) 2003-2017 Free Software Foundation, Inc.
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
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <endian.h>
20#include <errno.h>
21#include <sysdep.h>
22#include <futex-internal.h>
23#include <pthread.h>
24#include <pthreadP.h>
25#include <stap-probe.h>
26#include <atomic.h>
27
28#include <shlib-compat.h>
29
30#include "pthread_cond_common.c"
31
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. */
38int
39__pthread_cond_broadcast (pthread_cond_t *cond)
40{
41 LIBC_PROBE (cond_broadcast, 1, cond);
42
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);
49
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)
58 {
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);
69 }
70
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);
87
88 return 0;
89}
90
91versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
92 GLIBC_2_3_2);
93