-
Notifications
You must be signed in to change notification settings - Fork 27
/
cs_rcu_guarded.h
213 lines (170 loc) · 5.29 KB
/
cs_rcu_guarded.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***********************************************************************
*
* Copyright (c) 2016-2024 Ansel Sermersheim
*
* This file is part of CsLibGuarded.
*
* CsLibGuarded is free software, released under the BSD 2-Clause license.
* For license details refer to LICENSE provided with this project.
*
* CsLibGuarded is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* https://opensource.org/licenses/BSD-2-Clause
*
***********************************************************************/
#ifndef CSLIBGUARDED_RCU_GUARDED_H
#define CSLIBGUARDED_RCU_GUARDED_H
#include <memory>
namespace libguarded
{
/**
\headerfile cs_rcu_guarded.h <CsLibGuarded/cs_rcu_guarded.h>
This templated class implements a mechanism which controls access
to an RCU data structure. The only way to access the underlying
data structure is to use either the lock_read or lock_write methods
to receive a read-only or writable handle to the data structure,
respectively.
*/
template <typename T>
class rcu_guarded
{
public:
class write_handle;
class read_handle;
template <typename... Us>
rcu_guarded(Us &&... data);
// write access
[[nodiscard]] write_handle lock_write();
// read access
[[nodiscard]] read_handle lock_read() const;
class write_handle
{
public:
using pointer = T *;
using element_type = T;
write_handle(T *ptr);
write_handle(const write_handle &other) = delete;
write_handle &operator=(const write_handle &other) = delete;
write_handle(write_handle &&other) {
m_ptr = other.m_ptr;
m_guard = std::move(other.m_guard);
m_accessed = other.m_accessed;
other.m_ptr = nullptr;
other.m_accessed = false;
}
write_handle &operator=(write_handle &&other) {
if (m_accessed) {
m_guard.rcu_write_unlock(*m_ptr);
}
m_ptr = other.m_ptr;
m_guard = std::move(other.m_guard);
m_accessed = other.m_accessed;
other.m_ptr = nullptr;
other.m_accessed = false;
}
~write_handle()
{
if (m_accessed) {
m_guard.rcu_write_unlock(*m_ptr);
}
}
T &operator*() const {
access();
return *m_ptr;
}
T *operator->() const {
access();
return m_ptr;
}
private:
void access() const {
if (! m_accessed) {
m_guard.rcu_write_lock(*m_ptr);
m_accessed = true;
}
}
T *m_ptr;
mutable typename T::rcu_write_guard m_guard;
mutable bool m_accessed;
};
class read_handle
{
public:
using pointer = const T *;
using element_type = const T;
read_handle(const T *ptr)
: m_ptr(ptr), m_accessed(false)
{
}
read_handle(const read_handle &other) = delete;
read_handle &operator=(const read_handle &other) = delete;
read_handle(read_handle &&other) {
m_ptr = other.m_ptr;
m_guard = std::move(other.m_guard);
m_accessed = other.m_accessed;
other.m_ptr = nullptr;
other.m_accessed = false;
}
read_handle &operator=(read_handle &&other) {
if (m_accessed) {
m_guard.rcu_read_unlock(*m_ptr);
}
m_ptr = other.m_ptr;
m_guard = std::move(other.m_guard);
m_accessed = other.m_accessed;
other.m_ptr = nullptr;
other.m_accessed = false;
}
~read_handle()
{
if (m_accessed) {
m_guard.rcu_read_unlock(*m_ptr);
}
}
const T &operator*() const {
access();
return *m_ptr;
}
const T *operator->() const {
access();
return m_ptr;
}
private:
void access() const {
if (! m_accessed) {
m_guard.rcu_read_lock(*m_ptr);
m_accessed = true;
}
}
const T *m_ptr;
mutable typename T::rcu_read_guard m_guard;
mutable bool m_accessed;
};
private:
T m_obj;
};
template <typename T>
template <typename... Us>
rcu_guarded<T>::rcu_guarded(Us &&... data)
: m_obj(std::forward<Us>(data)...)
{
}
template <typename T>
auto rcu_guarded<T>::lock_write() -> write_handle
{
return write_handle(&m_obj);
}
template <typename T>
auto rcu_guarded<T>::lock_read() const -> read_handle
{
return read_handle(&m_obj);
}
template <typename T>
rcu_guarded<T>::write_handle::write_handle(T *ptr)
: m_ptr(ptr), m_accessed(false)
{
}
} // namespace libguarded
#endif