diff options
Diffstat (limited to 'src/zencore/include')
| -rw-r--r-- | src/zencore/include/zencore/workthreadpool.h | 46 |
1 files changed, 37 insertions, 9 deletions
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 <zencore/zencore.h> -#include <zencore/blockingqueue.h> #include <zencore/refcount.h> #include <exception> #include <functional> -#include <system_error> -#include <thread> -#include <vector> +#include <future> 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<IWork> Work); void ScheduleWork(std::function<void()>&& Work); - [[nodiscard]] size_t PendingWork() const; + template<typename Func> + auto EnqueueTask(std::packaged_task<Func> Task); + + [[nodiscard]] size_t PendingWorkItemCount() const; private: - void WorkerThreadFunction(); + struct Impl; + struct ThreadStartInfo; - std::vector<std::thread> m_WorkerThreads; - BlockingQueue<Ref<IWork>> m_WorkQueue; + std::unique_ptr<Impl> m_Impl; }; +////////////////////////////////////////////////////////////////////////// + +template<typename Func> +auto +WorkerThreadPool::EnqueueTask(std::packaged_task<Func> Task) +{ + struct FutureWork : IWork + { + FutureWork(std::packaged_task<Func> Task) : m_Task{std::move(Task)} {} + virtual void Execute() override { m_Task(); } + + std::packaged_task<Func> m_Task; + }; + + Ref<FutureWork> 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 |