-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.cpp
74 lines (58 loc) · 1.27 KB
/
scheduler.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "scheduler.hpp"
#include "abstract_task.hpp"
#include "future.hpp"
#include "cycle.hpp"
#include <exception>
#include <utility>
#include <tuple>
namespace cosche
{
Scheduler::Scheduler() : _running(false) {}
void Scheduler::run()
{
_running = true;
AbstractTask* task;
while (!cyclic() && (!empty() || waiting()) && !_throwing)
{
if (!empty())
{
task = top()._task;
*(task->_context) = std::get<0>((*(task->_context))(task));
}
checkFutures();
}
if (cyclic())
{
throw Cycle(cycle());
}
if (_throwing)
{
std::rethrow_exception(_throwing);
}
_running = false;
}
bool Scheduler::running() const { return _running; }
void Scheduler::checkFutures()
{
auto it = _futures.begin();
while (it != _futures.end())
{
if (it->first->ready())
{
wake(it->second);
it->second->_future = it->first;
it = _futures.erase(it);
}
else
{
it++;
}
}
}
void Scheduler::haltWaitingFuture(std::shared_ptr<AbstractFuture>&& future,
AbstractTask* task)
{
halt(task);
_futures[std::move(future)] = task;
}
} // end cosche namespace