align.h: Implement ALIGN_FLOOR macro
[lttng-tools.git] / src / common / waiter.c
CommitLineData
287a512f
JG
1/*
2 * Copyright (C) 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License, version 2.1 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * This code is originally adapted from userspace-rcu's urcu-wait.h
19 */
20
21#include "waiter.h"
22#include <urcu/uatomic.h>
23#include <urcu/futex.h>
24#include <assert.h>
25#include "error.h"
26#include <poll.h>
27
28/*
29 * Number of busy-loop attempts before waiting on futex.
30 */
31#define WAIT_ATTEMPTS 1000
32
33enum waiter_state {
34 /* WAITER_WAITING is compared directly (futex compares it). */
35 WAITER_WAITING = 0,
36 /* non-zero are used as masks. */
37 WAITER_WOKEN_UP = (1 << 0),
38 WAITER_RUNNING = (1 << 1),
39 WAITER_TEARDOWN = (1 << 2),
40};
41
42LTTNG_HIDDEN
43void lttng_waiter_init(struct lttng_waiter *waiter)
44{
45 cds_wfs_node_init(&waiter->wait_queue_node);
46 uatomic_set(&waiter->state, WAITER_WAITING);
47 cmm_smp_mb();
48}
49
50/*
51 * User must init "waiter" before passing its memory to waker thread.
52 */
53LTTNG_HIDDEN
54void lttng_waiter_wait(struct lttng_waiter *waiter)
55{
56 unsigned int i;
57
58 DBG("Beginning of waiter wait period");
59 /* Load and test condition before read state */
60 cmm_smp_rmb();
61 for (i = 0; i < WAIT_ATTEMPTS; i++) {
62 if (uatomic_read(&waiter->state) != WAITER_WAITING) {
63 goto skip_futex_wait;
64 }
65 caa_cpu_relax();
66 }
67 while (futex_noasync(&waiter->state, FUTEX_WAIT, WAITER_WAITING,
68 NULL, NULL, 0)) {
69 switch (errno) {
70 case EWOULDBLOCK:
71 /* Value already changed. */
72 goto skip_futex_wait;
73 case EINTR:
74 /* Retry if interrupted by signal. */
75 break; /* Get out of switch. */
76 default:
77 /* Unexpected error. */
78 PERROR("futex_noasync");
79 abort();
80 }
81 }
82skip_futex_wait:
83
84 /* Tell waker thread than we are running. */
85 uatomic_or(&waiter->state, WAITER_RUNNING);
86
87 /*
88 * Wait until waker thread lets us know it's ok to tear down
89 * memory allocated for struct lttng_waiter.
90 */
91 for (i = 0; i < WAIT_ATTEMPTS; i++) {
92 if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) {
93 break;
94 }
95 caa_cpu_relax();
96 }
97 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
98 poll(NULL, 0, 10);
99 }
100 assert(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
101 DBG("End of waiter wait period");
102}
103
104/*
105 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
106 * execution. In this scheme, the waiter owns the node memory, and we only allow
107 * it to free this memory when it sees the WAITER_TEARDOWN flag.
108 */
109LTTNG_HIDDEN
110void lttng_waiter_wake_up(struct lttng_waiter *waiter)
111{
112 cmm_smp_mb();
113 assert(uatomic_read(&waiter->state) == WAITER_WAITING);
114 uatomic_set(&waiter->state, WAITER_WOKEN_UP);
115 if (!(uatomic_read(&waiter->state) & WAITER_RUNNING)) {
116 if (futex_noasync(&waiter->state, FUTEX_WAKE, 1,
117 NULL, NULL, 0) < 0) {
118 PERROR("futex_noasync");
119 abort();
120 }
121 }
122 /* Allow teardown of struct urcu_wait memory. */
123 uatomic_or(&waiter->state, WAITER_TEARDOWN);
124}
This page took 0.040224 seconds and 5 git commands to generate.