aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/guard.h
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:15 +0300
commit72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch)
treeda2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/guard.h
parent778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff)
downloadydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/guard.h')
-rw-r--r--util/system/guard.h230
1 files changed, 115 insertions, 115 deletions
diff --git a/util/system/guard.h b/util/system/guard.h
index efc091d5f8..d47a1a90eb 100644
--- a/util/system/guard.h
+++ b/util/system/guard.h
@@ -1,28 +1,28 @@
#pragma once
#include <util/generic/noncopyable.h>
-
-template <class T>
-struct TCommonLockOps {
+
+template <class T>
+struct TCommonLockOps {
static inline void Acquire(T* t) noexcept {
- t->Acquire();
- }
-
+ t->Acquire();
+ }
+
static inline void Release(T* t) noexcept {
- t->Release();
- }
-};
-
+ t->Release();
+ }
+};
+
template <class T>
-struct TTryLockOps: public TCommonLockOps<T> {
+struct TTryLockOps: public TCommonLockOps<T> {
static inline bool TryAcquire(T* t) noexcept {
return t->TryAcquire();
}
};
-//must be used with great care
+//must be used with great care
template <class TOps>
-struct TInverseLockOps: public TOps {
+struct TInverseLockOps: public TOps {
template <class T>
static inline void Acquire(T* t) noexcept {
TOps::Release(t);
@@ -34,108 +34,108 @@ struct TInverseLockOps: public TOps {
}
};
-template <class T, class TOps = TCommonLockOps<T>>
-class TGuard: public TNonCopyable {
-public:
+template <class T, class TOps = TCommonLockOps<T>>
+class TGuard: public TNonCopyable {
+public:
inline TGuard(const T& t) noexcept {
- Init(&t);
- }
-
+ Init(&t);
+ }
+
inline TGuard(const T* t) noexcept {
- Init(t);
- }
-
+ Init(t);
+ }
+
inline TGuard(TGuard&& g) noexcept
- : T_(g.T_)
- {
+ : T_(g.T_)
+ {
g.T_ = nullptr;
- }
-
+ }
+
inline ~TGuard() {
- Release();
- }
-
+ Release();
+ }
+
inline void Release() noexcept {
- if (WasAcquired()) {
- TOps::Release(T_);
+ if (WasAcquired()) {
+ TOps::Release(T_);
T_ = nullptr;
- }
- }
-
+ }
+ }
+
explicit inline operator bool() const noexcept {
- return WasAcquired();
- }
-
+ return WasAcquired();
+ }
+
inline bool WasAcquired() const noexcept {
return T_ != nullptr;
- }
-
+ }
+
inline T* GetMutex() const noexcept {
- return T_;
- }
-
-private:
+ return T_;
+ }
+
+private:
inline void Init(const T* t) noexcept {
- T_ = const_cast<T*>(t);
- TOps::Acquire(T_);
- }
-
-private:
- T* T_;
-};
-
-/*
- * {
- * auto guard = Guard(Lock_);
- * some code under guard
- * }
- */
-template <class T>
-static inline TGuard<T> Guard(const T& t) {
- return {&t};
-}
-
-/*
- * with_lock (Lock_) {
- * some code under guard
- * }
- */
-#define with_lock(X) \
- if (auto Y_GENERATE_UNIQUE_ID(__guard) = ::Guard(X); false) { \
- } else
-
-/*
- * auto guard = Guard(Lock_);
- * ... some code under lock
- * {
- * auto unguard = Unguard(guard);
- * ... some code not under lock
- * }
- * ... some code under lock
- */
-template <class T, class TOps = TCommonLockOps<T>>
-using TInverseGuard = TGuard<T, TInverseLockOps<TOps>>;
-
-template <class T, class TOps>
-static inline TInverseGuard<T, TOps> Unguard(const TGuard<T, TOps>& guard) {
- return {guard.GetMutex()};
-}
-
-template <class T>
-static inline TInverseGuard<T> Unguard(const T& mutex) {
- return {&mutex};
-}
-
-template <class T, class TOps = TTryLockOps<T>>
+ T_ = const_cast<T*>(t);
+ TOps::Acquire(T_);
+ }
+
+private:
+ T* T_;
+};
+
+/*
+ * {
+ * auto guard = Guard(Lock_);
+ * some code under guard
+ * }
+ */
+template <class T>
+static inline TGuard<T> Guard(const T& t) {
+ return {&t};
+}
+
+/*
+ * with_lock (Lock_) {
+ * some code under guard
+ * }
+ */
+#define with_lock(X) \
+ if (auto Y_GENERATE_UNIQUE_ID(__guard) = ::Guard(X); false) { \
+ } else
+
+/*
+ * auto guard = Guard(Lock_);
+ * ... some code under lock
+ * {
+ * auto unguard = Unguard(guard);
+ * ... some code not under lock
+ * }
+ * ... some code under lock
+ */
+template <class T, class TOps = TCommonLockOps<T>>
+using TInverseGuard = TGuard<T, TInverseLockOps<TOps>>;
+
+template <class T, class TOps>
+static inline TInverseGuard<T, TOps> Unguard(const TGuard<T, TOps>& guard) {
+ return {guard.GetMutex()};
+}
+
+template <class T>
+static inline TInverseGuard<T> Unguard(const T& mutex) {
+ return {&mutex};
+}
+
+template <class T, class TOps = TTryLockOps<T>>
class TTryGuard: public TNonCopyable {
-public:
+public:
inline TTryGuard(const T& t) noexcept {
- Init(&t);
- }
+ Init(&t);
+ }
inline TTryGuard(const T* t) noexcept {
- Init(t);
- }
+ Init(t);
+ }
inline TTryGuard(TTryGuard&& g) noexcept
: T_(g.T_)
@@ -144,33 +144,33 @@ public:
}
inline ~TTryGuard() {
- Release();
- }
+ Release();
+ }
inline void Release() noexcept {
- if (WasAcquired()) {
- TOps::Release(T_);
+ if (WasAcquired()) {
+ TOps::Release(T_);
T_ = nullptr;
}
- }
+ }
inline bool WasAcquired() const noexcept {
return T_ != nullptr;
- }
+ }
explicit inline operator bool() const noexcept {
- return WasAcquired();
- }
-
-private:
+ return WasAcquired();
+ }
+
+private:
inline void Init(const T* t) noexcept {
T_ = nullptr;
- T* tMutable = const_cast<T*>(t);
- if (TOps::TryAcquire(tMutable)) {
- T_ = tMutable;
+ T* tMutable = const_cast<T*>(t);
+ if (TOps::TryAcquire(tMutable)) {
+ T_ = tMutable;
}
- }
+ }
-private:
- T* T_;
+private:
+ T* T_;
};