From dadaca86b25919a2e72941e1828dac0496377847 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Thu, 4 May 2023 16:40:33 +0100 Subject: [PATCH] feat: improve fast crit section --- src/game/engine/fast_critical_section.cpp | 41 +++++++++++++++++++++++ src/game/engine/fast_critical_section.hpp | 24 +++++++++++++ src/game/game.cpp | 11 ++++++ src/game/game.hpp | 7 ++++ src/std_include.hpp | 1 + 5 files changed, 84 insertions(+) create mode 100644 src/game/engine/fast_critical_section.cpp create mode 100644 src/game/engine/fast_critical_section.hpp diff --git a/src/game/engine/fast_critical_section.cpp b/src/game/engine/fast_critical_section.cpp new file mode 100644 index 0000000..dea09d6 --- /dev/null +++ b/src/game/engine/fast_critical_section.cpp @@ -0,0 +1,41 @@ +#include +#include "game/game.hpp" + +#include "fast_critical_section.hpp" + +namespace game::engine +{ + FastCriticalSectionScopeRead::FastCriticalSectionScopeRead(native::FastCriticalSection* cs) + { + this->cs_ = cs; + if (this->cs_) + { + native::Sys_LockRead(this->cs_); + } + } + + FastCriticalSectionScopeRead::~FastCriticalSectionScopeRead() + { + if (this->cs_) + { + native::Sys_UnlockRead(this->cs_); + } + } + + FastCriticalSectionScopeWrite::FastCriticalSectionScopeWrite(native::FastCriticalSection* cs) + { + this->cs_ = cs; + if (this->cs_) + { + native::Sys_LockWrite(this->cs_); + } + } + + FastCriticalSectionScopeWrite::~FastCriticalSectionScopeWrite() + { + if (this->cs_) + { + native::Sys_UnlockWrite(this->cs_); + } + } +} \ No newline at end of file diff --git a/src/game/engine/fast_critical_section.hpp b/src/game/engine/fast_critical_section.hpp new file mode 100644 index 0000000..378b1f5 --- /dev/null +++ b/src/game/engine/fast_critical_section.hpp @@ -0,0 +1,24 @@ +#pragma once + +namespace game::engine +{ + class FastCriticalSectionScopeRead + { + public: + FastCriticalSectionScopeRead(native::FastCriticalSection* cs); + ~FastCriticalSectionScopeRead(); + + private: + native::FastCriticalSection* cs_; + }; + + class FastCriticalSectionScopeWrite + { + public: + FastCriticalSectionScopeWrite(native::FastCriticalSection* cs); + ~FastCriticalSectionScopeWrite(); + + private: + native::FastCriticalSection* cs_; + }; +} diff --git a/src/game/game.cpp b/src/game/game.cpp index ab02c1a..865a04f 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -72,6 +72,8 @@ namespace game Sys_Sleep_t Sys_Sleep; Sys_FreeFileList_t Sys_FreeFileList; Sys_MessageBox_t Sys_MessageBox; + Sys_LockWrite_t Sys_LockWrite; + Sys_TempPriorityEnd_t Sys_TempPriorityEnd; PMem_AllocFromSource_NoDebug_t PMem_AllocFromSource_NoDebug; @@ -569,6 +571,13 @@ namespace game InterlockedDecrement(&critSect->readCount); } + void Sys_UnlockWrite(FastCriticalSection* critSect) + { + assert(critSect->writeCount > 0); + InterlockedDecrement(&critSect->writeCount); + Sys_TempPriorityEnd(&critSect->tempPriority); + } + [[noreturn]] void Sys_OutOfMemErrorInternal(const char* filename, int line) { Sys_EnterCriticalSection(CRITSECT_FATAL_ERROR); @@ -801,6 +810,8 @@ namespace game native::Sys_Sleep = native::Sys_Sleep_t(SELECT_VALUE(0x438600, 0x55F460)); native::Sys_FreeFileList = native::Sys_FreeFileList_t(SELECT_VALUE(0x486380, 0x5C4F90)); native::Sys_MessageBox = native::Sys_MessageBox_t(SELECT_VALUE(0x4664D0, 0x5CD180)); + native::Sys_LockWrite = native::Sys_LockWrite_t(SELECT_VALUE(0x4C9E70, 0x5502D0)); + native::Sys_TempPriorityEnd = native::Sys_TempPriorityEnd_t(SELECT_VALUE(0x4DCF00, 0x4C8CF0)); native::PMem_AllocFromSource_NoDebug = native::PMem_AllocFromSource_NoDebug_t(SELECT_VALUE(0x449E50, 0x5C15C0)); diff --git a/src/game/game.hpp b/src/game/game.hpp index 8b272b3..bbef7cf 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -164,6 +164,12 @@ namespace game typedef int (*Sys_MessageBox_t)(const char* lpText, const char* lpCaption, unsigned int uType, int defaultValue); extern Sys_MessageBox_t Sys_MessageBox; + typedef void (*Sys_LockWrite_t)(FastCriticalSection* critSect); + extern Sys_LockWrite_t Sys_LockWrite; + + typedef void (*Sys_TempPriorityEnd_t)(TempPriority*); + extern Sys_TempPriorityEnd_t Sys_TempPriorityEnd; + typedef void* (*PMem_AllocFromSource_NoDebug_t)(unsigned int size, unsigned int alignment, unsigned int type, int source); extern PMem_AllocFromSource_NoDebug_t PMem_AllocFromSource_NoDebug; @@ -427,6 +433,7 @@ namespace game bool Sys_IsServerThread(); void Sys_LockRead(FastCriticalSection* critSect); void Sys_UnlockRead(FastCriticalSection* critSect); + void Sys_UnlockWrite(FastCriticalSection* critSect); [[noreturn]] void Sys_OutOfMemErrorInternal(const char* filename, int line); bool FS_Initialized(); diff --git a/src/std_include.hpp b/src/std_include.hpp index e3f2fd5..af48ceb 100644 --- a/src/std_include.hpp +++ b/src/std_include.hpp @@ -39,6 +39,7 @@ #endif #include +#include #include #include #include