waiter: modernize the waiter interface
[lttng-tools.git] / src / common / waiter.hpp
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
JG
6 *
7 * This code is originally adapted from userspace-rcu's urcu-wait.h
8 */
9
10#ifndef LTTNG_WAITER_H
11#define LTTNG_WAITER_H
12
13#define _LGPL_SOURCE
14
28f23191
JG
15#include "macros.hpp"
16
17#include <stdbool.h>
287a512f
JG
18#include <stdint.h>
19#include <urcu/wfstack.h>
287a512f 20
32670d71
JG
21namespace lttng {
22namespace synchro {
23class waiter;
24class wait_queue;
25
26class waker {
27 friend waiter;
28
29public:
30 waker(const waker&) = default;
31 waker(waker&&) = default;
32 waker& operator=(const waker& other)
33 {
34 _state = other._state;
35 return *this;
36 }
37 waker& operator=(waker&& other)
38 {
39 _state = other._state;
40 return *this;
41 }
42
43 void wake();
44
45 ~waker() = default;
287a512f 46
32670d71
JG
47private:
48 waker(int32_t& state) : _state{ state }
49 {
50 }
51
52 int32_t& _state;
f40b76ae
JG
53};
54
32670d71
JG
55class waiter final {
56 friend wait_queue;
287a512f 57
32670d71
JG
58public:
59 waiter();
287a512f 60
32670d71
JG
61 /* Deactivate copy and assignment. */
62 waiter(const waiter&) = delete;
63 waiter(waiter&&) = delete;
64 waiter& operator=(const waiter&) = delete;
65 waiter& operator=(waiter&&) = delete;
66 ~waiter() = default;
f40b76ae 67
32670d71
JG
68 void arm() noexcept;
69 void wait();
f40b76ae 70
32670d71 71 waker get_waker();
f40b76ae 72
32670d71
JG
73private:
74 cds_wfs_node _wait_queue_node;
75 int32_t _state;
76};
77
78class wait_queue final {
79public:
80 wait_queue();
81
82 /* Deactivate copy and assignment. */
83 wait_queue(const wait_queue&) = delete;
84 wait_queue(wait_queue&&) = delete;
85 wait_queue& operator=(const wait_queue&) = delete;
86 wait_queue& operator=(wait_queue&&) = delete;
87 ~wait_queue() = default;
88
89 /*
90 * Atomically add a waiter to a wait queue.
91 * A full memory barrier is issued before being added to the wait queue.
92 */
93 void add(waiter& waiter) noexcept;
94 /*
95 * Wake every waiter present in the wait queue and remove them from
96 * the queue.
97 */
98 void wake_all();
99
100private:
101 cds_wfs_stack _stack;
102};
103} /* namespace synchro */
104} /* namespace lttng */
287a512f
JG
105
106#endif /* LTTNG_WAITER_H */
This page took 0.069242 seconds and 4 git commands to generate.