Merge branch 'master' into port_register_notification_defer
[jack2.git] / macosx / JackMachThread.cpp
blob9a7ed47db4683207d33b1a1a369a2a9b37b11eea
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.
21 #include "JackMachThread.h"
22 #include "JackError.h"
24 namespace Jack
27 int JackMachThread::SetThreadToPriority(pthread_t thread, UInt32 inPriority, Boolean inIsFixed, UInt64 period, UInt64 computation, UInt64 constraint)
29 if (inPriority == 96) {
30 // REAL-TIME / TIME-CONSTRAINT THREAD
31 thread_time_constraint_policy_data_t theTCPolicy;
33 theTCPolicy.period = AudioConvertNanosToHostTime(period);
34 theTCPolicy.computation = AudioConvertNanosToHostTime(computation);
35 theTCPolicy.constraint = AudioConvertNanosToHostTime(constraint);
36 theTCPolicy.preemptible = true;
37 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);
38 jack_log("JackMachThread::thread_policy_set res = %ld", res);
39 return (res == KERN_SUCCESS) ? 0 : -1;
40 } else {
41 // OTHER THREADS
42 thread_extended_policy_data_t theFixedPolicy;
43 thread_precedence_policy_data_t thePrecedencePolicy;
44 SInt32 relativePriority;
46 // [1] SET FIXED / NOT FIXED
47 theFixedPolicy.timeshare = !inIsFixed;
48 thread_policy_set(pthread_mach_thread_np(thread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT);
50 // [2] SET PRECEDENCE
51 // N.B.: We expect that if thread A created thread B, and the program wishes to change
52 // the priority of thread B, then the call to change the priority of thread B must be
53 // made by thread A.
54 // This assumption allows us to use pthread_self() to correctly calculate the priority
55 // of the feeder thread (since precedency policy's importance is relative to the
56 // spawning thread's priority.)
57 relativePriority = inPriority - GetThreadSetPriority(pthread_self());
59 thePrecedencePolicy.importance = relativePriority;
60 kern_return_t res = thread_policy_set(pthread_mach_thread_np(thread), THREAD_PRECEDENCE_POLICY, (thread_policy_t) & thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT);
61 jack_log("JackMachThread::thread_policy_set res = %ld", res);
62 return (res == KERN_SUCCESS) ? 0 : -1;
66 // returns the thread's priority as it was last set by the API
67 UInt32 JackMachThread::GetThreadSetPriority(pthread_t thread)
69 return GetThreadPriority(thread, THREAD_SET_PRIORITY);
72 // returns the thread's priority as it was last scheduled by the Kernel
73 UInt32 JackMachThread::GetThreadScheduledPriority(pthread_t thread)
75 return GetThreadPriority(thread, THREAD_SCHEDULED_PRIORITY);
78 UInt32 JackMachThread::GetThreadPriority(pthread_t thread, int inWhichPriority)
80 thread_basic_info_data_t threadInfo;
81 policy_info_data_t thePolicyInfo;
82 unsigned int count;
84 // get basic info
85 count = THREAD_BASIC_INFO_COUNT;
86 thread_info(pthread_mach_thread_np(thread), THREAD_BASIC_INFO, (thread_info_t)&threadInfo, &count);
88 switch (threadInfo.policy) {
89 case POLICY_TIMESHARE:
90 count = POLICY_TIMESHARE_INFO_COUNT;
91 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&(thePolicyInfo.ts), &count);
92 if (inWhichPriority == THREAD_SCHEDULED_PRIORITY) {
93 return thePolicyInfo.ts.cur_priority;
94 } else {
95 return thePolicyInfo.ts.base_priority;
97 break;
99 case POLICY_FIFO:
100 count = POLICY_FIFO_INFO_COUNT;
101 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_FIFO_INFO, (thread_info_t)&(thePolicyInfo.fifo), &count);
102 if ( (thePolicyInfo.fifo.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
103 return thePolicyInfo.fifo.depress_priority;
105 return thePolicyInfo.fifo.base_priority;
106 break;
108 case POLICY_RR:
109 count = POLICY_RR_INFO_COUNT;
110 thread_info(pthread_mach_thread_np(thread), THREAD_SCHED_RR_INFO, (thread_info_t)&(thePolicyInfo.rr), &count);
111 if ( (thePolicyInfo.rr.depressed) && (inWhichPriority == THREAD_SCHEDULED_PRIORITY) ) {
112 return thePolicyInfo.rr.depress_priority;
114 return thePolicyInfo.rr.base_priority;
115 break;
118 return 0;
121 int JackMachThread::GetParams(pthread_t thread, UInt64* period, UInt64* computation, UInt64* constraint)
123 thread_time_constraint_policy_data_t theTCPolicy;
124 mach_msg_type_number_t count = THREAD_TIME_CONSTRAINT_POLICY_COUNT;
125 boolean_t get_default = false;
127 kern_return_t res = thread_policy_get(pthread_mach_thread_np(thread),
128 THREAD_TIME_CONSTRAINT_POLICY,
129 (thread_policy_t) & theTCPolicy,
130 &count,
131 &get_default);
132 if (res == KERN_SUCCESS) {
133 *period = AudioConvertHostTimeToNanos(theTCPolicy.period);
134 *computation = AudioConvertHostTimeToNanos(theTCPolicy.computation);
135 *constraint = AudioConvertHostTimeToNanos(theTCPolicy.constraint);
136 jack_log("JackMachThread::GetParams period = %ld computation = %ld constraint = %ld", long(*period / 1000.0f), long(*computation / 1000.0f), long(*constraint / 1000.0f));
137 return 0;
138 } else {
139 return -1;
143 int JackMachThread::Kill()
145 // pthread_cancel still not yet implemented in Darwin (TO CHECK ON TIGER)
146 jack_log("JackMachThread::Kill");
148 if (fThread != (pthread_t)NULL) { // If thread has been started
149 mach_port_t machThread = pthread_mach_thread_np(fThread);
150 int res = (thread_terminate(machThread) == KERN_SUCCESS) ? 0 : -1;
151 fThread = (pthread_t)NULL;
152 return res;
153 } else {
154 return -1;
158 int JackMachThread::AcquireRealTime()
160 jack_log("JackMachThread::AcquireRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
161 long(fPeriod / 1000), long(fComputation / 1000), long(fConstraint / 1000));
162 return (fThread != (pthread_t)NULL) ? AcquireRealTimeImp(fThread, fPeriod, fComputation, fConstraint) : -1;
165 int JackMachThread::AcquireSelfRealTime()
167 jack_log("JackMachThread::AcquireRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld",
168 long(fPeriod / 1000), long(fComputation / 1000), long(fConstraint / 1000));
169 return AcquireRealTimeImp(pthread_self(), fPeriod, fComputation, fConstraint);
172 int JackMachThread::AcquireRealTime(int priority)
174 fPriority = priority;
175 return AcquireRealTime();
178 int JackMachThread::AcquireSelfRealTime(int priority)
180 fPriority = priority;
181 return AcquireSelfRealTime();
184 int JackMachThread::AcquireRealTimeImp(pthread_t thread, UInt64 period, UInt64 computation, UInt64 constraint)
186 SetThreadToPriority(thread, 96, true, period, computation, constraint);
187 return 0;
190 int JackMachThread::DropRealTime()
192 return (fThread != (pthread_t)NULL) ? DropRealTimeImp(fThread) : -1;
195 int JackMachThread::DropSelfRealTime()
197 return DropRealTimeImp(pthread_self());
200 int JackMachThread::DropRealTimeImp(pthread_t thread)
202 SetThreadToPriority(thread, 63, false, 0, 0, 0);
203 return 0;
206 void JackMachThread::SetParams(UInt64 period, UInt64 computation, UInt64 constraint)
208 fPeriod = period;
209 fComputation = computation;
210 fConstraint = constraint;
213 } // end of namespace