From 68615703c1b45e382e1a7f34f6b9669f9626955b Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Thu, 4 Aug 2022 15:54:37 +0530 Subject: [PATCH] Fix `KProcess`/`SetThreadPriority` PI CAS The condition for exiting the CAS loops is incorrect in several places which leads to additional loops, while this doesn't make the behavior incorrect it does lead to redundant iterations. Co-authored-by: Billy Laws --- app/src/main/cpp/skyline/kernel/svc.cpp | 2 +- app/src/main/cpp/skyline/kernel/types/KProcess.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/cpp/skyline/kernel/svc.cpp b/app/src/main/cpp/skyline/kernel/svc.cpp index 7fe8d0d2..752f3b0d 100644 --- a/app/src/main/cpp/skyline/kernel/svc.cpp +++ b/app/src/main/cpp/skyline/kernel/svc.cpp @@ -365,7 +365,7 @@ namespace skyline::kernel::svc { // If the new priority is equivalent to the current priority then we don't need to CAS newPriority = thread->priority.load(); newPriority = std::min(newPriority, priority); - } while (newPriority != priority && thread->priority.compare_exchange_strong(newPriority, priority)); + } while (newPriority != priority && !thread->priority.compare_exchange_strong(newPriority, priority)); state.scheduler->UpdatePriority(thread); thread->UpdatePriorityInheritance(); } diff --git a/app/src/main/cpp/skyline/kernel/types/KProcess.cpp b/app/src/main/cpp/skyline/kernel/types/KProcess.cpp index 885bc318..f6856789 100644 --- a/app/src/main/cpp/skyline/kernel/types/KProcess.cpp +++ b/app/src/main/cpp/skyline/kernel/types/KProcess.cpp @@ -181,7 +181,7 @@ namespace skyline::kernel::type { do { basePriority = state.thread->basePriority.load(); newPriority = std::min(basePriority, highestPriorityThread->priority.load()); - } while (basePriority != newPriority && state.thread->priority.compare_exchange_strong(basePriority, newPriority)); + } while (basePriority != newPriority && !state.thread->priority.compare_exchange_strong(basePriority, newPriority)); state.scheduler->UpdatePriority(state.thread); } else { i8 priority, basePriority; @@ -199,7 +199,7 @@ namespace skyline::kernel::type { do { ownerPriority = nextOwner->priority.load(); priority = std::min(ownerPriority, nextWaiter->priority.load()); - } while (ownerPriority != priority && nextOwner->priority.compare_exchange_strong(ownerPriority, priority)); + } while (ownerPriority != priority && !nextOwner->priority.compare_exchange_strong(ownerPriority, priority)); __atomic_store_n(mutex, nextOwner->waitTag | HandleWaitersBit, __ATOMIC_SEQ_CST); } else {