-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduler.mpp
More file actions
249 lines (217 loc) · 6.08 KB
/
Scheduler.mpp
File metadata and controls
249 lines (217 loc) · 6.08 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
export module CppUtils.Thread.Scheduler;
import std;
import CppUtils.Chrono.Concept;
import CppUtils.Execution.ScopeGuard;
import CppUtils.Thread.UniqueLocker;
import CppUtils.Thread.SharedLocker;
import CppUtils.Thread.ThreadLoop;
import CppUtils.Thread.ThreadPool;
export namespace CppUtils::Thread
{
class Scheduler final
{
public:
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
private:
using Task = std::function<void()>;
struct Item final
{
std::size_t id;
TimePoint time;
Task task;
std::atomic_bool cancelled = false;
};
struct Compare final
{
inline auto operator()(const std::shared_ptr<Item>& a,
const std::shared_ptr<Item>& b) const noexcept -> bool
{
if (a->time != b->time)
return a->time < b->time;
return a->id < b->id;
}
};
struct Items final
{
using Set = std::set<std::shared_ptr<Item>, Compare>;
using Map = std::unordered_map<std::size_t, std::shared_ptr<Item>>;
Set set;
Map map;
std::size_t processingTasks = 0;
};
public:
inline Scheduler(
Clock::duration step = Clock::duration::zero(),
std::size_t numberThreads = std::thread::hardware_concurrency(),
std::function<void(std::exception_ptr)> onError = nullptr,
std::function<void()> finally = nullptr):
m_step{step},
m_finally{std::move(finally)},
m_threadPool{
numberThreads,
onError,
[this] {
auto _ = Execution::ScopeGuard{[&] {
auto accessor = m_items.access();
if (--accessor.value().processingTasks == 0)
m_finishedCondition.notify_all();
}};
if (m_finally)
m_finally();
}},
m_threadLoop{
[this] { runLoop(); },
[this] {
m_workCondition.notify_all();
m_finishedCondition.notify_all();
},
onError}
{
m_threadLoop.start();
}
inline ~Scheduler() noexcept
{
m_threadLoop.requestStop();
cancelAll();
}
inline auto schedule(Task task, TimePoint when) -> std::size_t
{
auto item = std::make_shared<Item>(m_nextId.fetch_add(1, std::memory_order_relaxed), when, std::move(task));
auto accessor = m_items.access();
auto& [set, map, processingTasks] = accessor.value();
set.insert(item);
map.emplace(item->id, item);
m_workCondition.notify_all();
return item->id;
}
inline auto schedule(Task task, Chrono::Duration auto delay) -> std::size_t
{
return schedule(std::move(task), Clock::now() + delay);
}
inline auto waitUntilFinished() -> void
{
auto accessor = m_items.access();
m_finishedCondition.wait(accessor.getLockGuard(), [&] {
return std::empty(accessor.value().set) and accessor.value().processingTasks == 0;
});
}
inline auto getScheduledTasksCount() const -> std::size_t
{
auto accessor = m_items.access();
return std::size(accessor.value().set) + accessor.value().processingTasks;
}
inline auto flush() -> void
{
while (true)
{
auto tasksToRun = std::vector<std::shared_ptr<Item>>{};
{
auto accessor = m_items.access();
auto& [set, map, processingTasks] = accessor.value();
if (std::empty(set))
{
if (processingTasks == 0)
break;
m_finishedCondition.wait(accessor.getLockGuard());
continue;
}
tasksToRun.assign(std::begin(set), std::end(set));
processingTasks += std::size(set);
map.clear();
set.clear();
}
dispatchTasks(std::move(tasksToRun));
}
m_workCondition.notify_all();
m_finishedCondition.notify_all();
waitUntilFinished();
}
inline auto cancel(std::size_t id) -> void
{
auto accessor = m_items.access();
auto& [set, map, processingTasks] = accessor.value();
if (auto it = map.find(id); it != std::end(map))
{
it->second->cancelled = true;
set.erase(it->second);
map.erase(it);
m_workCondition.notify_all();
m_finishedCondition.notify_all();
}
}
inline auto cancelAll() -> void
{
auto accessor = m_items.access();
auto& [set, map, processingTasks] = accessor.value();
for (auto& [_, item] : map)
item->cancelled = true;
map.clear();
set.clear();
m_workCondition.notify_all();
m_finishedCondition.notify_all();
}
private:
inline auto runLoop() -> void
{
auto tasksToRun = std::vector<std::shared_ptr<Item>>{};
{
auto accessor = m_items.access();
auto& [set, map, processingTasks] = accessor.value();
if (std::empty(set))
m_workCondition.wait(accessor.getLockGuard(), [this, &set] {
return not std::empty(set) or m_threadLoop.isStopRequested();
});
else if (auto first = *std::begin(set); first->time > Clock::now())
m_workCondition.wait_until(accessor.getLockGuard(), first->time, [this, &set, first] {
return m_threadLoop.isStopRequested() or std::empty(set) or (*std::begin(set) != first) or ((*std::begin(set))->time <= Clock::now());
});
if (m_threadLoop.isStopRequested() or std::empty(set) or (*std::begin(set))->time > Clock::now())
return;
auto first = *std::begin(set);
const auto endTime = (m_step == Clock::duration::zero()) ? first->time : first->time + m_step;
for (auto it = std::begin(set); it != std::end(set);)
if ((*it)->time <= endTime)
{
tasksToRun.push_back(*it);
map.erase((*it)->id);
it = set.erase(it);
++accessor.value().processingTasks;
}
else
break;
if (std::empty(set))
m_finishedCondition.notify_all();
}
dispatchTasks(std::move(tasksToRun));
}
private:
inline auto dispatchTasks(std::vector<std::shared_ptr<Item>> tasks) -> void
{
for (auto&& item : tasks)
{
try
{
m_threadPool.call([item] {
if (not item->cancelled)
std::invoke(item->task);
});
}
catch (...)
{
auto accessor = m_items.access();
if (--accessor.value().processingTasks == 0)
m_finishedCondition.notify_all();
}
}
}
std::atomic_size_t m_nextId = 0;
Clock::duration m_step;
UniqueLocker<Items> m_items;
std::function<void()> m_finally;
std::condition_variable m_workCondition;
std::condition_variable m_finishedCondition;
ThreadPool m_threadPool;
ThreadLoop m_threadLoop;
};
}