elf: Add more DT_RELR tests
[glibc.git] / nptl / pthread_cond_signal.c
blob14800ba00ba926eac3bbcba29b496b60db2717be
1 /* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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 <atomic.h>
25 #include <stdint.h>
27 #include <shlib-compat.h>
28 #include <stap-probe.h>
30 #include "pthread_cond_common.c"
32 /* See __pthread_cond_wait for a high-level description of the algorithm. */
33 int
34 ___pthread_cond_signal (pthread_cond_t *cond)
36 LIBC_PROBE (cond_signal, 1, cond);
38 /* First check whether there are waiters. Relaxed MO is fine for that for
39 the same reasons that relaxed MO is fine when observing __wseq (see
40 below). */
41 unsigned int wrefs = atomic_load_relaxed (&cond->__data.__wrefs);
42 if (wrefs >> 3 == 0)
43 return 0;
44 int private = __condvar_get_private (wrefs);
46 __condvar_acquire_lock (cond, private);
48 /* Load the waiter sequence number, which represents our relative ordering
49 to any waiters. Relaxed MO is sufficient for that because:
50 1) We can pick any position that is allowed by external happens-before
51 constraints. In particular, if another __pthread_cond_wait call
52 happened before us, this waiter must be eligible for being woken by
53 us. The only way do establish such a happens-before is by signaling
54 while having acquired the mutex associated with the condvar and
55 ensuring that the signal's critical section happens after the waiter.
56 Thus, the mutex ensures that we see that waiter's __wseq increase.
57 2) Once we pick a position, we do not need to communicate this to the
58 program via a happens-before that we set up: First, any wake-up could
59 be a spurious wake-up, so the program must not interpret a wake-up as
60 an indication that the waiter happened before a particular signal;
61 second, a program cannot detect whether a waiter has not yet been
62 woken (i.e., it cannot distinguish between a non-woken waiter and one
63 that has been woken but hasn't resumed execution yet), and thus it
64 cannot try to deduce that a signal happened before a particular
65 waiter. */
66 unsigned long long int wseq = __condvar_load_wseq_relaxed (cond);
67 unsigned int g1 = (wseq & 1) ^ 1;
68 wseq >>= 1;
69 bool do_futex_wake = false;
71 /* If G1 is still receiving signals, we put the signal there. If not, we
72 check if G2 has waiters, and if so, quiesce and switch G1 to the former
73 G2; if this results in a new G1 with waiters (G2 might have cancellations
74 already, see __condvar_quiesce_and_switch_g1), we put the signal in the
75 new G1. */
76 if ((cond->__data.__g_size[g1] != 0)
77 || __condvar_quiesce_and_switch_g1 (cond, wseq, &g1, private))
79 /* Add a signal. Relaxed MO is fine because signaling does not need to
80 establish a happens-before relation (see above). We do not mask the
81 release-MO store when initializing a group in
82 __condvar_quiesce_and_switch_g1 because we use an atomic
83 read-modify-write and thus extend that store's release sequence. */
84 atomic_fetch_add_relaxed (cond->__data.__g_signals + g1, 2);
85 cond->__data.__g_size[g1]--;
86 /* TODO Only set it if there are indeed futex waiters. */
87 do_futex_wake = true;
90 __condvar_release_lock (cond, private);
92 if (do_futex_wake)
93 futex_wake (cond->__data.__g_signals + g1, 1, private);
95 return 0;
97 versioned_symbol (libpthread, ___pthread_cond_signal, pthread_cond_signal,
98 GLIBC_2_3_2);
99 libc_hidden_ver (___pthread_cond_signal, __pthread_cond_signal)
100 #ifndef SHARED
101 strong_alias (___pthread_cond_signal, __pthread_cond_signal)
102 #endif