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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/platform_thread.h"
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <memory>
#include "base/debug/activity_tracker.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/threading/platform_thread_internal_posix.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread_id_name_manager.h"
#include "build/build_config.h"
#if !defined(OS_MACOSX) && !defined(OS_FUCHSIA) && !defined(OS_NACL)
#include "base/posix/can_lower_nice_to.h"
#endif
#if defined(OS_LINUX)
#include <sys/syscall.h>
#endif
#if defined(OS_FUCHSIA)
#include <zircon/process.h>
#else
#include <sys/resource.h>
#endif
namespace base {
void InitThreading();
void TerminateOnThread();
size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes);
namespace {
struct ThreadParams {
ThreadParams()
: delegate(nullptr), joinable(false), priority(ThreadPriority::NORMAL) {}
PlatformThread::Delegate* delegate;
bool joinable;
ThreadPriority priority;
};
void* ThreadFunc(void* params) {
PlatformThread::Delegate* delegate = nullptr;
{
std::unique_ptr<ThreadParams> thread_params(
static_cast<ThreadParams*>(params));
delegate = thread_params->delegate;
if (!thread_params->joinable)
base::ThreadRestrictions::SetSingletonAllowed(false);
#if !defined(OS_NACL)
// Threads on linux/android may inherit their priority from the thread
// where they were created. This explicitly sets the priority of all new
// threads.
PlatformThread::SetCurrentThreadPriority(thread_params->priority);
#endif
}
ThreadIdNameManager::GetInstance()->RegisterThread(
PlatformThread::CurrentHandle().platform_handle(),
PlatformThread::CurrentId());
delegate->ThreadMain();
ThreadIdNameManager::GetInstance()->RemoveName(
PlatformThread::CurrentHandle().platform_handle(),
PlatformThread::CurrentId());
base::TerminateOnThread();
return nullptr;
}
bool CreateThread(size_t stack_size,
bool joinable,
PlatformThread::Delegate* delegate,
PlatformThreadHandle* thread_handle,
ThreadPriority priority) {
DCHECK(thread_handle);
base::InitThreading();
pthread_attr_t attributes;
pthread_attr_init(&attributes);
// Pthreads are joinable by default, so only specify the detached
// attribute if the thread should be non-joinable.
if (!joinable)
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
// Get a better default if available.
if (stack_size == 0)
stack_size = base::GetDefaultThreadStackSize(attributes);
if (stack_size > 0)
pthread_attr_setstacksize(&attributes, stack_size);
std::unique_ptr<ThreadParams> params(new ThreadParams);
params->delegate = delegate;
params->joinable = joinable;
params->priority = priority;
pthread_t handle;
int err = pthread_create(&handle, &attributes, ThreadFunc, params.get());
bool success = !err;
if (success) {
// ThreadParams should be deleted on the created thread after used.
ignore_result(params.release());
} else {
// Value of |handle| is undefined if pthread_create fails.
handle = 0;
errno = err;
PLOG(ERROR) << "pthread_create";
}
*thread_handle = PlatformThreadHandle(handle);
pthread_attr_destroy(&attributes);
return success;
}
#if defined(OS_LINUX)
// Store the thread ids in local storage since calling the SWI can
// expensive and PlatformThread::CurrentId is used liberally. Clear
// the stored value after a fork() because forking changes the thread
// id. Forking without going through fork() (e.g. clone()) is not
// supported, but there is no known usage. Using thread_local is
// fine here (despite being banned) since it is going to be allowed
// but is blocked on a clang bug for Mac (https://crbug.com/829078)
// and we can't use ThreadLocalStorage because of re-entrancy due to
// CHECK/DCHECKs.
thread_local pid_t g_thread_id = -1;
class InitAtFork {
public:
InitAtFork() { pthread_atfork(nullptr, nullptr, internal::ClearTidCache); }
};
#endif // defined(OS_LINUX)
} // namespace
#if defined(OS_LINUX)
namespace internal {
void ClearTidCache() {
g_thread_id = -1;
}
} // namespace internal
#endif // defined(OS_LINUX)
// static
PlatformThreadId PlatformThread::CurrentId() {
// Pthreads doesn't have the concept of a thread ID, so we have to reach down
// into the kernel.
#if defined(OS_MACOSX)
return pthread_mach_thread_np(pthread_self());
#elif defined(OS_LINUX)
static NoDestructor<InitAtFork> init_at_fork;
if (g_thread_id == -1) {
g_thread_id = syscall(__NR_gettid);
} else {
DCHECK_EQ(g_thread_id, syscall(__NR_gettid))
<< "Thread id stored in TLS is different from thread id returned by "
"the system. It is likely that the process was forked without going "
"through fork().";
}
return g_thread_id;
#elif defined(OS_ANDROID)
return gettid();
#elif defined(OS_FUCHSIA)
return zx_thread_self();
#elif defined(OS_SOLARIS) || defined(OS_QNX)
return pthread_self();
#elif defined(OS_NACL) && defined(__GLIBC__)
return pthread_self();
#elif defined(OS_NACL) && !defined(__GLIBC__)
// Pointers are 32-bits in NaCl.
return reinterpret_cast<int32_t>(pthread_self());
#elif defined(OS_POSIX) && defined(OS_AIX)
return pthread_self();
#elif defined(OS_POSIX) && !defined(OS_AIX)
return reinterpret_cast<int64_t>(pthread_self());
#endif
}
// static
PlatformThreadRef PlatformThread::CurrentRef() {
return PlatformThreadRef(pthread_self());
}
// static
PlatformThreadHandle PlatformThread::CurrentHandle() {
return PlatformThreadHandle(pthread_self());
}
// static
void PlatformThread::YieldCurrentThread() {
sched_yield();
}
// static
void PlatformThread::Sleep(TimeDelta duration) {
struct timespec sleep_time, remaining;
// Break the duration into seconds and nanoseconds.
// NOTE: TimeDelta's microseconds are int64s while timespec's
// nanoseconds are longs, so this unpacking must prevent overflow.
sleep_time.tv_sec = duration.InSeconds();
duration -= TimeDelta::FromSeconds(sleep_time.tv_sec);
sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds
while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
sleep_time = remaining;
}
// static
const char* PlatformThread::GetName() {
return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
}
// static
bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle,
ThreadPriority priority) {
return CreateThread(stack_size, true /* joinable thread */, delegate,
thread_handle, priority);
}
// static
bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
return CreateNonJoinableWithPriority(stack_size, delegate,
ThreadPriority::NORMAL);
}
// static
bool PlatformThread::CreateNonJoinableWithPriority(size_t stack_size,
Delegate* delegate,
ThreadPriority priority) {
PlatformThreadHandle unused;
bool result = CreateThread(stack_size, false /* non-joinable thread */,
delegate, &unused, priority);
return result;
}
// static
void PlatformThread::Join(PlatformThreadHandle thread_handle) {
// Record the event that this thread is blocking upon (for hang diagnosis).
base::debug::ScopedThreadJoinActivity thread_activity(&thread_handle);
// Joining another thread may block the current thread for a long time, since
// the thread referred to by |thread_handle| may still be running long-lived /
// blocking tasks.
base::internal::ScopedBlockingCallWithBaseSyncPrimitives scoped_blocking_call(
FROM_HERE, base::BlockingType::MAY_BLOCK);
CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), nullptr));
}
// static
void PlatformThread::Detach(PlatformThreadHandle thread_handle) {
CHECK_EQ(0, pthread_detach(thread_handle.platform_handle()));
}
// Mac and Fuchsia have their own Set/GetCurrentThreadPriority()
// implementations.
#if !defined(OS_MACOSX) && !defined(OS_FUCHSIA)
// static
bool PlatformThread::CanIncreaseThreadPriority(ThreadPriority priority) {
#if defined(OS_NACL)
return false;
#else
auto platform_specific_ability =
internal::CanIncreaseCurrentThreadPriorityForPlatform(priority);
if (platform_specific_ability)
return platform_specific_ability.value();
return internal::CanLowerNiceTo(
internal::ThreadPriorityToNiceValue(priority));
#endif // defined(OS_NACL)
}
// static
void PlatformThread::SetCurrentThreadPriorityImpl(ThreadPriority priority) {
#if defined(OS_NACL)
NOTIMPLEMENTED();
#else
if (internal::SetCurrentThreadPriorityForPlatform(priority))
return;
// setpriority(2) should change the whole thread group's (i.e. process)
// priority. However, as stated in the bugs section of
// http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current
// Linux/NPTL implementation of POSIX threads, the nice value is a per-thread
// attribute". Also, 0 is prefered to the current thread id since it is
// equivalent but makes sandboxing easier (https://crbug.com/399473).
const int nice_setting = internal::ThreadPriorityToNiceValue(priority);
if (setpriority(PRIO_PROCESS, 0, nice_setting)) {
DVPLOG(1) << "Failed to set nice value of thread ("
<< PlatformThread::CurrentId() << ") to " << nice_setting;
}
#endif // defined(OS_NACL)
}
// static
ThreadPriority PlatformThread::GetCurrentThreadPriority() {
#if defined(OS_NACL)
NOTIMPLEMENTED();
return ThreadPriority::NORMAL;
#else
// Mirrors SetCurrentThreadPriority()'s implementation.
auto platform_specific_priority =
internal::GetCurrentThreadPriorityForPlatform();
if (platform_specific_priority)
return platform_specific_priority.value();
// Need to clear errno before calling getpriority():
// http://man7.org/linux/man-pages/man2/getpriority.2.html
errno = 0;
int nice_value = getpriority(PRIO_PROCESS, 0);
if (errno != 0) {
DVPLOG(1) << "Failed to get nice value of thread ("
<< PlatformThread::CurrentId() << ")";
return ThreadPriority::NORMAL;
}
return internal::NiceValueToThreadPriority(nice_value);
#endif // !defined(OS_NACL)
}
#endif // !defined(OS_MACOSX) && !defined(OS_FUCHSIA)
// static
size_t PlatformThread::GetDefaultThreadStackSize() {
pthread_attr_t attributes;
pthread_attr_init(&attributes);
return base::GetDefaultThreadStackSize(attributes);
}
} // namespace base
|