edb53b7b59e0e842791a4ed9529e681f6d24439d
[lttng-tools.git] / src / common / urcu.hpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef LTTNG_URCU_H
9 #define LTTNG_URCU_H
10
11 #define _LGPL_SOURCE
12 #include <urcu.h>
13 #include <mutex>
14
15 namespace lttng {
16 namespace urcu {
17
18 namespace details {
19 /*
20 * Wrapper around an urcu read lock which satisfies the 'Mutex' named
21 * requirements of C++11. Satisfying those requirements facilitates the use of
22 * standard concurrency support library facilities.
23 *
24 * read_lock is under the details namespace since it is unlikely to be used
25 * directly by exception-safe code. See read_lock_guard.
26 */
27 class read_lock {
28 public:
29 read_lock() = default;
30 ~read_lock() = default;
31
32 /* "Not copyable" and "not moveable" Mutex requirements. */
33 read_lock(read_lock const&) = delete;
34 read_lock(read_lock&&) = delete;
35 read_lock& operator=(read_lock&&) = delete;
36 read_lock& operator=(const read_lock&) = delete;
37
38 void lock()
39 {
40 rcu_read_lock();
41 }
42
43 bool try_lock()
44 {
45 lock();
46 return true;
47 }
48
49 void unlock()
50 {
51 rcu_read_unlock();
52 }
53 };
54 } /* namespace details */
55
56 /*
57 * Provides the basic concept of std::lock_guard for rcu reader locks.
58 *
59 * The RCU reader lock is held for the duration of lock_guard's lifetime.
60 */
61 class read_lock_guard {
62 public:
63 read_lock_guard() = default;
64 ~read_lock_guard() = default;
65
66 read_lock_guard(const read_lock_guard&) = delete;
67 read_lock_guard(read_lock_guard&&) = delete;
68 read_lock_guard& operator=(read_lock_guard&&) = delete;
69 read_lock_guard& operator=(const read_lock_guard&) = delete;
70
71 private:
72 details::read_lock _lock;
73 std::lock_guard<details::read_lock> _guard{ _lock };
74 };
75
76 using unique_read_lock = std::unique_lock<details::read_lock>;
77
78 } /* namespace urcu */
79 } /* namespace lttng */
80
81 #endif /* LTTNG_URCU_H */
This page took 0.032873 seconds and 4 git commands to generate.