summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2017-10-30 15:57:39 +0100
committerRobin Gareus <robin@gareus.org>2017-10-30 16:31:38 +0100
commitc0dbe8eaaf45369b4fc40c881ff4d60bedef6b81 (patch)
tree45d3866a647756941534d743cdf36d712784d3dc
parentfee03dc467fe0e96f2d1d8035d66da1cdadd1c3b (diff)
Move coreaudio_set_realtime_policy into libpbd
-rw-r--r--libs/backends/coreaudio/coreaudio_backend.cc38
-rw-r--r--libs/backends/coreaudio/coreaudio_backend.h1
-rw-r--r--libs/pbd/pbd/pthread_utils.h3
-rw-r--r--libs/pbd/pthread_utils.cc34
4 files changed, 41 insertions, 35 deletions
diff --git a/libs/backends/coreaudio/coreaudio_backend.cc b/libs/backends/coreaudio/coreaudio_backend.cc
index fc94145ed5..ce7f833e57 100644
--- a/libs/backends/coreaudio/coreaudio_backend.cc
+++ b/libs/backends/coreaudio/coreaudio_backend.cc
@@ -333,10 +333,10 @@ CoreAudioBackend::set_buffer_size (uint32_t bs)
_samples_per_period = bs;
_pcmio->set_samples_per_period(bs);
if (_run) {
- coreaudio_set_realtime_policy (_main_thread);
+ pbd_mach_set_realtime_policy (_main_thread, 1e9 * _samples_per_period / _samplerate);
}
for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i) {
- coreaudio_set_realtime_policy (*i);
+ pbd_mach_set_realtime_policy (*i, 1e9 * _samples_per_period / _samplerate);
}
//engine.buffer_size_change (bs);
return 0;
@@ -822,34 +822,6 @@ CoreAudioBackend::coreaudio_process_thread (void *arg)
return 0;
}
-bool
-CoreAudioBackend::coreaudio_set_realtime_policy (pthread_t thread_id) const
-{
- 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 ("Coreaudio Main 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
-
- double period_ns = 1e9 * _samples_per_period / _samplerate;
- policy.period = AudioConvertNanosToHostTime (period_ns);
- policy.computation = AudioConvertNanosToHostTime (period_ns * .9);
- policy.constraint = AudioConvertNanosToHostTime (period_ns * .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 ("Coreaudio Proc 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;
-}
-
int
CoreAudioBackend::create_process_thread (boost::function<void()> func)
{
@@ -872,7 +844,7 @@ CoreAudioBackend::create_process_thread (boost::function<void()> func)
pthread_attr_destroy (&attr);
}
- if (coreaudio_set_realtime_policy (thread_id)) {
+ if (pbd_mach_set_realtime_policy (thread_id, 1e9 * _samples_per_period / _samplerate)) {
PBD::warning << _("AudioEngine: process thread failed to set mach realtime policy.") << endmsg;
}
@@ -1733,7 +1705,7 @@ CoreAudioBackend::freewheel_thread ()
AudioEngine::thread_init_callback (this);
_midiio->set_enabled(false);
reset_midi_parsers ();
- coreaudio_set_realtime_policy (_main_thread);
+ pbd_mach_set_realtime_policy (_main_thread, 1e9 * _samples_per_period / _samplerate);
}
// process port updates first in every cycle.
@@ -1799,7 +1771,7 @@ CoreAudioBackend::process_callback (const uint32_t n_samples, const uint64_t hos
_reinit_thread_callback = false;
_main_thread = pthread_self();
AudioEngine::thread_init_callback (this);
- coreaudio_set_realtime_policy (_main_thread);
+ pbd_mach_set_realtime_policy (_main_thread, 1e9 * _samples_per_period / _samplerate);
}
if (pthread_mutex_trylock (&_process_callback_mutex)) {
diff --git a/libs/backends/coreaudio/coreaudio_backend.h b/libs/backends/coreaudio/coreaudio_backend.h
index 7e8c55f2eb..5884b569ac 100644
--- a/libs/backends/coreaudio/coreaudio_backend.h
+++ b/libs/backends/coreaudio/coreaudio_backend.h
@@ -439,7 +439,6 @@ class CoreAudioBackend : public AudioBackend {
/* process threads */
static void* coreaudio_process_thread (void *);
- bool coreaudio_set_realtime_policy (pthread_t) const;
std::vector<pthread_t> _threads;
struct ThreadData {
diff --git a/libs/pbd/pbd/pthread_utils.h b/libs/pbd/pbd/pthread_utils.h
index a1660eb073..e917fa27ce 100644
--- a/libs/pbd/pbd/pthread_utils.h
+++ b/libs/pbd/pbd/pthread_utils.h
@@ -60,7 +60,8 @@ LIBPBD_API int pbd_realtime_pthread_create (
void *(*start_routine) (void *),
void *arg);
-LIBPBD_API int pbd_set_thread_priority (pthread_t, const int policy, int priority);
+LIBPBD_API int pbd_set_thread_priority (pthread_t, const int policy, int priority);
+LIBPBD_API bool pbd_mach_set_realtime_policy (pthread_t thread_id, double period_ns);
namespace PBD {
LIBPBD_API extern void notify_event_loops_about_thread_creation (pthread_t, const std::string&, int requests = 256);
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, &param);
}
+
+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
+}