diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | 1f553f46fb4f3c5eec631352cdd900a0709016af (patch) | |
tree | a231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/messagebus/latch.h | |
parent | c4de7efdedc25b49cbea74bd589eecb61b55b60a (diff) | |
download | ydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/messagebus/latch.h')
-rw-r--r-- | library/cpp/messagebus/latch.h | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/library/cpp/messagebus/latch.h b/library/cpp/messagebus/latch.h index 373f4c0e13..d40aef2719 100644 --- a/library/cpp/messagebus/latch.h +++ b/library/cpp/messagebus/latch.h @@ -1,53 +1,53 @@ -#pragma once - +#pragma once + #include <util/system/condvar.h> -#include <util/system/mutex.h> - -class TLatch { -private: - // 0 for unlocked, 1 for locked - TAtomic Locked; - TMutex Mutex; - TCondVar CondVar; - -public: +#include <util/system/mutex.h> + +class TLatch { +private: + // 0 for unlocked, 1 for locked + TAtomic Locked; + TMutex Mutex; + TCondVar CondVar; + +public: TLatch() : Locked(0) { } - - void Wait() { - // optimistic path - if (AtomicGet(Locked) == 0) { - return; - } - - TGuard<TMutex> guard(Mutex); + + void Wait() { + // optimistic path + if (AtomicGet(Locked) == 0) { + return; + } + + TGuard<TMutex> guard(Mutex); while (AtomicGet(Locked) == 1) { - CondVar.WaitI(Mutex); - } - } - - bool TryWait() { - return AtomicGet(Locked) == 0; - } - - void Unlock() { - // optimistic path - if (AtomicGet(Locked) == 0) { - return; - } - - TGuard<TMutex> guard(Mutex); + CondVar.WaitI(Mutex); + } + } + + bool TryWait() { + return AtomicGet(Locked) == 0; + } + + void Unlock() { + // optimistic path + if (AtomicGet(Locked) == 0) { + return; + } + + TGuard<TMutex> guard(Mutex); AtomicSet(Locked, 0); - CondVar.BroadCast(); - } - - void Lock() { - AtomicSet(Locked, 1); - } - - bool IsLocked() { - return AtomicGet(Locked); - } -}; + CondVar.BroadCast(); + } + + void Lock() { + AtomicSet(Locked, 1); + } + + bool IsLocked() { + return AtomicGet(Locked); + } +}; |