From 02029576c4eb23c99e780df2b3499b01edf7de5e Mon Sep 17 00:00:00 2001 From: Yuri Date: Mon, 9 Jan 2017 11:38:55 -0800 Subject: Using the process realtime priority instead of the thread realtime priority when sufficient. --- libjack/thread.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/libjack/thread.c b/libjack/thread.c index d17e7d8..4727c07 100644 --- a/libjack/thread.c +++ b/libjack/thread.c @@ -33,6 +33,11 @@ #include #include #include +#if defined(__FreeBSD__) +#include +#include +#include +#endif #include "local.h" @@ -267,20 +272,45 @@ jack_acquire_real_time_scheduling (pthread_t thread, int priority) #else /* !JACK_USE_MACH_THREADS */ +static int +jack_process_already_has_real_time_scheduling (int priority) +{ +#if defined(__FreeBSD__) + int res; + struct rtprio rtp; + res = rtprio(RTP_LOOKUP, getpid(), &rtp); + if (res == 0 && rtp.type == RTP_PRIO_REALTIME && rtp.prio <= priority) { + jack_info("process already runs at sufficient realtime priority %u (<=%d)", + (unsigned)rtp.prio, + priority); + return 1; // process priority is sufficient + } +#endif + return 0; // no or don't know +} + int jack_drop_real_time_scheduling (pthread_t thread) { struct sched_param rtparam; - int x; + int x, policy; + + if ((x = pthread_getschedparam (thread, &policy, &rtparam)) != 0) { + jack_error ("cannot read thread scheduling priority(%s)\n", + strerror (errno)); + return -1; + } + if (policy == SCHED_OTHER) + return 0; // already memset (&rtparam, 0, sizeof(rtparam)); - rtparam.sched_priority = 0; if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) { jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno)); return -1; } + return 0; } @@ -290,6 +320,9 @@ jack_acquire_real_time_scheduling (pthread_t thread, int priority) struct sched_param rtparam; int x; + if (jack_process_already_has_real_time_scheduling (priority) != 0) + return 0; + memset (&rtparam, 0, sizeof(rtparam)); rtparam.sched_priority = priority; -- cgit v1.2.1 From 51d8d22a9be16060ccf6a81a4e1544a7e4b196df Mon Sep 17 00:00:00 2001 From: Yuri Date: Mon, 9 Jan 2017 16:14:52 -0800 Subject: Added curly braces. --- libjack/thread.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libjack/thread.c b/libjack/thread.c index 4727c07..a7466cc 100644 --- a/libjack/thread.c +++ b/libjack/thread.c @@ -300,8 +300,9 @@ jack_drop_real_time_scheduling (pthread_t thread) strerror (errno)); return -1; } - if (policy == SCHED_OTHER) + if (policy == SCHED_OTHER) { return 0; // already + } memset (&rtparam, 0, sizeof(rtparam)); @@ -320,8 +321,9 @@ jack_acquire_real_time_scheduling (pthread_t thread, int priority) struct sched_param rtparam; int x; - if (jack_process_already_has_real_time_scheduling (priority) != 0) + if (jack_process_already_has_real_time_scheduling (priority) != 0) { return 0; + } memset (&rtparam, 0, sizeof(rtparam)); rtparam.sched_priority = priority; -- cgit v1.2.1