From 1110808a9d39d4b808aef724c861a2e1a38d2a69 Mon Sep 17 00:00:00 2001
From: Devtools Arcadia <arcadia-devtools@yandex-team.ru>
Date: Mon, 7 Feb 2022 18:08:42 +0300
Subject: intermediate changes ref:cde9a383711a11544ce7e107a78147fb96cc4029

---
 library/cpp/messagebus/cc_semaphore.h | 36 +++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 library/cpp/messagebus/cc_semaphore.h

(limited to 'library/cpp/messagebus/cc_semaphore.h')

diff --git a/library/cpp/messagebus/cc_semaphore.h b/library/cpp/messagebus/cc_semaphore.h
new file mode 100644
index 0000000000..0df8a3d664
--- /dev/null
+++ b/library/cpp/messagebus/cc_semaphore.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "latch.h"
+
+template <typename TThis>
+class TComplexConditionSemaphore {
+private:
+    TLatch Latch;
+
+public:
+    void Updated() {
+        if (GetThis()->TryWait()) {
+            Latch.Unlock();
+        }
+    }
+
+    void Wait() {
+        while (!GetThis()->TryWait()) {
+            Latch.Lock();
+            if (GetThis()->TryWait()) {
+                Latch.Unlock();
+                return;
+            }
+            Latch.Wait();
+        }
+    }
+
+    bool IsLocked() {
+        return Latch.IsLocked();
+    }
+
+private:
+    TThis* GetThis() {
+        return static_cast<TThis*>(this);
+    }
+};
-- 
cgit v1.2.3