OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <dlfcn.h> | 8 #include <dlfcn.h> |
| 9 #include <mach/mach.h> |
| 10 #include <mach/mach_time.h> |
| 11 #include <mach/thread_policy.h> |
9 | 12 |
10 #include "base/logging.h" | 13 #include "base/logging.h" |
11 | 14 |
12 namespace base { | 15 namespace base { |
13 | 16 |
14 // If Cocoa is to be used on more than one thread, it must know that the | 17 // If Cocoa is to be used on more than one thread, it must know that the |
15 // application is multithreaded. Since it's possible to enter Cocoa code | 18 // application is multithreaded. Since it's possible to enter Cocoa code |
16 // from threads created by pthread_thread_create, Cocoa won't necessarily | 19 // from threads created by pthread_thread_create, Cocoa won't necessarily |
17 // be aware that the application is multithreaded. Spawning an NSThread is | 20 // be aware that the application is multithreaded. Spawning an NSThread is |
18 // enough to get Cocoa to set up for multithreaded operation, so this is done | 21 // enough to get Cocoa to set up for multithreaded operation, so this is done |
(...skipping 25 matching lines...) Expand all Loading... |
44 | 47 |
45 // Mac OS X does not expose the length limit of the name, so | 48 // Mac OS X does not expose the length limit of the name, so |
46 // hardcode it. | 49 // hardcode it. |
47 const int kMaxNameLength = 63; | 50 const int kMaxNameLength = 63; |
48 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); | 51 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
49 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. | 52 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. |
50 // See http://crbug.com/47058 | 53 // See http://crbug.com/47058 |
51 dynamic_pthread_setname_np(shortened_name.c_str()); | 54 dynamic_pthread_setname_np(shortened_name.c_str()); |
52 } | 55 } |
53 | 56 |
| 57 namespace { |
| 58 |
| 59 void SetPriorityNormal(mach_port_t mach_thread_id) { |
| 60 // Make thread standard policy. |
| 61 thread_standard_policy policy; |
| 62 kern_return_t result = thread_policy_set(mach_thread_id, |
| 63 THREAD_STANDARD_POLICY, |
| 64 (thread_policy_t)&policy, |
| 65 THREAD_STANDARD_POLICY_COUNT); |
| 66 |
| 67 DCHECK_EQ(KERN_SUCCESS, result); |
| 68 } |
| 69 |
| 70 // Enables time-contraint policy and priority suitable for low-latency, |
| 71 // glitch-resistant audio. |
| 72 void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { |
| 73 kern_return_t result; |
| 74 |
| 75 // Increase thread priority to real-time. |
| 76 |
| 77 // Make thread fixed priority. |
| 78 thread_extended_policy_data_t policy; |
| 79 policy.timeshare = 0; // Set to 1 for a non-fixed thread. |
| 80 result = thread_policy_set(mach_thread_id, |
| 81 THREAD_EXTENDED_POLICY, |
| 82 (thread_policy_t)&policy, |
| 83 THREAD_EXTENDED_POLICY_COUNT); |
| 84 |
| 85 DCHECK_EQ(KERN_SUCCESS, result); |
| 86 |
| 87 // Set to relatively high priority. |
| 88 thread_precedence_policy_data_t precedence; |
| 89 precedence.importance = 63; |
| 90 result = thread_policy_set(mach_thread_id, |
| 91 THREAD_PRECEDENCE_POLICY, |
| 92 (thread_policy_t)&precedence, |
| 93 THREAD_PRECEDENCE_POLICY_COUNT); |
| 94 DCHECK_EQ(KERN_SUCCESS, result); |
| 95 |
| 96 // Most important, set real-time constraints. |
| 97 |
| 98 // Define the guaranteed and max fraction of time for the audio thread. |
| 99 // These "duty cycle" values can range from 0 to 1. A value of 0.5 |
| 100 // means the scheduler would give half the time to the thread. |
| 101 // These values have empirically been found to yield good behavior. |
| 102 // Good means that audio performance is high and other threads won't starve. |
| 103 const double kGuaranteedAudioDutyCycle = 0.75; |
| 104 const double kMaxAudioDutyCycle = 0.85; |
| 105 |
| 106 // Define constants determining how much time the audio thread can |
| 107 // use in a given time quantum. All times are in milliseconds. |
| 108 |
| 109 // About 128 frames @44.1KHz |
| 110 const double kTimeQuantum = 2.9; |
| 111 |
| 112 // Time guaranteed each quantum. |
| 113 const double kAudioTimeNeeded = kGuaranteedAudioDutyCycle * kTimeQuantum; |
| 114 |
| 115 // Maximum time each quantum. |
| 116 const double kMaxTimeAllowed = kMaxAudioDutyCycle * kTimeQuantum; |
| 117 |
| 118 // Get the conversion factor from milliseconds to absolute time |
| 119 // which is what the time-constraints call needs. |
| 120 mach_timebase_info_data_t tb_info; |
| 121 mach_timebase_info(&tb_info); |
| 122 double ms_to_abs_time = |
| 123 ((double)tb_info.denom / (double)tb_info.numer) * 1000000; |
| 124 |
| 125 thread_time_constraint_policy_data_t time_constraints; |
| 126 time_constraints.period = kTimeQuantum * ms_to_abs_time; |
| 127 time_constraints.computation = kAudioTimeNeeded * ms_to_abs_time; |
| 128 time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; |
| 129 time_constraints.preemptible = 0; |
| 130 |
| 131 result = thread_policy_set(mach_thread_id, |
| 132 THREAD_TIME_CONSTRAINT_POLICY, |
| 133 (thread_policy_t)&time_constraints, |
| 134 THREAD_TIME_CONSTRAINT_POLICY_COUNT); |
| 135 DCHECK_EQ(KERN_SUCCESS, result); |
| 136 } |
| 137 |
| 138 } // anonymous namespace |
| 139 |
| 140 // static |
| 141 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
| 142 ThreadPriority priority) { |
| 143 // Convert from pthread_t to mach thread identifier. |
| 144 mach_port_t mach_thread_id = pthread_mach_thread_np(handle); |
| 145 |
| 146 switch (priority) { |
| 147 case kThreadPriority_Normal: |
| 148 SetPriorityNormal(mach_thread_id); |
| 149 break; |
| 150 case kThreadPriority_RealtimeAudio: |
| 151 SetPriorityRealtimeAudio(mach_thread_id); |
| 152 break; |
| 153 } |
| 154 } |
| 155 |
54 } // namespace base | 156 } // namespace base |
OLD | NEW |