115class SnapshotHandle
final {
117 SnapshotHandle(SnapshotHandle&& other)
noexcept
118 : record_(std::exchange(other.record_,
nullptr)), free_list_(std::exchange(other.free_list_,
nullptr)) {}
121 if (record_ !=
nullptr) {
122 UASSERT(free_list_ !=
nullptr);
123 record_->data.reset();
124 free_list_->list.Push(*record_);
129 template <
typename ,
typename Traits>
130 friend class Variable;
132 template <
typename ,
typename Traits>
133 friend class WritablePtr;
135 explicit SnapshotHandle(impl::SnapshotRecord<T>& record, impl::SnapshotRecordFreeList<T>& free_list)
noexcept
136 : record_(&record), free_list_(&free_list) {}
138 impl::SnapshotRecord<T>* record_;
139 impl::SnapshotRecordFreeList<T>* free_list_;
153class AsyncDeleter
final {
155 ~AsyncDeleter() { wait_token_storage_.WaitForAllTokens(); }
157 template <
typename T>
158 void Delete(SnapshotHandle<T>&& handle)
noexcept {
159 if constexpr (std::is_trivially_destructible_v<T> || std::is_same_v<T, std::string>) {
160 SyncDeleter{}.Delete(std::move(handle));
165 [token = wait_token_storage_.GetToken(), handle = std::move(handle)]()
mutable {}
177 utils::impl::WaitTokenStorage wait_token_storage_;
233class [[nodiscard]] ReadablePtr
final {
235 explicit ReadablePtr(
const Variable<T, RcuTraits>& ptr) {
236 auto* record = ptr.current_.load();
241 lock_ = record->indicator.GetLock();
263 concurrent::impl::AsymmetricThreadFenceLight();
267 auto* new_current = ptr.current_.load(std::memory_order_seq_cst);
268 if (new_current == record) {
273 record = new_current;
276 ptr_ = &*record->data;
279 ReadablePtr(ReadablePtr&& other)
noexcept =
default;
280 ReadablePtr& operator=(ReadablePtr&& other)
noexcept =
default;
281 ReadablePtr(
const ReadablePtr& other) =
default;
282 ReadablePtr& operator=(
const ReadablePtr& other) =
default;
283 ~ReadablePtr() =
default;
285 const T* Get()
const& USERVER_IMPL_LIFETIME_BOUND {
290 const T* Get() && {
return GetOnRvalue(); }
292 const T* operator->()
const& USERVER_IMPL_LIFETIME_BOUND {
return Get(); }
293 const T* operator->() && {
return GetOnRvalue(); }
295 const T& operator*()
const& USERVER_IMPL_LIFETIME_BOUND {
return *Get(); }
296 const T& operator*() && {
return *GetOnRvalue(); }
299 const T* GetOnRvalue() {
300 static_assert(!
sizeof(T),
"Don't use temporary ReadablePtr, store it to a variable");
305 concurrent::impl::StripedReadIndicatorLock lock_;
319class [[nodiscard]] WritablePtr
final {
323 explicit WritablePtr(Variable<T, RcuTraits>& var)
326 record_(&var.EmplaceSnapshot(*var.current_.load()->data))
330 template <
typename... Args>
331 WritablePtr(Variable<T, RcuTraits>& var, std::in_place_t, Args&&... initial_value_args)
334 record_(&var.EmplaceSnapshot(std::forward<Args>(initial_value_args)...))
338 WritablePtr(WritablePtr&& other)
noexcept
339 : var_(other.var_), lock_(std::move(other.lock_)), record_(std::exchange(other.record_,
nullptr)) {}
343 var_.DeleteSnapshot(*record_);
352 var_.DoAssign(*std::exchange(record_,
nullptr), lock_);
356 T* Get() & USERVER_IMPL_LIFETIME_BOUND {
358 return &*record_->data;
361 T* Get() && {
return GetOnRvalue(); }
363 T* operator->() & USERVER_IMPL_LIFETIME_BOUND {
return Get(); }
364 T* operator->() && {
return GetOnRvalue(); }
366 T& operator*() & USERVER_IMPL_LIFETIME_BOUND {
return *Get(); }
367 T& operator*() && {
return *GetOnRvalue(); }
370 [[noreturn]]
static T* GetOnRvalue() {
371 static_assert(!
sizeof(T),
"Don't use temporary WritablePtr, store it to a variable");
375 Variable<T, RcuTraits>& var_;
376 std::unique_lock<
typename RcuTraits::MutexType> lock_;
377 impl::SnapshotRecord<T>* record_;
408class Variable
final {
411 "RcuTraits should publicly inherit from rcu::DefaultRcuTraits"
415 using MutexType =
typename RcuTraits::MutexType;
416 using DeleterType =
typename RcuTraits::DeleterType;
421 template <
typename... Args>
424 : current_(&EmplaceSnapshot(std::forward<Args>(initial_value_args)...))
427 Variable(
const Variable&) =
delete;
428 Variable(Variable&&) =
delete;
429 Variable& operator=(
const Variable&) =
delete;
430 Variable& operator=(Variable&&) =
delete;
434 auto* record = current_.load();
435 UASSERT_MSG(record->indicator.IsFree(),
"RCU variable is destroyed while being used");
439 retired_list_.RemoveAndDisposeIf(
440 [](impl::SnapshotRecord<T>&) {
return true; },
441 [](impl::SnapshotRecord<T>& record) {
442 UASSERT_MSG(record.indicator.IsFree(),
"RCU variable is destroyed while being used");
449 ReadablePtr<T, RcuTraits>
Read()
const {
return ReadablePtr<T, RcuTraits>(*
this); }
460 WritablePtr<T, RcuTraits>
StartWrite() {
return WritablePtr<T, RcuTraits>(*
this); }
464 template <
typename... Args>
466 return WritablePtr<T, RcuTraits>(*
this, std::in_place, std::forward<Args>(args)...);
470 void Assign(T new_value) { WritablePtr<T, RcuTraits>(*
this, std::in_place, std::move(new_value)).Commit(); }
473 template <
typename... Args>
475 WritablePtr<T, RcuTraits>(*
this, std::in_place, std::forward<Args>(args)...).Commit();
479 std::unique_lock lock(mutex_, std::try_to_lock);
480 if (!lock.owns_lock()) {
485 ScanRetiredList(lock);
489 friend class ReadablePtr<T, RcuTraits>;
490 friend class WritablePtr<T, RcuTraits>;
492 void DoAssign(impl::SnapshotRecord<T>& new_snapshot, std::unique_lock<MutexType>& lock) {
496 auto*
const old_snapshot = current_.load();
497 current_.store(&new_snapshot, std::memory_order_seq_cst);
500 retired_list_.Push(*old_snapshot);
501 ScanRetiredList(lock);
504 template <
typename... Args>
505 [[nodiscard]] impl::SnapshotRecord<T>& EmplaceSnapshot(Args&&... args) {
506 auto*
const free_list_record = free_list_.list.TryPop();
507 auto& record = free_list_record ? *free_list_record : *
new impl::SnapshotRecord<T>{};
511 record.data.emplace(std::forward<Args>(args)...);
513 free_list_.list.Push(record);
520 void ScanRetiredList(std::unique_lock<MutexType>& lock)
noexcept {
522 if (retired_list_.IsEmpty()) {
526 concurrent::impl::AsymmetricThreadFenceHeavy();
528 retired_list_.RemoveAndDisposeIf(
529 [](impl::SnapshotRecord<T>& record) {
return record.indicator.IsFree(); },
530 [&](impl::SnapshotRecord<T>& record) { DeleteSnapshot(record); }
534 void DeleteSnapshot(impl::SnapshotRecord<T>& record)
noexcept {
536 noexcept(deleter_.Delete(SnapshotHandle<T>{record, free_list_})),
537 "DeleterType::Delete must be noexcept"
539 deleter_.Delete(SnapshotHandle<T>{record, free_list_});
544 impl::SnapshotRecordFreeList<T> free_list_;
545 impl::SnapshotRecordRetiredList<T> retired_list_;
548 DeleterType deleter_{};
551 std::atomic<impl::SnapshotRecord<T>*> current_;