Build fix: missing stdio.h include in signal-helper.hpp
[lttng-tools.git] / src / common / waiter.cpp
... / ...
CommitLineData
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 "waiter.hpp"
10#include <urcu/uatomic.h>
11#include <urcu/futex.h>
12#include "error.hpp"
13#include <poll.h>
14
15/*
16 * Number of busy-loop attempts before waiting on futex.
17 */
18#define WAIT_ATTEMPTS 1000
19
20enum waiter_state {
21 /* WAITER_WAITING is compared directly (futex compares it). */
22 WAITER_WAITING = 0,
23 /* non-zero are used as masks. */
24 WAITER_WOKEN_UP = (1 << 0),
25 WAITER_RUNNING = (1 << 1),
26 WAITER_TEARDOWN = (1 << 2),
27};
28
29void lttng_waiter_init(struct lttng_waiter *waiter)
30{
31 cds_wfs_node_init(&waiter->wait_queue_node);
32 uatomic_set(&waiter->state, WAITER_WAITING);
33 cmm_smp_mb();
34}
35
36/*
37 * User must init "waiter" before passing its memory to waker thread.
38 */
39void lttng_waiter_wait(struct lttng_waiter *waiter)
40{
41 unsigned int i;
42
43 DBG("Beginning of waiter wait period");
44 /* Load and test condition before read state */
45 cmm_smp_rmb();
46 for (i = 0; i < WAIT_ATTEMPTS; i++) {
47 if (uatomic_read(&waiter->state) != WAITER_WAITING) {
48 goto skip_futex_wait;
49 }
50 caa_cpu_relax();
51 }
52 while (uatomic_read(&waiter->state) == WAITER_WAITING) {
53 if (!futex_noasync(&waiter->state, FUTEX_WAIT, WAITER_WAITING, NULL, NULL, 0)) {
54 /*
55 * Prior queued wakeups queued by unrelated code
56 * using the same address can cause futex wait to
57 * return 0 even through the futex value is still
58 * WAITER_WAITING (spurious wakeups). Check
59 * the value again in user-space to validate
60 * whether it really differs from WAITER_WAITING.
61 */
62 continue;
63 }
64 switch (errno) {
65 case EAGAIN:
66 /* Value already changed. */
67 goto skip_futex_wait;
68 case EINTR:
69 /* Retry if interrupted by signal. */
70 break; /* Get out of switch. Check again. */
71 default:
72 /* Unexpected error. */
73 PERROR("futex_noasync");
74 abort();
75 }
76 }
77skip_futex_wait:
78
79 /* Tell waker thread than we are running. */
80 uatomic_or(&waiter->state, WAITER_RUNNING);
81
82 /*
83 * Wait until waker thread lets us know it's ok to tear down
84 * memory allocated for struct lttng_waiter.
85 */
86 for (i = 0; i < WAIT_ATTEMPTS; i++) {
87 if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) {
88 break;
89 }
90 caa_cpu_relax();
91 }
92 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
93 poll(NULL, 0, 10);
94 }
95 LTTNG_ASSERT(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
96 DBG("End of waiter wait period");
97}
98
99/*
100 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
101 * execution. In this scheme, the waiter owns the node memory, and we only allow
102 * it to free this memory when it sees the WAITER_TEARDOWN flag.
103 */
104void lttng_waiter_wake_up(struct lttng_waiter *waiter)
105{
106 cmm_smp_mb();
107 LTTNG_ASSERT(uatomic_read(&waiter->state) == WAITER_WAITING);
108 uatomic_set(&waiter->state, WAITER_WOKEN_UP);
109 if (!(uatomic_read(&waiter->state) & WAITER_RUNNING)) {
110 if (futex_noasync(&waiter->state, FUTEX_WAKE, 1,
111 NULL, NULL, 0) < 0) {
112 PERROR("futex_noasync");
113 abort();
114 }
115 }
116 /* Allow teardown of struct urcu_wait memory. */
117 uatomic_or(&waiter->state, WAITER_TEARDOWN);
118}
This page took 0.023418 seconds and 4 git commands to generate.