From c80a875a7481f400940fdc5e436533f602b73ae7 Mon Sep 17 00:00:00 2001 From: Trung Nguyen Date: Fri, 9 Jun 2023 17:38:20 +1000 Subject: [PATCH] libroot: fix pthread_[g/s]etschedparam Make `pthread_getschedparam` and `pthread_setschedparam` handle scheduling policies more consistently with `sched_get_priority_min` and `sched_get_priority_max`. Threads running in real-time priority will appear to be under the `SCHED_RR` policy, while normal threads will appaer to be `SCHED_OTHER`. This prevents POSIX code using `sched_get_priority_min` with the calling thread's current policy returned by `pthread_getschedparam` to adjust its priority from unwantedly promote into real-time code and affect overall system performance. Change-Id: I9664257dc1b98db579e55218ce352cb762524b0c Reviewed-on: https://review.haiku-os.org/c/haiku/+/6556 Reviewed-by: Adrien Destugues Tested-by: Commit checker robot --- src/system/libroot/posix/pthread/pthread.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/system/libroot/posix/pthread/pthread.cpp b/src/system/libroot/posix/pthread/pthread.cpp index 56fb8f0bbb..1d392d7844 100644 --- a/src/system/libroot/posix/pthread/pthread.cpp +++ b/src/system/libroot/posix/pthread/pthread.cpp @@ -285,8 +285,12 @@ pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param) if (status == B_BAD_THREAD_ID) return ESRCH; param->sched_priority = info.priority; - if (policy != NULL) - *policy = SCHED_RR; + if (policy != NULL) { + if (info.priority >= B_FIRST_REAL_TIME_PRIORITY) + *policy = SCHED_RR; + else + *policy = SCHED_OTHER; + } return 0; } @@ -296,8 +300,12 @@ pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param) { status_t status; - if (policy != SCHED_RR) + if (policy != SCHED_RR && policy != SCHED_OTHER) return ENOTSUP; + if (policy == SCHED_RR && param->sched_priority < B_FIRST_REAL_TIME_PRIORITY) + return EINVAL; + if (policy == SCHED_OTHER && param->sched_priority >= B_FIRST_REAL_TIME_PRIORITY) + return EINVAL; status = _kern_set_thread_priority(thread->id, param->sched_priority); if (status == B_BAD_THREAD_ID) return ESRCH;