Chromium Code Reviews| 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 | |
|
brettw
2011/05/12 22:26:03
This should be a complete sentence w/ capitals and
Chris Rogers
2011/05/12 23:01:55
Done.
| |
| 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 constants determining how much time we will be guaranteed. | |
| 99 // All times are in milliseconds. | |
| 100 | |
| 101 // About 128 frames @44.1KHz | |
| 102 const double kTimeQuantum = 2.9; | |
| 103 | |
| 104 // Time guaranteed each quantum. 75% Duty cycle. | |
| 105 const double kAudioTimeNeeded = 0.75 * kTimeQuantum; | |
| 106 | |
| 107 // Maximum time allowed for work. | |
| 108 const double kMaxTimeAllowed = 0.85 * kTimeQuantum; | |
|
brettw
2011/05/12 22:26:03
All of these numbers look magic. Where did you get
Chris Rogers
2011/05/12 23:01:55
I've added comments and defined more constants to
| |
| 109 | |
| 110 // Get the conversion factor from milliseconds to absolute time | |
| 111 // which is what the time-constraints call needs. | |
| 112 mach_timebase_info_data_t tb_info; | |
| 113 mach_timebase_info(&tb_info); | |
| 114 double msToAbsTime = | |
|
brettw
2011/05/12 22:26:03
Should should use lowercase_with_underscores.
Chris Rogers
2011/05/12 23:01:55
Done.
| |
| 115 ((double)tb_info.denom / (double)tb_info.numer) * 1000000; | |
| 116 | |
| 117 thread_time_constraint_policy_data_t time_constraints; | |
| 118 time_constraints.period = kTimeQuantum * msToAbsTime; | |
| 119 time_constraints.computation = kAudioTimeNeeded * msToAbsTime; | |
| 120 time_constraints.constraint = kMaxTimeAllowed * msToAbsTime; | |
| 121 time_constraints.preemptible = 0; | |
| 122 | |
| 123 result = thread_policy_set(mach_thread_id, | |
| 124 THREAD_TIME_CONSTRAINT_POLICY, | |
| 125 (thread_policy_t)&time_constraints, | |
| 126 THREAD_TIME_CONSTRAINT_POLICY_COUNT); | |
| 127 DCHECK_EQ(KERN_SUCCESS, result); | |
| 128 } | |
| 129 | |
| 130 } // anonymous namespace | |
| 131 | |
| 132 // static | |
| 133 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | |
| 134 ThreadPriority priority) { | |
| 135 // Convert from pthread_t to mach thread identifier. | |
| 136 mach_port_t mach_thread_id = pthread_mach_thread_np(handle); | |
| 137 | |
| 138 switch (priority) { | |
| 139 case kThreadPriority_Normal: | |
| 140 SetPriorityNormal(mach_thread_id); | |
| 141 break; | |
| 142 case kThreadPriority_RealtimeAudio: | |
| 143 SetPriorityRealtimeAudio(mach_thread_id); | |
| 144 break; | |
| 145 } | |
| 146 } | |
| 147 | |
| 54 } // namespace base | 148 } // namespace base |
| OLD | NEW |