From 7642d72a52d91af859f27178f620e3037252c48d Mon Sep 17 00:00:00 2001 From: Luke Street Date: Wed, 1 Apr 2026 18:30:12 -0600 Subject: [PATCH] Allow threads to gracefully shutdown --- extern/aurora | 2 +- .../include/JSystem/JKernel/JKRThread.h | 11 ++ libs/JSystem/src/JAudio2/JASTaskThread.cpp | 11 ++ libs/JSystem/src/JFramework/JFWSystem.cpp | 4 + libs/JSystem/src/JKernel/JKRAram.cpp | 9 ++ libs/JSystem/src/JKernel/JKRAramStream.cpp | 9 ++ libs/JSystem/src/JKernel/JKRDecomp.cpp | 9 ++ libs/JSystem/src/JKernel/JKRThread.cpp | 8 ++ libs/JSystem/src/JUtility/JUTVideo.cpp | 2 + src/d/d_resorce.cpp | 9 +- src/dusk/OSMutex.cpp | 9 ++ src/dusk/imgui/ImGuiConsole.cpp | 101 ------------------ src/dusk/stubs.cpp | 33 +++++- src/m_Do/m_Do_DVDError.cpp | 50 +++------ src/m_Do/m_Do_MemCard.cpp | 13 ++- src/m_Do/m_Do_main.cpp | 13 +-- 16 files changed, 135 insertions(+), 158 deletions(-) diff --git a/extern/aurora b/extern/aurora index d7722670ae..31ee02fe06 160000 --- a/extern/aurora +++ b/extern/aurora @@ -1 +1 @@ -Subproject commit d7722670ae3b1977e513eea0167a3d9009c606c2 +Subproject commit 31ee02fe0636d56a15fbe147e9e8673b84943a1e diff --git a/libs/JSystem/include/JSystem/JKernel/JKRThread.h b/libs/JSystem/include/JSystem/JKernel/JKRThread.h index 2830252281..79217f3675 100644 --- a/libs/JSystem/include/JSystem/JKernel/JKRThread.h +++ b/libs/JSystem/include/JSystem/JKernel/JKRThread.h @@ -101,11 +101,22 @@ public: } return message; } +#ifdef TARGET_PC + OSMessage waitMessageBlock(BOOL* received) { + OSMessage message; + BOOL rv = OSReceiveMessage(&mMessageQueue, &message, OS_MESSAGE_BLOCK); + if (received) { + *received = rv; + } + return message; + } +#else OSMessage waitMessageBlock() { OSMessage message; OSReceiveMessage(&mMessageQueue, &message, OS_MESSAGE_BLOCK); return message; } +#endif void jamMessageBlock(OSMessage message) { OSJamMessage(&mMessageQueue, message, OS_MESSAGE_BLOCK); } diff --git a/libs/JSystem/src/JAudio2/JASTaskThread.cpp b/libs/JSystem/src/JAudio2/JASTaskThread.cpp index 365424deb4..d5dfd089ea 100644 --- a/libs/JSystem/src/JAudio2/JASTaskThread.cpp +++ b/libs/JSystem/src/JAudio2/JASTaskThread.cpp @@ -85,7 +85,15 @@ void* JASTaskThread::run() { JASThreadCallStack* callstack; OSInitFastCast(); do { +#ifdef TARGET_PC + BOOL received = FALSE; + callstack = static_cast(waitMessageBlock(&received)); + if (!received) { + break; + } +#else callstack = static_cast(waitMessageBlock()); +#endif if (field_0x84) { OSSleepThread(&threadQueue_); } @@ -98,6 +106,9 @@ void* JASTaskThread::run() { JASKernel::getCommandHeap()->free(callstack); } while (true); +#ifdef TARGET_PC + return NULL; +#endif } void JASTaskThread::pause(bool param_0) { diff --git a/libs/JSystem/src/JFramework/JFWSystem.cpp b/libs/JSystem/src/JFramework/JFWSystem.cpp index e84d19a824..b4effd547a 100644 --- a/libs/JSystem/src/JFramework/JFWSystem.cpp +++ b/libs/JSystem/src/JFramework/JFWSystem.cpp @@ -79,11 +79,15 @@ void JFWSystem::init() { JUTGamePad::init(); +#ifndef TARGET_PC JUTDirectPrint* dbPrint = JUTDirectPrint::start(); +#endif JUTAssertion::create(); +#ifndef TARGET_PC JUTException::create(dbPrint); +#endif systemFont = JKR_NEW JUTResFont(CSetUpParam::systemFontRes, NULL); diff --git a/libs/JSystem/src/JKernel/JKRAram.cpp b/libs/JSystem/src/JKernel/JKRAram.cpp index 8049ee0069..32afe1cef4 100644 --- a/libs/JSystem/src/JKernel/JKRAram.cpp +++ b/libs/JSystem/src/JKernel/JKRAram.cpp @@ -94,7 +94,13 @@ void* JKRAram::run(void) { OSInitMessageQueue(&sMessageQueue, sMessageBuffer, 4); do { OSMessage msg; +#ifdef TARGET_PC + if (!OSReceiveMessage(&sMessageQueue, &msg, OS_MESSAGE_BLOCK)) { + break; + } +#else OSReceiveMessage(&sMessageQueue, &msg, OS_MESSAGE_BLOCK); +#endif JKRAramCommand* message = (JKRAramCommand*)msg; int result = message->field_0x00; JKRAMCommand* command = (JKRAMCommand*)message->command; @@ -106,6 +112,9 @@ void* JKRAram::run(void) { break; } } while (true); +#ifdef TARGET_PC + return NULL; +#endif } void JKRAram::checkOkAddress(u8* addr, u32 size, JKRAramBlock* block, u32 param_4) { diff --git a/libs/JSystem/src/JKernel/JKRAramStream.cpp b/libs/JSystem/src/JKernel/JKRAramStream.cpp index a9dc570ae0..a30f23dfaa 100644 --- a/libs/JSystem/src/JKernel/JKRAramStream.cpp +++ b/libs/JSystem/src/JKernel/JKRAramStream.cpp @@ -43,7 +43,13 @@ void* JKRAramStream::run() { for (;;) { OSMessage message; +#ifdef TARGET_PC + if (!OSReceiveMessage(&sMessageQueue, &message, OS_MESSAGE_BLOCK)) { + break; + } +#else OSReceiveMessage(&sMessageQueue, &message, OS_MESSAGE_BLOCK); +#endif JKRAramStreamCommand* command = (JKRAramStreamCommand*)message; switch (command->mType) { @@ -55,6 +61,9 @@ void* JKRAramStream::run() { break; } } +#ifdef TARGET_PC + return NULL; +#endif } s32 JKRAramStream::readFromAram() { diff --git a/libs/JSystem/src/JKernel/JKRDecomp.cpp b/libs/JSystem/src/JKernel/JKRDecomp.cpp index 3f01e60e15..776823531d 100644 --- a/libs/JSystem/src/JKernel/JKRDecomp.cpp +++ b/libs/JSystem/src/JKernel/JKRDecomp.cpp @@ -34,7 +34,13 @@ void* JKRDecomp::run() { OSInitMessageQueue(&sMessageQueue, sMessageBuffer, 8); for (;;) { OSMessage message; +#ifdef TARGET_PC + if (!OSReceiveMessage(&sMessageQueue, &message, OS_MESSAGE_BLOCK)) { + break; + } +#else OSReceiveMessage(&sMessageQueue, &message, OS_MESSAGE_BLOCK); +#endif JKRDecompCommand* command = (JKRDecompCommand*)message; decode(command->mSrcBuffer, command->mDstBuffer, command->mSrcLength, command->mDstLength); @@ -57,6 +63,9 @@ void* JKRDecomp::run() { OSSendMessage(&command->mMessageQueue, (OSMessage)1, OS_MESSAGE_NOBLOCK); } } +#ifdef TARGET_PC + return NULL; +#endif } JKRDecompCommand* JKRDecomp::prepareCommand(u8* srcBuffer, u8* dstBuffer, u32 srcLength, diff --git a/libs/JSystem/src/JKernel/JKRThread.cpp b/libs/JSystem/src/JKernel/JKRThread.cpp index 6ba97e3d4a..f90b00422c 100644 --- a/libs/JSystem/src/JKernel/JKRThread.cpp +++ b/libs/JSystem/src/JKernel/JKRThread.cpp @@ -309,7 +309,15 @@ void* JKRTask::run() { }; OSInitFastCast(); while (true) { +#ifdef TARGET_PC + BOOL received = FALSE; + TaskMessage* msg = (TaskMessage*)waitMessageBlock(&received); + if (!received) { + break; + } +#else TaskMessage* msg = (TaskMessage*)waitMessageBlock(); +#endif if (msg->field_0x0) { msg->field_0x0(msg->field_0x4); check(); diff --git a/libs/JSystem/src/JUtility/JUTVideo.cpp b/libs/JSystem/src/JUtility/JUTVideo.cpp index 1656aa7e0f..bb1f2cee3b 100644 --- a/libs/JSystem/src/JUtility/JUTVideo.cpp +++ b/libs/JSystem/src/JUtility/JUTVideo.cpp @@ -81,12 +81,14 @@ void JUTVideo::preRetraceProc(u32 retrace_count) { static void* frameBuffer = NULL; +#ifndef TARGET_PC if (frameBuffer) { const GXRenderModeObj* renderMode = JUTGetVideoManager()->getRenderMode(); u16 width = renderMode->fbWidth; u16 height = renderMode->efbHeight; JUTDirectPrint::getManager()->changeFrameBuffer(frameBuffer, width, height); } +#endif if (getManager()->mSetBlack == 1) { s32 frame_count = getManager()->mSetBlackFrameCount; diff --git a/src/d/d_resorce.cpp b/src/d/d_resorce.cpp index ab21e140c0..0bf537bfd7 100644 --- a/src/d/d_resorce.cpp +++ b/src/d/d_resorce.cpp @@ -20,10 +20,9 @@ #include #include -#include "dusk/logging.h" - #ifndef __MWERKS__ #include "dusk/extras.h" +#include "dusk/logging.h" #endif dRes_info_c::dRes_info_c() { @@ -102,11 +101,13 @@ static void setIndirectTex(J3DModelData* i_modelData) { if (memcmp(textureName, "dummy", 6) == 0) { texture->setResTIMG(i, *mDoGph_gInf_c::getFrameBufferTimg()); } -#if !TARGET_PC if (memcmp(textureName, "Zbuffer", 8) == 0) { +#if !TARGET_PC texture->setResTIMG(i, *mDoGph_gInf_c::getZbufferTimg()); - } +#else + DuskLog.warn("Zbuffer texture binding not yet supported"); #endif + } } } diff --git a/src/dusk/OSMutex.cpp b/src/dusk/OSMutex.cpp index 8e26402e0d..ef1a528f1b 100644 --- a/src/dusk/OSMutex.cpp +++ b/src/dusk/OSMutex.cpp @@ -72,6 +72,15 @@ static PCCondData& GetCondData(OSCond* cond) { return *it->second; } +void ClearCondMap() { + std::lock_guard lock(GetCondMapMutex()); + auto& map = GetCondMap(); + for (auto& pair : map) { + pair.second->cv.notify_all(); + } + map.clear(); +} + // ============================================================================ // C API functions // ============================================================================ diff --git a/src/dusk/imgui/ImGuiConsole.cpp b/src/dusk/imgui/ImGuiConsole.cpp index 60ba229ead..52bbef5f85 100644 --- a/src/dusk/imgui/ImGuiConsole.cpp +++ b/src/dusk/imgui/ImGuiConsole.cpp @@ -223,104 +223,3 @@ namespace dusk { } } } - -class Limiter -{ - using delta_clock = std::chrono::high_resolution_clock; - using duration_t = std::chrono::nanoseconds; - -public: - void Reset() - { - m_oldTime = delta_clock::now(); - } - - void Sleep(duration_t targetFrameTime) - { - if (targetFrameTime.count() == 0) - { - return; - } - - auto start = delta_clock::now(); - duration_t adjustedSleepTime = SleepTime(targetFrameTime); - if (adjustedSleepTime.count() > 0) - { - NanoSleep(adjustedSleepTime); - duration_t overslept = TimeSince(start) - adjustedSleepTime; - if (overslept < duration_t{ targetFrameTime }) - { - m_overheadTimes[m_overheadTimeIdx] = overslept; - m_overheadTimeIdx = (m_overheadTimeIdx + 1) % m_overheadTimes.size(); - } - } - Reset(); - } - - duration_t SleepTime(duration_t targetFrameTime) - { - const auto sleepTime = duration_t{ targetFrameTime } - TimeSince(m_oldTime); - m_overhead = std::accumulate(m_overheadTimes.begin(), m_overheadTimes.end(), duration_t{}) / - m_overheadTimes.size(); - if (sleepTime > m_overhead) - { - return sleepTime - m_overhead; - } - return duration_t{ 0 }; - } - -private: - delta_clock::time_point m_oldTime; - std::array m_overheadTimes{}; - size_t m_overheadTimeIdx = 0; - duration_t m_overhead = duration_t{ 0 }; - - duration_t TimeSince(delta_clock::time_point start) - { - return std::chrono::duration_cast(delta_clock::now() - start); - } - -#if _WIN32 - bool m_initialized; - double m_countPerNs; - - void NanoSleep(const duration_t duration) - { - if (!m_initialized) - { - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - m_countPerNs = static_cast(freq.QuadPart) / 1000000000.0; - m_initialized = true; - } - - DWORD ms = std::chrono::duration_cast(duration).count(); - auto tickCount = - static_cast(static_cast(duration.count()) * m_countPerNs); - LARGE_INTEGER count; - QueryPerformanceCounter(&count); - if (ms > 10) - { - // Adjust for Sleep overhead - ::Sleep(ms - 10); - } - auto end = count.QuadPart + tickCount; - do - { - QueryPerformanceCounter(&count); - } while (count.QuadPart < end); - } -#else - void NanoSleep(const duration_t duration) - { - std::this_thread::sleep_for(duration); - } -#endif -}; - -static Limiter g_frameLimiter; -void frame_limiter() -{ - g_frameLimiter.Sleep( - std::chrono::duration_cast(std::chrono::seconds{ 1 }) / 60); -} diff --git a/src/dusk/stubs.cpp b/src/dusk/stubs.cpp index 4e259ff97e..0b9520f3e3 100644 --- a/src/dusk/stubs.cpp +++ b/src/dusk/stubs.cpp @@ -11,7 +11,7 @@ #include #include #include - +#include #ifndef _WIN32 #include @@ -84,6 +84,16 @@ static PCMessageQueueData& GetMsgQueueData(OSMessageQueue* mq) { return *it->second; } +static void ClearMsgQueueMap() { + std::lock_guard lock(GetMsgQueueMapMutex()); + auto& map = GetMsgQueueMap(); + for (auto & [_, value] : map) { + value->cvReceive.notify_all(); + value->cvSend.notify_all(); + } + map.clear(); +} + void OSInitMessageQueue(OSMessageQueue* mq, void* msgArray, s32 msgCount) { if (!mq) return; mq->queueSend.head = mq->queueSend.tail = nullptr; @@ -104,7 +114,10 @@ int OSSendMessage(OSMessageQueue* mq, void* msg, s32 flags) { if (mq->usedCount >= mq->msgCount) { if (flags == OS_MESSAGE_NOBLOCK) return 0; // BLOCK: wait until space is available - data.cvSend.wait(lock, [mq]() { return mq->usedCount < mq->msgCount; }); + data.cvSend.wait(lock, [mq] { return mq->usedCount < mq->msgCount || dusk::IsShuttingDown; }); + } + if (dusk::IsShuttingDown) { + return 0; } s32 idx = (mq->firstIndex + mq->usedCount) % mq->msgCount; @@ -124,7 +137,10 @@ int OSReceiveMessage(OSMessageQueue* mq, void* msg, s32 flags) { if (mq->usedCount == 0) { if (flags == OS_MESSAGE_NOBLOCK) return 0; // BLOCK: wait until a message arrives - data.cvReceive.wait(lock, [mq]() { return mq->usedCount > 0; }); + data.cvReceive.wait(lock, [mq] { return mq->usedCount > 0 || dusk::IsShuttingDown; }); + } + if (dusk::IsShuttingDown) { + return 0; } if (msg) { @@ -146,7 +162,10 @@ int OSJamMessage(OSMessageQueue* mq, void* msg, s32 flags) { if (mq->usedCount >= mq->msgCount) { if (flags == OS_MESSAGE_NOBLOCK) return 0; // BLOCK: wait until space is available - data.cvSend.wait(lock, [mq]() { return mq->usedCount < mq->msgCount; }); + data.cvSend.wait(lock, [mq] { return mq->usedCount < mq->msgCount || dusk::IsShuttingDown; }); + } + if (dusk::IsShuttingDown) { + return 0; } // Jam inserts at the front of the queue @@ -185,8 +204,12 @@ BOOL OSGetResetButtonState() { return FALSE; } BOOL OSInitFont(OSFontHeader* fontData) { return FALSE; } BOOL OSLink(OSModuleInfo* newModule, void* bss) { return TRUE; } +void ClearCondMap(); void OSResetSystem(int reset, u32 resetCode, BOOL forceMenu) { OSReport("[PC] OSResetSystem called (reset=%d, code=%u)\n", reset, resetCode); + dusk::IsShuttingDown = true; + ClearMsgQueueMap(); + ClearCondMap(); } void OSSetStringTable(void* stringTable) {} @@ -998,7 +1021,7 @@ f32 GXGetYScaleFactor(u16 efbHeight, u16 xfbHeight) { void GXInitTexCacheRegion(GXTexRegion* region, GXBool is_32b_mipmap, u32 tmem_even, GXTexCacheSize size_even, u32 tmem_odd, GXTexCacheSize size_odd) { STUB_LOG(); -} +} // XXX, this should be some struct? // GXRenderModeObj GXNtsc480IntDf; //GXRenderModeObj GXNtsc480Int; diff --git a/src/m_Do/m_Do_DVDError.cpp b/src/m_Do/m_Do_DVDError.cpp index 62fd71ed21..2cfef5fbb4 100644 --- a/src/m_Do/m_Do_DVDError.cpp +++ b/src/m_Do/m_Do_DVDError.cpp @@ -5,17 +5,11 @@ #include "m_Do/m_Do_DVDError.h" #include "JSystem/JKernel/JKRAssertHeap.h" -#include +#include #include "m_Do/m_Do_dvd_thread.h" #include "m_Do/m_Do_ext.h" #include "m_Do/m_Do_Reset.h" -// Added for the sleep workaround -#include -#include - -#include "dusk/os.h" - #if PLATFORM_GCN const int stack_size = 3072; #else @@ -31,24 +25,25 @@ static OSThread DvdErr_thread; static u8 DvdErr_stack[stack_size] ATTRIBUTE_ALIGN(16); #pragma pop -// Alarm is not needed for the PC workaround -// static OSAlarm Alarm; +static OSAlarm Alarm; void mDoDvdErr_ThdInit() { +#ifdef TARGET_PC + // Thread is not necessary on PC + return; +#endif + if (mDoDvdErr_initialized) { return; } - // OSTime time = OSGetTime(); // Unused in workaround + OSTime time = OSGetTime(); - OSCreateThread(&DvdErr_thread, (void* (*)(void*))mDoDvdErr_Watch, NULL, - DvdErr_stack + sizeof(DvdErr_stack), sizeof(DvdErr_stack), - OSGetThreadPriority(OSGetCurrentThread()) - 3, 1); + OSCreateThread(&DvdErr_thread, (void*(*)(void*))mDoDvdErr_Watch, NULL, DvdErr_stack + sizeof(DvdErr_stack), + sizeof(DvdErr_stack), OSGetThreadPriority(OSGetCurrentThread()) - 3, 1); OSResumeThread(&DvdErr_thread); - - // PC Workaround: Disable Alarm logic. The thread will sleep itself. - // OSCreateAlarm(&Alarm); - // OSSetPeriodicAlarm(&Alarm, time, OS_BUS_CLOCK / 4, AlarmHandler); + OSCreateAlarm(&Alarm); + OSSetPeriodicAlarm(&Alarm, time, OS_BUS_CLOCK / 4, AlarmHandler); mDoDvdErr_initialized = true; } @@ -56,22 +51,15 @@ void mDoDvdErr_ThdInit() { void mDoDvdErr_ThdCleanup() { if (mDoDvdErr_initialized) { OSCancelThread(&DvdErr_thread); - // OSCancelAlarm(&Alarm); // Disable Alarm cancel + OSCancelAlarm(&Alarm); mDoDvdErr_initialized = false; } } static void mDoDvdErr_Watch(void*) { #if PLATFORM_GCN -#ifndef TARGET_PC OSDisableInterrupts(); #endif -#endif - -#if TARGET_PC - OSSetCurrentThreadName("DVD error thread"); -#endif - JKRThread(OSGetCurrentThread(), 0); JKRSetCurrentHeap(mDoExt_getAssertHeap()); @@ -82,20 +70,10 @@ static void mDoDvdErr_Watch(void*) { if (status == DVD_STATE_FATAL_ERROR) { mDoDvdThd::suspend(); } - - // PC Workaround: - // Instead of suspending and waiting for an Alarm (which might not be implemented), - // we simply sleep for a short duration. - // OS_BUS_CLOCK / 4 corresponds to roughly 1/4th of a second on GC. - // We use 250ms here to simulate the periodic check. - - // OSSuspendThread(&DvdErr_thread); // <-- Original causing deadlock without Alarm - std::this_thread::sleep_for(std::chrono::milliseconds(250)); - + OSSuspendThread(&DvdErr_thread); } while (true); } static void AlarmHandler(OSAlarm*, OSContext*) { - // This handler is no longer called in the PC workaround OSResumeThread(&DvdErr_thread); } \ No newline at end of file diff --git a/src/m_Do/m_Do_MemCard.cpp b/src/m_Do/m_Do_MemCard.cpp index bbb94f2bcd..36de5480e5 100644 --- a/src/m_Do/m_Do_MemCard.cpp +++ b/src/m_Do/m_Do_MemCard.cpp @@ -11,6 +11,7 @@ #include "m_Do/m_Do_Reset.h" #include "os_report.h" #include "dusk/os.h" +#include "dusk/main.h" #if PLATFORM_WII || PLATFORM_SHIELD #include @@ -103,11 +104,21 @@ void mDoMemCd_Ctrl_c::ThdInit() { void mDoMemCd_Ctrl_c::main() { do { OSLockMutex(&mMutex); - while (mCardCommand == COMM_NONE_e) { + while (mCardCommand == COMM_NONE_e +#ifdef TARGET_PC + && !dusk::IsShuttingDown +#endif + ) { OSWaitCond(&mCond, &mMutex); } OSUnlockMutex(&mMutex); +#ifdef TARGET_PC + if (dusk::IsShuttingDown) { + break; + } +#endif + switch (mCardCommand) { #if PLATFORM_GCN || PLATFORM_WII case COMM_RESTORE_e: diff --git a/src/m_Do/m_Do_main.cpp b/src/m_Do/m_Do_main.cpp index b3fb05b0b9..03ff6b8c0f 100644 --- a/src/m_Do/m_Do_main.cpp +++ b/src/m_Do/m_Do_main.cpp @@ -108,12 +108,8 @@ s32 LOAD_COPYDATE(void*) { AuroraInfo auroraInfo; void main01(void) { - #if TARGET_PC - Limiter frameLimiter{}; - #endif - OS_REPORT("\x1b[m"); - GXSetColorUpdate(GX_ENABLE); + // 1. Setup mDoMch_Create(); mDoGph_Create(); @@ -191,10 +187,6 @@ void main01(void) { mDoAud_Execute(); aurora_end_frame(); - - #if TARGET_PC - frameLimiter.Sleep(DUSK_FRAME_PERIOD); - #endif } while (true); exit:; @@ -312,7 +304,8 @@ int game_main(int argc, char* argv[]) { fflush(stdout); fflush(stderr); - dusk::IsShuttingDown = true; + // Notifies all CVs and causes threads to exit + OSResetSystem(OS_RESET_SHUTDOWN, 0, 0); aurora_shutdown();