Repository navigation
Expand file tree
/
Copy paththread_pool.cc
More file actions
316 lines (275 loc) · 9.55 KB
/
Copy paththread_pool.cc
File metadata and controls
316 lines (275 loc) · 9.55 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/util/thread_pool.h"
#include <algorithm>
#include <condition_variable>
#include <deque>
#include <list>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "arrow/util/io_util.h"
#include "arrow/util/logging.h"
namespace arrow {
namespace internal {
struct ThreadPool::State {
State() : desired_capacity_(0), please_shutdown_(false), quick_shutdown_(false) {}
// NOTE: in case locking becomes too expensive, we can investigate lock-free FIFOs
// such as https://github.com/cameron314/concurrentqueue
std::mutex mutex_;
std::condition_variable cv_;
std::condition_variable cv_shutdown_;
std::list<std::thread> workers_;
// Trashcan for finished threads
std::vector<std::thread> finished_workers_;
std::deque<std::function<void()>> pending_tasks_;
// Desired number of threads
int desired_capacity_;
// Are we shutting down?
bool please_shutdown_;
bool quick_shutdown_;
};
// The worker loop is an independent function so that it can keep running
// after the ThreadPool is destroyed.
static void WorkerLoop(std::shared_ptr<ThreadPool::State> state,
std::list<std::thread>::iterator it) {
std::unique_lock<std::mutex> lock(state->mutex_);
// Since we hold the lock, `it` now points to the correct thread object
// (LaunchWorkersUnlocked has exited)
DCHECK_EQ(std::this_thread::get_id(), it->get_id());
// If too many threads, we should secede from the pool
const auto should_secede = [&]() -> bool {
return state->workers_.size() > static_cast<size_t>(state->desired_capacity_);
};
while (true) {
// By the time this thread is started, some tasks may have been pushed
// or shutdown could even have been requested. So we only wait on the
// condition variable at the end of the loop.
// Execute pending tasks if any
while (!state->pending_tasks_.empty() && !state->quick_shutdown_) {
// We check this opportunistically at each loop iteration since
// it releases the lock below.
if (should_secede()) {
break;
}
{
std::function<void()> task = std::move(state->pending_tasks_.front());
state->pending_tasks_.pop_front();
lock.unlock();
task();
}
lock.lock();
}
// Now either the queue is empty *or* a quick shutdown was requested
if (state->please_shutdown_ || should_secede()) {
break;
}
// Wait for next wakeup
state->cv_.wait(lock);
}
// We're done. Move our thread object to the trashcan of finished
// workers. This has two motivations:
// 1) the thread object doesn't get destroyed before this function finishes
// (but we could call thread::detach() instead)
// 2) we can explicitly join() the trashcan threads to make sure all OS threads
// are exited before the ThreadPool is destroyed. Otherwise subtle
// timing conditions can lead to false positives with Valgrind.
DCHECK_EQ(std::this_thread::get_id(), it->get_id());
state->finished_workers_.push_back(std::move(*it));
state->workers_.erase(it);
if (state->please_shutdown_) {
// Notify the function waiting in Shutdown().
state->cv_shutdown_.notify_one();
}
}
ThreadPool::ThreadPool()
: sp_state_(std::make_shared<ThreadPool::State>()),
state_(sp_state_.get()),
shutdown_on_destroy_(true) {
#ifndef _WIN32
pid_ = getpid();
#endif
}
ThreadPool::~ThreadPool() {
if (shutdown_on_destroy_) {
ARROW_UNUSED(Shutdown(false /* wait */));
}
}
void ThreadPool::ProtectAgainstFork() {
#ifndef _WIN32
pid_t current_pid = getpid();
if (pid_ != current_pid) {
// Reinitialize internal state in child process after fork()
// Ideally we would use pthread_at_fork(), but that doesn't allow
// storing an argument, hence we'd need to maintain a list of all
// existing ThreadPools.
int capacity = state_->desired_capacity_;
auto new_state = std::make_shared<ThreadPool::State>();
new_state->please_shutdown_ = state_->please_shutdown_;
new_state->quick_shutdown_ = state_->quick_shutdown_;
pid_ = current_pid;
sp_state_ = new_state;
state_ = sp_state_.get();
// Launch worker threads anew
if (!state_->please_shutdown_) {
ARROW_UNUSED(SetCapacity(capacity));
}
}
#endif
}
Status ThreadPool::SetCapacity(int threads) {
ProtectAgainstFork();
std::unique_lock<std::mutex> lock(state_->mutex_);
if (state_->please_shutdown_) {
return Status::Invalid("operation forbidden during or after shutdown");
}
if (threads <= 0) {
return Status::Invalid("ThreadPool capacity must be > 0");
}
CollectFinishedWorkersUnlocked();
state_->desired_capacity_ = threads;
int diff = static_cast<int>(threads - state_->workers_.size());
if (diff > 0) {
LaunchWorkersUnlocked(diff);
} else if (diff < 0) {
// Wake threads to ask them to stop
state_->cv_.notify_all();
}
return Status::OK();
}
int ThreadPool::GetCapacity() {
ProtectAgainstFork();
std::unique_lock<std::mutex> lock(state_->mutex_);
return state_->desired_capacity_;
}
int ThreadPool::GetActualCapacity() {
ProtectAgainstFork();
std::unique_lock<std::mutex> lock(state_->mutex_);
return static_cast<int>(state_->workers_.size());
}
Status ThreadPool::Shutdown(bool wait) {
ProtectAgainstFork();
std::unique_lock<std::mutex> lock(state_->mutex_);
if (state_->please_shutdown_) {
return Status::Invalid("Shutdown() already called");
}
state_->please_shutdown_ = true;
state_->quick_shutdown_ = !wait;
state_->cv_.notify_all();
state_->cv_shutdown_.wait(lock, [this] { return state_->workers_.empty(); });
if (!state_->quick_shutdown_) {
DCHECK_EQ(state_->pending_tasks_.size(), 0);
} else {
state_->pending_tasks_.clear();
}
CollectFinishedWorkersUnlocked();
return Status::OK();
}
void ThreadPool::CollectFinishedWorkersUnlocked() {
for (auto& thread : state_->finished_workers_) {
// Make sure OS thread has exited
thread.join();
}
state_->finished_workers_.clear();
}
void ThreadPool::LaunchWorkersUnlocked(int threads) {
std::shared_ptr<State> state = sp_state_;
for (int i = 0; i < threads; i++) {
state_->workers_.emplace_back();
auto it = --(state_->workers_.end());
*it = std::thread([state, it] { WorkerLoop(state, it); });
}
}
Status ThreadPool::SpawnReal(std::function<void()> task) {
{
ProtectAgainstFork();
std::lock_guard<std::mutex> lock(state_->mutex_);
if (state_->please_shutdown_) {
return Status::Invalid("operation forbidden during or after shutdown");
}
CollectFinishedWorkersUnlocked();
state_->pending_tasks_.push_back(std::move(task));
}
state_->cv_.notify_one();
return Status::OK();
}
Status ThreadPool::Make(int threads, std::shared_ptr<ThreadPool>* out) {
auto pool = std::shared_ptr<ThreadPool>(new ThreadPool());
RETURN_NOT_OK(pool->SetCapacity(threads));
*out = std::move(pool);
return Status::OK();
}
// ----------------------------------------------------------------------
// Global thread pool
static int ParseOMPEnvVar(const char* name) {
// OMP_NUM_THREADS is a comma-separated list of positive integers.
// We are only interested in the first (top-level) number.
std::string str;
if (!GetEnvVar(name, &str).ok()) {
return 0;
}
auto first_comma = str.find_first_of(',');
if (first_comma != std::string::npos) {
str = str.substr(0, first_comma);
}
try {
return std::max(0, std::stoi(str));
} catch (...) {
return 0;
}
}
int ThreadPool::DefaultCapacity() {
int capacity, limit;
capacity = ParseOMPEnvVar("OMP_NUM_THREADS");
if (capacity == 0) {
capacity = std::thread::hardware_concurrency();
}
limit = ParseOMPEnvVar("OMP_THREAD_LIMIT");
if (limit > 0) {
capacity = std::min(limit, capacity);
}
if (capacity == 0) {
ARROW_LOG(WARNING) << "Failed to determine the number of available threads, "
"using a hardcoded arbitrary value";
capacity = 4;
}
return capacity;
}
// Helper for the singleton pattern
std::shared_ptr<ThreadPool> ThreadPool::MakeCpuThreadPool() {
std::shared_ptr<ThreadPool> pool;
ARROW_CHECK_OK(ThreadPool::Make(ThreadPool::DefaultCapacity(), &pool));
// On Windows, the global ThreadPool destructor may be called after
// non-main threads have been killed by the OS, and hang in a condition
// variable.
// On Unix, we want to avoid leak reports by Valgrind.
#ifdef _WIN32
pool->shutdown_on_destroy_ = false;
#endif
return pool;
}
ThreadPool* GetCpuThreadPool() {
static std::shared_ptr<ThreadPool> singleton = ThreadPool::MakeCpuThreadPool();
return singleton.get();
}
} // namespace internal
int GetCpuThreadPoolCapacity() { return internal::GetCpuThreadPool()->GetCapacity(); }
Status SetCpuThreadPoolCapacity(int threads) {
return internal::GetCpuThreadPool()->SetCapacity(threads);
}
} // namespace arrow