summaryrefslogtreecommitdiff
path: root/lib/worker.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 14:49:52 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 14:49:52 +0100
commit70bddc3f8ef3eeb020b4d6114f3abf4ab160003f (patch)
treef8e83df70e00c9f7f3588638c756ac66a8ab0199 /lib/worker.h
parentExternalise a neater definition of TGAHead (diff)
parentLoad texture images in Worker (diff)
downloadilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.bz2
ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.xz
ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.zip
Merge branch 'worker' into 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