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
|
#include "propagating_storage.h"
#include <library/cpp/yt/small_containers/compact_flat_map.h>
#include <library/cpp/yt/threading/fork_aware_spin_lock.h>
namespace NYT::NConcurrency {
////////////////////////////////////////////////////////////////////////////////
class TPropagatingStorageImplBase
{
public:
using TStorage = TCompactFlatMap<std::type_index, std::any, 16>;
bool IsEmpty() const
{
return Data_.empty();
}
const std::any* GetRaw(const std::type_info& typeInfo) const
{
auto iter = Data_.find(std::type_index(typeInfo));
return iter == Data_.end() ? nullptr : &iter->second;
}
std::optional<std::any> ExchangeRaw(std::any value)
{
std::type_index key(value.type());
auto iter = Data_.find(key);
if (iter == Data_.end()) {
Data_.emplace(key, std::move(value));
return std::nullopt;
}
return std::exchange(iter->second, std::move(value));
}
std::optional<std::any> RemoveRaw(const std::type_info& typeInfo)
{
auto iter = Data_.find(std::type_index(typeInfo));
if (iter == Data_.end()) {
return std::nullopt;
}
auto result = std::make_optional<std::any>(iter->second);
Data_.erase(iter);
return result;
}
DEFINE_SIGNAL_SIMPLE(void(), OnBeforeUninstall);
DEFINE_SIGNAL_SIMPLE(void(), OnAfterInstall);
private:
TStorage Data_;
};
////////////////////////////////////////////////////////////////////////////////
class TPropagatingStorage::TImpl
: public TRefCounted
, public TPropagatingStorageImplBase
{
public:
TImpl() = default;
TIntrusivePtr<TImpl> Clone() const
{
return New<TImpl>(static_cast<const TPropagatingStorageImplBase&>(*this));
}
private:
DECLARE_NEW_FRIEND()
explicit TImpl(const TPropagatingStorageImplBase& base)
: TPropagatingStorageImplBase(base)
{ }
};
////////////////////////////////////////////////////////////////////////////////
TPropagatingStorage::TPropagatingStorage() = default;
TPropagatingStorage::TPropagatingStorage(TIntrusivePtr<TImpl> impl)
: Impl_(std::move(impl))
{ }
TPropagatingStorage::~TPropagatingStorage() = default;
TPropagatingStorage::TPropagatingStorage(const TPropagatingStorage& other) = default;
TPropagatingStorage::TPropagatingStorage(TPropagatingStorage&& other) = default;
TPropagatingStorage& TPropagatingStorage::operator=(const TPropagatingStorage& other) = default;
TPropagatingStorage& TPropagatingStorage::operator=(TPropagatingStorage&& other) = default;
bool TPropagatingStorage::IsNull() const
{
return !static_cast<bool>(Impl_);
}
bool TPropagatingStorage::IsEmpty() const
{
return !Impl_ || Impl_->IsEmpty();
}
const std::any* TPropagatingStorage::FindRaw(const std::type_info& typeInfo) const
{
if (!Impl_) {
return nullptr;
}
return Impl_->GetRaw(typeInfo);
}
std::optional<std::any> TPropagatingStorage::ExchangeRaw(std::any value)
{
EnsureUnique();
return Impl_->ExchangeRaw(std::move(value));
}
std::optional<std::any> TPropagatingStorage::RemoveRaw(const std::type_info& typeInfo)
{
EnsureUnique();
return Impl_->RemoveRaw(typeInfo);
}
void TPropagatingStorage::SubscribeOnAfterInstall(const TCallback<void()>& callback)
{
EnsureUnique();
Impl_->SubscribeOnAfterInstall(callback);
}
void TPropagatingStorage::UnsubscribeOnAfterInstall(const TCallback<void()>& callback)
{
EnsureUnique();
Impl_->UnsubscribeOnAfterInstall(callback);
}
void TPropagatingStorage::SubscribeOnBeforeUninstall(const TCallback<void()>& callback)
{
EnsureUnique();
Impl_->SubscribeOnBeforeUninstall(callback);
}
void TPropagatingStorage::UnsubscribeOnBeforeUninstall(const TCallback<void()>& callback)
{
EnsureUnique();
Impl_->UnsubscribeOnBeforeUninstall(callback);
}
TPropagatingStorage TPropagatingStorage::Create()
{
return TPropagatingStorage(New<TImpl>());
}
void TPropagatingStorage::EnsureUnique()
{
if (!Impl_) {
Impl_ = New<TImpl>();
return;
}
// NB(gepardo). It can be proved that this code doesn't clone only if there are no references to this storage
// in other threads, so our copy-on-write mechanism doesn't result in data races.
//
// Basically, we need to prove the following:
//
// 1) All the previous unrefs happens-before we obtain the reference count. This is true, because GetRefCount()
// does acquire-load on the reference counter, while Unref() does release-store on it.
//
// 2) Modifying the object happens-before taking any new references. This is true, because we are the only owner
// of the reference, so Ref() can only be done later in this thread, so modifications will be sequenced-before
// taking new references.
auto refCount = Impl_->GetRefCount();
if (refCount == 1) {
return;
}
YT_VERIFY(refCount > 1);
Impl_ = Impl_->Clone();
}
////////////////////////////////////////////////////////////////////////////////
class TPropagatingStorageManager
{
public:
static TPropagatingStorageManager* Get()
{
return Singleton<TPropagatingStorageManager>();
}
TPropagatingStorage& GetCurrentPropagatingStorage()
{
return *Slot_;
}
const TPropagatingStorage& GetPropagatingStorage(const TFls& fls)
{
return *Slot_.Get(fls);
}
void InstallGlobalSwitchHandler(TPropagatingStorageGlobalSwitchHandler handler)
{
auto guard = Guard(Lock_);
int index = SwitchHandlerCount_.load();
YT_VERIFY(index < MaxSwitchHandlerCount);
SwitchHandlers_[index] = handler;
++SwitchHandlerCount_;
}
TPropagatingStorage SwitchPropagatingStorage(TPropagatingStorage newStorage)
{
auto& storage = *Slot_;
int count = SwitchHandlerCount_.load(std::memory_order::acquire);
for (int index = 0; index < count; ++index) {
SwitchHandlers_[index](storage, newStorage);
}
return std::exchange(storage, std::move(newStorage));
}
private:
TFlsSlot<TPropagatingStorage> Slot_;
NThreading::TForkAwareSpinLock Lock_;
static constexpr int MaxSwitchHandlerCount = 16;
std::array<TPropagatingStorageGlobalSwitchHandler, MaxSwitchHandlerCount> SwitchHandlers_;
std::atomic<int> SwitchHandlerCount_ = 0;
TPropagatingStorageManager() = default;
Y_DECLARE_SINGLETON_FRIEND()
};
TPropagatingStorage& GetCurrentPropagatingStorage()
{
return TPropagatingStorageManager::Get()->GetCurrentPropagatingStorage();
}
const TPropagatingStorage& GetPropagatingStorage(const TFls& fls)
{
return TPropagatingStorageManager::Get()->GetPropagatingStorage(fls);
}
void InstallGlobalPropagatingStorageSwitchHandler(TPropagatingStorageGlobalSwitchHandler handler)
{
TPropagatingStorageManager::Get()->InstallGlobalSwitchHandler(handler);
}
////////////////////////////////////////////////////////////////////////////////
TPropagatingStorageGuard::TPropagatingStorageGuard(TPropagatingStorage storage)
: OldStorage_(TPropagatingStorageManager::Get()->SwitchPropagatingStorage(std::move(storage)))
{ }
TPropagatingStorageGuard::~TPropagatingStorageGuard()
{
TPropagatingStorageManager::Get()->SwitchPropagatingStorage(std::move(OldStorage_));
}
const TPropagatingStorage& TPropagatingStorageGuard::GetOldStorage() const
{
return OldStorage_;
}
////////////////////////////////////////////////////////////////////////////////
TNullPropagatingStorageGuard::TNullPropagatingStorageGuard()
: TPropagatingStorageGuard(TPropagatingStorage())
{ }
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NConcurrency
|