summaryrefslogtreecommitdiff
path: root/lib/worker.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 18:03:34 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 18:03:34 +0100
commit5776a36b454fac04617313da011d7aa2b0e834d3 (patch)
tree1eb96d07e9a17a51e5763f397fc003f762cd2e75 /lib/worker.h
parentMerge branch 'model-factory-textures' (diff)
parentAdd an asset template and use it to define all the foliage assets in the plan... (diff)
downloadilt-5776a36b454fac04617313da011d7aa2b0e834d3.tar.bz2
ilt-5776a36b454fac04617313da011d7aa2b0e834d3.tar.xz
ilt-5776a36b454fac04617313da011d7aa2b0e834d3.zip
Merge branch 'assimp'
Diffstat (limited to 'lib/worker.h')
-rw-r--r--lib/worker.h132
1 files changed, 97 insertions, 35 deletions
diff --git a/lib/worker.h b/lib/worker.h
index 7cd06f9..d9a5a6f 100644
--- a/lib/worker.h
+++ b/lib/worker.h
@@ -1,60 +1,122 @@
#pragma once
+#include <deque>
+#include <functional>
+#include <future>
+#include <memory>
+#include <mutex>
+#include <semaphore>
+#include <special_members.hpp>
+#include <thread>
#include <utility>
-class Work;
-
-#if __cpp_lib_semaphore
-# include <deque>
-# include <memory>
-# include <mutex>
-# include <semaphore>
-# include <special_members.hpp>
-# include <thread>
-# include <vector>
+#include <vector>
class Worker {
public:
+ class WorkItem {
+ protected:
+ WorkItem(Worker * worker) : worker {worker} { }
+ virtual ~WorkItem() = default;
+ NO_MOVE(WorkItem);
+ NO_COPY(WorkItem);
+
+ void
+ assist() const
+ {
+ worker->assist();
+ }
+ Worker * worker;
+
+ public:
+ virtual void doWork() = 0;
+ };
+
+ template<typename T> class WorkItemT : public WorkItem {
+ public:
+ T
+ get()
+ {
+ using namespace std::chrono_literals;
+ while (future.wait_for(0s) == std::future_status::timeout) {
+ assist();
+ }
+ return future.get();
+ }
+
+ protected:
+ WorkItemT(Worker * worker) : WorkItem {worker} { }
+
+ std::promise<T> promise;
+ std::future<T> future {promise.get_future()};
+ friend Worker;
+ };
+
+ template<typename... Params>
+ static auto
+ addWork(Params &&... params)
+ {
+ return instance.addWorkImpl(std::forward<Params>(params)...);
+ }
+ template<typename T> using WorkPtrT = std::shared_ptr<WorkItemT<T>>;
+
+private:
+ template<typename T, typename... Params> class WorkItemTImpl : public WorkItemT<T> {
+ public:
+ WorkItemTImpl(Worker * worker, Params &&... params) :
+ WorkItemT<T> {worker}, params {std::forward<Params>(params)...}
+ {
+ }
+
+ private:
+ void
+ doWork() override
+ {
+ try {
+ if constexpr (std::is_void_v<T>) {
+ std::apply(std::invoke<Params &...>, params);
+ WorkItemT<T>::promise.set_value();
+ }
+ else {
+ WorkItemT<T>::promise.set_value(std::apply(std::invoke<Params &...>, params));
+ }
+ }
+ catch (...) {
+ WorkItemT<T>::promise.set_exception(std::current_exception());
+ }
+ }
+
+ std::tuple<Params...> params;
+ };
+
Worker();
~Worker();
NO_COPY(Worker);
NO_MOVE(Worker);
- using WorkPtr = std::unique_ptr<Work>;
+ using WorkPtr = std::shared_ptr<WorkItem>;
- template<typename T, typename... Params>
- void
- addWork(Params &&... params)
- requires std::is_base_of_v<Work, T>
+ template<typename... Params>
+ auto
+ addWorkImpl(Params &&... params)
{
- addWork(std::make_unique<T>(std::forward<Params>(params)...));
+ using T = decltype(std::invoke(std::forward<Params>(params)...));
+ auto work = std::make_shared<WorkItemTImpl<T, Params...>>(this, std::forward<Params>(params)...);
+ addWorkPtr(work);
+ return work;
}
- void addWork(WorkPtr w);
-
-private:
+ void addWorkPtr(WorkPtr w);
void worker();
+ void assist();
- using Threads = std::vector<std::thread>;
+ using Threads = std::vector<std::jthread>;
using ToDo = std::deque<WorkPtr>;
- Threads threads;
ToDo todo;
std::counting_semaphore<16> todoLen;
std::mutex todoMutex;
-};
-
-#else
+ Threads threads;
-class Worker {
-public:
- template<typename T, typename... Params>
- void
- addWork(Params &&... params)
- requires std::is_base_of_v<Work, T>
- {
- T(std::forward<Params>(params)...).doWork();
- }
+ static Worker instance;
};
-
-#endif