Commit | Line | Data |
---|---|---|
0a325f4d JG |
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 | |
0a325f4d | 12 | #include <mutex> |
28f23191 | 13 | #include <urcu.h> |
0a325f4d JG |
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; | |
9d89db29 | 30 | ~read_lock() = default; |
0a325f4d JG |
31 | |
32 | /* "Not copyable" and "not moveable" Mutex requirements. */ | |
9d89db29 JG |
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; | |
0a325f4d JG |
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: | |
5c7248cd | 63 | read_lock_guard() = default; |
9d89db29 | 64 | ~read_lock_guard() = default; |
0a325f4d | 65 | |
9d89db29 JG |
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; | |
0a325f4d JG |
70 | |
71 | private: | |
72 | details::read_lock _lock; | |
9d89db29 | 73 | std::lock_guard<details::read_lock> _guard{ _lock }; |
0a325f4d JG |
74 | }; |
75 | ||
d7bfb9b0 JG |
76 | using unique_read_lock = std::unique_lock<details::read_lock>; |
77 | ||
0a325f4d JG |
78 | } /* namespace urcu */ |
79 | } /* namespace lttng */ | |
80 | ||
81 | #endif /* LTTNG_URCU_H */ |