diff options
author | Robin Gareus <robin@gareus.org> | 2017-08-29 17:14:34 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2017-08-29 17:25:23 +0200 |
commit | 229c9584bfa7f84a246ccfc53779372d5d63cce4 (patch) | |
tree | c1e7e744baf48078e8379d2a58cd13bbee5077b9 /libs | |
parent | f72c8190354b662a93d30c7de4ffdc460a21ea20 (diff) |
Add a PBD API to set pthread priority
Diffstat (limited to 'libs')
-rw-r--r-- | libs/pbd/base_ui.cc | 25 | ||||
-rw-r--r-- | libs/pbd/pbd/pthread_utils.h | 8 | ||||
-rw-r--r-- | libs/pbd/pthread_utils.cc | 59 |
3 files changed, 68 insertions, 24 deletions
diff --git a/libs/pbd/base_ui.cc b/libs/pbd/base_ui.cc index b04b755ec8..36c6216d45 100644 --- a/libs/pbd/base_ui.cc +++ b/libs/pbd/base_ui.cc @@ -82,30 +82,7 @@ BaseUI::new_request_type () 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); + return pbd_set_thread_priority (pthread_self(), policy, priority); } void diff --git a/libs/pbd/pbd/pthread_utils.h b/libs/pbd/pbd/pthread_utils.h index 8f70fdac5b..2ef442caa5 100644 --- a/libs/pbd/pbd/pthread_utils.h +++ b/libs/pbd/pbd/pthread_utils.h @@ -54,6 +54,14 @@ LIBPBD_API void pthread_kill_all (int signum); LIBPBD_API const char* pthread_name (); LIBPBD_API void pthread_set_name (const char* name); +LIBPBD_API int pbd_realtime_pthread_create ( + const int policy, int priority, const size_t stacksize, + pthread_t *thread, + void *(*start_routine) (void *), + void *arg); + +LIBPBD_API int pbd_set_thread_priority (pthread_t, const int policy, int priority); + namespace PBD { LIBPBD_API extern void notify_event_loops_about_thread_creation (pthread_t, const std::string&, int requests = 256); LIBPBD_API extern PBD::Signal3<void,pthread_t,std::string,uint32_t> ThreadCreatedWithRequestSize; diff --git a/libs/pbd/pthread_utils.cc b/libs/pbd/pthread_utils.cc index 5daa60ac42..c9b8f2980a 100644 --- a/libs/pbd/pthread_utils.cc +++ b/libs/pbd/pthread_utils.cc @@ -207,3 +207,62 @@ pthread_cancel_one (pthread_t thread) pthread_cancel (thread); pthread_mutex_unlock (&thread_map_lock); } + +int +pbd_realtime_pthread_create ( + const int policy, int priority, const size_t stacksize, + pthread_t *thread, + void *(*start_routine) (void *), + void *arg) +{ + int rv; + + pthread_attr_t attr; + struct sched_param parm; + + const int p_min = sched_get_priority_min (policy); + const int p_max = sched_get_priority_max (policy); + priority += p_max; + if (priority > p_max) priority = p_max; + if (priority < p_min) priority = p_min; + parm.sched_priority = priority; + + pthread_attr_init (&attr); + pthread_attr_setschedpolicy (&attr, policy); + pthread_attr_setschedparam (&attr, &parm); + pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); + pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setstacksize (&attr, stacksize); + rv = pthread_create (thread, &attr, start_routine, arg); + pthread_attr_destroy (&attr); + return rv; +} + +int +pbd_set_thread_priority (pthread_t thread, const int policy, int priority) +{ + 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; // BaseUI backwards compat. + } + + 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 (thread, SCHED_FIFO, ¶m); +} |