From 8374e23014f2254c7de23cf6f0e09e7dabd5303f Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 5 Aug 2017 00:54:34 +0200 Subject: Add API to set thread-priority (for ctrl-surfaces) --- libs/pbd/base_ui.cc | 32 ++++++++++++++++++++++++++++++++ libs/pbd/pbd/base_ui.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/libs/pbd/base_ui.cc b/libs/pbd/base_ui.cc index f22d83264e..b04b755ec8 100644 --- a/libs/pbd/base_ui.cc +++ b/libs/pbd/base_ui.cc @@ -28,6 +28,9 @@ #include #include +#include +#include + #include "pbd/base_ui.h" #include "pbd/debug.h" #include "pbd/pthread_utils.h" @@ -76,6 +79,35 @@ BaseUI::new_request_type () return rt; } +int +BaseUI::set_thread_priority (const int policy, int priority) const +{ + struct sched_param param; + memset (¶m, 0, sizeof (param)); + + /* POSIX requires a spread of at least 32 steps between min..max */ + const int p_min = sched_get_priority_min (policy); // Linux: 1 + 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 = 7; + } + + if (priority > 0) { + priority += p_min; + } else { + priority += p_max; + } + if (priority > p_max) priority = p_max; + if (priority < p_min) priority = p_min; + param.sched_priority = priority; + + return pthread_setschedparam (pthread_self(), SCHED_FIFO, ¶m); +} + void BaseUI::main_thread () { diff --git a/libs/pbd/pbd/base_ui.h b/libs/pbd/pbd/base_ui.h index 029d8223ea..b8569718ae 100644 --- a/libs/pbd/pbd/base_ui.h +++ b/libs/pbd/pbd/base_ui.h @@ -91,6 +91,8 @@ class LIBPBD_API BaseUI : public sigc::trackable, public PBD::EventLoop virtual void thread_init () {}; + int set_thread_priority (const int policy = SCHED_FIFO, int priority = 0) const; + /** Called when there input ready on the request_channel */ bool request_handler (Glib::IOCondition); -- cgit v1.2.3