-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabstract_task.cpp
70 lines (53 loc) · 1.23 KB
/
abstract_task.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
#include "abstract_task.hpp"
#include "scheduler.hpp"
#include <iostream>
#include <utility>
#include <future>
#include <tuple>
namespace cosche
{
AbstractTask::AbstractTask(Scheduler& scheduler) :
_scheduler(scheduler),
_context(std::make_shared<Context>(start)),
_onCycle(std::make_shared<std::packaged_task<void()>>
(
[&]()
{
std::cerr << "The task " << id() << " belongs to a cycle." << std::endl;
}
))
{}
Context AbstractTask::start(Context context, AbstractTask* task)
{
*(task->_context) = std::move(context);
task->run();
task->_scheduler.erase(task);
task->recycle();
context = std::move(*(task->_context));
return context;
}
void AbstractTask::attach(AbstractTask& task)
{
_scheduler.attach(this, &task);
if (_scheduler.running())
{
*_context = std::get<0>((*_context)(this));
}
}
void AbstractTask::detach(AbstractTask& task)
{
_scheduler.detach(this, &task);
}
void AbstractTask::release()
{
_scheduler.release(this);
}
void AbstractTask::onCycle()
{
if (_onCycle->valid())
{
_onCycle->operator()();
}
}
Scheduler& AbstractTask::scheduler() const { return _scheduler; }
} // end cosche namespace