-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.cxx
104 lines (90 loc) · 2.56 KB
/
Scheduler.cxx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Scheduler.hxx"
#include <algorithm>
/**
* Destructor. Deletes any tasks left on active queue
*/
Scheduler::~Scheduler() {
std::for_each(runningTasks.begin(),runningTasks.end(),[](Task*x){delete x;});
}
/**
* Returns task queue
*/
std::list<Task*>* Scheduler::getTaskQueue(){
return &runningTasks;
}
/**
* Returns blocked task queue
*/
std::list<Task*>* Scheduler::getBlockedQueue(){
return &blockedTasks;
}
/**
* Set the schedule to be used
*/
void Scheduler::setSchedule(Schedule* sch) {
schedule = sch;
}
/**
* Increment time passed, unload+log finished tasks
*/
void Scheduler::updateTasks(int timestep) {
for(int j = 0; j < timestep;++j) {
//Outputs time if in verbose mode
logger.reportStep(time);
//Decrement timers on blocked tasks
std::for_each(blockedTasks.begin(),blockedTasks.end(),[&](Task* a){
a->updateTask(time);
});
//Reorder running tasks
sortQueue();
//Run the first maxSimult tasks
std::list<Task*>::const_iterator task = runningTasks.begin();
for(uint i = 0; i < maxSimult && i < runningTasks.size();++i,++task){
logger.reportRun(*task);
if( (*task)->updateTask(time) && !(*task)->getFinished()) { //Returns true if blocked
blockedTasks.push_back(*task);
logger.reportBlock(*task);
}
}
//Unblock tasks finished with "IO"
blockedTasks.remove_if([&](Task* a){
if(a->getBlockRemaining() <= 0){
runningTasks.push_back(a);
logger.reportUnblock(a);
}
return a->getBlockRemaining() <= 0;
});
//Log and remove finished Tasks
runningTasks.remove_if([&](Task* a){
//adds Task to Logger queue if finished, then is removed from runningTasks.
if(a->getFinished()||time >= a->getDeadline()){
logTask(a);
if(a->getFinished()) {
logger.reportFinish(a);
} else {
logger.reportMissedDeadline(a);
}
}
return a->getFinished() || a->getBlockRemaining() || time >= a->getDeadline();
});
++time;
}
}
/**
* Uses the Schedule strategy to order tasks
*/
void Scheduler::sortQueue() {
schedule->reorder();
}
/**
* Sends a task to the Logger
*/
void Scheduler::logTask(Task* task) {
logger.addTask(task);
}
/**
* Calls the logger to read all finished tasks
*/
void Scheduler::logOutput(){
logger.readTasks();
}