]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2003-2024 Free Software Foundation, Inc. |
a88c9263 | 2 | This file is part of the GNU C Library. |
a88c9263 UD |
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 | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
a88c9263 UD |
17 | |
18 | #include <endian.h> | |
19 | #include <errno.h> | |
20 | #include <sysdep.h> | |
ed19993b | 21 | #include <futex-internal.h> |
a88c9263 UD |
22 | #include <pthread.h> |
23 | #include <pthreadP.h> | |
ed19993b TR |
24 | #include <atomic.h> |
25 | #include <stdint.h> | |
a88c9263 UD |
26 | |
27 | #include <shlib-compat.h> | |
5acf7263 | 28 | #include <stap-probe.h> |
69431c9a | 29 | |
ed19993b | 30 | #include "pthread_cond_common.c" |
a88c9263 | 31 | |
ed19993b | 32 | /* See __pthread_cond_wait for a high-level description of the algorithm. */ |
a88c9263 | 33 | int |
08129b15 | 34 | ___pthread_cond_signal (pthread_cond_t *cond) |
a88c9263 | 35 | { |
5acf7263 RM |
36 | LIBC_PROBE (cond_signal, 1, cond); |
37 | ||
ed19993b TR |
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); | |
45 | ||
46 | __condvar_acquire_lock (cond, private); | |
47 | ||
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; | |
70 | ||
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)) | |
a88c9263 | 78 | { |
ed19993b TR |
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; | |
a88c9263 UD |
88 | } |
89 | ||
ed19993b TR |
90 | __condvar_release_lock (cond, private); |
91 | ||
92 | if (do_futex_wake) | |
93 | futex_wake (cond->__data.__g_signals + g1, 1, private); | |
a88c9263 UD |
94 | |
95 | return 0; | |
96 | } | |
08129b15 | 97 | versioned_symbol (libpthread, ___pthread_cond_signal, pthread_cond_signal, |
a88c9263 | 98 | GLIBC_2_3_2); |
08129b15 | 99 | libc_hidden_ver (___pthread_cond_signal, __pthread_cond_signal) |
eef936eb FW |
100 | #ifndef SHARED |
101 | strong_alias (___pthread_cond_signal, __pthread_cond_signal) | |
102 | #endif |