diff options
author | eivanov89 <eivanov89@yandex-team.com> | 2024-11-19 10:49:39 +0300 |
---|---|---|
committer | eivanov89 <eivanov89@yandex-team.com> | 2024-11-19 11:00:38 +0300 |
commit | 907667c6ffd6222845fe8e18fd548e508250b5d0 (patch) | |
tree | b9f330f164d1e38eceb989f731a3bb3cd200cb77 /library/cpp/lwtrace | |
parent | 81f817f446c49b24a1ddb183dbd51b110993f6ce (diff) | |
download | ydb-907667c6ffd6222845fe8e18fd548e508250b5d0.tar.gz |
use C++ atomic with proper default constructor
commit_hash:1c25da3ce2125ca4d8d1e3e026ef0d2a6d0aa355
Diffstat (limited to 'library/cpp/lwtrace')
-rw-r--r-- | library/cpp/lwtrace/rwspinlock.h | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/library/cpp/lwtrace/rwspinlock.h b/library/cpp/lwtrace/rwspinlock.h index 5e4608e068..1effd5fe0e 100644 --- a/library/cpp/lwtrace/rwspinlock.h +++ b/library/cpp/lwtrace/rwspinlock.h @@ -1,6 +1,6 @@ #pragma once -#include <library/cpp/deprecated/atomic/atomic.h> +#include <atomic> #include <util/system/spinlock.h> @@ -27,16 +27,16 @@ // * writer can release lock (State = 0: -> READING) struct TRWSpinLock { - TAtomic State; // must be initialized by 'TRWSpinLock myLock = {0};' construction + std::atomic_signed_lock_free State; void Init() noexcept { - State = 0; + State.store(0, std::memory_order_relaxed); } void AcquireRead() noexcept { while (true) { - TAtomic a = AtomicGet(State); - if ((a & 1) == 0 && AtomicCas(&State, a + 2, a)) { + std::atomic_signed_lock_free::value_type a = State.load(std::memory_order_acquire); + if ((a & 1) == 0 && State.compare_exchange_strong(a, a + 2, std::memory_order_acquire)) { break; } SpinLockPause(); @@ -44,25 +44,29 @@ struct TRWSpinLock { } void ReleaseRead() noexcept { - AtomicAdd(State, -2); + State.fetch_add(-2, std::memory_order_release); } void AcquireWrite() noexcept { while (true) { - TAtomic a = AtomicGet(State); - if ((a & 1) == 0 && AtomicCas(&State, a + 1, a)) { + std::atomic_signed_lock_free::value_type a = State.load(std::memory_order_acquire); + if ((a & 1) == 0 && State.compare_exchange_strong(a, a + 1, std::memory_order_acquire)) { break; } SpinLockPause(); } - while (!AtomicCas(&State, TAtomicBase(-1), 1)) { + while (true) { + std::atomic_signed_lock_free::value_type a = 1; + if (State.compare_exchange_strong(a, -1, std::memory_order_acquire)) { + break; + } SpinLockPause(); } } void ReleaseWrite() noexcept { - AtomicSet(State, 0); + State.store(0, std::memory_order_release); } }; |