summaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/async_semaphore.h
diff options
context:
space:
mode:
authortldr <[email protected]>2022-10-05 22:50:54 +0300
committertldr <[email protected]>2022-10-05 22:50:54 +0300
commit57087fd2e94af7191eef09606ca6a395ab0fe129 (patch)
tree7c06867a9bdafb3127014d2aa1625a43857fcc0f /library/cpp/threading/future/async_semaphore.h
parent195aa2d16c6b08a6c3e72b9a3ce33b97f9104ba9 (diff)
[vcs][library][ydb] move TAsyncSemaphore from ydb/library to library/cpp/threading
Потребовалось для
Diffstat (limited to 'library/cpp/threading/future/async_semaphore.h')
-rw-r--r--library/cpp/threading/future/async_semaphore.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/library/cpp/threading/future/async_semaphore.h b/library/cpp/threading/future/async_semaphore.h
new file mode 100644
index 00000000000..fa4cabad3cd
--- /dev/null
+++ b/library/cpp/threading/future/async_semaphore.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include <library/cpp/threading/future/future.h>
+
+#include <util/system/spinlock.h>
+#include <util/generic/ptr.h>
+
+#include <list>
+#include <functional>
+
+namespace NThreading {
+
+class TAsyncSemaphore: public TThrRefBase {
+public:
+ using TPtr = TIntrusivePtr<TAsyncSemaphore>;
+
+ class TAutoRelease {
+ public:
+ TAutoRelease(TAsyncSemaphore::TPtr sem)
+ : Sem(std::move(sem))
+ {
+ }
+ TAutoRelease(TAutoRelease&& other)
+ : Sem(std::move(other.Sem))
+ {
+ }
+ ~TAutoRelease();
+
+ std::function<void (const TFuture<void>&)> DeferRelease();
+
+ private:
+ TAsyncSemaphore::TPtr Sem;
+ };
+
+ static TPtr Make(size_t count);
+
+ TFuture<TPtr> AcquireAsync();
+ void Release();
+ void Cancel();
+
+ TAutoRelease MakeAutoRelease() {
+ return {this};
+ }
+
+private:
+ TAsyncSemaphore(size_t count);
+
+private:
+ size_t Count_;
+ bool Cancelled_ = false;
+ TAdaptiveLock Lock_;
+ std::list<TPromise<TPtr>> Promises_;
+};
+
+} // namespace NThreading