Merge pull request #205 from TakaRikka/shutdown

Allow threads to gracefully shutdown
This commit is contained in:
TakaRikka
2026-04-01 18:32:31 -07:00
committed by GitHub
16 changed files with 135 additions and 158 deletions
+1 -1
@@ -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);
}
@@ -85,7 +85,15 @@ void* JASTaskThread::run() {
JASThreadCallStack* callstack;
OSInitFastCast();
do {
#ifdef TARGET_PC
BOOL received = FALSE;
callstack = static_cast<JASThreadCallStack*>(waitMessageBlock(&received));
if (!received) {
break;
}
#else
callstack = static_cast<JASThreadCallStack*>(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) {
@@ -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);
+9
View File
@@ -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) {
@@ -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() {
+9
View File
@@ -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,
+8
View File
@@ -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();
+2
View File
@@ -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;
+5 -4
View File
@@ -20,10 +20,9 @@
#include <cstdio>
#include <cstring>
#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
}
}
}
+9
View File
@@ -72,6 +72,15 @@ static PCCondData& GetCondData(OSCond* cond) {
return *it->second;
}
void ClearCondMap() {
std::lock_guard<std::mutex> lock(GetCondMapMutex());
auto& map = GetCondMap();
for (auto& pair : map) {
pair.second->cv.notify_all();
}
map.clear();
}
// ============================================================================
// C API functions
// ============================================================================
-101
View File
@@ -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<duration_t, 4> 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<duration_t>(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<double>(freq.QuadPart) / 1000000000.0;
m_initialized = true;
}
DWORD ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
auto tickCount =
static_cast<LONGLONG>(static_cast<double>(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::nanoseconds>(std::chrono::seconds{ 1 }) / 60);
}
+28 -5
View File
@@ -11,7 +11,7 @@
#include <unordered_map>
#include <memory>
#include <dusk/logging.h>
#include <dusk/main.h>
#ifndef _WIN32
#include <sys/time.h>
@@ -84,6 +84,16 @@ static PCMessageQueueData& GetMsgQueueData(OSMessageQueue* mq) {
return *it->second;
}
static void ClearMsgQueueMap() {
std::lock_guard<std::mutex> 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;
+14 -36
View File
@@ -5,17 +5,11 @@
#include "m_Do/m_Do_DVDError.h"
#include "JSystem/JKernel/JKRAssertHeap.h"
#include <os.h>
#include <dolphin/os.h>
#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 <chrono>
#include <thread>
#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);
}
+12 -1
View File
@@ -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 <revolution/nand.h>
@@ -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:
+3 -10
View File
@@ -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();