1/* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18#include <endian.h>
19#include <errno.h>
20#include <sysdep.h>
21#include <futex-internal.h>
22#include <pthread.h>
23#include <pthreadP.h>
24#include <stap-probe.h>
25#include <atomic.h>
26
27#include <shlib-compat.h>
28
29#include "pthread_cond_common.c"
30
31
32/* We do the following steps from __pthread_cond_signal in one critical
33 section: (1) signal all waiters in G1, (2) close G1 so that it can become
34 the new G2 and make G2 the new G1, and (3) signal all waiters in the new
35 G1. We don't need to do all these steps if there are no waiters in G1
36 and/or G2. See __pthread_cond_signal for further details. */
37int
38___pthread_cond_broadcast (pthread_cond_t *cond)
39{
40 LIBC_PROBE (cond_broadcast, 1, cond);
41
42 unsigned int wrefs = atomic_load_relaxed (&cond->__data.__wrefs);
43 if (wrefs >> 3 == 0)
44 return 0;
45 int private = __condvar_get_private (flags: wrefs);
46
47 __condvar_acquire_lock (cond, private);
48
49 unsigned long long int wseq = __condvar_load_wseq_relaxed (cond);
50 unsigned int g2 = wseq & 1;
51 unsigned int g1 = g2 ^ 1;
52 wseq >>= 1;
53 bool do_futex_wake = false;
54
55 /* Step (1): signal all waiters remaining in G1. */
56 if (cond->__data.__g_size[g1] != 0)
57 {
58 /* Add as many signals as the remaining size of the group. */
59 atomic_fetch_add_relaxed (cond->__data.__g_signals + g1,
60 cond->__data.__g_size[g1] << 1);
61 cond->__data.__g_size[g1] = 0;
62
63 /* We need to wake G1 waiters before we quiesce G1 below. */
64 /* TODO Only set it if there are indeed futex waiters. We could
65 also try to move this out of the critical section in cases when
66 G2 is empty (and we don't need to quiesce). */
67 futex_wake (futex_word: cond->__data.__g_signals + g1, INT_MAX, private);
68 }
69
70 /* G1 is complete. Step (2) is next unless there are no waiters in G2, in
71 which case we can stop. */
72 if (__condvar_quiesce_and_switch_g1 (cond, wseq, g1index: &g1, private))
73 {
74 /* Step (3): Send signals to all waiters in the old G2 / new G1. */
75 atomic_fetch_add_relaxed (cond->__data.__g_signals + g1,
76 cond->__data.__g_size[g1] << 1);
77 cond->__data.__g_size[g1] = 0;
78 /* TODO Only set it if there are indeed futex waiters. */
79 do_futex_wake = true;
80 }
81
82 __condvar_release_lock (cond, private);
83
84 if (do_futex_wake)
85 futex_wake (futex_word: cond->__data.__g_signals + g1, INT_MAX, private);
86
87 return 0;
88}
89versioned_symbol (libc, ___pthread_cond_broadcast,
90 pthread_cond_broadcast, GLIBC_2_3_2);
91libc_hidden_ver (___pthread_cond_broadcast, __pthread_cond_broadcast)
92#ifndef SHARED
93strong_alias (___pthread_cond_broadcast, __pthread_cond_broadcast)
94#endif
95

source code of glibc/nptl/pthread_cond_broadcast.c