3ddb68feb7c76dd91f09bec9d7664c02224627a2
[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 "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
20 enum 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
29 void 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 */
39 void 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 (futex_noasync(&waiter->state, FUTEX_WAIT, WAITER_WAITING,
53 NULL, NULL, 0)) {
54 switch (errno) {
55 case EWOULDBLOCK:
56 /* Value already changed. */
57 goto skip_futex_wait;
58 case EINTR:
59 /* Retry if interrupted by signal. */
60 break; /* Get out of switch. */
61 default:
62 /* Unexpected error. */
63 PERROR("futex_noasync");
64 abort();
65 }
66 }
67 skip_futex_wait:
68
69 /* Tell waker thread than we are running. */
70 uatomic_or(&waiter->state, WAITER_RUNNING);
71
72 /*
73 * Wait until waker thread lets us know it's ok to tear down
74 * memory allocated for struct lttng_waiter.
75 */
76 for (i = 0; i < WAIT_ATTEMPTS; i++) {
77 if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) {
78 break;
79 }
80 caa_cpu_relax();
81 }
82 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
83 poll(NULL, 0, 10);
84 }
85 LTTNG_ASSERT(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
86 DBG("End of waiter wait period");
87 }
88
89 /*
90 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
91 * execution. In this scheme, the waiter owns the node memory, and we only allow
92 * it to free this memory when it sees the WAITER_TEARDOWN flag.
93 */
94 void lttng_waiter_wake_up(struct lttng_waiter *waiter)
95 {
96 cmm_smp_mb();
97 LTTNG_ASSERT(uatomic_read(&waiter->state) == WAITER_WAITING);
98 uatomic_set(&waiter->state, WAITER_WOKEN_UP);
99 if (!(uatomic_read(&waiter->state) & WAITER_RUNNING)) {
100 if (futex_noasync(&waiter->state, FUTEX_WAKE, 1,
101 NULL, NULL, 0) < 0) {
102 PERROR("futex_noasync");
103 abort();
104 }
105 }
106 /* Allow teardown of struct urcu_wait memory. */
107 uatomic_or(&waiter->state, WAITER_TEARDOWN);
108 }
This page took 0.031871 seconds and 4 git commands to generate.