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
|
#ifndef ATOMIC_INTRUSIVE_PTR_INL_H_
#error "Direct inclusion of this file is not allowed, include atomic_intrusive_ptr.h"
// For the sake of sane code completion.
#include "atomic_intrusive_ptr.h"
#endif
#undef ATOMIC_INTRUSIVE_PTR_INL_H_
#include <util/system/spinlock.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T>
TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(std::nullptr_t)
{ }
template <class T>
TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(TIntrusivePtr<T> other)
{
Store(std::move(other));
return *this;
}
template <class T>
TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(std::nullptr_t)
{
Reset();
return *this;
}
template <class T>
TAtomicIntrusivePtr<T>::operator bool() const
{
return Get();
}
#if defined(_lsan_enabled_) || defined(_asan_enabled_)
template <class T>
TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TIntrusivePtr<T> other)
: Ptr_(std::move(other))
{ }
template <class T>
TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other)
: Ptr_(std::move(other))
{ }
template <class T>
TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr() = default;
template <class T>
TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Acquire() const
{
auto guard = Guard(Lock_);
return Ptr_;
}
template <class T>
TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
{
auto guard = Guard(Lock_);
Ptr_.Swap(other);
return other;
}
template <class T>
void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
{
Exchange(std::move(other));
}
template <class T>
void TAtomicIntrusivePtr<T>::Reset()
{
Exchange(nullptr);
}
template <class T>
bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
{
auto guard = Guard(Lock_);
if (Ptr_.Get() != comparePtr) {
comparePtr = Ptr_.Get();
return false;
}
auto targetPtr = TIntrusivePtr<T>(target, /*addReference*/ false);
Ptr_.Swap(targetPtr);
guard.Release();
// targetPtr will die here.
return true;
}
template <class T>
bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
{
return CompareAndSwap(comparePtr, target.Release());
}
template <class T>
typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
{
auto guard = Guard(Lock_);
return Ptr_.Get();
}
#else
template <class T>
TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TIntrusivePtr<T> other)
: Ptr_(AcquireObject(other.Release(), /*consumeRef*/ true))
{ }
template <class T>
TAtomicIntrusivePtr<T>::TAtomicIntrusivePtr(TAtomicIntrusivePtr&& other)
: Ptr_(other.Ptr_.load(std::memory_order::relaxed))
{
other.Ptr_.store(nullptr, std::memory_order::relaxed);
}
template <class T>
TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr()
{
ReleaseObject(Ptr_.load());
}
template <class T>
TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Acquire() const
{
auto ptr = Ptr_.load();
while (true) {
auto [obj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
if (!obj) {
return {};
}
YT_VERIFY(localRefs < ReservedRefCount);
auto newLocalRefs = localRefs + 1;
if (newLocalRefs == ReservedRefCount) {
SpinLockPause();
ptr = Ptr_.load();
continue;
}
// Can not Ref(obj) here because it can be destroyed.
auto newPtr = TTaggedPtr(obj, newLocalRefs).Pack();
if (Ptr_.compare_exchange_weak(ptr, newPtr)) {
ptr = newPtr;
if (Y_UNLIKELY(newLocalRefs > ReservedRefCount / 2)) {
Ref(obj, ReservedRefCount / 2);
// Decrease local ref count.
while (true) {
auto [currentObj, localRefs] = TTaggedPtr<T>::Unpack(ptr);
if (currentObj != obj || localRefs <= ReservedRefCount / 2) {
Unref(obj, ReservedRefCount / 2);
break;
}
if (Ptr_.compare_exchange_weak(ptr, TTaggedPtr(obj, localRefs - ReservedRefCount / 2).Pack())) {
break;
}
}
}
return TIntrusivePtr<T>(obj, false);
}
}
}
template <class T>
TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other)
{
auto [obj, localRefs] = TTaggedPtr<T>::Unpack(Ptr_.exchange(AcquireObject(other.Release(), /*consumeRef*/ true)));
DoRelease(obj, localRefs + 1);
return TIntrusivePtr<T>(obj, false);
}
template <class T>
void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other)
{
ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), /*consumeRef*/ true)));
}
template <class T>
void TAtomicIntrusivePtr<T>::Reset()
{
ReleaseObject(Ptr_.exchange(0));
}
template <class T>
bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, T* target)
{
auto* targetPtr = AcquireObject(target, /*consumeRef*/ false);
auto currentPtr = Ptr_.load();
if (UnpackPointer<T>(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
ReleaseObject(currentPtr);
return true;
}
comparePtr = UnpackPointer<T>(currentPtr).Ptr;
ReleaseObject(targetPtr);
return false;
}
template <class T>
bool TAtomicIntrusivePtr<T>::CompareAndSwap(TRawPtr& comparePtr, TIntrusivePtr<T> target)
{
// TODO(lukyan): Make helper for packed owning ptr?
auto targetPtr = AcquireObject(target.Release(), /*consumeRef*/ true);
auto currentPtr = Ptr_.load();
if (TTaggedPtr<T>::Unpack(currentPtr).Ptr == comparePtr && Ptr_.compare_exchange_strong(currentPtr, targetPtr)) {
ReleaseObject(currentPtr);
return true;
}
comparePtr = TTaggedPtr<T>::Unpack(currentPtr).Ptr;
ReleaseObject(targetPtr);
return false;
}
template <class T>
typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
{
return TTaggedPtr<void>::Unpack(Ptr_.load()).Ptr;
}
template <class T>
typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::get() const
{
return Get();
}
template <class T>
TPackedPtr TAtomicIntrusivePtr<T>::AcquireObject(T* obj, bool consumeRef)
{
if (obj) {
Ref(obj, ReservedRefCount - static_cast<int>(consumeRef));
}
return TTaggedPtr(obj).Pack();
}
template <class T>
void TAtomicIntrusivePtr<T>::ReleaseObject(TPackedPtr packedPtr)
{
auto [obj, localRefs] = TTaggedPtr<T>::Unpack(packedPtr);
DoRelease(obj, localRefs);
}
template <class T>
void TAtomicIntrusivePtr<T>::DoRelease(T* obj, int refs)
{
if (obj) {
Unref(obj, ReservedRefCount - refs);
}
}
#endif
////////////////////////////////////////////////////////////////////////////////
template <class T>
bool operator==(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
{
return lhs.Get() == rhs.Get();
}
template <class T>
bool operator==(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
{
return lhs.Get() == rhs.Get();
}
template <class T>
bool operator!=(const TAtomicIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
{
return lhs.Get() != rhs.Get();
}
template <class T>
bool operator!=(const TIntrusivePtr<T>& lhs, const TAtomicIntrusivePtr<T>& rhs)
{
return lhs.Get() != rhs.Get();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|