1 // Copyright 2014 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"
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"
19 #include <sys/resource.h>
20 #include <sys/syscall.h>
22 #include <sys/types.h>
29 int ThreadNiceValue(ThreadPriority priority
) {
31 case kThreadPriority_RealtimeAudio
:
33 case kThreadPriority_Background
:
35 case kThreadPriority_Normal
:
37 case kThreadPriority_Display
:
40 NOTREACHED() << "Unknown priority.";
47 void PlatformThread::SetName(const char* name
) {
48 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name
);
49 tracked_objects::ThreadData::InitializeThreadContext(name
);
52 // On FreeBSD we can get the thread names to show up in the debugger by
53 // setting the process name for the LWP. We don't want to do this for the
54 // main thread because that would rename the process, causing tools like
55 // killall to stop working.
56 if (PlatformThread::CurrentId() == getpid())
58 setproctitle("%s", name
);
59 #endif // !defined(OS_NACL)
63 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle
,
64 ThreadPriority priority
) {
66 if (priority
== kThreadPriority_RealtimeAudio
) {
67 const struct sched_param kRealTimePrio
= { 8 };
68 if (pthread_setschedparam(pthread_self(), SCHED_RR
, &kRealTimePrio
) == 0) {
69 // Got real time priority, no need to set nice level.
74 // setpriority(2) will set a thread's priority if it is passed a tid as
75 // the 'process identifier', not affecting the rest of the threads in the
76 // process. Setting this priority will only succeed if the user has been
77 // granted permission to adjust nice values on the system.
78 DCHECK_NE(handle
.id_
, kInvalidThreadId
);
79 const int kNiceSetting
= ThreadNiceValue(priority
);
80 if (setpriority(PRIO_PROCESS
, handle
.id_
, kNiceSetting
)) {
81 DVPLOG(1) << "Failed to set nice value of thread ("
82 << handle
.id_
<< ") to " << kNiceSetting
;
84 #endif // !defined(OS_NACL)
87 void InitThreading() {}
89 void InitOnThread() {}
91 void TerminateOnThread() {}
93 size_t GetDefaultThreadStackSize(const pthread_attr_t
& attributes
) {
94 #if !defined(THREAD_SANITIZER)
97 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
98 // default stack size isn't enough for some browser tests.
99 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).