summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/threading/async_queue.h
diff options
context:
space:
mode:
authorvvvv <[email protected]>2024-11-01 15:41:40 +0300
committervvvv <[email protected]>2024-11-01 15:55:52 +0300
commit3325f745e67f7f442790822b5c9c5e9996708be7 (patch)
treef7318d68bbe8990092715436444b05297ce35777 /yql/essentials/utils/threading/async_queue.h
parent6dce3f1c71786f2694b73b1a5155efc58f4557dd (diff)
Moved yql/utils YQL-19206
Также была выделена жирная зависимость из yql/utils в yql/utils/network, в результате library/cpp/getopt была добавлена явно в те проекты, которые ее ранее наследовали, а не указывали явно commit_hash:36aa4c41f807b4cbbf70a3ed7ac0a1a5079bb75d
Diffstat (limited to 'yql/essentials/utils/threading/async_queue.h')
-rw-r--r--yql/essentials/utils/threading/async_queue.h51
1 files changed, 51 insertions, 0 deletions
diff --git a/yql/essentials/utils/threading/async_queue.h b/yql/essentials/utils/threading/async_queue.h
new file mode 100644
index 00000000000..3dd75b9e08a
--- /dev/null
+++ b/yql/essentials/utils/threading/async_queue.h
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <library/cpp/threading/future/future.h>
+#include <library/cpp/threading/future/async.h>
+
+#include <util/thread/pool.h>
+#include <util/generic/ptr.h>
+#include <util/generic/function.h>
+#include <util/system/guard.h>
+#include <util/system/rwlock.h>
+
+#include <exception>
+
+namespace NYql {
+
+class TAsyncQueue: public TThrRefBase {
+public:
+ using TPtr = TIntrusivePtr<TAsyncQueue>;
+
+ static TPtr Make(size_t numThreads, const TString& poolName);
+
+ void Stop() {
+ auto guard = TWriteGuard(Lock_);
+ if (MtpQueue_) {
+ MtpQueue_->Stop();
+ MtpQueue_.Destroy();
+ }
+ }
+
+ template <typename TCallable>
+ [[nodiscard]]
+ ::NThreading::TFuture<::NThreading::TFutureType<::TFunctionResult<TCallable>>> Async(TCallable&& func) {
+ {
+ auto guard = TReadGuard(Lock_);
+ if (MtpQueue_) {
+ return ::NThreading::Async(std::move(func), *MtpQueue_);
+ }
+ }
+
+ return ::NThreading::MakeErrorFuture<::NThreading::TFutureType<::TFunctionResult<TCallable>>>(std::make_exception_ptr(yexception() << "Thread pool is already stopped"));
+ }
+
+private:
+ TAsyncQueue(size_t numThreads, const TString& poolName);
+
+private:
+ TRWMutex Lock_;
+ THolder<IThreadPool> MtpQueue_;
+};
+
+} // NYql