blob: 6f0c466643beda04b754edf7db337c72ae44db9c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include "core/task.hpp"
#include <functional>
#include <memory>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iostream>
#include "debug.hpp"
using namespace std;
namespace flat {
namespace core {
task::task(task::callback f, priority_t p)
: prioritized(p), m_callback(f) {}
std::shared_ptr<task> job::delegate_task(task::callback f, priority_t p) {
auto shared = std::make_shared<task>(f, p);
insert(shared);
npdebug("Task number: ", this->size());
return shared;
}
void job::add_task(task::callback f, priority_t p) {
insert(std::make_shared<task>(f, p));
}
void job::invoke_tasks() {
// expired tasks to remove
std::vector<job::value_type> to_erase;
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);
}
// delete expired tasks
for (auto tp : to_erase) {
erase(tp);
}
}
}
}
|