summaryrefslogtreecommitdiffstats
path: root/engine
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-23 17:09:11 +0100
committerNao Pross <naopross@thearcway.org>2019-01-23 17:09:36 +0100
commitd67447c90ef9bbb4abce24e425e698a846f1bd5d (patch)
treea28790170659abac3223eb75da629bf3c8c44236 /engine
parentDelete build directory (diff)
parentFix bug in RAII jobs, debug session on ts3 (diff)
downloadflatland-d67447c90ef9bbb4abce24e425e698a846f1bd5d.tar.gz
flatland-d67447c90ef9bbb4abce24e425e698a846f1bd5d.zip
Merge branch 'raii-task'
Diffstat (limited to 'engine')
-rw-r--r--engine/include/core/priority.hpp2
-rw-r--r--engine/include/core/task.hpp6
-rw-r--r--engine/task.cpp24
3 files changed, 18 insertions, 14 deletions
diff --git a/engine/include/core/priority.hpp b/engine/include/core/priority.hpp
index 1219d26..d6c9b35 100644
--- a/engine/include/core/priority.hpp
+++ b/engine/include/core/priority.hpp
@@ -33,7 +33,7 @@ namespace flat {
return lhs.priority() < rhs.priority();
}
- bool operator()(const std::weak_ptr<prioritized> lhs, const std::weak_ptr<prioritized>& rhs) {
+ bool operator()(const std::weak_ptr<prioritized> lhs, const std::weak_ptr<prioritized> rhs) {
if (auto l = lhs.lock()) {
if (auto r = rhs.lock()) {
// if both valid, check their priority
diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp
index d886fcd..570e547 100644
--- a/engine/include/core/task.hpp
+++ b/engine/include/core/task.hpp
@@ -24,9 +24,13 @@ namespace flat {
callback m_callback;
};
- struct job : private queue<std::weak_ptr<task>> {
+ struct job : public queue<std::weak_ptr<task>> {
std::shared_ptr<task> make_task(task::callback f, priority_t p = priority_t::none);
void invoke_tasks();
+
+ inline void operator()() {
+ invoke_tasks();
+ }
};
}
}
diff --git a/engine/task.cpp b/engine/task.cpp
index 7f1b08b..8950633 100644
--- a/engine/task.cpp
+++ b/engine/task.cpp
@@ -2,6 +2,8 @@
#include <functional>
#include <memory>
+#include <algorithm>
+#include <cassert>
#include <vector>
@@ -18,27 +20,25 @@ namespace flat {
auto shared = std::make_shared<task>(f, p);
insert(shared);
+
return shared;
}
void job::invoke_tasks() {
- //std::for_each(begin(), end(), [&](auto tp) {
-
- vector<weak_ptr<task>> to_erase;
+ // expired tasks to remove
+ std::vector<job::value_type> to_erase;
- for (auto tp : (*this)) {
- if (tp.expired())
+ for (auto tp : *this) {
+ // check that the task has not expired
+ if (std::shared_ptr<task> t = tp.lock())
+ std::invoke(*t);
+ else
to_erase.push_back(tp);
}
- for (auto tp : to_erase)
+ // delete expired tasks
+ for (auto tp : to_erase) {
erase(tp);
-
- for (auto tp : (*this)) {
- // check that the task has not expired
- // run task
- auto t = tp.lock();
- std::invoke(*t);
}
}