Threads: Wait timeouts work like hardware's, one rule for all of them

Every wait with a timeout behaves the same on hardware (pspautotests
threads/scheduling/waittimeouts). The deadline is taken, and the alarm set
up a moment later. If the deadline has passed by then, the wait fails with
WAIT_TIMEOUT at once, without yielding or writing the timeout back. That's
usual for 0us, half the time for 1us, and rare after; AllocateVpl does more
first. Otherwise it ends max(t, 205us) + ~35us after the call. Each object
had its own guess (24/245, 25/250, 20/250 and so on), and only MsgPipe had
the immediate case.

__KernelWaitTimesOutAtOnce() and __KernelWaitTimeoutUs() now do it for
semaphores, event flags, mutexes, lwmutexes, mbx, msgpipes, fpl, vpl and
WaitThreadEnd. The latency past the deadline isn't counted in the time
left written back.

Outcomes that hardware decides by the clock's phase (these, and
sceKernelDelayThread returning at once) go with the likelier one. Ones
between 50% and certain are instead spread evenly over calls, so a polling
loop can't lock into never yielding (sceKernelThread section version 7).
This replaces the pseudo-random choice for delays.

Also adds threads/scheduling/readyqueue, which already passes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-09-30 09:06:30 -06:00
1 parent f731e45f40
commit fb9ac8397e
10 files changed
+116 -98

No files matched your search

+6 -10
View File
@@ -302,16 +302,8 @@ static void __KernelSetEventFlagTimeout(EventFlag *e, u32 timeoutPtr) {
if (timeoutPtr == 0 || eventFlagWaitTimer == -1)
return;
int micro = (int) Memory::ReadOrException_U32(timeoutPtr);
// This seems like the actual timing of timeouts on hardware.
if (micro <= 1)
micro = 25;
else if (micro <= 209)
micro = 240;
// This should call __KernelEventFlagTimeout() later, unless we cancel it.
CoreTiming::ScheduleEvent(usToCycles(micro), eventFlagWaitTimer, __KernelGetCurThread());
u32 micro = Memory::ReadOrException_U32(timeoutPtr);
CoreTiming::ScheduleEvent(usToCycles(__KernelWaitTimeoutUs(micro)), eventFlagWaitTimer, __KernelGetCurThread());
}
int sceKernelWaitEventFlag(SceUID id, u32 bits, u32 wait, u32 outBitsPtr, u32 timeoutPtr) {
@@ -345,6 +337,8 @@ int sceKernelWaitEventFlag(SceUID id, u32 bits, u32 wait, u32 outBitsPtr, u32 ti
return hleLogDebug(Log::sceKernel, SCE_KERNEL_ERROR_EVF_MULTI);
}
if (__KernelWaitTimesOutAtOnce(timeoutPtr))
return hleLogDebug(Log::sceKernel, SCE_KERNEL_ERROR_WAIT_TIMEOUT, "timed out at once");
(void)hleLogDebug(Log::sceKernel, 0, "waiting");
// No match - must wait.
@@ -408,6 +402,8 @@ int sceKernelWaitEventFlagCB(SceUID id, u32 bits, u32 wait, u32 outBitsPtr, u32
return hleLogDebug(Log::sceKernel, SCE_KERNEL_ERROR_EVF_MULTI);
}
if (!doCallbackWait && __KernelWaitTimesOutAtOnce(timeoutPtr))
return hleLogDebug(Log::sceKernel, SCE_KERNEL_ERROR_WAIT_TIMEOUT, "timed out at once");
(void)hleLogDebug(Log::sceKernel, 0, "waiting");
// No match - must wait.