diff options
author | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
---|---|---|
committer | hor911 <hor911@ydb.tech> | 2023-02-09 12:40:11 +0300 |
commit | 24689527cd888aa8a640ecb5077e656b3520d373 (patch) | |
tree | a613ff4cd9567b7113e8376a17f8b85897a42790 /library/cpp/unified_agent_client/async_joiner.h | |
parent | 8642d3642932f03663ba7d2d9670707c192207fd (diff) | |
download | ydb-24689527cd888aa8a640ecb5077e656b3520d373.tar.gz |
Log backend move
Diffstat (limited to 'library/cpp/unified_agent_client/async_joiner.h')
-rw-r--r-- | library/cpp/unified_agent_client/async_joiner.h | 42 |
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>; +} |