summaryrefslogtreecommitdiff
path: root/lib/worker.cpp
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.cpp
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.cpp')
-rw-r--r--lib/worker.cpp39
1 files changed, 28 insertions, 11 deletions
diff --git a/lib/worker.cpp b/lib/worker.cpp
index fd255c7..45fb6df 100644
--- a/lib/worker.cpp
+++ b/lib/worker.cpp
@@ -1,27 +1,24 @@
#include "worker.h"
-#if __cpp_lib_semaphore
-# include "work.h"
-# include <algorithm>
-# include <iterator>
-# include <mutex>
+#include <algorithm>
+#include <iterator>
+#include <mutex>
+
+Worker Worker::instance;
Worker::Worker() : todoLen {0}
{
std::generate_n(std::back_inserter(threads), std::thread::hardware_concurrency(), [this]() {
- return std::thread {&Worker::worker, this};
+ return std::jthread {&Worker::worker, this};
});
}
Worker::~Worker()
{
todoLen.release(std::thread::hardware_concurrency());
- std::for_each(threads.begin(), threads.end(), [](auto & th) {
- th.join();
- });
}
void
-Worker::addWork(WorkPtr j)
+Worker::addWorkPtr(WorkPtr j)
{
std::lock_guard<std::mutex> lck {todoMutex};
todoLen.release();
@@ -45,4 +42,24 @@ Worker::worker()
j->doWork();
}
}
-#endif
+
+void
+Worker::assist()
+{
+ auto job = [this]() {
+ using namespace std::chrono_literals;
+ if (todoLen.try_acquire_for(100us)) {
+ if (std::lock_guard<std::mutex> lck {todoMutex}; todo.size()) {
+ WorkPtr x = std::move(todo.front());
+ if (x) {
+ todo.pop_front();
+ }
+ return x;
+ }
+ }
+ return WorkPtr {};
+ };
+ if (auto j = job()) {
+ j->doWork();
+ }
+}