diff options
author | Maxim Yurchuk <maxim-yurchuk@ydb.tech> | 2024-11-20 17:37:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-20 17:37:57 +0000 |
commit | f76323e9b295c15751e51e3443aa47a36bee8023 (patch) | |
tree | 4113c8cad473a33e0f746966e0cf087252fa1d7a /library/cpp/lwtrace/rwspinlock.h | |
parent | 753ecb8d410a4cb459c26f3a0082fb2d1724fe63 (diff) | |
parent | a7b9a6afea2a9d7a7bfac4c5eb4c1a8e60adb9e6 (diff) | |
download | ydb-f76323e9b295c15751e51e3443aa47a36bee8023.tar.gz |
Merge pull request #11788 from ydb-platform/mergelibs-241120-1113
Library import 241120-1113
Diffstat (limited to 'library/cpp/lwtrace/rwspinlock.h')
-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); } }; |