From 2a735f1fb72367ce72a6fae9a6034dd7ab979a2c Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Thu, 14 Sep 2023 20:22:54 +0200 Subject: [PATCH] coreinit: Use native COS locks instead of STL --- src/Cafe/OS/libs/coreinit/coreinit_FS.cpp | 17 +++++++++-------- src/Cafe/OS/libs/coreinit/coreinit_FS.h | 4 +++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Cafe/OS/libs/coreinit/coreinit_FS.cpp b/src/Cafe/OS/libs/coreinit/coreinit_FS.cpp index 26636eae..a2f59d4d 100644 --- a/src/Cafe/OS/libs/coreinit/coreinit_FS.cpp +++ b/src/Cafe/OS/libs/coreinit/coreinit_FS.cpp @@ -42,17 +42,16 @@ bool strcpy_whole(char* dst, size_t dstLength, const char* src) namespace coreinit { - std::mutex sFSClientLock; - std::recursive_mutex sFSGlobalMutex; + SysAllocator s_fsGlobalMutex; inline void FSLockMutex() { - sFSGlobalMutex.lock(); + OSLockMutex(&s_fsGlobalMutex); } inline void FSUnlockMutex() { - sFSGlobalMutex.unlock(); + OSUnlockMutex(&s_fsGlobalMutex); } void _debugVerifyCommand(const char* stage, FSCmdBlockBody_t* fsCmdBlockBody); @@ -251,7 +250,7 @@ namespace coreinit fsCmdQueueBE->dequeueHandlerFuncMPTR = _swapEndianU32(dequeueHandlerFuncMPTR); fsCmdQueueBE->numCommandsInFlight = 0; fsCmdQueueBE->numMaxCommandsInFlight = numMaxCommandsInFlight; - coreinit::OSInitMutexEx(&fsCmdQueueBE->mutex, nullptr); + coreinit::OSFastMutex_Init(&fsCmdQueueBE->fastMutex, nullptr); fsCmdQueueBE->firstMPTR = _swapEndianU32(0); fsCmdQueueBE->lastMPTR = _swapEndianU32(0); } @@ -672,12 +671,12 @@ namespace coreinit _debugVerifyCommand("FSCmdSubmitResult", fsCmdBlockBody); FSClientBody_t* fsClientBody = fsCmdBlockBody->fsClientBody.GetPtr(); - sFSClientLock.lock(); // OSFastMutex_Lock(&fsClientBody->fsCmdQueue.mutex) + OSFastMutex_Lock(&fsClientBody->fsCmdQueue.fastMutex); fsCmdBlockBody->cancelState &= ~(1 << 0); // clear cancel bit if (fsClientBody->currentCmdBlockBody.GetPtr() == fsCmdBlockBody) fsClientBody->currentCmdBlockBody = nullptr; fsCmdBlockBody->statusCode = _swapEndianU32(FSA_CMD_STATUS_CODE_D900A24); - sFSClientLock.unlock(); + OSFastMutex_Unlock(&fsClientBody->fsCmdQueue.fastMutex); // send result via msg queue or callback cemu_assert_debug(!fsCmdBlockBody->asyncResult.fsAsyncParamsNew.ioMsgQueue != !fsCmdBlockBody->asyncResult.fsAsyncParamsNew.userCallback); // either must be set fsCmdBlockBody->ukn09EA = 0; @@ -1433,7 +1432,7 @@ namespace coreinit return (FSStatus)FS_RESULT::SUCCESS; } - sint32 FSAppendFile(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, uint32 size, uint32 count, uint32 fileHandle, uint32 errorMask) + sint32 FSAppendFile(FSClient_t* fsClient, FSCmdBlock_t* fsCmdBlock, uint32 size, uint32 count, uint32 fileHandle, uint32 errorMask) { StackAllocator asyncParams; __FSAsyncToSyncInit(fsClient, fsCmdBlock, asyncParams); @@ -2640,6 +2639,8 @@ namespace coreinit void InitializeFS() { + OSInitMutex(&s_fsGlobalMutex); + cafeExportRegister("coreinit", FSInit, LogType::CoreinitFile); cafeExportRegister("coreinit", FSShutdown, LogType::CoreinitFile); diff --git a/src/Cafe/OS/libs/coreinit/coreinit_FS.h b/src/Cafe/OS/libs/coreinit/coreinit_FS.h index 0355c9aa..2a57f7da 100644 --- a/src/Cafe/OS/libs/coreinit/coreinit_FS.h +++ b/src/Cafe/OS/libs/coreinit/coreinit_FS.h @@ -42,7 +42,7 @@ namespace coreinit /* +0x00 */ MPTR firstMPTR; /* +0x04 */ MPTR lastMPTR; - /* +0x08 */ OSMutex mutex; + /* +0x08 */ OSFastMutex fastMutex; /* +0x34 */ MPTR dequeueHandlerFuncMPTR; /* +0x38 */ uint32be numCommandsInFlight; /* +0x3C */ uint32 numMaxCommandsInFlight; @@ -50,6 +50,8 @@ namespace coreinit }; DEFINE_ENUM_FLAG_OPERATORS(FSCmdQueue::QUEUE_FLAG); + static_assert(sizeof(FSCmdQueue) == 0x44); + #define FS_CLIENT_BUFFER_SIZE (5888) #define FS_CMD_BLOCK_SIZE (2688)