From 55bbb28a8010135809a7499f22aed1f6c6a7c004 Mon Sep 17 00:00:00 2001 From: Marco Rodolfi Date: Thu, 16 Jan 2025 16:19:31 +0100 Subject: [PATCH] [threading] Linux fixes on setting an incorrect priority This call was failing since SCHED_FIFO doesn't support negative priorities, but only positive ones, see third paragraph of scheduling policies: https://man7.org/linux/man-pages/man7/sched.7.html. Additionally Linux do provice up to 99 levels, but I've limited myself to the required UNIX standard of 32, and split the priority levels evenly from that. I've also added a couple of rows to debug additional issues in the future like this. --- src/xenia/base/threading.h | 10 ++++++++++ src/xenia/base/threading_posix.cc | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/xenia/base/threading.h b/src/xenia/base/threading.h index a145c3830..26e5267bf 100644 --- a/src/xenia/base/threading.h +++ b/src/xenia/base/threading.h @@ -402,6 +402,7 @@ class Timer : public WaitHandle { virtual bool Cancel() = 0; }; +#if XE_PLATFORM_WINDOWS struct ThreadPriority { static const int32_t kLowest = -2; static const int32_t kBelowNormal = -1; @@ -409,6 +410,15 @@ struct ThreadPriority { static const int32_t kAboveNormal = 1; static const int32_t kHighest = 2; }; +#else +struct ThreadPriority { + static const int32_t kLowest = 1; + static const int32_t kBelowNormal = 8; + static const int32_t kNormal = 16; + static const int32_t kAboveNormal = 24; + static const int32_t kHighest = 32; +}; +#endif // Models a Win32-like thread object. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx diff --git a/src/xenia/base/threading_posix.cc b/src/xenia/base/threading_posix.cc index f72f72d6f..733592800 100644 --- a/src/xenia/base/threading_posix.cc +++ b/src/xenia/base/threading_posix.cc @@ -27,6 +27,8 @@ #include #include +#include "logging.h" + #if XE_PLATFORM_ANDROID #include @@ -660,8 +662,18 @@ class PosixCondition : public PosixConditionBase { WaitStarted(); sched_param param{}; param.sched_priority = new_priority; - if (pthread_setschedparam(thread_, SCHED_FIFO, ¶m) != 0) - assert_always(); + int res = pthread_setschedparam(thread_, SCHED_FIFO, ¶m); + if (res != 0) { + switch (res) { + case EPERM: + XELOGW("Permission denied while setting priority"); + break; + case EINVAL: + assert_always(); + default: + XELOGW("Unknown error while setting priority"); + } + } } void QueueUserCallback(std::function callback) {