From 89a0040f1b5e83eaab116d04283e241543c9b4e2 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Thu, 10 Jun 2021 03:16:35 +0200 Subject: [PATCH] Allow to override rt priority for internal backends --- libs/pbd/pbd/pthread_utils.h | 20 +++++++++-------- libs/pbd/pthread_utils.cc | 43 ++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/libs/pbd/pbd/pthread_utils.h b/libs/pbd/pbd/pthread_utils.h index c6e809157a..47f8357b9e 100644 --- a/libs/pbd/pbd/pthread_utils.h +++ b/libs/pbd/pbd/pthread_utils.h @@ -54,15 +54,9 @@ /* these are relative to sched_get_priority_max() * see pbd_absolute_rt_priority() */ -#ifdef PLATFORM_WINDOWS -# define PBD_RT_PRI_MAIN -1 -# define PBD_RT_PRI_MIDI -2 -# define PBD_RT_PRI_PROC -2 -#else -# define PBD_RT_PRI_MAIN -20 -# define PBD_RT_PRI_MIDI -21 -# define PBD_RT_PRI_PROC -22 -#endif +# define PBD_RT_PRI_MAIN pbd_pthread_priority (THREAD_MAIN) +# define PBD_RT_PRI_MIDI pbd_pthread_priority (THREAD_MIDI) +# define PBD_RT_PRI_PROC pbd_pthread_priority (THREAD_PROC) LIBPBD_API int pthread_create_and_store (std::string name, pthread_t *thread, void * (*start_routine)(void *), void * arg); LIBPBD_API void pthread_cancel_one (pthread_t thread); @@ -71,6 +65,14 @@ LIBPBD_API void pthread_kill_all (int signum); LIBPBD_API const char* pthread_name (); LIBPBD_API void pthread_set_name (const char* name); +enum PBDThreadClass { + THREAD_MAIN, // main audio I/O thread + THREAD_MIDI, // MIDI I/O threads + THREAD_PROC // realtime worker +}; + +LIBPBD_API int pbd_pthread_priority (PBDThreadClass); + LIBPBD_API int pbd_pthread_create ( const size_t stacksize, pthread_t *thread, diff --git a/libs/pbd/pthread_utils.cc b/libs/pbd/pthread_utils.cc index 78bf942171..69f1072148 100644 --- a/libs/pbd/pthread_utils.cc +++ b/libs/pbd/pthread_utils.cc @@ -277,6 +277,42 @@ pbd_pthread_create ( return rv; } +int +pbd_pthread_priority (PBDThreadClass which) +{ + /* fall back to use values relative to max */ +#ifdef PLATFORM_WINDOWS + switch (which) { + case THREAD_MAIN: + return -1; + case THREAD_MIDI: + return -2; + default: + case THREAD_PROC: + return -2; + } +#else + int base = -20; + const char* p = getenv ("ARDOUR_SCHED_PRI"); + if (p && *p) { + base = atoi (p); + if (base > -5 && base < 5) { + base = -20; + } + } + + switch (which) { + case THREAD_MAIN: + return base; + case THREAD_MIDI: + return base - 1; + default: + case THREAD_PROC: + return base - 2; + } +#endif +} + int pbd_absolute_rt_priority (int policy, int priority) { @@ -285,13 +321,12 @@ pbd_absolute_rt_priority (int policy, int priority) const int p_max = sched_get_priority_max (policy); // Linux: 99 if (priority == 0) { - /* use default. XXX this should be relative to audio (JACK) thread, - * internal backends use -20 (Audio), -21 (MIDI), -22 (compuation) - */ priority = (p_min + p_max) / 2; } else if (priority > 0) { - priority += p_min; + /* value relative to minium */ + priority += p_min - 1; } else { + /* value relative maximum */ priority += p_max + 1; }