Fix JASDram allocation race condition

Not sure if useful as long-term plan involves abandoning the DSP entirely.
This commit is contained in:
PJB3005
2026-02-26 23:19:15 +01:00
parent 06801cbd66
commit b6a6c4fd0e
3 changed files with 40 additions and 2 deletions
+6
View File
@@ -31,6 +31,12 @@ struct JASAudioThread : public JKRThread, public JASGlobalInstance<JASAudioThrea
/* 0x84 */ bool sbPauseFlag; // type unsure
static volatile int snIntCount; // type unsure
#if TARGET_PC
static bool sThreadInitComplete;
static OSMutex sThreadInitCompleteMutex;
static OSCond sThreadInitCompleteCond;
#endif
};
#endif /* JASAUDIOTHREAD_H */
+18
View File
@@ -21,7 +21,18 @@ JASAudioThread::JASAudioThread(int stackSize, int msgCount, u32 threadPriority)
OSInitThreadQueue(&sThreadQueue);
}
#if TARGET_PC
bool JASAudioThread::sThreadInitComplete = false;
OSMutex JASAudioThread::sThreadInitCompleteMutex;
OSCond JASAudioThread::sThreadInitCompleteCond;
#endif
void JASAudioThread::create(s32 threadPriority) {
#if TARGET_PC
OSInitMutex(&sThreadInitCompleteMutex);
OSInitCond(&sThreadInitCompleteCond);
#endif
#if PLATFORM_GCN
const int size = 0x1400;
#else
@@ -68,6 +79,13 @@ void* JASAudioThread::run() {
JASPoolAllocObject_MultiThreaded<JASChannel>::newMemPool(0x48);
JASDriver::startDMA();
#if TARGET_PC
OSLockMutex(&sThreadInitCompleteMutex);
sThreadInitComplete = true;
OSUnlockMutex(&sThreadInitCompleteMutex);
OSSignalCond(&sThreadInitCompleteCond);
#endif
OSMessage msg;
while (true) {
msg = waitMessageBlock();
+16 -2
View File
@@ -1,16 +1,17 @@
#include "d/dolzel.h" // IWYU pragma: keep
#include "Z2AudioLib/Z2AudioMgr.h"
#include "JSystem/JAudio2/JASAiCtrl.h"
#include "JSystem/JAudio2/JASAudioThread.h"
#include "JSystem/JAudio2/JASDriverIF.h"
#include "JSystem/JAudio2/JASResArcLoader.h"
#include "JSystem/JAudio2/JASSeqParser.h"
#include "JSystem/JAudio2/JAUInitializer.h"
#include "JSystem/JAudio2/JAUSectionHeap.h"
#include "JSystem/JAudio2/JAUStreamAramMgr.h"
#include "JSystem/JAudio2/JAUSeqCollection.h"
#include "JSystem/JAudio2/JAUStreamAramMgr.h"
#include "JSystem/JKernel/JKRSolidHeap.h"
#include "Z2AudioLib/Z2AudioArcLoader.h"
#include "Z2AudioLib/Z2AudioMgr.h"
#include "Z2AudioLib/Z2Param.h"
#include "Z2AudioLib/Z2SoundHandles.h"
@@ -105,6 +106,19 @@ void Z2AudioMgr::init(JKRSolidHeap* heap, u32 memSize, void* baaData, JKRArchive
JASPoolAllocObject<Z2SoundHandlePool>::newMemPool(0x4e);
OS_REPORT("[Z2AudioMgr::init]before Create Section: %d\n", heap->getFreeSize());
#if TARGET_PC
// Fix a race condition with OS threading where JAUNewSectionHeap will use all the remaining
// space in the JASDram heap before JASAudioThread has finished initializing.
OSLockMutex(&JASAudioThread::sThreadInitCompleteMutex);
while (!JASAudioThread::sThreadInitComplete) {
OSWaitCond(
&JASAudioThread::sThreadInitCompleteCond,
&JASAudioThread::sThreadInitCompleteMutex);
}
OSUnlockMutex(&JASAudioThread::sThreadInitCompleteMutex);
#endif
JAUSectionHeap* sectionHeap = JAUNewSectionHeap(true);
sectionHeap->setSeqDataArchive(seqArc);
size_t resMaxSize = JASResArcLoader::getResMaxSize(seqArc);