From 8667509907b8829b5148ee856dce675001051b01 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Tue, 22 Mar 2022 13:50:08 +0100 Subject: move workthreadpool to zencore (#63) --- zenhttp/httpsys.h | 2 +- zenhttp/workthreadpool.cpp | 77 ---------------------------------------------- zenhttp/workthreadpool.h | 46 --------------------------- 3 files changed, 1 insertion(+), 124 deletions(-) delete mode 100644 zenhttp/workthreadpool.cpp delete mode 100644 zenhttp/workthreadpool.h (limited to 'zenhttp') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 0453b8740..d6bd34890 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -15,8 +15,8 @@ #if ZEN_WITH_HTTPSYS # define _WINSOCKAPI_ # include +# include # include "iothreadpool.h" -# include "workthreadpool.h" # include diff --git a/zenhttp/workthreadpool.cpp b/zenhttp/workthreadpool.cpp deleted file mode 100644 index 41eaaae94..000000000 --- a/zenhttp/workthreadpool.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#include "workthreadpool.h" - -#include - -namespace zen { - -namespace detail { - struct LambdaWork : IWork - { - LambdaWork(auto Work) : WorkFunction(Work) {} - virtual void Execute() override { WorkFunction(); } - - std::function WorkFunction; - }; -} // namespace detail - -WorkerThreadPool::WorkerThreadPool(int InThreadCount) -{ - for (int i = 0; i < InThreadCount; ++i) - { - m_WorkerThreads.emplace_back(&WorkerThreadPool::WorkerThreadFunction, this); - } -} - -WorkerThreadPool::~WorkerThreadPool() -{ - m_WorkQueue.CompleteAdding(); - - for (std::thread& Thread : m_WorkerThreads) - { - Thread.join(); - } - - m_WorkerThreads.clear(); -} - -void -WorkerThreadPool::ScheduleWork(Ref Work) -{ - m_WorkQueue.Enqueue(std::move(Work)); -} - -void -WorkerThreadPool::ScheduleWork(std::function&& Work) -{ - m_WorkQueue.Enqueue(new detail::LambdaWork(Work)); -} - -void -WorkerThreadPool::WorkerThreadFunction() -{ - do - { - Ref Work; - if (m_WorkQueue.WaitAndDequeue(Work)) - { - try - { - Work->Execute(); - } - catch (std::exception& e) - { - Work->m_Exception = std::current_exception(); - - ZEN_WARN("Caught exception in worker thread: {}", e.what()); - } - } - else - { - return; - } - } while (true); -} - -} // namespace zen \ No newline at end of file diff --git a/zenhttp/workthreadpool.h b/zenhttp/workthreadpool.h deleted file mode 100644 index 834339d50..000000000 --- a/zenhttp/workthreadpool.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - -#pragma once - -#include - -#include -#include - -#include -#include -#include -#include -#include - -namespace zen { - -struct IWork : public RefCounted -{ - virtual void Execute() = 0; - - inline std::exception_ptr GetException() { return m_Exception; } - -private: - std::exception_ptr m_Exception; - - friend class WorkerThreadPool; -}; - -class WorkerThreadPool -{ -public: - WorkerThreadPool(int InThreadCount); - ~WorkerThreadPool(); - - void ScheduleWork(Ref Work); - void ScheduleWork(std::function&& Work); - - void WorkerThreadFunction(); - -private: - std::vector m_WorkerThreads; - BlockingQueue> m_WorkQueue; -}; - -} // namespace zen -- cgit v1.2.3