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