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 | |
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 | ||
31 | /* "Not copyable" and "not moveable" Mutex requirements. */ | |
32 | read_lock(read_lock const &) = delete; | |
33 | read_lock &operator=(read_lock const &) = delete; | |
34 | ||
35 | void lock() | |
36 | { | |
37 | rcu_read_lock(); | |
38 | } | |
39 | ||
40 | bool try_lock() | |
41 | { | |
42 | lock(); | |
43 | return true; | |
44 | } | |
45 | ||
46 | void unlock() | |
47 | { | |
48 | rcu_read_unlock(); | |
49 | } | |
50 | }; | |
51 | } /* namespace details */ | |
52 | ||
53 | /* | |
54 | * Provides the basic concept of std::lock_guard for rcu reader locks. | |
55 | * | |
56 | * The RCU reader lock is held for the duration of lock_guard's lifetime. | |
57 | */ | |
58 | class read_lock_guard { | |
59 | public: | |
60 | read_lock_guard() : _guard(_lock) | |
61 | { | |
62 | } | |
63 | ||
64 | read_lock_guard(const read_lock_guard &) = delete; | |
65 | ||
66 | private: | |
67 | details::read_lock _lock; | |
68 | std::lock_guard<details::read_lock> _guard; | |
69 | }; | |
70 | ||
d7bfb9b0 JG |
71 | using unique_read_lock = std::unique_lock<details::read_lock>; |
72 | ||
0a325f4d JG |
73 | } /* namespace urcu */ |
74 | } /* namespace lttng */ | |
75 | ||
76 | #endif /* LTTNG_URCU_H */ |