diff options
author | Robin Gareus <robin@gareus.org> | 2017-10-30 15:57:39 +0100 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2017-10-30 16:31:38 +0100 |
commit | c0dbe8eaaf45369b4fc40c881ff4d60bedef6b81 (patch) | |
tree | 45d3866a647756941534d743cdf36d712784d3dc /libs/pbd/pthread_utils.cc | |
parent | fee03dc467fe0e96f2d1d8035d66da1cdadd1c3b (diff) |
Move coreaudio_set_realtime_policy into libpbd
Diffstat (limited to 'libs/pbd/pthread_utils.cc')
-rw-r--r-- | libs/pbd/pthread_utils.cc | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libs/pbd/pthread_utils.cc b/libs/pbd/pthread_utils.cc index c9b8f2980a..118c54b97f 100644 --- a/libs/pbd/pthread_utils.cc +++ b/libs/pbd/pthread_utils.cc @@ -266,3 +266,37 @@ pbd_set_thread_priority (pthread_t thread, const int policy, int priority) return pthread_setschedparam (thread, SCHED_FIFO, ¶m); } + +bool +pbd_mach_set_realtime_policy (pthread_t thread_id, double period_ns) +{ +#ifdef _APPLE_ + thread_time_constraint_policy_data_t policy; +#ifndef NDEBUG + mach_msg_type_number_t msgt = 4; + boolean_t dflt = false; + kern_return_t rv = thread_policy_get (pthread_mach_thread_np (_main_thread), + THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t) &policy, + &msgt, &dflt); + printf ("Mach Thread(%p) %d %d %d %d DFLT %d OK: %d\n", _main_thread, policy.period, policy.computation, policy.constraint, policy.preemptible, dflt, rv == KERN_SUCCESS); +#endif + + mach_timebase_info_data_t timebase_info; + mach_timebase_info(&timebase_info); + const double period_clk = period_ns * (double)timebase_info.denom / (double)timebase_info.numer; + + policy.period = period_clk; + policy.computation = period_clk * .9; + policy.constraint = period_clk * .95; + policy.preemptible = true; + kern_return_t res = thread_policy_set (pthread_mach_thread_np (thread_id), + THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t) &policy, + THREAD_TIME_CONSTRAINT_POLICY_COUNT); + +#ifndef NDEBUG + printf ("Mach Thread(%p) %d %d %d %d OK: %d\n", thread_id, policy.period, policy.computation, policy.constraint, policy.preemptible, res == KERN_SUCCESS); +#endif + return res != KERN_SUCCESS; +#endif + return false; // OK +} |