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 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <mach/mach.h> | 8 #include <mach/mach.h> |
9 #include <mach/mach_time.h> | 9 #include <mach/mach_time.h> |
10 #include <mach/thread_policy.h> | 10 #include <mach/thread_policy.h> |
11 #include <sys/resource.h> | 11 #include <sys/resource.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/mac/mach_logging.h" |
17 #include "base/threading/thread_id_name_manager.h" | 18 #include "base/threading/thread_id_name_manager.h" |
18 #include "base/tracked_objects.h" | 19 #include "base/tracked_objects.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
22 // If Cocoa is to be used on more than one thread, it must know that the | 23 // If Cocoa is to be used on more than one thread, it must know that the |
23 // application is multithreaded. Since it's possible to enter Cocoa code | 24 // application is multithreaded. Since it's possible to enter Cocoa code |
24 // from threads created by pthread_thread_create, Cocoa won't necessarily | 25 // from threads created by pthread_thread_create, Cocoa won't necessarily |
25 // be aware that the application is multithreaded. Spawning an NSThread is | 26 // be aware that the application is multithreaded. Spawning an NSThread is |
26 // enough to get Cocoa to set up for multithreaded operation, so this is done | 27 // enough to get Cocoa to set up for multithreaded operation, so this is done |
(...skipping 27 matching lines...) Expand all Loading... |
54 pthread_setname_np(shortened_name.c_str()); | 55 pthread_setname_np(shortened_name.c_str()); |
55 } | 56 } |
56 | 57 |
57 namespace { | 58 namespace { |
58 | 59 |
59 void SetPriorityNormal(mach_port_t mach_thread_id) { | 60 void SetPriorityNormal(mach_port_t mach_thread_id) { |
60 // Make thread standard policy. | 61 // Make thread standard policy. |
61 // Please note that this call could fail in rare cases depending | 62 // Please note that this call could fail in rare cases depending |
62 // on runtime conditions. | 63 // on runtime conditions. |
63 thread_standard_policy policy; | 64 thread_standard_policy policy; |
64 kern_return_t result = thread_policy_set(mach_thread_id, | 65 kern_return_t result = |
65 THREAD_STANDARD_POLICY, | 66 thread_policy_set(mach_thread_id, |
66 (thread_policy_t)&policy, | 67 THREAD_STANDARD_POLICY, |
67 THREAD_STANDARD_POLICY_COUNT); | 68 reinterpret_cast<thread_policy_t>(&policy), |
| 69 THREAD_STANDARD_POLICY_COUNT); |
68 | 70 |
69 if (result != KERN_SUCCESS) | 71 if (result != KERN_SUCCESS) |
70 DVLOG(1) << "thread_policy_set() failure: " << result; | 72 MACH_DVLOG(1, result) << "thread_policy_set"; |
71 } | 73 } |
72 | 74 |
73 // Enables time-contraint policy and priority suitable for low-latency, | 75 // Enables time-contraint policy and priority suitable for low-latency, |
74 // glitch-resistant audio. | 76 // glitch-resistant audio. |
75 void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { | 77 void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { |
76 kern_return_t result; | |
77 | |
78 // Increase thread priority to real-time. | 78 // Increase thread priority to real-time. |
79 | 79 |
80 // Please note that the thread_policy_set() calls may fail in | 80 // Please note that the thread_policy_set() calls may fail in |
81 // rare cases if the kernel decides the system is under heavy load | 81 // rare cases if the kernel decides the system is under heavy load |
82 // and is unable to handle boosting the thread priority. | 82 // and is unable to handle boosting the thread priority. |
83 // In these cases we just return early and go on with life. | 83 // In these cases we just return early and go on with life. |
84 | 84 |
85 // Make thread fixed priority. | 85 // Make thread fixed priority. |
86 thread_extended_policy_data_t policy; | 86 thread_extended_policy_data_t policy; |
87 policy.timeshare = 0; // Set to 1 for a non-fixed thread. | 87 policy.timeshare = 0; // Set to 1 for a non-fixed thread. |
88 result = thread_policy_set(mach_thread_id, | 88 kern_return_t result = |
89 THREAD_EXTENDED_POLICY, | 89 thread_policy_set(mach_thread_id, |
90 (thread_policy_t)&policy, | 90 THREAD_EXTENDED_POLICY, |
91 THREAD_EXTENDED_POLICY_COUNT); | 91 reinterpret_cast<thread_policy_t>(&policy), |
| 92 THREAD_EXTENDED_POLICY_COUNT); |
92 if (result != KERN_SUCCESS) { | 93 if (result != KERN_SUCCESS) { |
93 DVLOG(1) << "thread_policy_set() failure: " << result; | 94 MACH_DVLOG(1, result) << "thread_policy_set"; |
94 return; | 95 return; |
95 } | 96 } |
96 | 97 |
97 // Set to relatively high priority. | 98 // Set to relatively high priority. |
98 thread_precedence_policy_data_t precedence; | 99 thread_precedence_policy_data_t precedence; |
99 precedence.importance = 63; | 100 precedence.importance = 63; |
100 result = thread_policy_set(mach_thread_id, | 101 result = thread_policy_set(mach_thread_id, |
101 THREAD_PRECEDENCE_POLICY, | 102 THREAD_PRECEDENCE_POLICY, |
102 (thread_policy_t)&precedence, | 103 reinterpret_cast<thread_policy_t>(&precedence), |
103 THREAD_PRECEDENCE_POLICY_COUNT); | 104 THREAD_PRECEDENCE_POLICY_COUNT); |
104 if (result != KERN_SUCCESS) { | 105 if (result != KERN_SUCCESS) { |
105 DVLOG(1) << "thread_policy_set() failure: " << result; | 106 MACH_DVLOG(1, result) << "thread_policy_set"; |
106 return; | 107 return; |
107 } | 108 } |
108 | 109 |
109 // Most important, set real-time constraints. | 110 // Most important, set real-time constraints. |
110 | 111 |
111 // Define the guaranteed and max fraction of time for the audio thread. | 112 // Define the guaranteed and max fraction of time for the audio thread. |
112 // These "duty cycle" values can range from 0 to 1. A value of 0.5 | 113 // These "duty cycle" values can range from 0 to 1. A value of 0.5 |
113 // means the scheduler would give half the time to the thread. | 114 // means the scheduler would give half the time to the thread. |
114 // These values have empirically been found to yield good behavior. | 115 // These values have empirically been found to yield good behavior. |
115 // Good means that audio performance is high and other threads won't starve. | 116 // Good means that audio performance is high and other threads won't starve. |
(...skipping 10 matching lines...) Expand all Loading... |
126 const double kAudioTimeNeeded = kGuaranteedAudioDutyCycle * kTimeQuantum; | 127 const double kAudioTimeNeeded = kGuaranteedAudioDutyCycle * kTimeQuantum; |
127 | 128 |
128 // Maximum time each quantum. | 129 // Maximum time each quantum. |
129 const double kMaxTimeAllowed = kMaxAudioDutyCycle * kTimeQuantum; | 130 const double kMaxTimeAllowed = kMaxAudioDutyCycle * kTimeQuantum; |
130 | 131 |
131 // Get the conversion factor from milliseconds to absolute time | 132 // Get the conversion factor from milliseconds to absolute time |
132 // which is what the time-constraints call needs. | 133 // which is what the time-constraints call needs. |
133 mach_timebase_info_data_t tb_info; | 134 mach_timebase_info_data_t tb_info; |
134 mach_timebase_info(&tb_info); | 135 mach_timebase_info(&tb_info); |
135 double ms_to_abs_time = | 136 double ms_to_abs_time = |
136 ((double)tb_info.denom / (double)tb_info.numer) * 1000000; | 137 (static_cast<double>(tb_info.denom) / tb_info.numer) * 1000000; |
137 | 138 |
138 thread_time_constraint_policy_data_t time_constraints; | 139 thread_time_constraint_policy_data_t time_constraints; |
139 time_constraints.period = kTimeQuantum * ms_to_abs_time; | 140 time_constraints.period = kTimeQuantum * ms_to_abs_time; |
140 time_constraints.computation = kAudioTimeNeeded * ms_to_abs_time; | 141 time_constraints.computation = kAudioTimeNeeded * ms_to_abs_time; |
141 time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; | 142 time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; |
142 time_constraints.preemptible = 0; | 143 time_constraints.preemptible = 0; |
143 | 144 |
144 result = thread_policy_set(mach_thread_id, | 145 result = |
145 THREAD_TIME_CONSTRAINT_POLICY, | 146 thread_policy_set(mach_thread_id, |
146 (thread_policy_t)&time_constraints, | 147 THREAD_TIME_CONSTRAINT_POLICY, |
147 THREAD_TIME_CONSTRAINT_POLICY_COUNT); | 148 reinterpret_cast<thread_policy_t>(&time_constraints), |
148 if (result != KERN_SUCCESS) | 149 THREAD_TIME_CONSTRAINT_POLICY_COUNT); |
149 DVLOG(1) << "thread_policy_set() failure: " << result; | 150 MACH_DVLOG_IF(1, result != KERN_SUCCESS, result) << "thread_policy_set"; |
150 | 151 |
151 return; | 152 return; |
152 } | 153 } |
153 | 154 |
154 } // anonymous namespace | 155 } // anonymous namespace |
155 | 156 |
156 // static | 157 // static |
157 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 158 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
158 ThreadPriority priority) { | 159 ThreadPriority priority) { |
159 // Convert from pthread_t to mach thread identifier. | 160 // Convert from pthread_t to mach thread identifier. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 #endif | 208 #endif |
208 } | 209 } |
209 | 210 |
210 void InitOnThread() { | 211 void InitOnThread() { |
211 } | 212 } |
212 | 213 |
213 void TerminateOnThread() { | 214 void TerminateOnThread() { |
214 } | 215 } |
215 | 216 |
216 } // namespace base | 217 } // namespace base |
OLD | NEW |