2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "JackMachThread.h"
23 #include "JackError.h"
25 #ifdef MY_TARGET_OS_IPHONE
26 #include "/Developer/Extras/CoreAudio/PublicUtility/CAHostTimeBase.h"
32 int JackMachThread::SetThreadToPriority(jack_native_thread_t thread, UInt32 inPriority, Boolean inIsFixed, UInt64 period, UInt64 computation, UInt64 constraint)
34 if (inPriority == 96) {
35 // REAL-TIME / TIME-CONSTRAINT THREAD
36 thread_time_constraint_policy_data_t theTCPolicy;
38 #ifdef MY_TARGET_OS_IPHONE
39 theTCPolicy.period = CAHostTimeBase::ConvertFromNanos(period);
40 theTCPolicy.computation = CAHostTimeBase::ConvertFromNanos(computation);
41 theTCPolicy.constraint = CAHostTimeBase::ConvertFromNanos(constraint);
43 theTCPolicy.period = AudioConvertNanosToHostTime(period);
44 theTCPolicy.computation = AudioConvertNanosToHostTime(computation);
45 theTCPolicy.constraint = AudioConvertNanosToHostTime(constraint);
47 theTCPolicy.preemptible = true;
48 kern_return_t res = thread_policy_set(pthread_mach_thread_np(thread), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t) &theTCPolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT);
49 jack_log("JackMachThread::thread_policy_set res = %ld", res);
50 return (res == KERN_SUCCESS) ? 0 : -1;
53 thread_extended_policy_data_t theFixedPolicy;
54 thread_precedence_policy_data_t thePrecedencePolicy;
55 SInt32 relativePriority;
57 // [1] SET FIXED / NOT FIXED
58 theFixedPolicy.timeshare = !inIsFixed;
59 thread_policy_set(pthread_mach_thread_np(thread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
62 // N.B.: We expect that if thread A created thread B, and the program wishes to change
63 // the priority of thread B, then the call to change the priority of thread B must be
65 // This assumption allows us to use pthread_self() to correctly calculate the priority
66 // of the feeder thread (since precedency policy's importance is relative to the
67 // spawning thread's priority.)
68 relativePriority = inPriority - GetThreadSetPriority(pthread_self());
70 thePrecedencePolicy.importance = relativePriority;
71 kern_return_t res = thread_policy_set(pthread_mach_thread_np(thread), THREAD_PRECEDENCE_POLICY, (thread_policy_t) &thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
72 jack_log("JackMachThread::thread_policy_set res = %ld", res);
73 return (res == KERN_SUCCESS) ? 0 : -1;
77 // returns the thread's priority as it was last set by the API
78 UInt32 JackMachThread::GetThreadSetPriority(jack_native_thread_t thread)
80 return GetThreadPriority(thread, THREAD_SET_PRIORITY);
83 // returns the thread's priority as it was last scheduled by the Kernel
84 UInt32 JackMachThread::GetThreadScheduledPriority(jack_native_thread_t thread)
86 return GetThreadPriority(thread, THREAD_SCHEDULED_PRIORITY);
89 UInt32 JackMachThread::GetThreadPriority(jack_native_thread_t thread, int inWhichPriority)
91 thread_basic_info_data_t threadInfo;
92 policy_info_data_t thePolicyInfo;
96 count = THREAD_BASIC_INFO_COUNT;
97 thread_info(pthread_mach_thread_np(thread), THREAD_BASIC_INFO, (thread_info_t)&threadInfo, &count);
99 switch (threadInfo.policy) {
100 case POLICY_TIMESHARE:
101 count = POLICY_TIMESHARE_INFO_COUNT;
102 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&(thePolicyInfo.ts), &count);
103 if (inWhichPriority == THREAD_SCHEDULED_PRIORITY) {
104 return thePolicyInfo.ts.cur_priority;
106 return thePolicyInfo.ts.base_priority;
111 count = POLICY_FIFO_INFO_COUNT;
112 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_FIFO_INFO, (thread_info_t)&(thePolicyInfo.fifo), &count);
113 if ( (thePolicyInfo.fifo.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
114 return thePolicyInfo.fifo.depress_priority;
116 return thePolicyInfo.fifo.base_priority;
120 count = POLICY_RR_INFO_COUNT;
121 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_RR_INFO, (thread_info_t)&(thePolicyInfo.rr), &count);
122 if ( (thePolicyInfo.rr.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
123 return thePolicyInfo.rr.depress_priority;
125 return thePolicyInfo.rr.base_priority;
132 int JackMachThread::GetParams(jack_native_thread_t thread, UInt64* period, UInt64* computation, UInt64* constraint)
134 thread_time_constraint_policy_data_t theTCPolicy;
135 mach_msg_type_number_t count = THREAD_TIME_CONSTRAINT_POLICY_COUNT;
136 boolean_t get_default = false;
138 kern_return_t res = thread_policy_get(pthread_mach_thread_np(thread),
139 THREAD_TIME_CONSTRAINT_POLICY,
140 (thread_policy_t) & theTCPolicy,
143 if (res == KERN_SUCCESS) {
144 #ifdef MY_TARGET_OS_IPHONE
145 *period = CAHostTimeBase::ConvertToNanos(theTCPolicy.period);
146 *computation = CAHostTimeBase::ConvertToNanos(theTCPolicy.computation);
147 *constraint = CAHostTimeBase::ConvertToNanos(theTCPolicy.constraint);
149 *period = AudioConvertHostTimeToNanos(theTCPolicy.period);
150 *computation = AudioConvertHostTimeToNanos(theTCPolicy.computation);
151 *constraint = AudioConvertHostTimeToNanos(theTCPolicy.constraint);
154 jack_log("JackMachThread::GetParams period = %ld computation = %ld constraint = %ld", long(*period / 1000.0f), long(*computation / 1000.0f), long(*constraint / 1000.0f));
161 int JackMachThread::Kill()
163 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
164 jack_log("JackMachThread::Kill");
165 mach_port_t machThread = pthread_mach_thread_np(fThread);
166 int res = (thread_terminate(machThread) == KERN_SUCCESS) ? 0 : -1;
168 fThread = (jack_native_thread_t)NULL;
175 int JackMachThread::AcquireRealTime()
177 jack_log("JackMachThread::AcquireRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
178 long(fPeriod / 1000), long(fComputation / 1000), long(fConstraint / 1000));
179 return (fThread != (jack_native_thread_t)NULL) ? AcquireRealTimeImp(fThread, fPeriod, fComputation, fConstraint) : -1;
182 int JackMachThread::AcquireSelfRealTime()
184 jack_log("JackMachThread::AcquireSelfRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
185 long(fPeriod / 1000), long(fComputation / 1000), long(fConstraint / 1000));
186 return AcquireRealTimeImp(pthread_self(), fPeriod, fComputation, fConstraint);
189 int JackMachThread::AcquireRealTime(int priority)
191 fPriority = priority;
192 return AcquireRealTime();
195 int JackMachThread::AcquireSelfRealTime(int priority)
197 fPriority = priority;
198 return AcquireSelfRealTime();
201 int JackMachThread::AcquireRealTimeImp(jack_native_thread_t thread, UInt64 period, UInt64 computation, UInt64 constraint)
203 SetThreadToPriority(thread, 96, true, period, computation, constraint);
207 int JackMachThread::DropRealTime()
209 return (fThread != (jack_native_thread_t)NULL) ? DropRealTimeImp(fThread) : -1;
212 int JackMachThread::DropSelfRealTime()
214 return DropRealTimeImp(pthread_self());
217 int JackMachThread::DropRealTimeImp(jack_native_thread_t thread)
219 SetThreadToPriority(thread, 63, false, 0, 0, 0);
223 void JackMachThread::SetParams(UInt64 period, UInt64 computation, UInt64 constraint)
226 fComputation = computation;
227 fConstraint = constraint;
230 } // end of namespace