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
|
#ifndef WEAK_PTR_INL_H_
#error "Direct inclusion of this file is not allowed, include weak_ptr.h"
// For the sake of sane code completion.
#include "weak_ptr.h"
#endif
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T>
TWeakPtr<T>::TWeakPtr(std::nullptr_t)
{ }
template <class T>
TWeakPtr<T>::TWeakPtr(T* p) noexcept
: T_(p)
{
#if defined(_tsan_enabled_)
if (T_) {
RefCounter_ = GetRefCounter(T_);
}
#endif
AcquireRef();
}
template <class T>
TWeakPtr<T>::TWeakPtr(const TIntrusivePtr<T>& ptr) noexcept
: TWeakPtr(ptr.Get())
{ }
template <class T>
template <class U, class>
TWeakPtr<T>::TWeakPtr(const TIntrusivePtr<U>& ptr) noexcept
: TWeakPtr(ptr.Get())
{
static_assert(
std::derived_from<T, TRefCountedBase>,
"Cast allowed only for types derived from TRefCountedBase");
}
template <class T>
TWeakPtr<T>::TWeakPtr(const TWeakPtr& other) noexcept
: T_(other.T_)
#if defined(_tsan_enabled_)
, RefCounter_(other.RefCounter_)
#endif
{
AcquireRef();
}
template <class T>
template <class U, class>
TWeakPtr<T>::TWeakPtr(const TWeakPtr<U>& other) noexcept
: TWeakPtr(other.Lock())
{
static_assert(
std::derived_from<T, TRefCountedBase>,
"Cast allowed only for types derived from TRefCountedBase");
}
template <class T>
TWeakPtr<T>::TWeakPtr(TWeakPtr&& other) noexcept
{
other.Swap(*this);
}
template <class T>
template <class U, class>
TWeakPtr<T>::TWeakPtr(TWeakPtr<U>&& other) noexcept
{
static_assert(
std::derived_from<T, TRefCountedBase>,
"Cast allowed only for types derived from TRefCountedBase");
TIntrusivePtr<U> strongOther = other.Lock();
if (strongOther) {
T_ = other.T_;
other.T_ = nullptr;
#if defined(_tsan_enabled_)
RefCounter_ = other.RefCounter_;
other.RefCounter_ = nullptr;
#endif
}
}
template <class T>
TWeakPtr<T>::~TWeakPtr()
{
ReleaseRef();
}
template <class T>
template <class U>
TWeakPtr<T>& TWeakPtr<T>::operator=(const TIntrusivePtr<U>& ptr) noexcept
{
static_assert(
std::is_convertible_v<U*, T*>,
"U* must be convertible to T*");
TWeakPtr(ptr).Swap(*this);
return *this;
}
template <class T>
TWeakPtr<T>& TWeakPtr<T>::operator=(const TWeakPtr& other) noexcept
{
TWeakPtr(other).Swap(*this);
return *this;
}
template <class T>
template <class U>
TWeakPtr<T>& TWeakPtr<T>::operator=(const TWeakPtr<U>& other) noexcept
{
static_assert(
std::is_convertible_v<U*, T*>,
"U* must be convertible to T*");
TWeakPtr(other).Swap(*this);
return *this;
}
template <class T>
TWeakPtr<T>& TWeakPtr<T>::operator=(TWeakPtr&& other) noexcept
{
other.Swap(*this);
return *this;
}
template <class T>
template <class U>
TWeakPtr<T>& TWeakPtr<T>::operator=(TWeakPtr<U>&& other) noexcept
{
static_assert(
std::is_convertible_v<U*, T*>,
"U* must be convertible to T*");
TWeakPtr(std::move(other)).Swap(*this);
return *this;
}
template <class T>
void TWeakPtr<T>::Reset() // noexcept
{
TWeakPtr().Swap(*this);
}
template <class T>
void TWeakPtr<T>::Reset(T* p) // noexcept
{
TWeakPtr(p).Swap(*this);
}
template <class T>
template <class U>
void TWeakPtr<T>::Reset(const TIntrusivePtr<U>& ptr) // noexcept
{
static_assert(
std::is_convertible_v<U*, T*>,
"U* must be convertible to T*");
TWeakPtr(ptr).Swap(*this);
}
template <class T>
void TWeakPtr<T>::Swap(TWeakPtr& other) noexcept
{
DoSwap(T_, other.T_);
#if defined(_tsan_enabled_)
DoSwap(RefCounter_, other.RefCounter_);
#endif
}
template <class T>
TIntrusivePtr<T> TWeakPtr<T>::Lock() const noexcept
{
return T_ && GetRefCounterImpl()->TryRef()
? TIntrusivePtr<T>(T_, false)
: TIntrusivePtr<T>();
}
template <class T>
bool TWeakPtr<T>::IsExpired() const noexcept
{
return !T_ || (GetRefCounterImpl()->GetRefCount() == 0);
}
template <class T>
T* TWeakPtr<T>::Get() const noexcept
{
return T_;
}
template <class T>
const TRefCounter* TWeakPtr<T>::TryGetRefCounterImpl() const
{
return T_ ? GetRefCounterImpl() : nullptr;
}
template <class T>
void TWeakPtr<T>::AcquireRef()
{
if (T_) {
GetRefCounterImpl()->WeakRef();
}
}
template <class T>
void TWeakPtr<T>::ReleaseRef()
{
if (T_) {
// Support incomplete type.
if (GetRefCounterImpl()->WeakUnref()) {
DeallocateRefCounted(T_);
}
}
}
#if defined(_tsan_enabled_)
template <class T>
const TRefCounter* TWeakPtr<T>::GetRefCounterImpl() const
{
return RefCounter_;
}
#else
template <class T>
const TRefCounter* TWeakPtr<T>::GetRefCounterImpl() const
{
return GetRefCounter(T_);
}
#endif
////////////////////////////////////////////////////////////////////////////////
template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, const TWeakPtr<T2>& rhs)
{
return lhs.Get() == rhs.Get();
}
template <class T1, class T2>
bool operator==(const TIntrusivePtr<T1>& lhs, const TWeakPtr<T2>& rhs)
{
return lhs.Get() == rhs.Get();
}
template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, const TIntrusivePtr<T2>& rhs)
{
return lhs.Get() == rhs.Get();
}
template <class T1, class T2>
bool operator==(T1* lhs, const TWeakPtr<T2>& rhs)
{
return lhs == rhs.Get();
}
template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, T2* rhs)
{
return lhs.Get() == rhs;
}
template <class T2>
bool operator==(std::nullptr_t, const TWeakPtr<T2>& rhs)
{
return nullptr == rhs.Get();
}
template <class T1>
bool operator==(const TWeakPtr<T1>& lhs, std::nullptr_t)
{
return lhs.Get() == nullptr;
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
TWeakPtr<T> MakeWeak(T* p)
{
return TWeakPtr<T>(p);
}
template <class T>
TWeakPtr<T> MakeWeak(const TIntrusivePtr<T>& p)
{
return TWeakPtr<T>(p);
}
template <typename T>
int ResetAndGetResidualRefCount(TIntrusivePtr<T>& pointer)
{
auto weakPointer = MakeWeak(pointer);
pointer.Reset();
pointer = weakPointer.Lock();
if (pointer) {
// This _may_ return 0 if we are again the only holder of the pointee.
return pointer->GetRefCount() - 1;
} else {
return 0;
}
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
std::size_t TTransparentWeakPtrHasher::operator()(const TWeakPtr<T>& ptr) const
{
return THash<const NYT::TRefCountedBase*>()(ptr.Get());
}
template <class T>
std::size_t TTransparentWeakPtrHasher::operator()(const TIntrusivePtr<T>& ptr) const
{
return THash<const NYT::TRefCountedBase*>()(ptr.Get());
}
template <class T>
std::size_t TTransparentWeakPtrHasher::operator()(T* ptr) const
{
return THash<const NYT::TRefCountedBase*>()(ptr);
}
////////////////////////////////////////////////////////////////////////////////
//! Abseil hash support for TWeakPtr.
template <class THash, class T>
THash AbslHashValue(THash hash, const TWeakPtr<T>& ptr)
{
return THash::combine(std::move(hash), ptr.Get());
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
////////////////////////////////////////////////////////////////////////////////
//! A hasher for TWeakPtr.
template <class T>
size_t THash<NYT::TWeakPtr<T>>::operator()(const NYT::TWeakPtr<T>& ptr) const
{
return THash<const NYT::TRefCountedBase*>()(ptr.Get());
}
////////////////////////////////////////////////////////////////////////////////
|