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) {
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;
58 theFixedPolicy.timeshare = !inIsFixed;
59 thread_policy_set(pthread_mach_thread_np(thread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
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;
78 UInt32 JackMachThread::GetThreadSetPriority(jack_native_thread_t thread)
80 return GetThreadPriority(thread, THREAD_SET_PRIORITY);
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()
166 if (fThread != (jack_native_thread_t)NULL) {
167 mach_port_t machThread = pthread_mach_thread_np(fThread);
168 int res = (thread_terminate(machThread) == KERN_SUCCESS) ? 0 : -1;
169 fThread = (jack_native_thread_t)NULL;
176 int JackMachThread::AcquireRealTime()
178 jack_log(
"JackMachThread::AcquireRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
179 long(fPeriod / 1000),
long(fComputation / 1000),
long(fConstraint / 1000));
180 return (fThread != (jack_native_thread_t)NULL) ? AcquireRealTimeImp(fThread, fPeriod, fComputation, fConstraint) : -1;
183 int JackMachThread::AcquireSelfRealTime()
185 jack_log(
"JackMachThread::AcquireSelfRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
186 long(fPeriod / 1000),
long(fComputation / 1000),
long(fConstraint / 1000));
187 return AcquireRealTimeImp(pthread_self(), fPeriod, fComputation, fConstraint);
190 int JackMachThread::AcquireRealTime(
int priority)
192 fPriority = priority;
193 return AcquireRealTime();
196 int JackMachThread::AcquireSelfRealTime(
int priority)
198 fPriority = priority;
199 return AcquireSelfRealTime();
202 int JackMachThread::AcquireRealTimeImp(jack_native_thread_t thread, UInt64 period, UInt64 computation, UInt64 constraint)
204 SetThreadToPriority(thread, 96,
true, period, computation, constraint);
208 int JackMachThread::DropRealTime()
210 return (fThread != (jack_native_thread_t)NULL) ? DropRealTimeImp(fThread) : -1;
213 int JackMachThread::DropSelfRealTime()
215 return DropRealTimeImp(pthread_self());
218 int JackMachThread::DropRealTimeImp(jack_native_thread_t thread)
220 SetThreadToPriority(thread, 63,
false, 0, 0, 0);
224 void JackMachThread::SetParams(UInt64 period, UInt64 computation, UInt64 constraint)
227 fComputation = computation;
228 fConstraint = constraint;
SERVER_EXPORT void jack_log(const char *fmt,...)