Build fix: missing type traits on gcc < 5.0
[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 "waiter.hpp"
287a512f
JG
10#include <urcu/uatomic.h>
11#include <urcu/futex.h>
c9e313bc 12#include "error.hpp"
287a512f
JG
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
287a512f
JG
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 */
287a512f
JG
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 (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 }
67skip_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 }
a0377dfe 85 LTTNG_ASSERT(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
287a512f
JG
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 */
287a512f
JG
94void lttng_waiter_wake_up(struct lttng_waiter *waiter)
95{
96 cmm_smp_mb();
a0377dfe 97 LTTNG_ASSERT(uatomic_read(&waiter->state) == WAITER_WAITING);
287a512f
JG
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.052319 seconds and 4 git commands to generate.