d185e299720617e64e7514d114e33cea4a0eb386
[lttng-tools.git] / src / common / waiter.cpp
1 /*
2 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 *
7 */
8
9 #include "error.hpp"
10 #include "macros.hpp"
11 #include "waiter.hpp"
12
13 #include <poll.h>
14 #include <urcu/futex.h>
15 #include <urcu/uatomic.h>
16
17 /*
18 * Number of busy-loop attempts before waiting on futex.
19 */
20 #define WAIT_ATTEMPTS 1000
21
22 enum waiter_state {
23 /* WAITER_WAITING is compared directly (futex compares it). */
24 WAITER_WAITING = 0,
25 /* non-zero are used as masks. */
26 WAITER_WOKEN_UP = (1 << 0),
27 WAITER_RUNNING = (1 << 1),
28 WAITER_TEARDOWN = (1 << 2),
29 };
30
31 void lttng_waiter_init(struct lttng_waiter *waiter)
32 {
33 cds_wfs_node_init(&waiter->wait_queue_node);
34 uatomic_set(&waiter->state, WAITER_WAITING);
35 cmm_smp_mb();
36 }
37
38 /*
39 * User must init "waiter" before passing its memory to waker thread.
40 */
41 void lttng_waiter_wait(struct lttng_waiter *waiter)
42 {
43 unsigned int i;
44
45 DBG("Beginning of waiter \"wait\" period");
46
47 /* Load and test condition before read state. */
48 cmm_smp_rmb();
49 for (i = 0; i < WAIT_ATTEMPTS; i++) {
50 if (uatomic_read(&waiter->state) != WAITER_WAITING) {
51 goto skip_futex_wait;
52 }
53
54 caa_cpu_relax();
55 }
56
57 while (uatomic_read(&waiter->state) == WAITER_WAITING) {
58 if (!futex_noasync(
59 &waiter->state, FUTEX_WAIT, WAITER_WAITING, nullptr, nullptr, 0)) {
60 /*
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.
67 */
68 continue;
69 }
70
71 switch (errno) {
72 case EAGAIN:
73 /* Value already changed. */
74 goto skip_futex_wait;
75 case EINTR:
76 /* Retry if interrupted by signal. */
77 break; /* Get out of switch. Check again. */
78 default:
79 /* Unexpected error. */
80 PERROR("futex_noasync");
81 abort();
82 }
83 }
84 skip_futex_wait:
85
86 /* Tell waker thread than we are running. */
87 uatomic_or(&waiter->state, WAITER_RUNNING);
88
89 /*
90 * Wait until waker thread lets us know it's ok to tear down
91 * memory allocated for struct lttng_waiter.
92 */
93 for (i = 0; i < WAIT_ATTEMPTS; i++) {
94 if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) {
95 break;
96 }
97
98 caa_cpu_relax();
99 }
100
101 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
102 poll(nullptr, 0, 10);
103 }
104
105 LTTNG_ASSERT(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
106 DBG("End of waiter \"wait\" period");
107 }
108
109 /*
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.
113 */
114 void lttng_waiter_wake(struct lttng_waiter *waiter)
115 {
116 cmm_smp_mb();
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");
122 abort();
123 }
124 }
125
126 /* Allow teardown of struct urcu_wait memory. */
127 uatomic_or(&waiter->state, WAITER_TEARDOWN);
128 }
129
130 void lttng_wait_queue_init(struct lttng_wait_queue *queue)
131 {
132 cds_wfs_init(&queue->stack);
133 }
134
135 void lttng_wait_queue_add(struct lttng_wait_queue *queue, struct lttng_waiter *waiter)
136 {
137 (void) cds_wfs_push(&queue->stack, &waiter->wait_queue_node);
138 }
139
140 void lttng_wait_queue_wake_all(struct lttng_wait_queue *queue)
141 {
142 cds_wfs_head *waiters;
143 cds_wfs_node *iter, *iter_n;
144
145 /* Move all waiters from the queue to our local stack. */
146 waiters = __cds_wfs_pop_all(&queue->stack);
147
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, &lttng_waiter::wait_queue_node);
151
152 /* Don't wake already running threads. */
153 if (waiter->state & WAITER_RUNNING) {
154 continue;
155 }
156
157 lttng_waiter_wake(waiter);
158 }
159 }
This page took 0.065504 seconds and 4 git commands to generate.