-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolExecutor.cpp
More file actions
165 lines (148 loc) · 5.28 KB
/
ThreadPoolExecutor.cpp
File metadata and controls
165 lines (148 loc) · 5.28 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
#include "ThreadPoolExecutor.h"
RejectedExecutionException::RejectedExecutionException(const std::string& arg) : runtime_error(arg) {
std::cerr << arg << std::endl;
}
void ThreadPoolExecutor::AbortPolicy::rejectedExecution(const std::function<void()>& func, ThreadPoolExecutor* e) {
throw RejectedExecutionException("Task rejected!");
}
void ThreadPoolExecutor::DiscardPolicy::rejectedExecution(const std::function<void()>& func, ThreadPoolExecutor* e) {
std::cerr << "Task rejected!" << std::endl;
}
void ThreadPoolExecutor::DiscardOldestPolicy::rejectedExecution(const std::function<void()>& func, ThreadPoolExecutor* e) {
if (!e->isShutdown()) {
e->workQueue->poll();
e->workQueue->put(func);
}
}
void ThreadPoolExecutor::CallerRunsPolicy::rejectedExecution(const std::function<void()>& func, ThreadPoolExecutor* e) {
if (!e->isShutdown()) {
func();
}
}
ThreadPoolExecutor::ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
std::unique_ptr<BlockingQueue<std::function<void()> > > workQueue,
std::unique_ptr<RejectedExecutionHandler> rejectHandler,
bool enableWaitComplete) :
corePoolSize(corePoolSize), maximumPoolSize(maximumPoolSize), keepAliveTime(keepAliveTime),
workQueue(std::move(workQueue)), rejectHandler(std::move(rejectHandler)),
thread_cnt(0), finished_cnt(0), stop_(false), enableWaitComplete(enableWaitComplete) {
}
ThreadPoolExecutor::~ThreadPoolExecutor() {
stop_ = true;
workQueue->close();
for (std::thread& worker : threads_)
worker.join();
}
std::function<void()> ThreadPoolExecutor::createCoreThread(const std::function<void()>& firstTask) {
return [this, firstTask] {
if (firstTask != nullptr) {
firstTask();
increaseFinishedCount();
}
while (true) {
auto task = workQueue->take();
if (stop_ || task == nullptr) {
thread_cnt--;
return;
}
task();
increaseFinishedCount();
}
};
}
std::function<void()> ThreadPoolExecutor::createTempThread(const std::function<void()>& firstTask) {
return [this, firstTask] {
if (firstTask != nullptr) {
firstTask();
increaseFinishedCount();
}
while (true) {
auto task = workQueue->poll(keepAliveTime);
if (stop_ || task == nullptr) {
thread_cnt--;
std::clog << "Temp thread exited." << std::endl;
return;
}
task();
increaseFinishedCount();
}
};
}
void ThreadPoolExecutor::waitForTaskComplete(int task_cnt) {
if (!enableWaitComplete)
throw std::invalid_argument("Wait complete is not enabled");
std::this_thread::yield();
auto task_finished = [this, task_cnt] { return finished_cnt == task_cnt && workQueue->empty(); };
while (true) {
std::unique_lock<std::mutex> lock(finish_mutex);
this->complete_condition.wait(lock, task_finished);
if (task_finished()) {
finished_cnt = 0;
return;
}
}
}
bool ThreadPoolExecutor::addWorker(bool core, const std::function<void()>& firstTask) {
while (true) {
int c_thread_num = this->thread_cnt;
if (core) {
if (c_thread_num < corePoolSize) {
if (thread_cnt.compare_exchange_weak(c_thread_num, c_thread_num + 1))
break;
} else {
return false;
}
} else {
if (c_thread_num < maximumPoolSize) {
if (thread_cnt.compare_exchange_weak(c_thread_num, c_thread_num + 1))
break;
} else {
return false;
}
}
}
if (core) {
std::thread th(createCoreThread(firstTask));
std::unique_lock lock(this->thread_lock);
this->threads_.emplace_back(std::move(th));
} else {
std::thread th(createTempThread(firstTask));
std::unique_lock lock(this->thread_lock);
this->threads_.emplace_back(std::move(th));
}
return true;
}
void ThreadPoolExecutor::enqueue(const std::function<void()>& task) {
if (thread_cnt < corePoolSize && addWorker(true, task)) {
return;
} else if (workQueue->offer(task)) {
if (thread_cnt == 0)
addWorker(false);
} else if (!addWorker(false, task)) {
reject(task);
}
}
void ThreadPoolExecutor::reject(const std::function<void()>& task) {
rejectHandler->rejectedExecution(task, this);
increaseFinishedCount();
}
void ThreadPoolExecutor::increaseFinishedCount() {
if (!enableWaitComplete) return;
std::unique_lock<std::mutex> lock(finish_mutex);
finished_cnt++;
complete_condition.notify_all();
}
bool ThreadPoolExecutor::isShutdown() const {
return this->stop_;
}
#if _WIN32
bool ThreadPoolExecutor::insidePool(DWORD thID) {
for (int i = 0; i < threads_.size(); i++) {
if (threads_[i].joinable()) {
DWORD cthId = ::GetThreadId(static_cast<HANDLE>(threads_[i].native_handle()));
if (cthId == thID) return true;
}
}
return false;
}
#endif