License: common: error_query: fix typo in SPDX specifier
[lttng-tools.git] / src / common / waiter.cpp
CommitLineData
287a512f 1/*
ab5be9fa
MJ
2 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
287a512f 4 *
ab5be9fa 5 * SPDX-License-Identifier: LGPL-2.1-only
287a512f 6 *
287a512f
JG
7 */
8
c9e313bc 9#include "error.hpp"
f40b76ae 10#include "macros.hpp"
28ab034a
JG
11#include "waiter.hpp"
12
287a512f 13#include <poll.h>
28ab034a
JG
14#include <urcu/futex.h>
15#include <urcu/uatomic.h>
287a512f
JG
16
17/*
18 * Number of busy-loop attempts before waiting on futex.
19 */
20#define WAIT_ATTEMPTS 1000
21
22enum waiter_state {
23 /* WAITER_WAITING is compared directly (futex compares it). */
28ab034a 24 WAITER_WAITING = 0,
287a512f 25 /* non-zero are used as masks. */
28ab034a
JG
26 WAITER_WOKEN_UP = (1 << 0),
27 WAITER_RUNNING = (1 << 1),
28 WAITER_TEARDOWN = (1 << 2),
287a512f
JG
29};
30
287a512f
JG
31void 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 */
287a512f
JG
41void lttng_waiter_wait(struct lttng_waiter *waiter)
42{
43 unsigned int i;
44
f40b76ae
JG
45 DBG("Beginning of waiter \"wait\" period");
46
47 /* Load and test condition before read state. */
287a512f
JG
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 }
f40b76ae 53
287a512f
JG
54 caa_cpu_relax();
55 }
f40b76ae 56
6e5438dc 57 while (uatomic_read(&waiter->state) == WAITER_WAITING) {
cd9adb8b
JG
58 if (!futex_noasync(
59 &waiter->state, FUTEX_WAIT, WAITER_WAITING, nullptr, nullptr, 0)) {
6e5438dc
MD
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 }
f40b76ae 70
287a512f 71 switch (errno) {
6e5438dc 72 case EAGAIN:
287a512f
JG
73 /* Value already changed. */
74 goto skip_futex_wait;
75 case EINTR:
76 /* Retry if interrupted by signal. */
28ab034a 77 break; /* Get out of switch. Check again. */
287a512f
JG
78 default:
79 /* Unexpected error. */
80 PERROR("futex_noasync");
81 abort();
82 }
83 }
84skip_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 }
f40b76ae 97
287a512f
JG
98 caa_cpu_relax();
99 }
f40b76ae 100
287a512f 101 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
cd9adb8b 102 poll(nullptr, 0, 10);
287a512f 103 }
f40b76ae 104
a0377dfe 105 LTTNG_ASSERT(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
f40b76ae 106 DBG("End of waiter \"wait\" period");
287a512f
JG
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 */
f40b76ae 114void lttng_waiter_wake(struct lttng_waiter *waiter)
287a512f
JG
115{
116 cmm_smp_mb();
a0377dfe 117 LTTNG_ASSERT(uatomic_read(&waiter->state) == WAITER_WAITING);
287a512f
JG
118 uatomic_set(&waiter->state, WAITER_WOKEN_UP);
119 if (!(uatomic_read(&waiter->state) & WAITER_RUNNING)) {
cd9adb8b 120 if (futex_noasync(&waiter->state, FUTEX_WAKE, 1, nullptr, nullptr, 0) < 0) {
287a512f
JG
121 PERROR("futex_noasync");
122 abort();
123 }
124 }
f40b76ae 125
287a512f
JG
126 /* Allow teardown of struct urcu_wait memory. */
127 uatomic_or(&waiter->state, WAITER_TEARDOWN);
128}
f40b76ae
JG
129
130void lttng_wait_queue_init(struct lttng_wait_queue *queue)
131{
132 cds_wfs_init(&queue->stack);
133}
134
135void 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
140void 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.066642 seconds and 4 git commands to generate.