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