-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.h
More file actions
382 lines (312 loc) · 11.5 KB
/
thread_pool.h
File metadata and controls
382 lines (312 loc) · 11.5 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
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
#pragma once
/*
* Thread pool implementation, allow to add task for execution on already existed threads
* Example:
#include <ext/thread/thread_pool.h>
std::set<ext::TaskId, ext::TaskIdHelper> taskList;
ext::thread_pool threadPool([&taskList, &listMutex](const ext::TaskId& taskId)
{
taskList.erase(taskId);
});
const auto maxThreads = std::thread::hardware_concurrency();
for (auto i = maxThreads; i != 0; --i)
{
taskList.emplace(threadPool.add_task([]()
{
...
}));
}
threadPool.wait_for_tasks();
*/
#include <algorithm>
#include <future>
#include <mutex>
#include <stdint.h>
#include <thread>
#include <deque>
#include <type_traits>
#include <ext/core/check.h>
#include <ext/core/noncopyable.h>
#include <ext/reflection/enum.h>
#include <ext/thread/event.h>
#include <ext/thread/thread.h>
#include <ext/types/uuid.h>
namespace ext {
// thread pool for execution tasks
class thread_pool : ext::NonCopyable
{
public:
using TaskId = ext::uuid;
/**
* \param onTaskDone callback on execution task by id
* \param threadsCount working threads count
*/
explicit thread_pool(std::function<void(const TaskId&)>&& onTaskDone,
std::uint_fast32_t threadsCount = std::thread::hardware_concurrency());
thread_pool(std::uint_fast32_t threadsCount = std::thread::hardware_concurrency());
[[nodiscard]] static thread_pool& GlobalInstance();
// interrupt and join all existing threads
~thread_pool();
// wait until task queue not empty
void wait_for_tasks() const;
// detach and clear all working threads list, after calling this function class will be in inconsistent state
void detach_all();
/**
* \brief Add task function to queue
* \tparam Function to invoke
* \tparam Args list of arguments passed to function
* \param function execution function
* \param args list of arguments passed to function
* \return pair of a taskId(created task identifier) and a future(with task result)
*/
template <typename Function, typename... Args>
std::pair<TaskId,
std::future<
std::invoke_result_t<Function, Args...>
>>
add_task(Function&& function, Args&&... args);
/**
* \brief Add task function to queue with high priority
* \tparam Function to invoke
* \tparam Args list of arguments passed to function
* \param function execution function
* \param args list of arguments passed to function
* \return pair of a taskId(created task identifier) and a future(with task result)
*/
template <typename Function, typename... Args>
std::pair<TaskId,
std::future<
std::invoke_result_t<Function, Args...>
>>
add_high_priority_task(Function&& function, Args&&... args);
// remove task from queue by id and interrupt if it is executing
// return true if task was removed or interrupted, false if task not found
bool stop_and_remove_task(const TaskId& taskId);
[[nodiscard]] std::size_t running_tasks_count() const noexcept;
// interrupt and remove all tasks from queue
void interrupt_and_remove_all_tasks();
private:
// main thread for workers
void worker(ext::thread& workerThread);
// task execution priority
enum class TaskPriority
{
eHigh,
eNormal,
};
/**
* \brief Add function to queue with high priority
* \tparam Function to invoke
* \tparam Args list of arguments passed to function
* \param priority task execution priority
* \param function execution function
* \param args list of arguments passed to function
* \return pair of a taskId(created task identifier) and a future(with task result)
*/
template <typename Function, typename... Args>
[[nodiscard]] std::pair<TaskId,
std::future<std::invoke_result_t<Function, Args...>>>
add_task_with_priority(TaskPriority priority, Function&& function, Args&&... args);
private:
// struct with task information
struct TaskInfo;
typedef std::unique_ptr<TaskInfo> TaskInfoPtr;
// synchronization of m_queueTasks
mutable std::mutex m_taskQueueMutex;
std::condition_variable m_taskQueueChangedNotifier;
mutable std::condition_variable m_allTasksDoneCv;
// queue of tasks ordered by TaskPriority
std::deque<TaskInfoPtr> m_queueTasks;
// callback to callers about task done
const std::function<void(const TaskId&)> m_onTaskDone;
// list of worker threads
std::vector<ext::thread> m_threads;
std::unordered_map<TaskId, ext::thread*> m_runningTasks;
std::atomic_bool m_threadPoolWorks = true;
};
// struct with task information
struct thread_pool::TaskInfo : ext::NonCopyable
{
explicit TaskInfo(TaskId id, const TaskPriority _priority, std::function<void()>&& _task) noexcept
: task(std::move(_task)), priority(_priority), taskId(std::move(id))
{}
TaskInfo(const TaskInfo&) = delete;
TaskInfo(TaskInfo&&) = delete;
const std::function<void()> task;
const TaskPriority priority;
const TaskId taskId;
};
inline thread_pool& thread_pool::GlobalInstance()
{
static thread_pool globalThreadPool;
return globalThreadPool;
}
template <typename Function, typename... Args>
std::pair<thread_pool::TaskId,
std::future<
std::invoke_result_t<Function, Args...>
>>
thread_pool::add_task(Function&& function, Args&&... args)
{
return add_task_with_priority(TaskPriority::eNormal, std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args>
std::pair<thread_pool::TaskId,
std::future<
std::invoke_result_t<Function, Args...>
>>
thread_pool::add_high_priority_task(Function&& function, Args&&... args)
{
return add_task_with_priority(TaskPriority::eHigh, std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args>
std::pair<thread_pool::TaskId,
std::future<
std::invoke_result_t<Function, Args...>
>>
thread_pool::add_task_with_priority(TaskPriority priority, Function&& function, Args&&... args)
{
using _Result = std::invoke_result_t<Function, Args...>;
using PackagedTaskPtr = std::unique_ptr<std::packaged_task<_Result()>>;
// packaging task with params
PackagedTaskPtr packagedTask = std::make_unique<std::packaged_task<_Result()>>(
ext::ThreadInvoker<Function, Args...>(
std::forward<Function>(function), std::forward<Args>(args)...)
);
std::future<_Result> resultFuture = packagedTask->get_future();
// adding task to a queue
TaskId taskId;
{
auto taskInfo = std::make_unique<TaskInfo>(taskId, priority,
[taskPointer = packagedTask.release()]()
{
PackagedTaskPtr taskPtr(taskPointer);
taskPtr->operator()();
});
std::lock_guard<std::mutex> lock(m_taskQueueMutex);
switch (priority)
{
case TaskPriority::eHigh:
{
const auto priorityIt = std::find_if(m_queueTasks.cbegin(), m_queueTasks.cend(),
[priority](const TaskInfoPtr& task)
{
return task->priority > priority;
});
m_queueTasks.emplace(priorityIt, std::move(taskInfo));
break;
}
case TaskPriority::eNormal:
m_queueTasks.emplace_back(std::move(taskInfo));
break;
default:
static_assert(ext::reflection::get_enum_size<TaskPriority>() == 2,
"Task priority has unsupported value, extent this enum");
EXT_UNREACHABLE();
}
m_taskQueueChangedNotifier.notify_one();
}
EXT_ASSERT(std::any_of(m_threads.begin(), m_threads.end(), std::mem_fn(&ext::thread::thread_works)))
<< "Threads interrupted or stopped";
return std::make_pair(std::move(taskId), std::move(resultFuture));
}
inline bool thread_pool::stop_and_remove_task(const TaskId& taskId)
{
ext::thread* threadToInterrupt = nullptr;
{
std::lock_guard lock(m_taskQueueMutex);
auto it = std::find_if(m_queueTasks.begin(), m_queueTasks.end(),
[&](const TaskInfoPtr& task) {
return task->taskId == taskId;
});
if (it != m_queueTasks.end())
{
m_queueTasks.erase(it);
return true;
}
auto runningIt = m_runningTasks.find(taskId);
if (runningIt == m_runningTasks.end())
return false;
threadToInterrupt = runningIt->second;
}
threadToInterrupt->interrupt();
std::unique_lock lock(m_taskQueueMutex);
m_allTasksDoneCv.wait(lock, [&] {
return m_runningTasks.find(taskId) == m_runningTasks.end();
});
return true;
}
[[nodiscard]] inline std::size_t thread_pool::running_tasks_count() const noexcept
{
std::lock_guard lock(m_taskQueueMutex);
return m_runningTasks.size();
}
inline void thread_pool::interrupt_and_remove_all_tasks()
{
{
std::lock_guard<std::mutex> lock(m_taskQueueMutex);
m_queueTasks.clear();
std::for_each(m_threads.begin(), m_threads.end(), std::mem_fn(&ext::thread::interrupt));
}
m_allTasksDoneCv.notify_all();
wait_for_tasks();
}
inline thread_pool::thread_pool(std::function<void(const TaskId&)>&& onTaskDone, std::uint_fast32_t threadsCount)
: m_onTaskDone(std::move(onTaskDone))
, m_threads(threadsCount, {})
{
for (auto& thread : m_threads)
thread.run(&thread_pool::worker, this, std::ref(thread));
}
inline thread_pool::thread_pool(std::uint_fast32_t threadsCount)
: thread_pool(nullptr, threadsCount)
{}
inline thread_pool::~thread_pool()
{
m_threadPoolWorks = false;
{
std::lock_guard<std::mutex> lock(m_taskQueueMutex);
std::for_each(m_threads.begin(), m_threads.end(), std::mem_fn(&ext::thread::interrupt));
m_queueTasks.clear();
m_taskQueueChangedNotifier.notify_all();
}
std::for_each(m_threads.begin(), m_threads.end(), [](ext::thread& thread) {
thread.join();
});
m_allTasksDoneCv.notify_all();
}
inline void thread_pool::wait_for_tasks() const
{
std::unique_lock lock(m_taskQueueMutex);
m_allTasksDoneCv.wait(lock, [&] {
return m_queueTasks.empty() && m_runningTasks.empty();
});
}
inline void thread_pool::worker(ext::thread& workerThread)
{
while (m_threadPoolWorks)
{
thread_pool::TaskInfoPtr taskToExecute;
{
std::unique_lock<std::mutex> lock(m_taskQueueMutex);
m_taskQueueChangedNotifier.wait(lock, [&]() { return !m_queueTasks.empty() || !m_threadPoolWorks; });
if (!m_threadPoolWorks && m_queueTasks.empty())
return;
taskToExecute = std::move(m_queueTasks.front());
m_queueTasks.pop_front();
m_runningTasks.emplace(taskToExecute->taskId, &workerThread);
}
taskToExecute->task();
if (m_onTaskDone)
m_onTaskDone(taskToExecute->taskId);
{
std::lock_guard lock(m_taskQueueMutex);
m_runningTasks.erase(taskToExecute->taskId);
}
// If thread was interrupted during task execution, we should restore it to be able to execute next tasks
if (workerThread.interrupted())
workerThread.restore_interrupted();
m_allTasksDoneCv.notify_all();
}
}
} // namespace ext