-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.cpp
More file actions
186 lines (148 loc) · 5.71 KB
/
Scheduler.cpp
File metadata and controls
186 lines (148 loc) · 5.71 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "pch.h"
#include "Scheduler.h"
#include <algorithm>
namespace ndtech {
Scheduler::Scheduler()
:m_wakeTime(system_clock::now()) {
m_thread = std::thread{ &Scheduler::Run, this };
}
Scheduler::Scheduler(std::thread thread)
:m_thread(std::move(thread)),
m_wakeTime(system_clock::now()) {
}
void Scheduler::Run() {
while (!m_done) {
std::unique_lock<std::mutex> lockGuard(m_waitMutex);
while (m_wakeTime > system_clock::now()) {
m_conditionVariable.wait_until(lockGuard, m_wakeTime, [this]() {return m_wakeTime <= system_clock::now(); });
}
ProcessReadyTasks();
}
}
void Scheduler::ProcessReadyTasks() {
auto beginProcessingTime = system_clock::now();
{
std::lock_guard lockGuard(m_repeatingTasksMutex);
// For each repeating tasks where the next time is less than the current processing time
// run the task
std::for_each(
m_repeatingTasks.begin(),
std::find_if(
m_repeatingTasks.begin(),
m_repeatingTasks.end(),
// check if the next time for the task is now
[this, beginProcessingTime](std::tuple<std::function<void(void)>, microseconds, time_point<system_clock>, time_point<system_clock>> task) {
return std::get<2>(task) > beginProcessingTime;
}),
// Update the next execution time and run the task
[](std::tuple<std::function<void(void)>, microseconds, time_point<system_clock>, time_point<system_clock>>& task) {
std::get<2>(task) = (std::get<2>(task) + std::get<1>(task));
std::get<0>(task)();
});
// Erase repeating tasks where until is less than the current processing time
m_repeatingTasks.erase(
m_repeatingTasks.begin(),
std::find_if(
m_repeatingTasks.begin(),
m_repeatingTasks.end(),
[this, beginProcessingTime](std::tuple<std::function<void(void)>, microseconds, time_point<system_clock>, time_point<system_clock>> task) {
return std::get<3>(task) > beginProcessingTime;
})
);
}
std::lock_guard lockGuard(m_tasksMutex);
std::for_each(
m_tasks.begin(),
std::find_if(
m_tasks.begin(),
m_tasks.end(),
// check if the scheduled time for the task is now or past
[this, beginProcessingTime](std::pair<std::function<void(void)>, time_point<system_clock>> task) {
bool returnValue = (task.second > beginProcessingTime);
return returnValue;
}),
// run the task
[](std::pair<std::function<void(void)>, time_point<system_clock>> task) {
task.first();
});
m_tasks.erase(
m_tasks.begin(),
std::find_if(
m_tasks.begin(),
m_tasks.end(),
// check if the scheduled time for the task is now or past
[this, beginProcessingTime](std::pair<std::function<void(void)>, time_point<system_clock>> task) {
return task.second > beginProcessingTime;
})
);
if (m_tasks.size() > 0) {
m_wakeTime = m_tasks[0].second;
}
else {
m_wakeTime = system_clock::now() + 300ms;
}
if (m_repeatingTasks.size() > 0) {
if (std::get<2>(m_repeatingTasks[0]) < m_wakeTime) {
m_wakeTime = std::get<2>(m_repeatingTasks[0]);
}
}
}
void Scheduler::AddTask(std::pair<std::function<void(void)>, time_point<system_clock>> task) {
{ // to scope the lock guard
std::lock_guard<std::mutex> guard(m_tasksMutex);
m_tasks.push_back(task);
if (task.second < m_wakeTime) {
m_wakeTime = task.second;
}
std::sort(
m_tasks.begin(),
m_tasks.end(),
[](std::pair<std::function<void(void)>, time_point<system_clock>> l, std::pair<std::function<void(void)>, time_point<system_clock>> r) {
return l.second < r.second;
});
}
if (m_wakeTime <= system_clock::now()) {
// signal the thread to wake
m_conditionVariable.notify_all();
}
}
void Scheduler::AddTask(std::function<void(void)> taskFunction) {
std::pair<std::function<void(void)>, time_point<system_clock>> task{ taskFunction, system_clock::now() };
{ // to scope the lock guard
std::lock_guard<std::mutex> guard(m_tasksMutex);
m_tasks.push_back(task);
if (task.second < m_wakeTime) {
m_wakeTime = task.second;
}
std::sort(
m_tasks.begin(),
m_tasks.end(),
[](std::pair<std::function<void(void)>, time_point<system_clock>> l, std::pair<std::function<void(void)>, time_point<system_clock>> r) {
return l.second < r.second;
});
}
if (m_wakeTime <= system_clock::now()) {
// signal the thread to wake
m_conditionVariable.notify_all();
}
}
// until set a year in the future if not explicitly set
void Scheduler::AddRepeatingTask(std::function<void(void)> task, microseconds interval, time_point<system_clock> until = system_clock::now() + 8760h) {
std::lock_guard repeatingTasksGuard(m_repeatingTasksMutex);
time_point<system_clock> nextExecution = system_clock::now() + interval;
m_repeatingTasks.push_back(std::make_tuple(task, interval, nextExecution, until));
if (nextExecution < m_wakeTime) {
m_wakeTime = nextExecution;
}
std::sort(
m_repeatingTasks.begin(),
m_repeatingTasks.end(),
[](std::tuple<std::function<void(void)>, microseconds, time_point<system_clock>, time_point<system_clock>> l, std::tuple<std::function<void(void)>, microseconds, time_point<system_clock>, time_point<system_clock>> r) {
return std::get<2>(l) < std::get<2>(r);
});
}
void Scheduler::Join() {
this->m_done = true;
m_thread.join();
}
}