platform_thread_linux.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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. 4 5#include "base/threading/platform_thread.h" 6 7#include <errno.h> 8#include <sched.h> 9 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" 17 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 25 26namespace base { 27 28namespace { 29int ThreadNiceValue(ThreadPriority priority) { 30 switch (priority) { 31 case kThreadPriority_RealtimeAudio: 32 return -10; 33 case kThreadPriority_Background: 34 return 10; 35 case kThreadPriority_Normal: 36 return 0; 37 case kThreadPriority_Display: 38 return -6; 39 default: 40 NOTREACHED() << "Unknown priority."; 41 return 0; 42 } 43} 44} // namespace 45 46// static 47void PlatformThread::SetName(const char* name) { 48 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); 49 tracked_objects::ThreadData::InitializeThreadContext(name); 50 51#if !defined(OS_NACL) 52 // On linux we can get the thread names to show up in the debugger by setting 53 // the process name for the LWP. We don't want to do this for the main 54 // thread because that would rename the process, causing tools like killall 55 // to stop working. 56 if (PlatformThread::CurrentId() == getpid()) 57 return; 58 59 // http://0pointer.de/blog/projects/name-your-threads.html 60 // Set the name for the LWP (which gets truncated to 15 characters). 61 // Note that glibc also has a 'pthread_setname_np' api, but it may not be 62 // available everywhere and it's only benefit over using prctl directly is 63 // that it can set the name of threads other than the current thread. 64 int err = prctl(PR_SET_NAME, name); 65 // We expect EPERM failures in sandboxed processes, just ignore those. 66 if (err < 0 && errno != EPERM) 67 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; 68#endif // !defined(OS_NACL) 69} 70 71// static 72void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 73 ThreadPriority priority) { 74#if !defined(OS_NACL) 75 if (priority == kThreadPriority_RealtimeAudio) { 76 const struct sched_param kRealTimePrio = { 8 }; 77 if (pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0) { 78 // Got real time priority, no need to set nice level. 79 return; 80 } 81 } 82 83 // setpriority(2) will set a thread's priority if it is passed a tid as 84 // the 'process identifier', not affecting the rest of the threads in the 85 // process. Setting this priority will only succeed if the user has been 86 // granted permission to adjust nice values on the system. 87 DCHECK_NE(handle.id_, kInvalidThreadId); 88 const int kNiceSetting = ThreadNiceValue(priority); 89 if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting)) { 90 DVPLOG(1) << "Failed to set nice value of thread (" 91 << handle.id_ << ") to " << kNiceSetting; 92 } 93#endif // !defined(OS_NACL) 94} 95 96void InitThreading() {} 97 98void InitOnThread() {} 99 100void TerminateOnThread() {} 101 102size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { 103#if !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) 104 return 0; 105#else 106 // ThreadSanitizer bloats the stack heavily. Evidence has been that the 107 // default stack size isn't enough for some browser tests. 108 // MemorySanitizer needs this as a temporary fix for http://crbug.com/353687 109 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux). 110#endif 111} 112 113} // namespace base 114