Skip to content

Commit

Permalink
coreinit: Tweak JD2019 workaround to avoid XCX softlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Exzap committed May 26, 2024
1 parent aadd2f4 commit 1ee9d5c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Cafe/OS/libs/coreinit/coreinit_Synchronization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ namespace coreinit
currentThread->mutexQueue.removeMutex(mutex);
mutex->owner = nullptr;
if (!mutex->threadQueue.isEmpty())
mutex->threadQueue.wakeupSingleThreadWaitQueue(true);
mutex->threadQueue.wakeupSingleThreadWaitQueue(true, true);
}
// currentThread->cancelState = currentThread->cancelState & ~0x10000;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Cafe/OS/libs/coreinit/coreinit_Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,14 @@ namespace coreinit
}

// returns true if thread runs on same core and has higher priority
bool __OSCoreShouldSwitchToThread(OSThread_t* currentThread, OSThread_t* newThread)
bool __OSCoreShouldSwitchToThread(OSThread_t* currentThread, OSThread_t* newThread, bool sharedPriorityAndAffinityWorkaround)
{
uint32 coreIndex = OSGetCoreId();
if (!newThread->context.hasCoreAffinitySet(coreIndex))
return false;
// special case: if current and new thread are running only on the same core then reschedule even if priority is equal
// this resolves a deadlock in Just Dance 2019 where one thread would always reacquire the same mutex within it's timeslice, blocking another thread on the same core from acquiring it
if ((1<<coreIndex) == newThread->context.affinity && currentThread->context.affinity == newThread->context.affinity && currentThread->effectivePriority == newThread->effectivePriority)
if (sharedPriorityAndAffinityWorkaround && (1<<coreIndex) == newThread->context.affinity && currentThread->context.affinity == newThread->context.affinity && currentThread->effectivePriority == newThread->effectivePriority)
return true;
// otherwise reschedule if new thread has higher priority
return newThread->effectivePriority < currentThread->effectivePriority;
Expand All @@ -791,7 +791,7 @@ namespace coreinit
// todo - only set this once?
thread->wakeUpTime = PPCInterpreter_getMainCoreCycleCounter();
// reschedule if thread has higher priority
if (PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread))
if (PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread, false))
PPCCore_switchToSchedulerWithLock();
}
return previousSuspendCount;
Expand Down Expand Up @@ -948,7 +948,7 @@ namespace coreinit
OSThread_t* currentThread = OSGetCurrentThread();
if (currentThread && currentThread != thread)
{
if (__OSCoreShouldSwitchToThread(currentThread, thread))
if (__OSCoreShouldSwitchToThread(currentThread, thread, false))
PPCCore_switchToSchedulerWithLock();
}
__OSUnlockScheduler();
Expand Down
6 changes: 3 additions & 3 deletions src/Cafe/OS/libs/coreinit/coreinit_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ namespace coreinit

// counterparts for queueAndWait
void cancelWait(OSThread_t* thread);
void wakeupEntireWaitQueue(bool reschedule);
void wakeupSingleThreadWaitQueue(bool reschedule);
void wakeupEntireWaitQueue(bool reschedule, bool sharedPriorityAndAffinityWorkaround = false);
void wakeupSingleThreadWaitQueue(bool reschedule, bool sharedPriorityAndAffinityWorkaround = false);

private:
OSThread_t* takeFirstFromQueue(size_t linkOffset)
Expand Down Expand Up @@ -611,7 +611,7 @@ namespace coreinit

// internal
void __OSAddReadyThreadToRunQueue(OSThread_t* thread);
bool __OSCoreShouldSwitchToThread(OSThread_t* currentThread, OSThread_t* newThread);
bool __OSCoreShouldSwitchToThread(OSThread_t* currentThread, OSThread_t* newThread, bool sharedPriorityAndAffinityWorkaround);
void __OSQueueThreadDeallocation(OSThread_t* thread);

bool __OSIsThreadActive(OSThread_t* thread);
Expand Down
9 changes: 5 additions & 4 deletions src/Cafe/OS/libs/coreinit/coreinit_ThreadQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ namespace coreinit

// counterpart for queueAndWait
// if reschedule is true then scheduler will switch to woken up thread (if it is runnable on the same core)
void OSThreadQueueInternal::wakeupEntireWaitQueue(bool reschedule)
// sharedPriorityAndAffinityWorkaround is currently a hack/placeholder for some special cases. A proper fix likely involves handling all the nuances of thread effective priority
void OSThreadQueueInternal::wakeupEntireWaitQueue(bool reschedule, bool sharedPriorityAndAffinityWorkaround)
{
cemu_assert_debug(__OSHasSchedulerLock());
bool shouldReschedule = false;
Expand All @@ -139,7 +140,7 @@ namespace coreinit
thread->state = OSThread_t::THREAD_STATE::STATE_READY;
thread->currentWaitQueue = nullptr;
coreinit::__OSAddReadyThreadToRunQueue(thread);
if (reschedule && thread->suspendCounter == 0 && PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread))
if (reschedule && thread->suspendCounter == 0 && PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread, sharedPriorityAndAffinityWorkaround))
shouldReschedule = true;
}
if (shouldReschedule)
Expand All @@ -148,7 +149,7 @@ namespace coreinit

// counterpart for queueAndWait
// if reschedule is true then scheduler will switch to woken up thread (if it is runnable on the same core)
void OSThreadQueueInternal::wakeupSingleThreadWaitQueue(bool reschedule)
void OSThreadQueueInternal::wakeupSingleThreadWaitQueue(bool reschedule, bool sharedPriorityAndAffinityWorkaround)
{
cemu_assert_debug(__OSHasSchedulerLock());
OSThread_t* thread = takeFirstFromQueue(offsetof(OSThread_t, waitQueueLink));
Expand All @@ -159,7 +160,7 @@ namespace coreinit
thread->state = OSThread_t::THREAD_STATE::STATE_READY;
thread->currentWaitQueue = nullptr;
coreinit::__OSAddReadyThreadToRunQueue(thread);
if (reschedule && thread->suspendCounter == 0 && PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread))
if (reschedule && thread->suspendCounter == 0 && PPCInterpreter_getCurrentInstance() && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread, sharedPriorityAndAffinityWorkaround))
shouldReschedule = true;
}
if (shouldReschedule)
Expand Down

0 comments on commit 1ee9d5c

Please sign in to comment.