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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#pragma once
#include <atomic>
#include <cstdint>
#include <type_traits>
#include <utility>
#include <util/system/yassert.h>
#if !defined(_x86_64_) && !defined(__aarch64__)
#error "only 64 bit platforms are supported"
#endif
namespace NPrivate {
#if defined(_x86_64_)
static constexpr unsigned char USABLE_ADDRESS_BITS = 48;
#elif defined(__aarch64__)
static constexpr unsigned char USABLE_ADDRESS_BITS = 52;
#else
#error "number of using bits is undefined"
#endif
struct TRefCounter {
static constexpr ui64 USE_INCREMENT = 2;
static constexpr ui64 DESTROYED_FLAG = 1;
std::atomic<ui64> ref_count_{USE_INCREMENT};
std::atomic<ui64> weak_count_{1};
virtual ~TRefCounter() noexcept(false) = default;
virtual void* GetPtr() noexcept = 0;
virtual void DestroyPayload() = 0;
template <class PayloadType>
PayloadType* GetPayload() noexcept {
return reinterpret_cast<PayloadType*>(GetPtr());
}
ui64 Ref(ui64 add = USE_INCREMENT) noexcept {
return ref_count_.fetch_add(add, std::memory_order_relaxed) + add;
}
ui64 Unref(ui64 sub = USE_INCREMENT) noexcept {
return ref_count_.fetch_sub(sub, std::memory_order_seq_cst) - sub;
}
void UnrefAndDelete(ui64 sub = USE_INCREMENT) {
if (Unref(sub) != 0)
return;
ui64 expect = 0;
bool flag_is_set = ref_count_.compare_exchange_strong(
expect, DESTROYED_FLAG,
std::memory_order_seq_cst, std::memory_order_relaxed);
if (!flag_is_set)
return;
DestroyPayload();
UnrefWeakAndDelete();
}
void* UnrefAndReleaseLast(ui64 sub = USE_INCREMENT) noexcept {
if (Unref(sub) != 0)
return nullptr;
ui64 expect = 0;
bool flag_is_set = ref_count_.compare_exchange_strong(
expect, DESTROYED_FLAG,
std::memory_order_seq_cst, std::memory_order_relaxed);
if (!flag_is_set)
return nullptr;
void* result = GetPtr();
UnrefWeakAndDelete();
return result;
}
bool RefFromWeak(ui64 add = USE_INCREMENT) noexcept {
ui64 result = Ref(add);
if (!(result & DESTROYED_FLAG))
return true;
UnrefAndDelete(add);
return false;
}
ui64 RefWeak(ui64 add = 1) noexcept {
return weak_count_.fetch_add(add, std::memory_order_relaxed) + add;
}
ui64 UnrefWeak(ui64 sub = 1) noexcept {
return weak_count_.fetch_sub(sub, std::memory_order_seq_cst) - sub;
}
void UnrefWeakAndDelete(ui64 sub = 1) noexcept {
if (UnrefWeak(sub) != 0)
return;
delete this;
}
};
template <class PayloadType>
struct TRefCounterWithPointer: public TRefCounter {
PayloadType* obj;
explicit TRefCounterWithPointer(PayloadType* ptr) noexcept
: obj(ptr)
{
}
~TRefCounterWithPointer() noexcept(noexcept(delete obj)) override {
delete obj;
}
void* GetPtr() noexcept override {
return obj;
}
void DestroyPayload() noexcept(noexcept(delete obj)) override {
delete obj;
obj = nullptr;
}
};
class TSharedBasePtr {
public:
TSharedBasePtr() noexcept = default;
TSharedBasePtr(TSharedBasePtr&) = delete;
void operator=(TSharedBasePtr&) = delete;
explicit TSharedBasePtr(TRefCounter* counter_ptr) noexcept
: ptr_((uintptr_t)counter_ptr)
{
Y_ABORT_UNLESS((ptr_.load(std::memory_order_relaxed) & ~PTR_MASK) == 0,
"you must provide a clean ptr");
}
template <class PayloadType>
explicit TSharedBasePtr(PayloadType* obj_ptr) noexcept
: TSharedBasePtr(
(TRefCounter*)new NPrivate::TRefCounterWithPointer<PayloadType>(obj_ptr)) {
}
static constexpr uintptr_t CONCURRENT_INCREMENT = (uintptr_t)1 << USABLE_ADDRESS_BITS;
static constexpr uintptr_t PTR_MASK = CONCURRENT_INCREMENT - 1;
TRefCounter* ConcurrentAcquire() noexcept {
auto result =
ptr_.fetch_add(CONCURRENT_INCREMENT, std::memory_order_seq_cst);
auto ptr_result = CleanUpPtr(result);
if (ptr_result)
ptr_result->Ref(CONCURRENT_INCREMENT + TRefCounter::USE_INCREMENT);
return ptr_result;
}
TRefCounter* ConcurrentWeakAcquire() noexcept {
auto result =
ptr_.fetch_add(CONCURRENT_INCREMENT, std::memory_order_seq_cst);
auto ptr_result = CleanUpPtr(result);
if (ptr_result) {
ptr_result->RefWeak();
ptr_result->Ref(CONCURRENT_INCREMENT);
}
return ptr_result;
}
static void DestroyPtr(TRefCounter* ptr) {
auto clean_ptr = CleanUpPtr(ptr);
if (!clean_ptr)
return;
ui64 cnt = GetCounter(ptr);
clean_ptr->UnrefAndDelete(cnt + TRefCounter::USE_INCREMENT);
}
static void* ReleaseLast(TRefCounter* ptr) noexcept {
auto clean_ptr = CleanUpPtr(ptr);
if (!clean_ptr)
return nullptr;
ui64 cnt = GetCounter(ptr);
return clean_ptr->UnrefAndReleaseLast(cnt + TRefCounter::USE_INCREMENT);
}
static void DestroyWeakPtr(TRefCounter* ptr) noexcept {
auto clean_ptr = CleanUpPtr(ptr);
if (clean_ptr) {
ui64 cnt = GetCounter(ptr);
clean_ptr->UnrefWeakAndDelete(cnt);
}
}
static TRefCounter* CleanUpPtr(TRefCounter* ptr) noexcept {
return (TRefCounter*)((uintptr_t)ptr & PTR_MASK);
}
static TRefCounter* CleanUpPtr(uintptr_t ptr) noexcept {
return (TRefCounter*)(ptr & PTR_MASK);
}
static uintptr_t GetCounter(TRefCounter* ptr) noexcept {
return (uintptr_t)ptr & ~PTR_MASK;
}
TRefCounter* GetClean() const noexcept {
return CleanUpPtr(ptr_.load(std::memory_order_relaxed));
}
TRefCounter* GetRaw() const noexcept {
return (TRefCounter*)ptr_.load(std::memory_order_relaxed);
}
TRefCounter* Swap(TRefCounter* other) noexcept {
return (TRefCounter*)ptr_.exchange(
(uintptr_t)other, std::memory_order_seq_cst);
}
size_t UseCount() const noexcept {
auto ptr = GetRaw();
auto clean_ptr = CleanUpPtr(ptr);
if (!clean_ptr)
return 0;
ui64 result = clean_ptr->ref_count_.load(std::memory_order_relaxed);
result -= GetCounter(ptr);
result /= TRefCounter::USE_INCREMENT;
return result;
}
private:
std::atomic<uintptr_t> ptr_{0};
};
}
// This class provides thread-safe and atomic operations
// with wait-free guarantee for copying and destroying shared pointers.
// You may safely copy this shared pointer from multiple threads without
// explicit locks, hazard pointers or other synchonization.
// This class makes use of high bits of 64-bit pointers which are normally
// ignored by hardware. Thus it works on x86_64 (amd64) platforms.
// Wait-free guarantee is supported under the following circumstances:
// - atomic<void*>.fetch_add is wait-free.
// - atomic<void*>.exchange is wait-free.
// - atomic<ui64>.compare_exchange_strong is wait-free.
// This class does not provide wait-free guarantee for arm64 or power_pc
// platforms because these platforms do not provide wait-free guarantee for
// necessary atomic operations. It requires more sophisticated algorithms
// to achieve wait-free guarantee using lock-free atomic operations.
template <class PayloadType>
class TTrueAtomicSharedPtr {
public:
TTrueAtomicSharedPtr() noexcept = default;
explicit TTrueAtomicSharedPtr(PayloadType* obj_ptr) noexcept
: ptr_(obj_ptr)
{
}
~TTrueAtomicSharedPtr() noexcept(PAYLOAD_DESTRUCTOR_IS_NOEXCEPT) {
auto ptr = ptr_.GetRaw();
NPrivate::TSharedBasePtr::DestroyPtr(ptr);
}
TTrueAtomicSharedPtr(const TTrueAtomicSharedPtr& other) noexcept
: ptr_(other.ptr_.ConcurrentAcquire())
{
}
TTrueAtomicSharedPtr(TTrueAtomicSharedPtr&& other) noexcept
: ptr_(other.ptr_.Swap(nullptr))
{
}
// WARNING: it is thread-safe but it is not atomic
TTrueAtomicSharedPtr& operator=(const TTrueAtomicSharedPtr& other) noexcept(PAYLOAD_DESTRUCTOR_IS_NOEXCEPT) {
auto new_ptr = other.ptr_.ConcurrentAcquire();
auto back_ptr = ptr_.Swap(new_ptr);
NPrivate::TSharedBasePtr::DestroyPtr(back_ptr);
return *this;
}
// WARNING: it is thread-safe but it is not atomic
TTrueAtomicSharedPtr& operator=(TTrueAtomicSharedPtr&& other) noexcept(PAYLOAD_DESTRUCTOR_IS_NOEXCEPT) {
auto back_ptr = other.ptr_.Swap(nullptr);
back_ptr = ptr_.Swap(back_ptr);
NPrivate::TSharedBasePtr::DestroyPtr(back_ptr);
return *this;
}
// WARNING: it is not thread safe, consider acquire a local copy
// before dereferencing
PayloadType* operator->() const noexcept {
NPrivate::TRefCounter* ptr = ptr_.GetClean();
Y_ABORT_UNLESS(ptr, "dereferencing nullptr");
return ptr->GetPayload<PayloadType>();
}
// WARNING: it is not thread safe, consider acquire a local copy
// before dereferencing
PayloadType& operator*() const noexcept {
NPrivate::TRefCounter* ptr = ptr_.GetClean();
Y_ABORT_UNLESS(ptr, "dereferencing nullptr");
return *ptr->GetPayload<PayloadType>();
}
// WARNING: it is not thread safe, consider acquire a local copy
// before getting a pointer
PayloadType* get() const noexcept {
auto ptr = ptr_.GetClean();
return ptr ? ptr->template GetPayload<PayloadType>() : nullptr;
}
explicit operator bool() const noexcept {
return ptr_.GetClean();
}
// WARNING: swap is thread-safe but it is not atomic at all circumstances.
// Having A with possible accesses from multiple threads and B with exclusive
// use from a single thread, then calling A.swap(B) is atomic.
void swap(TTrueAtomicSharedPtr<PayloadType>& other) noexcept {
NPrivate::TRefCounter* acquired = other.ptr_.ConcurrentAcquire();
acquired = ptr_.Swap(acquired);
acquired = other.ptr_.Swap(acquired);
NPrivate::TSharedBasePtr::DestroyPtr(acquired);
}
void reset() noexcept {
auto back_ptr = ptr_.Swap(nullptr);
NPrivate::TSharedBasePtr::DestroyPtr(back_ptr);
}
// ReleaseLast release and returns a controlled object if there is no other
// references to the object, otherwise returns nullptr.
// This pointer becomes empty.
// This is useful in order to postpone destruction of
// a heavy-destructible object.
PayloadType* ReleaseLast() noexcept {
NPrivate::TRefCounter* cnt_obj = ptr_.Swap(nullptr);
void* obj_ptr = NPrivate::TSharedBasePtr::ReleaseLast(cnt_obj);
return reinterpret_cast<PayloadType*>(obj_ptr);
}
private:
mutable NPrivate::TSharedBasePtr ptr_;
explicit TTrueAtomicSharedPtr(NPrivate::TRefCounter* control) noexcept
: ptr_(control)
{
}
static constexpr bool PAYLOAD_DESTRUCTOR_IS_NOEXCEPT =
std::is_nothrow_destructible<PayloadType>::value;
template <class T>
friend class TTrueAtomicWeakPtr;
};
template <class PayloadType, class... ArgTypes>
TTrueAtomicSharedPtr<PayloadType> MakeTrueAtomicShared(ArgTypes&&... args) {
auto obj_ptr = new PayloadType(std::forward<ArgTypes>(args)...);
return TTrueAtomicSharedPtr<PayloadType>(obj_ptr);
}
// This class provides thread-safe and atomic operations
// with wait-free guarantee for copying and destroying weak pointers.
// See description of TTrueAtomicSharedPtr.
// All guarantees are the same as for TTrueAtomicSharedPtr.
template <class PayloadType>
class TTrueAtomicWeakPtr {
public:
TTrueAtomicWeakPtr() noexcept = default;
TTrueAtomicWeakPtr(const TTrueAtomicSharedPtr<PayloadType>& shared_ptr) noexcept
: ptr_(shared_ptr.ptr_.ConcurrentWeakAcquire())
{
}
TTrueAtomicWeakPtr(const TTrueAtomicWeakPtr& other) noexcept
: ptr_(other.ptr_.ConcurrentWeakAcquire())
{
}
TTrueAtomicWeakPtr& operator=(const TTrueAtomicWeakPtr& other) noexcept {
auto new_ptr = other.ptr_.ConcurrentWeakAcquire();
auto back_ptr = ptr_.Swap(new_ptr);
NPrivate::TSharedBasePtr::DestroyWeakPtr(back_ptr);
return *this;
}
TTrueAtomicWeakPtr& operator=(TTrueAtomicWeakPtr&& other) noexcept {
auto new_ptr = other.ptr_.Swap(nullptr);
auto back_ptr = ptr_.Swap(new_ptr);
NPrivate::TSharedBasePtr::DestroyWeakPtr(back_ptr);
return *this;
}
TTrueAtomicSharedPtr<PayloadType> lock() noexcept {
// create local TTrueAtomicWeakPtr to avoid concurrent changes of this
TTrueAtomicWeakPtr<PayloadType> local(*this);
if (!local.ptr_.GetRaw()->RefFromWeak())
return TTrueAtomicSharedPtr<PayloadType>();
return TTrueAtomicSharedPtr<PayloadType>(ptr_.GetClean());
}
void reset() noexcept {
auto back_ptr = ptr_.Swap(nullptr);
NPrivate::TSharedBasePtr::DestroyWeakPtr(back_ptr);
}
// WARNING: swap is thread-safe but it is not atomic at all circumstances.
// Having A with possible accesses from multiple threads and B with exclusive
// use from a single thread, then calling A.swap(B) is atomic.
void swap(TTrueAtomicWeakPtr& other) noexcept {
auto copy = other.ptr_.ConcurrentWeakAcquire();
auto back_ptr = ptr_.Swap(copy);
back_ptr = other.ptr_.Swap(back_ptr);
NPrivate::TSharedBasePtr::DestroyWeakPtr(back_ptr);
}
size_t use_count() const noexcept {
return ptr_.UseCount();
}
private:
mutable NPrivate::TSharedBasePtr ptr_{nullptr};
};
|