d185e299720617e64e7514d114e33cea4a0eb386
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.
20 #define WAIT_ATTEMPTS 1000
23 /* WAITER_WAITING is compared directly (futex compares it). */
25 /* non-zero are used as masks. */
26 WAITER_WOKEN_UP
= (1 << 0),
27 WAITER_RUNNING
= (1 << 1),
28 WAITER_TEARDOWN
= (1 << 2),
31 void lttng_waiter_init(struct lttng_waiter
*waiter
)
33 cds_wfs_node_init(&waiter
->wait_queue_node
);
34 uatomic_set(&waiter
->state
, WAITER_WAITING
);
39 * User must init "waiter" before passing its memory to waker thread.
41 void lttng_waiter_wait(struct lttng_waiter
*waiter
)
45 DBG("Beginning of waiter \"wait\" period");
47 /* Load and test condition before read state. */
49 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
50 if (uatomic_read(&waiter
->state
) != WAITER_WAITING
) {
57 while (uatomic_read(&waiter
->state
) == WAITER_WAITING
) {
59 &waiter
->state
, FUTEX_WAIT
, WAITER_WAITING
, nullptr, nullptr, 0)) {
61 * Prior queued wakeups queued by unrelated code
62 * using the same address can cause futex wait to
63 * return 0 even through the futex value is still
64 * WAITER_WAITING (spurious wakeups). Check
65 * the value again in user-space to validate
66 * whether it really differs from WAITER_WAITING.
73 /* Value already changed. */
76 /* Retry if interrupted by signal. */
77 break; /* Get out of switch. Check again. */
79 /* Unexpected error. */
80 PERROR("futex_noasync");
86 /* Tell waker thread than we are running. */
87 uatomic_or(&waiter
->state
, WAITER_RUNNING
);
90 * Wait until waker thread lets us know it's ok to tear down
91 * memory allocated for struct lttng_waiter.
93 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
94 if (uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
) {
101 while (!(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
)) {
102 poll(nullptr, 0, 10);
105 LTTNG_ASSERT(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
);
106 DBG("End of waiter \"wait\" period");
110 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
111 * execution. In this scheme, the waiter owns the node memory, and we only allow
112 * it to free this memory when it sees the WAITER_TEARDOWN flag.
114 void lttng_waiter_wake(struct lttng_waiter
*waiter
)
117 LTTNG_ASSERT(uatomic_read(&waiter
->state
) == WAITER_WAITING
);
118 uatomic_set(&waiter
->state
, WAITER_WOKEN_UP
);
119 if (!(uatomic_read(&waiter
->state
) & WAITER_RUNNING
)) {
120 if (futex_noasync(&waiter
->state
, FUTEX_WAKE
, 1, nullptr, nullptr, 0) < 0) {
121 PERROR("futex_noasync");
126 /* Allow teardown of struct urcu_wait memory. */
127 uatomic_or(&waiter
->state
, WAITER_TEARDOWN
);
130 void lttng_wait_queue_init(struct lttng_wait_queue
*queue
)
132 cds_wfs_init(&queue
->stack
);
135 void lttng_wait_queue_add(struct lttng_wait_queue
*queue
, struct lttng_waiter
*waiter
)
137 (void) cds_wfs_push(&queue
->stack
, &waiter
->wait_queue_node
);
140 void lttng_wait_queue_wake_all(struct lttng_wait_queue
*queue
)
142 cds_wfs_head
*waiters
;
143 cds_wfs_node
*iter
, *iter_n
;
145 /* Move all waiters from the queue to our local stack. */
146 waiters
= __cds_wfs_pop_all(&queue
->stack
);
148 /* Wake all waiters in our stack head. */
149 cds_wfs_for_each_blocking_safe (waiters
, iter
, iter_n
) {
150 auto *waiter
= lttng::utils::container_of(iter
, <tng_waiter::wait_queue_node
);
152 /* Don't wake already running threads. */
153 if (waiter
->state
& WAITER_RUNNING
) {
157 lttng_waiter_wake(waiter
);
This page took 0.065504 seconds and 4 git commands to generate.