037feca2260b34d2e99cd553369e3763abb28d33
2 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
14 #include <urcu/futex.h>
15 #include <urcu/uatomic.h>
18 /* Number of busy-loop attempts before waiting on futex. */
19 constexpr auto wait_attempt_count
= 1000;
22 /* WAITER_WAITING is compared directly (futex compares it). */
24 /* non-zero are used as masks. */
25 WAITER_WOKEN_UP
= (1 << 0),
26 WAITER_RUNNING
= (1 << 1),
27 WAITER_TEARDOWN
= (1 << 2),
31 lttng::synchro::waiter::waiter()
36 void lttng::synchro::waiter::arm() noexcept
38 cds_wfs_node_init(&_wait_queue_node
);
39 uatomic_set(&_state
, WAITER_WAITING
);
44 * User must arm "waiter" before passing its memory to waker thread.
46 void lttng::synchro::waiter::wait()
48 DBG("Beginning of waiter \"wait\" period");
50 /* Load and test condition before read state. */
52 for (unsigned int i
= 0; i
< wait_attempt_count
; i
++) {
53 if (uatomic_read(&_state
) != WAITER_WAITING
) {
60 while (uatomic_read(&_state
) == WAITER_WAITING
) {
62 &_state
, FUTEX_WAIT
, WAITER_WAITING
, nullptr, nullptr, 0)) {
64 * Prior queued wakeups queued by unrelated code
65 * using the same address can cause futex wait to
66 * return 0 even through the futex value is still
67 * WAITER_WAITING (spurious wakeups). Check
68 * the value again in user-space to validate
69 * whether it really differs from WAITER_WAITING.
76 /* Value already changed. */
79 /* Retry if interrupted by signal. */
80 break; /* Get out of switch. Check again. */
82 /* Unexpected error. */
83 PERROR("futex_noasync");
89 /* Tell waker thread than we are running. */
90 uatomic_or(&_state
, WAITER_RUNNING
);
93 * Wait until waker thread lets us know it's ok to tear down
94 * memory allocated for struct lttng_waiter.
96 for (unsigned int i
= 0; i
< wait_attempt_count
; i
++) {
97 if (uatomic_read(&_state
) & WAITER_TEARDOWN
) {
104 while (!(uatomic_read(&_state
) & WAITER_TEARDOWN
)) {
105 poll(nullptr, 0, 10);
108 LTTNG_ASSERT(uatomic_read(&_state
) & WAITER_TEARDOWN
);
109 DBG("End of waiter \"wait\" period");
112 lttng::synchro::waker
lttng::synchro::waiter::get_waker()
114 return lttng::synchro::waker(_state
);
118 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
119 * execution. In this scheme, the waiter owns the node memory, and we only allow
120 * it to free this memory when it sees the WAITER_TEARDOWN flag.
122 void lttng::synchro::waker::wake()
126 LTTNG_ASSERT(uatomic_read(&_state
) == WAITER_WAITING
);
128 uatomic_set(&_state
, WAITER_WOKEN_UP
);
129 if (!(uatomic_read(&_state
) & WAITER_RUNNING
)) {
130 if (futex_noasync(&_state
, FUTEX_WAKE
, 1, nullptr, nullptr, 0) < 0) {
131 PERROR("futex_noasync");
136 /* Allow teardown of struct urcu_wait memory. */
137 uatomic_or(&_state
, WAITER_TEARDOWN
);
140 lttng::synchro::wait_queue::wait_queue()
142 cds_wfs_init(&_stack
);
145 void lttng::synchro::wait_queue::add(waiter
&waiter
) noexcept
147 (void) cds_wfs_push(&_stack
, &waiter
._wait_queue_node
);
150 void lttng::synchro::wait_queue::wake_all()
152 /* Move all waiters from the queue to our local stack. */
153 auto *waiters
= __cds_wfs_pop_all(&_stack
);
155 /* Wake all waiters in our stack head. */
156 cds_wfs_node
*iter
, *iter_n
;
157 cds_wfs_for_each_blocking_safe(waiters
, iter
, iter_n
) {
158 auto& waiter
= *lttng::utils::container_of(
159 iter
, <tng::synchro::waiter::_wait_queue_node
);
161 /* Don't wake already running threads. */
162 if (waiter
._state
& WAITER_RUNNING
) {
166 waiter
.get_waker().wake();
This page took 0.032179 seconds and 4 git commands to generate.