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 -5
View File
@@ -269,15 +269,16 @@ static bool __KernelSetMsgPipeTimeout(u32 timeoutPtr) {
if (timeoutPtr == 0 || waitTimer == -1)
return true;
int micro = (int)Memory::ReadOrException_U32(timeoutPtr);
if (micro <= 2) {
// Always at once up to 2us, never from 3us: what threads/msgpipe/send and receive record, where
// each call comes straight after a checkpoint and the code is presumably cold. Back to back the
// hardware does 2us at once less often, as other waits do.
if (__KernelWaitTimesOutAtOnce(timeoutPtr, 210, 55)) {
// Don't wait or reschedule, just timeout immediately.
return false;
}
if (micro <= 210)
micro = 250;
CoreTiming::ScheduleEvent(usToCycles(micro), waitTimer, __KernelGetCurThread());
const u32 micro = Memory::ReadOrException_U32(timeoutPtr);
CoreTiming::ScheduleEvent(usToCycles(__KernelWaitTimeoutUs(micro)), waitTimer, __KernelGetCurThread());
return true;
}