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

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

Issue 6949009: Add support for real-time audio threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 7 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
OLDNEW
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
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 // static
58 void PlatformThread::EnableRealtimeAudioPerformance(
59 PlatformThreadHandle handle) {
60
61 // Convert from pthread_t to mach thread identifier.
62 mach_port_t mach_thread_id = pthread_mach_thread_np(handle);
63
64 kern_return_t result;
65
66 // Increase thread priority to real-time.
67
68 // Make thread fixed priority.
69 thread_extended_policy_data_t policy;
70 policy.timeshare = 0; // set to 1 for a non-fixed thread
71 result = thread_policy_set(mach_thread_id,
72 THREAD_EXTENDED_POLICY,
73 (thread_policy_t)&policy,
74 THREAD_EXTENDED_POLICY_COUNT);
75
76 DCHECK_EQ(KERN_SUCCESS, result);
77
78 // Set to relatively high priority.
79 thread_precedence_policy_data_t precedence;
80 precedence.importance = 63;
81 result = thread_policy_set(mach_thread_id,
82 THREAD_PRECEDENCE_POLICY,
83 (thread_policy_t)&precedence,
84 THREAD_PRECEDENCE_POLICY_COUNT);
85 DCHECK_EQ(KERN_SUCCESS, result);
86
87 // Most important, set real-time constraints.
88
89 // Define constants determining how much time we will be guaranteed.
90 // All times are in milliseconds.
91
92 // About 128 frames @44.1KHz
93 const double kTimeQuantum = 2.9;
94
95 // Time guaranteed each quantum. 75% Duty cycle.
96 const double kAudioTimeNeeded = 0.75 * kTimeQuantum;
97
98 // Maximum time allowed for work.
99 const double kMaxTimeAllowed = 0.85 * kTimeQuantum;
100
101 // Get the conversion factor from milliseconds to absolute time
102 // which is what the time-constraints call needs.
103 mach_timebase_info_data_t tb_info;
104 mach_timebase_info(&tb_info);
105 double msToAbsTime =
106 ((double)tb_info.denom / (double)tb_info.numer) * 1000000;
107
108 thread_time_constraint_policy_data_t time_constraints;
109 time_constraints.period = kTimeQuantum * msToAbsTime;
110 time_constraints.computation = kAudioTimeNeeded * msToAbsTime;
111 time_constraints.constraint = kMaxTimeAllowed * msToAbsTime;
112 time_constraints.preemptible = 0;
113
114 result = thread_policy_set(mach_thread_id,
115 THREAD_TIME_CONSTRAINT_POLICY,
116 (thread_policy_t)&time_constraints,
117 THREAD_TIME_CONSTRAINT_POLICY_COUNT);
118 DCHECK_EQ(KERN_SUCCESS, result);
119 }
120
54 } // namespace base 121 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698