From 960da1596ca4e5ff8665bc6b6ece8d765fec9c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 28 Sep 2026 17:41:40 -0600 Subject: [PATCH] Threads: Charge for filling the stack on create, and for delete From pspautotests threads/scheduling/costs: sceKernelCreateThread takes about 150us plus roughly a cycle per byte of stack, which the kernel fills with 0xFF (1.3ms for 256KB), and sceKernelDeleteThread 50-100us whatever the stack size. Brings threads/scheduling/scheduling a good deal closer; what's left needs a thread that wakes during a long syscall to preempt it. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceKernelThread.cpp | 10 +++++++++- pspautotests | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 8cd668313b..e5b8091d1c 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -1919,7 +1919,13 @@ int __KernelCreateThread(const char *threadName, SceUID moduleID, u32 entry, u32 // Creating a thread resumes dispatch automatically. Probably can't create without it. dispatchEnabled = true; - hleEatCycles(32000); + // Measured by tests/threads/scheduling/costs: about 150us, plus filling the stack with 0xFF at + // around a cycle per byte - 1.3ms for a 256KB stack. + int createCycles = 32000; + if ((attr & PSP_THREAD_ATTR_NO_FILLSTACK) == 0 && stacksize > 0) { + createCycles += stacksize - stacksize / 64; + } + hleEatCycles(createCycles); // This won't schedule to the new thread, but it may to one woken from eating cycles. // Technically, this should not eat all at once, and reschedule in the middle, but that's hard. hleReSchedule("thread created"); @@ -2240,6 +2246,8 @@ int sceKernelDeleteThread(int threadID) { return hleLogError(Log::sceKernel, SCE_KERNEL_ERROR_NOT_DORMANT); } + // 50-100us whatever the stack size, per tests/threads/scheduling/costs. + hleEatCycles(15000); return hleLogDebug(Log::sceKernel, __KernelDeleteThread(threadID, SCE_KERNEL_ERROR_THREAD_TERMINATED, "thread deleted")); } } diff --git a/pspautotests b/pspautotests index c67239046d..aa3b943f18 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit c67239046db47d83351ced09e4e66d0cf3382f9c +Subproject commit aa3b943f18bcafe1289e284966cd9b581f973966