Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: base/threading/platform_thread_mac.mm

Issue 7789007: Don't DCHECK thread_policy_set() calls because they can fail during runtime (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 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> 9 #include <mach/mach.h>
10 #include <mach/mach_time.h> 10 #include <mach/mach_time.h>
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 61
62 // static 62 // static
63 const char* PlatformThread::GetName() { 63 const char* PlatformThread::GetName() {
64 return current_thread_name.Get(); 64 return current_thread_name.Get();
65 } 65 }
66 66
67 namespace { 67 namespace {
68 68
69 void SetPriorityNormal(mach_port_t mach_thread_id) { 69 void SetPriorityNormal(mach_port_t mach_thread_id) {
70 // Make thread standard policy. 70 // Make thread standard policy.
71 // Please note that this call could fail in rare cases depending
72 // on runtime conditions.
71 thread_standard_policy policy; 73 thread_standard_policy policy;
72 kern_return_t result = thread_policy_set(mach_thread_id, 74 kern_return_t result = thread_policy_set(mach_thread_id,
73 THREAD_STANDARD_POLICY, 75 THREAD_STANDARD_POLICY,
74 (thread_policy_t)&policy, 76 (thread_policy_t)&policy,
75 THREAD_STANDARD_POLICY_COUNT); 77 THREAD_STANDARD_POLICY_COUNT);
76 78
77 DCHECK_EQ(KERN_SUCCESS, result); 79 if (result != KERN_SUCCESS)
80 VLOG(1) << "thread_policy_set() failure: " << result;
78 } 81 }
79 82
80 // Enables time-contraint policy and priority suitable for low-latency, 83 // Enables time-contraint policy and priority suitable for low-latency,
81 // glitch-resistant audio. 84 // glitch-resistant audio.
82 void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { 85 void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) {
83 kern_return_t result; 86 kern_return_t result;
84 87
85 // Increase thread priority to real-time. 88 // Increase thread priority to real-time.
86 89
90 // Please note that the thread_policy_set() calls may fail in
91 // rare cases if the kernel decides the system is under heavy load
92 // and is unable to handle boosting the thread priority.
93 // In these cases we just return early and go on with life.
94
87 // Make thread fixed priority. 95 // Make thread fixed priority.
88 thread_extended_policy_data_t policy; 96 thread_extended_policy_data_t policy;
89 policy.timeshare = 0; // Set to 1 for a non-fixed thread. 97 policy.timeshare = 0; // Set to 1 for a non-fixed thread.
90 result = thread_policy_set(mach_thread_id, 98 result = thread_policy_set(mach_thread_id,
91 THREAD_EXTENDED_POLICY, 99 THREAD_EXTENDED_POLICY,
92 (thread_policy_t)&policy, 100 (thread_policy_t)&policy,
93 THREAD_EXTENDED_POLICY_COUNT); 101 THREAD_EXTENDED_POLICY_COUNT);
94 102 if (result != KERN_SUCCESS) {
95 DCHECK_EQ(KERN_SUCCESS, result); 103 VLOG(1) << "thread_policy_set() failure: " << result;
104 return;
105 }
96 106
97 // Set to relatively high priority. 107 // Set to relatively high priority.
98 thread_precedence_policy_data_t precedence; 108 thread_precedence_policy_data_t precedence;
99 precedence.importance = 63; 109 precedence.importance = 63;
100 result = thread_policy_set(mach_thread_id, 110 result = thread_policy_set(mach_thread_id,
101 THREAD_PRECEDENCE_POLICY, 111 THREAD_PRECEDENCE_POLICY,
102 (thread_policy_t)&precedence, 112 (thread_policy_t)&precedence,
103 THREAD_PRECEDENCE_POLICY_COUNT); 113 THREAD_PRECEDENCE_POLICY_COUNT);
104 DCHECK_EQ(KERN_SUCCESS, result); 114 if (result != KERN_SUCCESS) {
115 VLOG(1) << "thread_policy_set() failure: " << result;
116 return;
117 }
105 118
106 // Most important, set real-time constraints. 119 // Most important, set real-time constraints.
107 120
108 // Define the guaranteed and max fraction of time for the audio thread. 121 // Define the guaranteed and max fraction of time for the audio thread.
109 // These "duty cycle" values can range from 0 to 1. A value of 0.5 122 // These "duty cycle" values can range from 0 to 1. A value of 0.5
110 // means the scheduler would give half the time to the thread. 123 // means the scheduler would give half the time to the thread.
111 // These values have empirically been found to yield good behavior. 124 // These values have empirically been found to yield good behavior.
112 // Good means that audio performance is high and other threads won't starve. 125 // Good means that audio performance is high and other threads won't starve.
113 const double kGuaranteedAudioDutyCycle = 0.75; 126 const double kGuaranteedAudioDutyCycle = 0.75;
114 const double kMaxAudioDutyCycle = 0.85; 127 const double kMaxAudioDutyCycle = 0.85;
(...skipping 20 matching lines...) Expand all
135 thread_time_constraint_policy_data_t time_constraints; 148 thread_time_constraint_policy_data_t time_constraints;
136 time_constraints.period = kTimeQuantum * ms_to_abs_time; 149 time_constraints.period = kTimeQuantum * ms_to_abs_time;
137 time_constraints.computation = kAudioTimeNeeded * ms_to_abs_time; 150 time_constraints.computation = kAudioTimeNeeded * ms_to_abs_time;
138 time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time; 151 time_constraints.constraint = kMaxTimeAllowed * ms_to_abs_time;
139 time_constraints.preemptible = 0; 152 time_constraints.preemptible = 0;
140 153
141 result = thread_policy_set(mach_thread_id, 154 result = thread_policy_set(mach_thread_id,
142 THREAD_TIME_CONSTRAINT_POLICY, 155 THREAD_TIME_CONSTRAINT_POLICY,
143 (thread_policy_t)&time_constraints, 156 (thread_policy_t)&time_constraints,
144 THREAD_TIME_CONSTRAINT_POLICY_COUNT); 157 THREAD_TIME_CONSTRAINT_POLICY_COUNT);
145 DCHECK_EQ(KERN_SUCCESS, result); 158 if (result != KERN_SUCCESS)
159 VLOG(1) << "thread_policy_set() failure: " << result;
160
161 return;
146 } 162 }
147 163
148 } // anonymous namespace 164 } // anonymous namespace
149 165
150 // static 166 // static
151 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 167 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
152 ThreadPriority priority) { 168 ThreadPriority priority) {
153 // Convert from pthread_t to mach thread identifier. 169 // Convert from pthread_t to mach thread identifier.
154 mach_port_t mach_thread_id = pthread_mach_thread_np(handle); 170 mach_port_t mach_thread_id = pthread_mach_thread_np(handle);
155 171
156 switch (priority) { 172 switch (priority) {
157 case kThreadPriority_Normal: 173 case kThreadPriority_Normal:
158 SetPriorityNormal(mach_thread_id); 174 SetPriorityNormal(mach_thread_id);
159 break; 175 break;
160 case kThreadPriority_RealtimeAudio: 176 case kThreadPriority_RealtimeAudio:
161 SetPriorityRealtimeAudio(mach_thread_id); 177 SetPriorityRealtimeAudio(mach_thread_id);
162 break; 178 break;
163 } 179 }
164 } 180 }
165 181
166 } // namespace base 182 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698