diff --git a/Core/HLE/sceKernelSemaphore.cpp b/Core/HLE/sceKernelSemaphore.cpp index 53d9b2b426..09e808ee55 100644 --- a/Core/HLE/sceKernelSemaphore.cpp +++ b/Core/HLE/sceKernelSemaphore.cpp @@ -188,6 +188,8 @@ void __KernelSemaEndCallback(SceUID threadID, SceUID prevCallbackId, u32 &return s->pausedWaitTimeouts.erase(pauseKey); s->ns.numWaitThreads++; + // TODO: Don't wake up if __KernelCurHasReadyCallbacks()? + bool wokeThreads; // Attempt to unlock. if (__KernelUnlockSemaForThread(s, threadID, error, 0, wokeThreads)) @@ -419,7 +421,9 @@ int __KernelWaitSema(SceUID id, int wantedCount, u32 timeoutPtr, const char *bad if (wantedCount > s->ns.maxCount || wantedCount <= 0) return SCE_KERNEL_ERROR_ILLEGAL_COUNT; - if (s->ns.currentCount >= wantedCount && s->ns.numWaitThreads == 0) + // If there are any callbacks, we always wait, and wake after the callbacks. + bool hasCallbacks = processCallbacks && __KernelCurHasReadyCallbacks(); + if (s->ns.currentCount >= wantedCount && s->ns.numWaitThreads == 0 && !hasCallbacks) { s->ns.currentCount -= wantedCount; if (processCallbacks) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index fdfcf628bf..72704cac2d 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -2765,6 +2765,20 @@ void ActionAfterCallback::run(MipsCall &call) { } } +bool __KernelCurHasReadyCallbacks() { + if (readyCallbacksCount == 0) + return false; + + Thread *thread = __GetCurrentThread(); + for (int i = 0; i < THREAD_CALLBACK_NUM_TYPES; i++) { + if (thread->readyCallbacks[i].size()) { + return true; + } + } + + return false; +} + // Check callbacks on the current thread only. // Returns true if any callbacks were processed on the current thread. bool __KernelCheckThreadCallbacks(Thread *thread, bool force) diff --git a/Core/HLE/sceKernelThread.h b/Core/HLE/sceKernelThread.h index 7d3a405a9c..ba5532a0e4 100644 --- a/Core/HLE/sceKernelThread.h +++ b/Core/HLE/sceKernelThread.h @@ -216,6 +216,7 @@ bool __KernelInCallback(); // Should be called by (nearly) all ...CB functions. bool __KernelCheckCallbacks(); bool __KernelForceCallbacks(); +bool __KernelCurHasReadyCallbacks(); class Thread; void __KernelSwitchContext(Thread *target, const char *reason); bool __KernelExecutePendingMipsCalls(Thread *currentThread, bool reschedAfter);