aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/async_joiner.h
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2023-03-31 10:54:08 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2023-03-31 12:28:07 +0300
commitfc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch)
treec15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/async_joiner.h
parent8a749596d40e91c896a1907afcd108d9221fbde1 (diff)
downloadydb-fc1cffcfa7f0497a1f97b384a24bcbf23362f3be.tar.gz
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/async_joiner.h')
-rw-r--r--library/cpp/unified_agent_client/async_joiner.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/async_joiner.h b/library/cpp/unified_agent_client/async_joiner.h
new file mode 100644
index 0000000000..ce392ef7bc
--- /dev/null
+++ b/library/cpp/unified_agent_client/async_joiner.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <library/cpp/threading/future/future.h>
+
+namespace NUnifiedAgent {
+ class TAsyncJoiner {
+ public:
+ inline TAsyncJoiner()
+ : Promise(NThreading::NewPromise())
+ , Refs(1)
+ {
+ }
+
+ inline i64 Ref(i64 count = 1) noexcept {
+ const auto result = Refs.fetch_add(count);
+ Y_VERIFY(result >= 1, "already joined");
+ return result;
+ }
+
+ inline i64 UnRef() noexcept {
+ const auto prev = Refs.fetch_sub(1);
+ Y_VERIFY(prev >= 1);
+ if (prev == 1) {
+ auto p = Promise;
+ p.SetValue();
+ }
+ return prev;
+ }
+
+ inline NThreading::TFuture<void> Join() noexcept {
+ auto result = Promise;
+ UnRef();
+ return result;
+ }
+
+ private:
+ NThreading::TPromise<void> Promise;
+ std::atomic<i64> Refs;
+ };
+
+ using TAsyncJoinerToken = TIntrusivePtr<TAsyncJoiner>;
+}