void thread_init_functor::operator()(int thread_index) { name_current_thread(thread_index); if (rt) { bool success = true; #ifdef NOVA_TT_PRIORITY_RT #ifdef JACK_BACKEND int priority = instance->realtime_priority(); if (priority < 0) success = false; #else int min, max; boost::tie(min, max) = thread_priority_interval_rt(); int priority = max - 3; priority = std::max(min, priority); #endif if (success) success = thread_set_priority_rt(priority); #endif #if defined(NOVA_TT_PRIORITY_PERIOD_COMPUTATION_CONSTRAINT) && defined (__APPLE__) double blocksize = server_arguments::instance().blocksize; double samplerate = server_arguments::instance().samplerate; double ns_per_block = 1e9 / samplerate * blocksize; success = thread_set_priority_rt(AudioConvertNanosToHostTime(ns_per_block), AudioConvertNanosToHostTime(ns_per_block - 2), AudioConvertNanosToHostTime(ns_per_block - 1), false); #endif if (!success) std::cerr << "Warning: cannot raise thread priority" << std::endl; } if (!thread_set_affinity(thread_index)) std::cerr << "Warning: cannot set thread affinity of audio helper thread" << std::endl; }
void setThreadToPriority (pthread_t inThread, UInt32 inPriority, Boolean inIsFixed, UInt64 inHALIOProcCycleDurationInNanoseconds) { if (inPriority == 96) { // REAL-TIME / TIME-CONSTRAINT THREAD thread_time_constraint_policy_data_t theTCPolicy; UInt64 theComputeQuanta; UInt64 thePeriod; UInt64 thePeriodNanos; thePeriodNanos = inHALIOProcCycleDurationInNanoseconds; theComputeQuanta = AudioConvertNanosToHostTime ( thePeriodNanos * 0.15 ); thePeriod = AudioConvertNanosToHostTime (thePeriodNanos); theTCPolicy.period = thePeriod; theTCPolicy.computation = theComputeQuanta; theTCPolicy.constraint = thePeriod; theTCPolicy.preemptible = true; thread_policy_set (pthread_mach_thread_np(inThread), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&theTCPolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT); } else { // OTHER THREADS thread_extended_policy_data_t theFixedPolicy; thread_precedence_policy_data_t thePrecedencePolicy; SInt32 relativePriority; // [1] SET FIXED / NOT FIXED theFixedPolicy.timeshare = !inIsFixed; thread_policy_set (pthread_mach_thread_np(inThread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT); // [2] SET PRECEDENCE // N.B.: We expect that if thread A created thread B, and the program wishes to change // the priority of thread B, then the call to change the priority of thread B must be // made by thread A. // This assumption allows us to use pthread_self() to correctly calculate the priority // of the feeder thread (since precedency policy's importance is relative to the // spawning thread's priority.) relativePriority = inPriority - getThreadSetPriority (pthread_self()); thePrecedencePolicy.importance = relativePriority; thread_policy_set (pthread_mach_thread_np(inThread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT); } }
static int SetThreadToPriority(pthread_t thread, UInt32 inPriority, Boolean inIsFixed, UInt64 period, UInt64 computation, UInt64 constraint) { if (inPriority == 96) { // REAL-TIME / TIME-CONSTRAINT THREAD thread_time_constraint_policy_data_t theTCPolicy; theTCPolicy.period = AudioConvertNanosToHostTime(period); theTCPolicy.computation = AudioConvertNanosToHostTime(computation); theTCPolicy.constraint = AudioConvertNanosToHostTime(constraint); theTCPolicy.preemptible = true; 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); return (res == KERN_SUCCESS) ? 0 : -1; } else { // OTHER THREADS thread_extended_policy_data_t theFixedPolicy; thread_precedence_policy_data_t thePrecedencePolicy; SInt32 relativePriority; theFixedPolicy.timeshare = !inIsFixed; thread_policy_set(pthread_mach_thread_np(thread), THREAD_EXTENDED_POLICY, (thread_policy_t)&theFixedPolicy, THREAD_EXTENDED_POLICY_COUNT); relativePriority = inPriority - GetThreadSetPriority(pthread_self()); thePrecedencePolicy.importance = relativePriority; kern_return_t res = thread_policy_set(pthread_mach_thread_np(thread), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&thePrecedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT); return (res == KERN_SUCCESS) ? 0 : -1; } }
static void _macosx_send(struct midi_port *p, const unsigned char *data, unsigned int len, unsigned int delay) { struct macosx_midi *m; m = (struct macosx_midi *)p->userdata; if (!m->x) { m->x = MIDIPacketListInit(m->pl); } /* msec to nsec? */ m->x = MIDIPacketListAdd(m->pl, sizeof(m->packet), m->x, (MIDITimeStamp)AudioConvertNanosToHostTime( AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) + (1000000*delay)), len, data); }
static void *Pt_CallbackProc(void *p) { pt_callback_parameters *parameters = (pt_callback_parameters *) p; int mytime = 1; kern_return_t error; thread_extended_policy_data_t extendedPolicy; thread_precedence_policy_data_t precedencePolicy; extendedPolicy.timeshare = 0; error = thread_policy_set(mach_thread_self(), THREAD_EXTENDED_POLICY, (thread_policy_t)&extendedPolicy, THREAD_EXTENDED_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread timeshare policy", error); } precedencePolicy.importance = THREAD_IMPORTANCE; error = thread_policy_set(mach_thread_self(), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&precedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread precedence policy", error); } /* to kill a process, just increment the pt_callback_proc_id */ /* printf("pt_callback_proc_id %d, id %d\n", pt_callback_proc_id, parameters->id); */ while (pt_callback_proc_id == parameters->id) { /* wait for a multiple of resolution ms */ UInt64 wait_time; int delay = mytime++ * parameters->resolution - Pt_Time(); PtTimestamp timestamp; if (delay < 0) delay = 0; wait_time = AudioConvertNanosToHostTime((UInt64)delay * NSEC_PER_MSEC); wait_time += AudioGetCurrentHostTime(); error = mach_wait_until(wait_time); timestamp = Pt_Time(); (*(parameters->callback))(timestamp, parameters->userData); } free(parameters); return NULL; }
static MIDITimeStamp midiTime(float latencySeconds) { // add the latency expressed in seconds, to the current host time base. UInt64 latencyNanos = 1000000000 * latencySeconds ; //secs to nano return (MIDITimeStamp)AudioGetCurrentHostTime() + AudioConvertNanosToHostTime(latencyNanos); }
JNIEXPORT jlong JNICALL Java_com_apple_audio_util_HostTime_AudioConvertNanosToHostTime (JNIEnv *, jclass, jlong inNanos) { return (jlong)AudioConvertNanosToHostTime((UInt64)inNanos); }