-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbasic_atomic_shared_ptr.hpp
More file actions
302 lines (226 loc) · 8.18 KB
/
basic_atomic_shared_ptr.hpp
File metadata and controls
302 lines (226 loc) · 8.18 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// A bare minimal atomic_shared_ptr implementation that exists to teach the main algorithm.
// Not efficient and not feature complete. Just for demonstration!!
//
// In particular, the following are absent:
// - No make_shared,
// - No custom deleters/allocators,
// - No weak_ptr,
// - No alias pointers,
// - No enable_shared_from_this
// - No memory orders. All seq_cst.
//
// See atomic_shared_ptr_custom.hpp, shared_ptr.hpp for a feature-complete and optimized implementation!
//
#pragma once
#include <atomic>
#include <memory>
#include <folly/synchronization/Hazptr.h>
namespace parlay {
namespace basic {
template<typename T>
class shared_ptr;
template<typename T>
class atomic_shared_ptr;
}
namespace details {
template<typename T>
struct basic_control_block : public folly::hazptr_obj_base<basic_control_block<T>> {
template<typename U>
friend class basic::atomic_shared_ptr;
template<typename... Args>
explicit basic_control_block(T* ptr_) noexcept : ref_count(1), ptr(ptr_) { }
basic_control_block(const basic_control_block &) = delete;
basic_control_block& operator=(const basic_control_block&) = delete;
~basic_control_block() = default;
// Increment the reference count. The reference count must not be zero
void increment_count() noexcept {
ref_count.fetch_add(1);
}
// Increment the reference count if it is not zero.
bool increment_if_nonzero() noexcept {
auto cnt = ref_count.load();
while (cnt > 0 && !ref_count.compare_exchange_weak(cnt, cnt + 1)) { }
return cnt > 0;
}
// Release a reference to the object.
void decrement_count() noexcept {
if (ref_count.fetch_sub(1) == 1) {
delete ptr;
this->retire();
}
}
std::atomic<long> ref_count;
T* ptr;
};
}
namespace basic {
template<typename T>
class shared_ptr {
template<typename U>
friend class atomic_shared_ptr;
// Private constructor used by atomic_shared_ptr::load
explicit shared_ptr(details::basic_control_block<T>* control_block_) : control_block(control_block_) {}
public:
using element_type = T;
// Decrement the reference count on destruction.
~shared_ptr() noexcept {
decrement();
}
constexpr shared_ptr() noexcept = default;
constexpr explicit(false) shared_ptr(std::nullptr_t) noexcept {} // NOLINT(google-explicit-constructor)
explicit shared_ptr(T* p) {
std::unique_ptr<T> up(p); // Hold inside a unique_ptr so that p is deleted if the allocation throws
control_block = new details::basic_control_block<T>(p);
up.release();
}
shared_ptr(const shared_ptr& other) noexcept : control_block(other.control_block) {
increment();
}
shared_ptr(shared_ptr&& other) noexcept : control_block(std::exchange(other.control_block, nullptr)) { }
shared_ptr& operator=(const shared_ptr& other) noexcept {
shared_ptr(other).swap(*this);
return *this;
}
shared_ptr& operator=(shared_ptr&& other) noexcept {
shared_ptr(std::move(other)).swap(*this);
return *this;
}
void swap(shared_ptr& other) noexcept {
std::swap(control_block, other.control_block);
}
void reset() noexcept {
shared_ptr().swap(*this);
}
void reset(std::nullptr_t) noexcept {
shared_ptr().swap(*this);
}
void reset(T* p) {
shared_ptr(p).swap(*this);
}
T* get() noexcept {
return control_block ? control_block->ptr : nullptr;
}
const T* get() const noexcept {
return control_block ? control_block->ptr : nullptr;
}
[[nodiscard]] T& operator*() noexcept requires (!std::is_void_v<T>) {
return *(this->get());
}
[[nodiscard]] const T& operator*() const noexcept requires (!std::is_void_v<T>) {
return *(this->get());
}
[[nodiscard]] T* operator->() const noexcept {
return this->get();
}
explicit operator bool() const noexcept {
return this->get() != nullptr;
}
[[nodiscard]] long use_count() const noexcept {
return control_block ? control_block->ref_count.load() : 0;
}
private:
void increment() noexcept {
if (control_block) {
control_block->increment_count();
}
}
void decrement() noexcept {
if (control_block) {
control_block->decrement_count();
}
}
details::basic_control_block<T>* control_block{nullptr};
};
template<typename T1, typename T2>
auto operator<=>(const shared_ptr<T1>& left, const shared_ptr<T2>& right) noexcept {
return left.get() <=> right.get();
}
template<typename T0>
auto operator<=>(const shared_ptr<T0>& left, std::nullptr_t) noexcept {
return left.get() <=> static_cast<shared_ptr<T0>::element_type*>(nullptr);
}
template<typename T0>
auto operator<=>(std::nullptr_t, const shared_ptr<T0>& right) noexcept {
return static_cast<shared_ptr<T0>::element_type*>(nullptr) <=> right.get();
}
template<typename T1, typename T2>
auto operator==(const shared_ptr<T1>& left, const shared_ptr<T2>& right) noexcept {
return left.get() == right.get();
}
template<typename T0>
auto operator==(const shared_ptr<T0>& left, std::nullptr_t) noexcept {
return left.get() == static_cast<shared_ptr<T0>::element_type*>(nullptr);
}
template<typename T0>
auto operator==(std::nullptr_t, const shared_ptr<T0>& right) noexcept {
return static_cast<shared_ptr<T0>::element_type*>(nullptr) == right.get();
}
template<typename T>
class atomic_shared_ptr {
using shared_ptr_type = shared_ptr<T>;
using control_block_type = details::basic_control_block<T>;
public:
constexpr atomic_shared_ptr() noexcept = default;
constexpr explicit(false) atomic_shared_ptr(std::nullptr_t) noexcept // NOLINT(google-explicit-constructor)
: control_block{nullptr} { }
atomic_shared_ptr(shared_ptr_type desired) { // NOLINT(google-explicit-constructor)
control_block.store(std::exchange(desired.control_block, nullptr));
}
atomic_shared_ptr(const atomic_shared_ptr&) = delete;
atomic_shared_ptr& operator=(const atomic_shared_ptr&) = delete;
~atomic_shared_ptr() { store(nullptr); }
bool is_lock_free() const noexcept {
return control_block.is_lock_free();
}
constexpr static bool is_always_lock_free = std::atomic<control_block_type*>::is_always_lock_free;
[[nodiscard]] shared_ptr_type load() const {
folly::hazptr_holder hp = folly::make_hazard_pointer();
control_block_type* current_control_block = nullptr;
do {
current_control_block = hp.protect(control_block);
} while (current_control_block != nullptr && !current_control_block->increment_if_nonzero());
return shared_ptr<T>(current_control_block);
}
void store(shared_ptr_type desired) {
auto new_control_block = std::exchange(desired.control_block, nullptr);
auto old_control_block = control_block.exchange(new_control_block);
if (old_control_block) {
old_control_block->decrement_count();
}
}
shared_ptr_type exchange(shared_ptr_type desired) noexcept {
auto new_control_block = std::exchange(desired.control_block, nullptr);
auto old_control_block = control_block.exchange(new_control_block);
return shared_ptr_type(old_control_block);
}
bool compare_exchange_weak(shared_ptr_type& expected, shared_ptr_type desired) {
auto expected_ctrl_block = expected.control_block;
auto desired_ctrl_block = desired.control_block;
if (control_block.compare_exchange_weak(expected_ctrl_block, desired_ctrl_block)) {
if (expected_ctrl_block) {
expected_ctrl_block->decrement_count();
}
desired.control_block = nullptr;
return true;
}
else {
expected = load(); // It's possible that expected ABAs and stays the same on failure, hence
return false; // why this algorithm can not be used to implement compare_exchange_strong
}
}
bool compare_exchange_strong(shared_ptr_type& expected, shared_ptr_type desired) {
auto expected_ctrl_block = expected.control_block;
// If expected changes then we have completed the operation (unsuccessfully), we only
// have to loop in case expected ABAs or the weak operation fails spuriously.
do {
if (compare_exchange_weak(expected, desired)) {
return true;
}
} while (expected_ctrl_block == expected.control_block);
return false;
}
private:
mutable std::atomic<control_block_type*> control_block;
};
} // namespace basic
} // namespace parlay