Correct JackPortAudioDriver::Open : special case for ASIO drivers.
[jack2.git] / macosx / JackMachThread.cpp
blob268c7c5446dcdbd0a5701a0f8aa47209a51fa7a4
1 /*
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"
27 #endif
29 namespace Jack
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);
42 #else
43 theTCPolicy.period = AudioConvertNanosToHostTime(period);
44 theTCPolicy.computation = AudioConvertNanosToHostTime(computation);
45 theTCPolicy.constraint = AudioConvertNanosToHostTime(constraint);
46 #endif
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;
51 } else {
52 // OTHER THREADS
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);
61 // [2] SET PRECEDENCE
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
64 // made by thread A.
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;
93 unsigned int count;
95 // get basic info
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;
105 } else {
106 return thePolicyInfo.ts.base_priority;
108 break;
110 case POLICY_FIFO:
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;
117 break;
119 case POLICY_RR:
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;
126 break;
129 return 0;
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,
141 &count,
142 &get_default);
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);
148 #else
149 *period = AudioConvertHostTimeToNanos(theTCPolicy.period);
150 *computation = AudioConvertHostTimeToNanos(theTCPolicy.computation);
151 *constraint = AudioConvertHostTimeToNanos(theTCPolicy.constraint);
152 #endif
154 jack_log("JackMachThread::GetParams period = %ld computation = %ld constraint = %ld", long(*period / 1000.0f), long(*computation / 1000.0f), long(*constraint / 1000.0f));
155 return 0;
156 } else {
157 return -1;
161 int JackMachThread::Kill()
163 // pthread_cancel still not yet implemented in Darwin (TO CHECK ON TIGER)
164 jack_log("JackMachThread::Kill");
166 if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
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;
170 return res;
171 } else {
172 return -1;
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);
205 return 0;
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);
221 return 0;
224 void JackMachThread::SetParams(UInt64 period, UInt64 computation, UInt64 constraint)
226 fPeriod = period;
227 fComputation = computation;
228 fConstraint = constraint;
231 } // end of namespace