Roll breakpad a513e85:7caf028 (svn 1384:1385)
[chromium-blink-merge.git] / base / threading / platform_thread_linux.cc
blobd9e2bd9932f5d107117eacd894d83334a9098d92
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/threading/platform_thread.h"
7 #include <errno.h>
8 #include <sched.h>
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/safe_strerror_posix.h"
14 #include "base/threading/thread_id_name_manager.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/tracked_objects.h"
18 #if !defined(OS_NACL)
19 #include <sys/prctl.h>
20 #include <sys/resource.h>
21 #include <sys/syscall.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #endif
26 namespace base {
28 namespace {
30 int ThreadNiceValue(ThreadPriority priority) {
31 switch (priority) {
32 case kThreadPriority_RealtimeAudio:
33 return -10;
34 case kThreadPriority_Background:
35 return 10;
36 case kThreadPriority_Normal:
37 return 0;
38 case kThreadPriority_Display:
39 return -6;
40 default:
41 NOTREACHED() << "Unknown priority.";
42 return 0;
46 } // namespace
48 // static
49 void PlatformThread::SetName(const char* name) {
50 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
51 tracked_objects::ThreadData::InitializeThreadContext(name);
53 #if !defined(OS_NACL)
54 // On linux we can get the thread names to show up in the debugger by setting
55 // the process name for the LWP. We don't want to do this for the main
56 // thread because that would rename the process, causing tools like killall
57 // to stop working.
58 if (PlatformThread::CurrentId() == getpid())
59 return;
61 // http://0pointer.de/blog/projects/name-your-threads.html
62 // Set the name for the LWP (which gets truncated to 15 characters).
63 // Note that glibc also has a 'pthread_setname_np' api, but it may not be
64 // available everywhere and it's only benefit over using prctl directly is
65 // that it can set the name of threads other than the current thread.
66 int err = prctl(PR_SET_NAME, name);
67 // We expect EPERM failures in sandboxed processes, just ignore those.
68 if (err < 0 && errno != EPERM)
69 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
70 #endif // !defined(OS_NACL)
73 // static
74 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
75 ThreadPriority priority) {
76 #if !defined(OS_NACL)
77 if (priority == kThreadPriority_RealtimeAudio) {
78 const struct sched_param kRealTimePrio = {8};
79 if (pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0) {
80 // Got real time priority, no need to set nice level.
81 return;
85 // setpriority(2) should change the whole thread group's (i.e. process)
86 // priority. however, on linux it will only change the target thread's
87 // priority. see the bugs section in
88 // http://man7.org/linux/man-pages/man2/getpriority.2.html.
89 // we prefer using 0 rather than the current thread id since they are
90 // equivalent but it makes sandboxing easier (https://crbug.com/399473).
91 DCHECK_NE(handle.id_, kInvalidThreadId);
92 const int kNiceSetting = ThreadNiceValue(priority);
93 const PlatformThreadId current_id = PlatformThread::CurrentId();
94 if (setpriority(PRIO_PROCESS,
95 handle.id_ == current_id ? 0 : handle.id_,
96 kNiceSetting)) {
97 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to "
98 << kNiceSetting;
100 #endif // !defined(OS_NACL)
103 void InitThreading() {}
105 void InitOnThread() {}
107 void TerminateOnThread() {}
109 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
110 #if !defined(THREAD_SANITIZER)
111 return 0;
112 #else
113 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
114 // default stack size isn't enough for some browser tests.
115 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
116 #endif
119 } // namespace base