aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/light_rw_lock/lightrwlock.cpp
diff options
context:
space:
mode:
authoragri <agri@yandex-team.ru>2022-02-10 16:48:12 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:12 +0300
commit2909866fbc652492b7d7cab3023cb19489dc4fd8 (patch)
treeb222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/threading/light_rw_lock/lightrwlock.cpp
parentd3530b2692e400bd4d29bd4f07cafaee139164e7 (diff)
downloadydb-2909866fbc652492b7d7cab3023cb19489dc4fd8.tar.gz
Restoring authorship annotation for <agri@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/threading/light_rw_lock/lightrwlock.cpp')
-rw-r--r--library/cpp/threading/light_rw_lock/lightrwlock.cpp224
1 files changed, 112 insertions, 112 deletions
diff --git a/library/cpp/threading/light_rw_lock/lightrwlock.cpp b/library/cpp/threading/light_rw_lock/lightrwlock.cpp
index 58456907d2..fbb63fd47f 100644
--- a/library/cpp/threading/light_rw_lock/lightrwlock.cpp
+++ b/library/cpp/threading/light_rw_lock/lightrwlock.cpp
@@ -1,113 +1,113 @@
-#include "lightrwlock.h"
-#include <util/system/spinlock.h>
-
-#if defined(_linux_)
-
-using namespace NS_LightRWLock;
-
-void TLightRWLock::WaitForUntrappedShared() {
- for (;;) {
- for (ui32 i = 0; i < SpinCount_; ++i) {
- SpinLockPause();
-
- if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0)
- return;
- }
-
- SequenceStore(UnshareFutex_, 1);
- if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0) {
- AtomicStore(UnshareFutex_, 0);
- return;
- }
- FutexWait(UnshareFutex_, 1);
- }
-}
-
-void TLightRWLock::WaitForExclusiveAndUntrappedShared() {
- for (;;) {
- for (ui32 i = 0; i < SpinCount_; ++i) {
- SpinLockPause();
-
- if (AtomicLoad(Counter_) >= 0)
- goto try_to_get_lock;
- if (AtomicLoad(TrappedFutex_) == 1)
- goto skip_store_trapped;
- }
-
- SequenceStore(TrappedFutex_, 1);
- skip_store_trapped:
-
- if (AtomicLoad(Counter_) < 0) {
- FutexWait(TrappedFutex_, 1);
- }
-
- try_to_get_lock:
- if (!AtomicSetBit(Counter_, 31))
- break;
- }
-
- for (ui32 j = 0;; ++j) {
- for (ui32 i = 0; i < SpinCount_; ++i) {
- if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0)
- return;
-
- SpinLockPause();
- }
-
- SequenceStore(UnshareFutex_, 1);
-
- if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0) {
- AtomicStore(UnshareFutex_, 0);
- return;
- }
-
- FutexWait(UnshareFutex_, 1);
- }
-}
-
-void TLightRWLock::WaitForUntrappedAndAcquireRead() {
- if (AtomicFetchAdd(Counter_, -1) < 0)
- goto skip_lock_try;
-
- for (;;) {
- again:
- if (Y_UNLIKELY(AtomicFetchAdd(Counter_, 1) >= 0)) {
- return;
- } else {
- if (AtomicFetchAdd(Counter_, -1) >= 0)
- goto again;
- }
-
- skip_lock_try:
+#include "lightrwlock.h"
+#include <util/system/spinlock.h>
+
+#if defined(_linux_)
+
+using namespace NS_LightRWLock;
+
+void TLightRWLock::WaitForUntrappedShared() {
+ for (;;) {
+ for (ui32 i = 0; i < SpinCount_; ++i) {
+ SpinLockPause();
+
+ if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0)
+ return;
+ }
+
+ SequenceStore(UnshareFutex_, 1);
+ if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0) {
+ AtomicStore(UnshareFutex_, 0);
+ return;
+ }
+ FutexWait(UnshareFutex_, 1);
+ }
+}
+
+void TLightRWLock::WaitForExclusiveAndUntrappedShared() {
+ for (;;) {
+ for (ui32 i = 0; i < SpinCount_; ++i) {
+ SpinLockPause();
+
+ if (AtomicLoad(Counter_) >= 0)
+ goto try_to_get_lock;
+ if (AtomicLoad(TrappedFutex_) == 1)
+ goto skip_store_trapped;
+ }
+
+ SequenceStore(TrappedFutex_, 1);
+ skip_store_trapped:
+
+ if (AtomicLoad(Counter_) < 0) {
+ FutexWait(TrappedFutex_, 1);
+ }
+
+ try_to_get_lock:
+ if (!AtomicSetBit(Counter_, 31))
+ break;
+ }
+
+ for (ui32 j = 0;; ++j) {
+ for (ui32 i = 0; i < SpinCount_; ++i) {
+ if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0)
+ return;
+
+ SpinLockPause();
+ }
+
+ SequenceStore(UnshareFutex_, 1);
+
+ if ((AtomicLoad(Counter_) & 0x7FFFFFFF) == 0) {
+ AtomicStore(UnshareFutex_, 0);
+ return;
+ }
+
+ FutexWait(UnshareFutex_, 1);
+ }
+}
+
+void TLightRWLock::WaitForUntrappedAndAcquireRead() {
+ if (AtomicFetchAdd(Counter_, -1) < 0)
+ goto skip_lock_try;
+
+ for (;;) {
+ again:
+ if (Y_UNLIKELY(AtomicFetchAdd(Counter_, 1) >= 0)) {
+ return;
+ } else {
+ if (AtomicFetchAdd(Counter_, -1) >= 0)
+ goto again;
+ }
+
+ skip_lock_try:
if (AtomicLoad(UnshareFutex_) && (AtomicLoad(Counter_) & 0x7FFFFFFF) == 0) {
- SequenceStore(UnshareFutex_, 0);
- FutexWake(UnshareFutex_, 1);
- }
-
- for (;;) {
- for (ui32 i = 0; i < SpinCount_; ++i) {
- SpinLockPause();
-
- if (AtomicLoad(Counter_) >= 0)
- goto again;
- if (AtomicLoad(TrappedFutex_) == 1)
- goto skip_store_trapped;
- }
-
- SequenceStore(TrappedFutex_, 1);
- skip_store_trapped:
-
- if (AtomicLoad(Counter_) < 0) {
- FutexWait(TrappedFutex_, 1);
- if (AtomicLoad(Counter_) < 0)
- goto again;
- } else if (AtomicLoad(TrappedFutex_)) {
- SequenceStore(TrappedFutex_, 0);
- FutexWake(TrappedFutex_, 0x7fffffff);
- }
- break;
- }
- }
-}
-
-#endif // _linux_
+ SequenceStore(UnshareFutex_, 0);
+ FutexWake(UnshareFutex_, 1);
+ }
+
+ for (;;) {
+ for (ui32 i = 0; i < SpinCount_; ++i) {
+ SpinLockPause();
+
+ if (AtomicLoad(Counter_) >= 0)
+ goto again;
+ if (AtomicLoad(TrappedFutex_) == 1)
+ goto skip_store_trapped;
+ }
+
+ SequenceStore(TrappedFutex_, 1);
+ skip_store_trapped:
+
+ if (AtomicLoad(Counter_) < 0) {
+ FutexWait(TrappedFutex_, 1);
+ if (AtomicLoad(Counter_) < 0)
+ goto again;
+ } else if (AtomicLoad(TrappedFutex_)) {
+ SequenceStore(TrappedFutex_, 0);
+ FutexWake(TrappedFutex_, 0x7fffffff);
+ }
+ break;
+ }
+ }
+}
+
+#endif // _linux_