-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduler.mpp
More file actions
297 lines (257 loc) · 7.63 KB
/
Copy pathScheduler.mpp
File metadata and controls
297 lines (257 loc) · 7.63 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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;
const void* owner = nullptr;
};
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::vector<std::shared_ptr<Item>> activeItems;
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, const void* owner = nullptr) -> std::size_t
{
auto item = std::make_shared<Item>(m_nextId.fetch_add(1, std::memory_order_relaxed), when, std::move(task), false, owner);
auto accessor = m_items.access();
auto& [set, map, activeItems, 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, const void* owner = nullptr) -> std::size_t
{
return schedule(std::move(task), Clock::now() + delay, owner);
}
inline auto waitUntilFinished() -> void
{
auto accessor = m_items.access();
m_finishedCondition.wait(accessor.getLockGuard(), [&] {
return std::ranges::empty(accessor.value().set) and accessor.value().processingTasks == 0;
});
}
inline auto getScheduledTasksCount() const -> std::size_t
{
auto accessor = m_items.access();
return std::ranges::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, activeItems, processingTasks] = accessor.value();
if (std::ranges::empty(set))
{
if (processingTasks == 0)
break;
m_finishedCondition.wait(accessor.getLockGuard());
continue;
}
tasksToRun.assign(std::ranges::begin(set), std::ranges::end(set));
processingTasks += std::ranges::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, activeItems, processingTasks] = accessor.value();
if (auto it = map.find(id); it != std::ranges::end(map))
{
it->second->cancelled = true;
set.erase(it->second);
map.erase(it);
m_workCondition.notify_all();
m_finishedCondition.notify_all();
}
}
inline auto cancelOwner(const void* owner) -> void
{
if (owner == nullptr)
return;
auto accessor = m_items.access();
auto& [set, map, activeItems, processingTasks] = accessor.value();
for (auto it = std::ranges::begin(map); it != std::ranges::end(map);)
if (it->second->owner == owner)
{
it->second->cancelled = true;
set.erase(it->second);
it = map.erase(it);
}
else
++it;
for (auto& item : activeItems)
if (item->owner == owner)
item->cancelled = true;
m_finishedCondition.wait(accessor.getLockGuard(), [&] {
return std::ranges::none_of(activeItems, [&](const auto& item) {
return item->owner == owner;
});
});
m_workCondition.notify_all();
m_finishedCondition.notify_all();
}
inline auto cancelAll() -> void
{
auto accessor = m_items.access();
auto& [set, map, activeItems, processingTasks] = accessor.value();
for (auto& [_, item] : map)
item->cancelled = true;
for (auto& item : activeItems)
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, activeItems, processingTasks] = accessor.value();
if (std::ranges::empty(set))
m_workCondition.wait(accessor.getLockGuard(), [this, &set] {
return not std::ranges::empty(set) or m_threadLoop.isStopRequested();
});
else if (auto first = *std::ranges::begin(set); first->time > Clock::now())
m_workCondition.wait_until(accessor.getLockGuard(), first->time, [this, &set, first] {
return m_threadLoop.isStopRequested() or std::ranges::empty(set) or (*std::ranges::begin(set) != first) or ((*std::ranges::begin(set))->time <= Clock::now());
});
if (m_threadLoop.isStopRequested() or std::ranges::empty(set) or (*std::ranges::begin(set))->time > Clock::now())
return;
auto first = *std::ranges::begin(set);
const auto endTime = (m_step == Clock::duration::zero()) ? first->time : first->time + m_step;
for (auto it = std::ranges::begin(set); it != std::ranges::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::ranges::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)
{
{
auto accessor = m_items.access();
accessor.value().activeItems.push_back(item);
}
try
{
m_threadPool.call([this, item] {
auto scope = Execution::ScopeGuard{[this, &item] {
auto accessor = m_items.access();
std::erase(accessor.value().activeItems, item);
m_finishedCondition.notify_all();
}};
if (not item->cancelled)
std::invoke(item->task);
});
}
catch (...)
{
auto accessor = m_items.access();
std::erase(accessor.value().activeItems, item);
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;
};
}