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