Skip to content

Commit

Permalink
coreinit: Use native COS locks instead of STL
Browse files Browse the repository at this point in the history
  • Loading branch information
Exzap committed Sep 14, 2023
1 parent 14dd7a7 commit 2a735f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/Cafe/OS/libs/coreinit/coreinit_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OSMutex> 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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<FSAsyncParamsNew_t> asyncParams;
__FSAsyncToSyncInit(fsClient, fsCmdBlock, asyncParams);
Expand Down Expand Up @@ -2640,6 +2639,8 @@ namespace coreinit

void InitializeFS()
{
OSInitMutex(&s_fsGlobalMutex);

cafeExportRegister("coreinit", FSInit, LogType::CoreinitFile);
cafeExportRegister("coreinit", FSShutdown, LogType::CoreinitFile);

Expand Down
4 changes: 3 additions & 1 deletion src/Cafe/OS/libs/coreinit/coreinit_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ namespace coreinit

/* +0x00 */ MPTR firstMPTR;
/* +0x04 */ MPTR lastMPTR;
/* +0x08 */ OSMutex mutex;
/* +0x08 */ OSFastMutex fastMutex;
/* +0x34 */ MPTR dequeueHandlerFuncMPTR;
/* +0x38 */ uint32be numCommandsInFlight;
/* +0x3C */ uint32 numMaxCommandsInFlight;
/* +0x40 */ betype<QUEUE_FLAG> queueFlags;
};
DEFINE_ENUM_FLAG_OPERATORS(FSCmdQueue::QUEUE_FLAG);

static_assert(sizeof(FSCmdQueue) == 0x44);

#define FS_CLIENT_BUFFER_SIZE (5888)
#define FS_CMD_BLOCK_SIZE (2688)

Expand Down

0 comments on commit 2a735f1

Please sign in to comment.