aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/rwspinlock.h
diff options
context:
space:
mode:
authorAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
committerAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
commit31773f157bf8164364649b5f470f52dece0a4317 (patch)
tree33d0f7eef45303ab68cf08ab381ce5e5e36c5240 /library/cpp/lwtrace/rwspinlock.h
parent2c7938962d8689e175574fc1e817c05049f27905 (diff)
parenteff600952d5dfe17942f38f510a8ac2b203bb3a5 (diff)
downloadydb-31773f157bf8164364649b5f470f52dece0a4317.tar.gz
Merge branch 'rightlib' into mergelibs-241120-1113
Diffstat (limited to 'library/cpp/lwtrace/rwspinlock.h')
-rw-r--r--library/cpp/lwtrace/rwspinlock.h24
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);
}
};