-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_pool.hpp
408 lines (341 loc) · 9.36 KB
/
thread_pool.hpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#pragma once
#include <vector>
#include <cstdint>
#include <functional>
#include <deque>
#include <type_traits>
#include <variant>
#include <cassert>
#include <thread>
#include <condition_variable>
#include <mutex>
namespace utils {
class thread_pool;
/// Exceptions for thread pool
struct cancelation_exception final : std::exception {
const char* what() const noexcept override {
return "getting result from cancelled task";
}
};
struct shutdown_exception final : std::exception {
const char* what() const noexcept override {
return "trying to push task into closed pool";
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail {
static inline thread_local thread_pool* curr_thread_pool{nullptr};
/// Unbounded multi-producers/multi-consumers queue
template <typename T>
class UnboundedMPMCQueue final {
public:
bool Put(T&& value) {
{
std::lock_guard lk(m_);
if (is_closed) {
return false;
}
data_.push_back(std::move(value));
}
cv_.notify_one();
return true;
}
std::optional<T> Take(bool wait = true) {
std::unique_lock lk(m_);
if (wait) {
cv_.wait(lk, [&] {
return is_closed || !data_.empty();
});
}
if (data_.empty()) {
return {};
}
T res = std::move(data_.front());
data_.pop_front();
return {std::move(res)};
}
void Close() {
{
std::lock_guard lk{m_};
is_closed = true;
}
cv_.notify_all();
}
std::size_t Size() const {
std::lock_guard lk{m_};
return data_.size();
}
private:
bool is_closed{false};
std::deque<T> data_;
mutable std::mutex m_;
std::condition_variable cv_;
};
/// States of tasks to share in share_state
enum class TaskState : std::uint32_t {
Runnable = 0,
Canceled,
Running,
Done,
};
/**
* Class with state to promise and future
*/
template <class Result>
class State final {
public:
using Callback = std::function<void(Result)>;
Result get();
void set_result(Result res);
bool is_running() const noexcept {
return state_.load(std::memory_order_acquire) == TaskState::Running;
}
bool is_done() const noexcept {
return state_.load(std::memory_order_acquire) == TaskState::Done;
}
bool is_canceled() const noexcept {
return state_.load(std::memory_order_acquire) == TaskState::Canceled;
}
void set_state(TaskState state) noexcept {
state_.store(state, std::memory_order_release);
}
void set_callback(const Callback& functor) {
std::unique_lock lk{m_};
if (state_.load(std::memory_order_acquire) == TaskState::Canceled) {
return;
}
if (std::holds_alternative<Result>(content_)) {
try {
functor(get<Result>(content_));
} catch (...) {
}
} else if (std::holds_alternative<std::monostate>(content_)) {
callback_ = functor;
}
}
void set_error(const std::exception_ptr& eptr) {
std::unique_lock lk{m_};
if (state_.load(std::memory_order_acquire) == TaskState::Canceled) {
return;
}
content_ = eptr;
lk.unlock();
cv_.notify_all();
}
void cancel() {
auto state = TaskState::Runnable;
if (state_.compare_exchange_strong(state, TaskState::Canceled,
std::memory_order_release,
std::memory_order_relaxed)) {
cv_.notify_all();
}
}
private:
std::atomic<TaskState> state_{TaskState::Runnable};
mutable std::mutex m_;
std::condition_variable cv_;
std::optional<Callback> callback_;
std::variant<std::monostate, Result, std::exception_ptr> content_{
std::monostate{}};
};
template <class Res>
using shared_state = std::shared_ptr<State<Res>>;
/**
* Future class
*/
template <class Result>
class Future final {
static_assert(!std::is_same_v<Result, void>);
public:
Future(const Future& other) = default;
Future& operator=(const Future& other) = default;
Future(Future&& other) noexcept = default;
Future& operator=(Future&& other) noexcept = default;
Future(shared_state<Result> state_) noexcept : state_(std::move(state_)) {
}
~Future() {
if (state_) {
cancel();
}
}
void invoke_on_completion(const std::function<void(Result)>& f) {
state_->set_callback(f);
}
Result get() {
return state_->get();
}
void cancel() noexcept {
state_->cancel();
}
bool is_running() const noexcept {
return state_->is_running();
}
bool is_done() const noexcept {
return state_->is_done();
}
bool is_canceled() const noexcept {
return state_->is_canceled();
}
private:
shared_state<Result> state_;
};
/**
* Promise class
*/
template <class Result>
class Promise final {
public:
explicit Promise(shared_state<Result> state) : state_(std::move(state)) {
}
bool is_canceled() const noexcept {
return state_->is_canceled();
}
void running() noexcept {
state_->set_state(TaskState::Running);
}
void done() noexcept {
state_->set_state(TaskState::Done);
}
void set_error(const std::exception_ptr& eptr) noexcept {
state_->set_error(eptr);
}
void set_result(Result res) noexcept {
state_->set_result(std::move(res));
}
private:
shared_state<Result> state_;
};
/// creates channel with promise and future
template <class ReturnVal>
auto make_contract() {
detail::shared_state<ReturnVal> state =
std::make_shared<detail::State<ReturnVal>>();
return std::pair{detail::Future(state), detail::Promise(state)};
}
} // namespace detail
///////////////////////////////////////////////////////////////////////////////////////////////////
class thread_pool final {
public:
template <class Res>
using task_t = detail::Future<Res>;
thread_pool(thread_pool&&) = delete;
thread_pool(const thread_pool&) = delete;
thread_pool& operator=(thread_pool&&) = delete;
thread_pool& operator=(const thread_pool&) = delete;
explicit thread_pool(
std::size_t threads_count = std::thread::hardware_concurrency()) {
workers_.reserve(threads_count);
for (std::size_t i = 0; i < threads_count; ++i) {
workers_.emplace_back([this]() {
detail::curr_thread_pool = this;
for (auto task = tasks_.Take(); bool(task); task = tasks_.Take()) {
try {
(*task)();
} catch (...) {
}
}
});
}
}
bool run_without_wait() {
auto task = tasks_.Take(/*wait=*/false);
if (task) {
(task.value())();
}
return (bool)task;
}
template <class Functor, class... Args>
void enqueue(Functor&& f, Args&&... args) {
bool success = tasks_.Put(
[func = std::move(f), ... args = std::forward<Args>(args)]() mutable {
try {
std::forward<Functor>(func)(std::forward<Args>(args)...);
} catch (...) {
}
});
if (!success) {
throw shutdown_exception{};
}
}
template <class Functor, class... Args>
auto submit(Functor&& f, Args&&... args) -> task_t<
decltype(std::forward<Functor>(f)(std::forward<Args>(args)...))> {
auto [ret, p] =
detail::make_contract<std::decay_t<decltype(std::forward<Functor>(f)(
std::forward<Args>(args)...))>>();
enqueue([promise = std::move(p), func = std::forward<Functor>(f),
... args = std::forward<Args>(args)]() mutable {
if (promise.is_canceled()) {
return;
}
promise.running();
try {
promise.set_result(std::forward<Functor>(func)(std::move(args)...));
} catch (...) {
promise.set_error(std::current_exception());
}
promise.done();
});
return std::move(ret);
}
std::size_t threads_count() const {
return workers_.size();
}
std::size_t remaining_tasks() const {
return tasks_.Size();
}
~thread_pool() {
tasks_.Close();
for (auto& el : workers_) {
if (el.joinable()) {
el.join();
}
}
}
private:
std::vector<std::thread> workers_;
detail::UnboundedMPMCQueue<std::function<void()>> tasks_;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
template <class Result>
void detail::State<Result>::set_result(Result res) {
std::unique_lock lk{m_};
TaskState check = state_.load(std::memory_order_acquire);
assert(check == TaskState::Running || check == TaskState::Canceled);
if (callback_) {
curr_thread_pool->enqueue(*callback_, res);
}
content_ = std::move(res);
lk.unlock();
cv_.notify_all();
}
template <class Result>
Result detail::State<Result>::get() {
if (state_.load(std::memory_order_acquire) == TaskState::Canceled) {
throw cancelation_exception{};
}
bool is_wait{false};
std::unique_lock lk{m_};
while (!std::holds_alternative<Result>(content_) &&
!std::holds_alternative<std::exception_ptr>(content_) &&
state_.load(std::memory_order_acquire) != TaskState::Canceled) {
if (is_wait) {
cv_.wait(lk);
is_wait = false;
} else if (curr_thread_pool) {
lk.unlock();
is_wait = !curr_thread_pool->run_without_wait();
lk.lock();
} else {
is_wait = true;
}
}
if (state_.load(std::memory_order_acquire) == TaskState::Canceled) {
throw cancelation_exception{};
}
if (std::holds_alternative<std::exception_ptr>(content_)) {
std::rethrow_exception(std::get<std::exception_ptr>(content_));
}
return std::get<Result>(content_);
}
} // namespace utils