mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-12-02 10:43:44 +01:00
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:
1 parent
f731e45f40
commit
fb9ac8397e
10 files changed
+116
-98
No files matched your search
@@ -294,16 +294,9 @@ static void __KernelSetSemaTimeout(PSPSemaphore *s, u32 timeoutPtr) {
|
||||
if (timeoutPtr == 0 || semaWaitTimer == -1)
|
||||
return;
|
||||
|
||||
int micro = (int)Memory::ReadOrException_U32(timeoutPtr);
|
||||
|
||||
// This happens to be how the hardware seems to time things.
|
||||
if (micro <= 3)
|
||||
micro = 24;
|
||||
else if (micro <= 249)
|
||||
micro = 245;
|
||||
|
||||
u32 micro = Memory::ReadOrException_U32(timeoutPtr);
|
||||
// This should call __KernelSemaTimeout() later, unless we cancel it.
|
||||
CoreTiming::ScheduleEvent(usToCycles(micro), semaWaitTimer, __KernelGetCurThread());
|
||||
CoreTiming::ScheduleEvent(usToCycles(__KernelWaitTimeoutUs(micro)), semaWaitTimer, __KernelGetCurThread());
|
||||
}
|
||||
|
||||
static int __KernelWaitSema(SceUID id, int wantedCount, u32 timeoutPtr, bool processCallbacks) {
|
||||
@@ -327,6 +320,8 @@ static int __KernelWaitSema(SceUID id, int wantedCount, u32 timeoutPtr, bool pro
|
||||
if (s->ns.currentCount >= wantedCount && s->waitingThreads.size() == 0 && !hasCallbacks) {
|
||||
s->ns.currentCount -= wantedCount;
|
||||
} else {
|
||||
if (!hasCallbacks && __KernelWaitTimesOutAtOnce(timeoutPtr))
|
||||
return SCE_KERNEL_ERROR_WAIT_TIMEOUT;
|
||||
SceUID threadID = __KernelGetCurThread();
|
||||
// May be in a tight loop timing out (where we don't remove from waitingThreads yet), don't want to add duplicates.
|
||||
if (std::find(s->waitingThreads.begin(), s->waitingThreads.end(), threadID) == s->waitingThreads.end())
|
||||
|
||||
Reference in new issue
Block a user