-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread-pool-executor.h
292 lines (253 loc) · 7.59 KB
/
thread-pool-executor.h
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
#include <memory>
#include <chrono>
#include <vector>
#include <functional>
#include <iostream>
#include <thread>
#include <queue>
#include <optional>
#include <mutex>
#include <condition_variable>
template <class T>
class ConcurrentQueue {
public:
bool TryPeek(T& value) {
std::lock_guard<std::mutex> lock(mutex_);
if (buffer_.empty()) {
return false;
}
value = std::move(buffer_.front());
buffer_.pop();
return true;
}
void Push(T value) {
std::lock_guard<std::mutex> lock(mutex_);
buffer_.emplace(std::move(value));
}
private:
std::mutex mutex_;
std::queue<T> buffer_;
};
enum TaskState { Created, Completed, Failed, Canceled };
class Task : public std::enable_shared_from_this<Task> {
public:
virtual ~Task() {
}
virtual void Run() = 0;
void AddDependency(std::shared_ptr<Task> dep) {
deps_.emplace_back(dep);
}
void AddTrigger(std::shared_ptr<Task> dep) {
trigs_.emplace_back(dep);
}
void SetTimeTrigger(std::chrono::system_clock::time_point at) {
deadline_ = at;
}
// Task::Run() completed without throwing exception
bool IsCompleted() {
return state_ == Completed;
}
// Task::Run() throwed exception
bool IsFailed() {
return state_ == Failed;
}
// Task was Canceled
bool IsCanceled() {
return state_ == Canceled;
}
// Task either completed, failed or was Canceled
bool IsFinished() {
return state_ != Created;
}
bool CanBeRun() {
if (deps_.empty() && trigs_.empty() && !deadline_.has_value() && !IsFinished()) {
return true;
}
bool deps_finished =
std::all_of(deps_.begin(), deps_.end(),
[](std::shared_ptr<Task> task) { return task->IsFinished(); }) &&
!deps_.empty();
bool trig_finished =
std::any_of(trigs_.begin(), trigs_.end(),
[](std::shared_ptr<Task> task) { return task->IsFinished(); });
bool deadline_passed =
deadline_.has_value() && std::chrono::system_clock::now() > deadline_;
return (deps_finished || trig_finished || deadline_passed) && !IsFinished();
}
void SetError(std::exception_ptr exc) {
task_exception_ = exc;
}
std::exception_ptr GetError() {
return task_exception_;
}
void Cancel() {
SetState(Canceled);
}
void Wait() {
std::unique_lock<std::mutex> lock(wait_mutex_);
while (!IsFinished()) {
done_.wait(lock);
}
}
void SetState(TaskState new_state) {
state_ = new_state;
done_.notify_all();
}
std::condition_variable done_;
std::mutex wait_mutex_;
private:
std::atomic<TaskState> state_;
std::vector<std::shared_ptr<Task>> deps_;
std::vector<std::shared_ptr<Task>> trigs_;
std::optional<std::chrono::system_clock::time_point> deadline_;
std::exception_ptr task_exception_;
};
template <class T>
class Future : public Task {
public:
Future(std::function<T()> fn) : fn_(fn) {
}
T Get() {
Wait();
if (IsFailed()) {
std::rethrow_exception(GetError());
// throw std::logic_error{"Test"};
}
return result_;
}
void Run() {
result_ = fn_();
}
private:
T result_;
std::function<T()> fn_;
};
template <class T>
using FuturePtr = std::shared_ptr<Future<T>>;
// Used instead of void in generic code
struct Unit {};
class Executor {
public:
Executor(uint32_t num_threads) {
for (uint32_t i = 0; i < num_threads; ++i) {
workers_.emplace_back([this] {
while (!executor_finished_) {
std::shared_ptr<Task> task;
if (task_queue_.TryPeek(task)) {
auto guard = std::lock_guard(task->wait_mutex_);
if (task->CanBeRun()) {
try {
task->Run();
task->SetState(Completed);
} catch (...) {
task->SetError(std::current_exception());
task->SetState(Failed);
}
} else if (task->IsFinished()) {
continue;
} else {
task_queue_.Push(task);
}
}
}
std::shared_ptr<Task> task;
while (task_queue_.TryPeek(task)) {
if (task->CanBeRun()) {
task->Cancel();
}
}
});
}
}
~Executor() {
StartShutdown();
WaitShutdown();
}
void Submit(std::shared_ptr<Task> task) {
if (executor_finished_) {
// TODO: remove all deps
task->Cancel();
return;
}
task_queue_.Push(std::move(task));
}
void StartShutdown() {
executor_finished_ = true;
}
void WaitShutdown() {
for (auto& worker : workers_) {
if (worker.joinable()) {
worker.join();
}
}
}
template <class T>
FuturePtr<T> Invoke(std::function<T()> fn) {
FuturePtr<T> task = std::make_shared<Future<T>>(fn);
Submit(task);
return task;
}
template <class Y, class T>
FuturePtr<Y> Then(FuturePtr<T> input, std::function<Y()> fn) {
FuturePtr<Y> future_b = std::make_shared<Future<Y>>(fn);
future_b->AddDependency(input);
Submit(future_b);
return future_b;
}
template <class T>
FuturePtr<std::vector<T>> WhenAll(std::vector<FuturePtr<T>> all) {
FuturePtr<std::vector<T>> task = std::make_shared<Future<std::vector<T>>>([all] {
std::vector<T> results;
results.reserve(all.size());
for (auto& dep : all) {
results.emplace_back(dep->Get());
}
return results;
});
for (auto& dep : all) {
task->AddDependency(dep);
}
Submit(task);
return task;
}
template <class T>
FuturePtr<T> WhenFirst(std::vector<FuturePtr<T>> all) {
FuturePtr<T> task = std::make_shared<Future<T>>([all] {
for (auto& trig : all) {
if (trig->IsFinished()) {
return trig->Get();
}
}
return all.at(0)->Get();
});
for (auto& trig : all) {
task->AddTrigger(trig);
}
Submit(task);
return task;
}
template <class T>
FuturePtr<std::vector<T>> WhenAllBeforeDeadline(
std::vector<FuturePtr<T>> all, std::chrono::system_clock::time_point deadline) {
FuturePtr<std::vector<T>> task = std::make_shared<Future<std::vector<T>>>([all] {
std::vector<T> results;
results.reserve(all.size());
for (auto& t : all) {
if (t->IsFinished()) {
results.emplace_back(t->Get());
}
}
return results;
});
task->SetTimeTrigger(deadline);
Submit(task);
return task;
}
private:
std::vector<std::thread> workers_;
std::atomic_bool executor_finished_ = false;
ConcurrentQueue<std::shared_ptr<Task>> task_queue_;
};
inline std::shared_ptr<Executor> MakeThreadPoolExecutor(uint32_t num_threads) {
return std::make_shared<Executor>(num_threads);
}