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.
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/>. */
22 #include <futex-internal.h>
25 #include <stap-probe.h>
28 #include <shlib-compat.h>
30 #include "pthread_cond_common.c"
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. */
39 __pthread_cond_broadcast (pthread_cond_t
*cond
)
41 LIBC_PROBE (cond_broadcast
, 1, cond
);
43 unsigned int wrefs
= atomic_load_relaxed (&cond
->__data
.__wrefs
);
46 int private = __condvar_get_private (wrefs
);
48 __condvar_acquire_lock (cond
, private);
50 unsigned long long int wseq
= __condvar_load_wseq_relaxed (cond
);
51 unsigned int g2
= wseq
& 1;
52 unsigned int g1
= g2
^ 1;
54 bool do_futex_wake
= false;
56 /* Step (1): signal all waiters remaining in G1. */
57 if (cond
->__data
.__g_size
[g1
] != 0)
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;
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);
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))
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. */
83 __condvar_release_lock (cond
, private);
86 futex_wake (cond
->__data
.__g_signals
+ g1
, INT_MAX
, private);
91 versioned_symbol (libpthread
, __pthread_cond_broadcast
, pthread_cond_broadcast
,