summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-23 17:02:24 +0100
committerNao Pross <naopross@thearcway.org>2019-01-23 17:02:24 +0100
commitd48d9f51970bd90ef9f1a8cbcf6207041e79a104 (patch)
tree0fcfa52b6f84722dc6119378106269cec025ed3b
parentUpdate .gitignore, delete makefile add -g flag to build (diff)
downloadflatland-d48d9f51970bd90ef9f1a8cbcf6207041e79a104.tar.gz
flatland-d48d9f51970bd90ef9f1a8cbcf6207041e79a104.zip
Fix bug in RAII jobs, debug session on ts3
-rw-r--r--engine/include/core/priority.hpp2
-rw-r--r--engine/include/core/task.hpp6
-rw-r--r--engine/task.cpp24
-rw-r--r--test/task_test.cpp14
4 files changed, 29 insertions, 17 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 f2b6058..c054625 100644
--- a/engine/task.cpp
+++ b/engine/task.cpp
@@ -2,6 +2,8 @@
#include <functional>
#include <memory>
+#include <algorithm>
+#include <cassert>
namespace flat {
@@ -12,20 +14,26 @@ namespace flat {
std::shared_ptr<task> job::make_task(task::callback f, priority_t p) {
auto shared = std::make_shared<task>(f, p);
insert(shared);
+
return shared;
}
void job::invoke_tasks() {
- std::for_each(begin(), end(), [&](auto tp) {
+ // expired tasks to remove
+ std::vector<job::value_type> to_remove;
+
+ for (auto tp : *this) {
// check that the task has not expired
- if (auto t = tp.lock()) {
- // run task
+ if (std::shared_ptr<task> t = tp.lock())
std::invoke(*t);
- } else {
- // delete task
- erase(tp);
- }
- });
+ else
+ to_remove.push_back(tp);
+ }
+
+ // delete expired tasks
+ for (auto tp : to_remove) {
+ erase(tp);
+ }
}
}
}
diff --git a/test/task_test.cpp b/test/task_test.cpp
index 199d510..85a1754 100644
--- a/test/task_test.cpp
+++ b/test/task_test.cpp
@@ -39,28 +39,28 @@ int main(int argc, char *argv[]) {
job f_job;
// test a function
- auto ciao_fn_task = f_job.make_task(hello);
+ auto hello_fn_task = f_job.make_task(hello);
// test a function ad make the pointer go out of scope
{
- auto hello_fn_task = f_job.make_task(ciao);
+ auto ciao_fn_task = f_job.make_task(ciao);
}
- f_job.invoke_tasks();
+ f_job();
std::cout << std::endl;
std::cout << "Testing methods" << std::endl;
- std::cout << " should print once: today we have no motd!" << std::endl;
+ std::cout << "should print once: today we have no motd!" << std::endl;
job m_job;
// test a method
message m;
- m_job.make_task(std::bind(&message::print_motd, m));
+ auto msg = m_job.make_task(std::bind(&message::print_motd, m));
// test a method of an object that goes out of scope
{
message out;
- m_job.make_task(std::bind(&message::print_date, out));
+ auto date = m_job.make_task(std::bind(&message::print_date, out));
}
// invoke tasks
@@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
//
// hello!
// hello world!
- m_job.invoke_tasks();
+ m_job();
return 0;
}