aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/include
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-05-15 17:55:52 +0200
committerGitHub <[email protected]>2023-05-15 17:55:52 +0200
commit14f4baabfa56eedb7b71235b951e993da4f641ef (patch)
tree86c08838a0179ae15910b791a1412c448340020d /src/zencore/include
parentv0.2.11 (diff)
downloadzen-14f4baabfa56eedb7b71235b951e993da4f641ef.tar.xz
zen-14f4baabfa56eedb7b71235b951e993da4f641ef.zip
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
Diffstat (limited to 'src/zencore/include')
-rw-r--r--src/zencore/include/zencore/workthreadpool.h46
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