Implement thread.h API
[jack2.git] / macosx / JackMachThread.cpp
blob69dd78d1eb096e76733fd6ea8e871744d55cb040
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2006 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 JackLog("JackMachThread::thread_policy_set %ld\n", 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 JackLog("JackMachThread::thread_policy_set %ld\n", 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(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(pthread_self()),
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 JackLog("JackMachThread::GetParams period = %ld computation = %ld constraint = %ld\n", 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)
147 if (fThread) { // If thread has been started
148 mach_port_t machThread = pthread_mach_thread_np(fThread);
149 return (thread_terminate(machThread) == KERN_SUCCESS) ? 0 : -1;
150 } else {
151 return -1;
155 int JackMachThread::AcquireRealTime()
157 JackLog("JackMachThread::AcquireRealTime fPeriod = %ld fComputation = %ld fConstraint = %ld\n",
158 long(fPeriod / 1000), long(fComputation / 1000), long(fConstraint / 1000));
160 return (fThread) ? AcquireRealTimeImp(fThread, fPeriod, fComputation, fConstraint) : -1;
163 int JackMachThread::AcquireRealTimeImp(pthread_t thread, UInt64 period, UInt64 computation, UInt64 constraint)
165 SetThreadToPriority(thread, 96, true, period, computation, constraint);
166 UInt64 int_period;
167 UInt64 int_computation;
168 UInt64 int_constraint;
169 GetParams(&int_period, &int_computation, &int_constraint);
170 return 0;
173 int JackMachThread::DropRealTime()
175 return (fThread) ? DropRealTimeImp(fThread) : -1;
178 int JackMachThread::DropRealTimeImp(pthread_t thread)
180 SetThreadToPriority(thread, 63, false, 0, 0, 0);
181 return 0;
184 void JackMachThread::SetParams(UInt64 period, UInt64 computation, UInt64 constraint)
186 fPeriod = period;
187 fComputation = computation;
188 fConstraint = constraint;
191 } // end of namespace