| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sched.h> | 8 #include <sched.h> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/logging.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" | 12 #include "base/threading/thread_id_name_manager.h" |
| 15 #include "base/threading/thread_restrictions.h" | |
| 16 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
| 17 | 14 |
| 18 #if !defined(OS_NACL) | 15 #if !defined(OS_NACL) |
| 19 #include <sys/resource.h> | 16 #include <pthread.h> |
| 20 #include <sys/syscall.h> | 17 #include <sys/prctl.h> |
| 21 #include <sys/time.h> | |
| 22 #include <sys/types.h> | 18 #include <sys/types.h> |
| 23 #include <unistd.h> | 19 #include <unistd.h> |
| 24 #endif | 20 #endif |
| 25 | 21 |
| 26 namespace base { | 22 namespace base { |
| 27 | 23 |
| 24 namespace internal { |
| 25 |
| 28 namespace { | 26 namespace { |
| 29 int ThreadNiceValue(ThreadPriority priority) { | 27 #if !defined(OS_NACL) |
| 30 switch (priority) { | 28 const struct sched_param kRealTimePrio = {8}; |
| 31 case kThreadPriority_RealtimeAudio: | 29 #endif |
| 32 return -10; | 30 } // namespace |
| 33 case kThreadPriority_Background: | 31 |
| 34 return 10; | 32 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { |
| 35 case kThreadPriority_Normal: | 33 { kThreadPriority_RealtimeAudio, -10 }, |
| 36 return 0; | 34 { kThreadPriority_Background, 10 }, |
| 37 case kThreadPriority_Display: | 35 { kThreadPriority_Normal, 0 }, |
| 38 return -6; | 36 { kThreadPriority_Display, -6 }, |
| 39 default: | |
| 40 NOTREACHED() << "Unknown priority."; | |
| 41 return 0; | |
| 42 } | |
| 43 } | 37 } |
| 44 } // namespace | 38 |
| 39 bool HandleSetThreadPriorityForPlatform(PlatformThreadHandle handle, |
| 40 ThreadPriority priority) { |
| 41 #if !defined(OS_NACL) |
| 42 return priority == kThreadPriority_RealtimeAudio && |
| 43 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0; |
| 44 #else |
| 45 return false; |
| 46 #endif |
| 47 } |
| 48 |
| 49 } // namespace internal |
| 45 | 50 |
| 46 // static | 51 // static |
| 47 void PlatformThread::SetName(const char* name) { | 52 void PlatformThread::SetName(const char* name) { |
| 48 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); | 53 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
| 49 tracked_objects::ThreadData::InitializeThreadContext(name); | 54 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 50 | 55 |
| 51 #if !defined(OS_NACL) | 56 #if !defined(OS_NACL) |
| 52 // On FreeBSD we can get the thread names to show up in the debugger by | 57 // 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 | 58 // 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 | 59 // main thread because that would rename the process, causing tools like |
| 55 // killall to stop working. | 60 // killall to stop working. |
| 56 if (PlatformThread::CurrentId() == getpid()) | 61 if (PlatformThread::CurrentId() == getpid()) |
| 57 return; | 62 return; |
| 58 setproctitle("%s", name); | 63 setproctitle("%s", name); |
| 59 #endif // !defined(OS_NACL) | 64 #endif // !defined(OS_NACL) |
| 60 } | 65 } |
| 61 | 66 |
| 62 // static | |
| 63 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | |
| 64 ThreadPriority priority) { | |
| 65 #if !defined(OS_NACL) | |
| 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. | |
| 70 return; | |
| 71 } | |
| 72 } | |
| 73 | |
| 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; | |
| 83 } | |
| 84 #endif // !defined(OS_NACL) | |
| 85 } | |
| 86 | |
| 87 void InitThreading() {} | 67 void InitThreading() {} |
| 88 | 68 |
| 89 void InitOnThread() {} | 69 void InitOnThread() {} |
| 90 | 70 |
| 91 void TerminateOnThread() {} | 71 void TerminateOnThread() {} |
| 92 | 72 |
| 93 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { | 73 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
| 94 #if !defined(THREAD_SANITIZER) | 74 #if !defined(THREAD_SANITIZER) |
| 95 return 0; | 75 return 0; |
| 96 #else | 76 #else |
| 97 // ThreadSanitizer bloats the stack heavily. Evidence has been that the | 77 // ThreadSanitizer bloats the stack heavily. Evidence has been that the |
| 98 // default stack size isn't enough for some browser tests. | 78 // default stack size isn't enough for some browser tests. |
| 99 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux). | 79 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux). |
| 100 #endif | 80 #endif |
| 101 } | 81 } |
| 102 | 82 |
| 103 } // namespace base | 83 } // namespace base |
| OLD | NEW |