From 14f4baabfa56eedb7b71235b951e993da4f641ef Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 15 May 2023 17:55:52 +0200 Subject: all threads should be named (#304) * added WorkerThreadPool naming, packaged_task support * name the http.sys thread pool service threads * added http.sys I/O threadpool naming * upstream cache I/O thread naming --- src/zencore/include/zencore/workthreadpool.h | 46 ++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'src/zencore/include') diff --git a/src/zencore/include/zencore/workthreadpool.h b/src/zencore/include/zencore/workthreadpool.h index 0ddc65298..c10c6ed12 100644 --- a/src/zencore/include/zencore/workthreadpool.h +++ b/src/zencore/include/zencore/workthreadpool.h @@ -4,17 +4,16 @@ #include -#include #include #include #include -#include -#include -#include +#include namespace zen { +////////////////////////////////////////////////////////////////////////// + struct IWork : public RefCounted { virtual void Execute() = 0; @@ -27,22 +26,51 @@ private: friend class WorkerThreadPool; }; +////////////////////////////////////////////////////////////////////////// + class WorkerThreadPool { public: - WorkerThreadPool(int InThreadCount); + explicit WorkerThreadPool(int InThreadCount); + WorkerThreadPool(int InThreadCount, std::string_view WorkerThreadBaseName); ~WorkerThreadPool(); void ScheduleWork(Ref Work); void ScheduleWork(std::function&& Work); - [[nodiscard]] size_t PendingWork() const; + template + auto EnqueueTask(std::packaged_task Task); + + [[nodiscard]] size_t PendingWorkItemCount() const; private: - void WorkerThreadFunction(); + struct Impl; + struct ThreadStartInfo; - std::vector m_WorkerThreads; - BlockingQueue> m_WorkQueue; + std::unique_ptr m_Impl; }; +////////////////////////////////////////////////////////////////////////// + +template +auto +WorkerThreadPool::EnqueueTask(std::packaged_task Task) +{ + struct FutureWork : IWork + { + FutureWork(std::packaged_task Task) : m_Task{std::move(Task)} {} + virtual void Execute() override { m_Task(); } + + std::packaged_task m_Task; + }; + + Ref Work{new FutureWork(std::move(Task))}; + + auto Future = Work->m_Task.get_future(); + ScheduleWork(std::move(Work)); + return Future; +} + +void workthreadpool_forcelink(); // internal + } // namespace zen -- cgit v1.2.3