-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathPosixThreadFactory.cpp
More file actions
379 lines (322 loc) · 10.5 KB
/
Copy pathPosixThreadFactory.cpp
File metadata and controls
379 lines (322 loc) · 10.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed 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 <thrift/lib/cpp/concurrency/PosixThreadFactory.h>
#include <cassert>
#include <ctime>
#include <iostream>
#include <glog/logging.h>
#include <folly/String.h>
#include <folly/portability/PThread.h>
#include <folly/portability/SysResource.h>
#include <folly/system/ThreadId.h>
#include <folly/system/ThreadName.h>
#include <thrift/lib/cpp/concurrency/Exception.h>
#include <thrift/lib/cpp/thrift_config.h>
namespace apache::thrift::concurrency {
using std::shared_ptr;
using std::weak_ptr;
const PosixThreadFactory::POLICY PosixThreadFactory::kDefaultPolicy;
const PosixThreadFactory::THREAD_PRIORITY PosixThreadFactory::kDefaultPriority;
// push our given name upstream into pthreads
bool PthreadThread::updateName() {
if (!pthread_ || name_.empty()) {
return false;
}
return folly::setThreadName(pthread_, name_);
}
PthreadThread::PthreadThread(
int policy,
int priority,
std::optional<int> stackSize,
bool detached,
shared_ptr<Runnable> runnable)
: pthread_(),
state_(uninitialized),
policy_(policy),
priority_(priority),
stackSize_(stackSize),
detached_(detached) {
this->Thread::runnable(runnable);
}
PthreadThread::~PthreadThread() {
/* Nothing references this thread, if it is not detached, do a join
now, otherwise the thread-id and, possibly, other resources will
be leaked. */
if (!detached_) {
try {
join();
} catch (...) {
// We're really hosed.
}
}
}
void PthreadThread::start() {
std::unique_lock<std::mutex> g(stateLock_);
if (state_ != uninitialized) {
return;
}
pthread_attr_t thread_attr;
if (pthread_attr_init(&thread_attr) != 0) {
throw SystemResourceException("pthread_attr_init failed");
}
if (pthread_attr_setdetachstate(
&thread_attr,
detached_ ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE) != 0) {
throw SystemResourceException("pthread_attr_setdetachstate failed");
}
// Set thread stack size
if (stackSize_ &&
pthread_attr_setstacksize(&thread_attr, MB * *stackSize_) != 0) {
throw SystemResourceException("pthread_attr_setstacksize failed");
}
// Create reference
shared_ptr<PthreadThread>* selfRef = new shared_ptr<PthreadThread>();
*selfRef = self_.lock();
state_ = starting;
if (pthread_create(&pthread_, &thread_attr, threadMain, (void*)selfRef) !=
0) {
throw SystemResourceException("pthread_create failed");
}
// Now that we have a thread, we can set the name we've been given, if any.
updateName();
}
void PthreadThread::join() {
std::unique_lock<std::mutex> g(stateLock_);
STATE join_state = state_;
if (!detached_ && join_state != uninitialized) {
void* ignore;
/* XXX
If join fails it is most likely due to the fact
that the last reference was the thread itself and cannot
join. This results in leaked threads and will eventually
cause the process to run out of thread resources.
We're beyond the point of throwing an exception. Not clear how
best to handle this. */
int res = pthread_join(pthread_, &ignore);
detached_ = (res == 0);
if (res != 0) {
LOG(ERROR) << "PthreadThread::join(): fail with code " << res;
}
} else {
LOG(ERROR) << "PthreadThread::join(): detached thread";
}
}
Thread::id_t PthreadThread::getId() {
#ifdef _WIN32
return (Thread::id_t)pthread_getw32threadid_np(pthread_);
#else
return (Thread::id_t)pthread_;
#endif
}
shared_ptr<Runnable> PthreadThread::runnable() const {
return Thread::runnable();
}
void PthreadThread::runnable(shared_ptr<Runnable> value) {
Thread::runnable(value);
}
void PthreadThread::weakRef(shared_ptr<PthreadThread> self) {
assert(self.get() == this);
self_ = weak_ptr<PthreadThread>(self);
}
bool PthreadThread::setName(const std::string& name) {
std::unique_lock<std::mutex> g(stateLock_);
name_ = name;
return updateName();
}
std::chrono::nanoseconds PthreadThread::usedCpuTime() const {
timespec tp{};
#ifdef __linux__
clockid_t clockid;
if (!pthread_getcpuclockid(pthread_, &clockid)) {
clock_gettime(clockid, &tp);
}
#endif
return std::chrono::nanoseconds(tp.tv_nsec) + std::chrono::seconds(tp.tv_sec);
}
void* PthreadThread::threadMain(void* arg) {
shared_ptr<PthreadThread> thread = *(shared_ptr<PthreadThread>*)arg;
delete reinterpret_cast<shared_ptr<PthreadThread>*>(arg);
if (thread == nullptr) {
return (void*)nullptr;
}
// Using pthread_attr_setschedparam() at thread creation doesn't actually
// change the new thread's priority for some reason... Other people on the
// 'net also complain about it. The solution is to set priority inside the
// new thread's threadMain.
if (thread->policy_ == SCHED_FIFO || thread->policy_ == SCHED_RR) {
struct sched_param sched_param;
sched_param.sched_priority = thread->priority_;
int err =
pthread_setschedparam(pthread_self(), thread->policy_, &sched_param);
if (err != 0) {
VLOG(1) << "pthread_setschedparam failed (are you root?) with error "
<< err << ": " << folly::errnoStr(err);
}
} else if (thread->policy_ == SCHED_OTHER) {
#ifndef _MSC_VER
if (setpriority(PRIO_PROCESS, 0, thread->priority_) != 0) {
VLOG(1) << "setpriority failed (are you root?) with error " << errno
<< ": " << folly::errnoStr(errno);
}
#endif
}
thread->runnable()->run();
return (void*)nullptr;
}
int PosixThreadFactory::Impl::toPthreadPolicy(POLICY policy) {
switch (policy) {
case OTHER:
return SCHED_OTHER;
case FIFO:
return SCHED_FIFO;
case ROUND_ROBIN:
return SCHED_RR;
}
return SCHED_OTHER;
}
/* static */
int PosixThreadFactory::Impl::toPthreadPriority(
POLICY policy, THREAD_PRIORITY priority) {
int pthread_policy = toPthreadPolicy(policy);
int min_priority = 0;
int max_priority = 0;
if (pthread_policy == SCHED_FIFO || pthread_policy == SCHED_RR) {
#ifdef THRIFT_HAVE_SCHED_GET_PRIORITY_MIN
min_priority = sched_get_priority_min(pthread_policy);
#endif
#ifdef THRIFT_HAVE_SCHED_GET_PRIORITY_MAX
max_priority = sched_get_priority_max(pthread_policy);
#endif
} else if (pthread_policy == SCHED_OTHER) {
min_priority = 19;
max_priority = -20;
}
int quanta = HIGHEST_PRI - LOWEST_PRI;
float stepsperquanta =
static_cast<float>(max_priority - min_priority) / quanta;
#ifdef _MSC_VER
return static_cast<int>(
min_priority + stepsperquanta * folly::to_underlying(priority));
#else
if (priority >= LOWEST_PRI && priority <= HIGHEST_PRI) {
return static_cast<int>(
min_priority + stepsperquanta * folly::to_underlying(priority));
} else if (priority == INHERITED_PRI && pthread_policy == SCHED_OTHER) {
errno = 0;
int prio = getpriority(PRIO_PROCESS, 0);
if (prio == -1 && errno != 0) {
PLOG(WARNING) << "getpriority failed";
} else {
return prio;
}
} else {
// Should never get here.
assert(false);
}
return static_cast<int>(
min_priority + stepsperquanta * folly::to_underlying(NORMAL));
#endif
}
PosixThreadFactory::Impl::Impl(
POLICY policy,
THREAD_PRIORITY priority,
std::optional<int> stackSize,
DetachState detached)
: policy_(policy),
priority_(priority),
stackSize_(stackSize),
detached_(detached) {}
shared_ptr<Thread> PosixThreadFactory::Impl::newThread(
const shared_ptr<Runnable>& runnable,
PosixThreadFactory::DetachState detachState) const {
shared_ptr<PthreadThread> result =
shared_ptr<PthreadThread>(new PthreadThread(
toPthreadPolicy(policy_),
toPthreadPriority(policy_, priority_),
stackSize_,
detachState == DETACHED,
runnable));
result->weakRef(result);
runnable->thread(result);
return result;
}
void PosixThreadFactory::Impl::setStackSize(int value) {
stackSize_ = value;
}
PosixThreadFactory::THREAD_PRIORITY PosixThreadFactory::Impl::getPriority()
const {
return priority_;
}
/**
* Sets priority.
*
* XXX
* Need to handle incremental priorities properly.
*/
void PosixThreadFactory::Impl::setPriority(THREAD_PRIORITY value) {
priority_ = value;
}
PosixThreadFactory::DetachState PosixThreadFactory::Impl::getDetachState()
const {
return detached_;
}
void PosixThreadFactory::Impl::setDetachState(DetachState value) {
detached_ = value;
}
Thread::id_t PosixThreadFactory::Impl::getCurrentThreadId() const {
return (Thread::id_t)folly::getCurrentThreadID();
}
PosixThreadFactory::PosixThreadFactory(
POLICY policy,
THREAD_PRIORITY priority,
std::optional<int> stackSize,
bool detached)
: impl_(new PosixThreadFactory::Impl(
policy, priority, stackSize, detached ? DETACHED : ATTACHED)) {}
PosixThreadFactory::PosixThreadFactory(DetachState detached)
: impl_(new PosixThreadFactory::Impl(
kDefaultPolicy, kDefaultPriority, std::nullopt, detached)) {}
shared_ptr<Thread> PosixThreadFactory::newThread(
const shared_ptr<Runnable>& runnable) const {
return impl_->newThread(runnable, impl_->getDetachState());
}
shared_ptr<Thread> PosixThreadFactory::newThread(
const shared_ptr<Runnable>& runnable, DetachState detachState) const {
return impl_->newThread(runnable, detachState);
}
void PosixThreadFactory::setStackSize(int value) {
impl_->setStackSize(value);
}
PosixThreadFactory::THREAD_PRIORITY PosixThreadFactory::getPriority() const {
return impl_->getPriority();
}
void PosixThreadFactory::setPriority(
PosixThreadFactory::THREAD_PRIORITY value) {
impl_->setPriority(value);
}
bool PosixThreadFactory::isDetached() const {
return impl_->getDetachState() == DETACHED;
}
void PosixThreadFactory::setDetached(bool value) {
impl_->setDetachState(value ? DETACHED : ATTACHED);
}
void PosixThreadFactory::setDetached(DetachState value) {
impl_->setDetachState(value);
}
Thread::id_t PosixThreadFactory::getCurrentThreadId() const {
return impl_->getCurrentThreadId();
}
} // namespace apache::thrift::concurrency