[Debug]: Refactor the crit sections class wrapper
This commit is contained in:
parent
d2686071aa
commit
0593d890da
@ -1,4 +1,5 @@
|
||||
#include <STDInclude.hpp>
|
||||
#include "Game/Engine/ScopedCriticalSection.hpp"
|
||||
|
||||
namespace Components
|
||||
{
|
||||
@ -288,7 +289,7 @@ namespace Components
|
||||
|
||||
sprintf_s(newFileName, "%s_%s.log", bug, Game::Live_GetLocalClientName(0));
|
||||
|
||||
Game::Sys_EnterCriticalSection(Game::CRITSECT_CONSOLE);
|
||||
Game::Engine::ScopedCriticalSection _(Game::CRITSECT_CONSOLE, Game::Engine::SCOPED_CRITSECT_NORMAL);
|
||||
|
||||
if (*Game::logfile)
|
||||
{
|
||||
@ -301,8 +302,6 @@ namespace Components
|
||||
const auto result = CopyFileA(from_ospath, to_ospath, 0);
|
||||
Game::Com_OpenLogFile();
|
||||
|
||||
Game::Sys_LeaveCriticalSection(Game::CRITSECT_CONSOLE);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
Logger::PrintError(1, "CopyFile failed({}) {} {}\n", GetLastError(), "console_mp.log", newFileName);
|
||||
|
82
src/Game/Engine/ScopedCriticalSection.cpp
Normal file
82
src/Game/Engine/ScopedCriticalSection.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include <STDInclude.hpp>
|
||||
#include "ScopedCriticalSection.hpp"
|
||||
|
||||
namespace Game::Engine
|
||||
{
|
||||
ScopedCriticalSection::ScopedCriticalSection(const CriticalSection s, const ScopedCriticalSectionType type)
|
||||
: s_(s), isScopedRelease_(false)
|
||||
{
|
||||
if (type == SCOPED_CRITSECT_NORMAL)
|
||||
{
|
||||
Sys_EnterCriticalSection(this->s_);
|
||||
this->hasOwnership_ = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type == SCOPED_CRITSECT_TRY)
|
||||
{
|
||||
this->hasOwnership_ = Sys_TryEnterCriticalSection(this->s_);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type == SCOPED_CRITSECT_RELEASE)
|
||||
{
|
||||
Sys_LeaveCriticalSection(this->s_);
|
||||
this->isScopedRelease_ = true;
|
||||
}
|
||||
|
||||
this->hasOwnership_ = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ScopedCriticalSection::~ScopedCriticalSection()
|
||||
{
|
||||
if (!this->hasOwnership_ || this->isScopedRelease_)
|
||||
{
|
||||
if (!this->hasOwnership_ && this->isScopedRelease_)
|
||||
{
|
||||
Sys_EnterCriticalSection(this->s_);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Sys_LeaveCriticalSection(this->s_);
|
||||
}
|
||||
}
|
||||
|
||||
void ScopedCriticalSection::enterCritSect()
|
||||
{
|
||||
assert(!this->hasOwnership_);
|
||||
|
||||
this->hasOwnership_ = true;
|
||||
Sys_EnterCriticalSection(this->s_);
|
||||
}
|
||||
|
||||
void ScopedCriticalSection::leaveCritSect()
|
||||
{
|
||||
assert(this->hasOwnership_);
|
||||
|
||||
this->hasOwnership_ = false;
|
||||
Sys_LeaveCriticalSection(this->s_);
|
||||
}
|
||||
|
||||
bool ScopedCriticalSection::tryEnterCritSect()
|
||||
{
|
||||
assert(!this->hasOwnership_);
|
||||
|
||||
const auto result = Sys_TryEnterCriticalSection(this->s_);
|
||||
this->hasOwnership_ = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ScopedCriticalSection::hasOwnership() const
|
||||
{
|
||||
return this->hasOwnership_;
|
||||
}
|
||||
|
||||
bool ScopedCriticalSection::isScopedRelease() const
|
||||
{
|
||||
return this->isScopedRelease_;
|
||||
}
|
||||
}
|
31
src/Game/Engine/ScopedCriticalSection.hpp
Normal file
31
src/Game/Engine/ScopedCriticalSection.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
namespace Game::Engine
|
||||
{
|
||||
enum ScopedCriticalSectionType
|
||||
{
|
||||
SCOPED_CRITSECT_NORMAL = 0x0,
|
||||
SCOPED_CRITSECT_DISABLED = 0x1,
|
||||
SCOPED_CRITSECT_RELEASE = 0x2,
|
||||
SCOPED_CRITSECT_TRY = 0x3,
|
||||
};
|
||||
|
||||
class ScopedCriticalSection
|
||||
{
|
||||
public:
|
||||
ScopedCriticalSection(CriticalSection s, ScopedCriticalSectionType type);
|
||||
~ScopedCriticalSection();
|
||||
|
||||
void enterCritSect();
|
||||
void leaveCritSect();
|
||||
[[nodiscard]] bool tryEnterCritSect();
|
||||
|
||||
[[nodiscard]] bool hasOwnership() const;
|
||||
[[nodiscard]] bool isScopedRelease() const;
|
||||
|
||||
private:
|
||||
CriticalSection s_;
|
||||
bool hasOwnership_;
|
||||
bool isScopedRelease_;
|
||||
};
|
||||
}
|
@ -24,6 +24,8 @@ namespace Game
|
||||
|
||||
char(*sys_exitCmdLine)[1024] = reinterpret_cast<char(*)[1024]>(0x649FB68);
|
||||
|
||||
RTL_CRITICAL_SECTION* s_criticalSection = reinterpret_cast<RTL_CRITICAL_SECTION*>(0x6499BC8);
|
||||
|
||||
void Sys_LockRead(FastCriticalSection* critSect)
|
||||
{
|
||||
InterlockedIncrement(&critSect->readCount);
|
||||
@ -35,4 +37,12 @@ namespace Game
|
||||
assert(critSect->readCount > 0);
|
||||
InterlockedDecrement(&critSect->readCount);
|
||||
}
|
||||
|
||||
bool Sys_TryEnterCriticalSection(CriticalSection critSect)
|
||||
{
|
||||
assert(static_cast<unsigned int>(critSect) <
|
||||
static_cast<unsigned int>(CRITSECT_COUNT));
|
||||
|
||||
return TryEnterCriticalSection(&s_criticalSection[critSect]) != FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +62,10 @@ namespace Game
|
||||
|
||||
extern char(*sys_exitCmdLine)[1024];
|
||||
|
||||
extern RTL_CRITICAL_SECTION* s_criticalSection;
|
||||
|
||||
extern void Sys_LockRead(FastCriticalSection* critSect);
|
||||
extern void Sys_UnlockRead(FastCriticalSection* critSect);
|
||||
|
||||
extern bool Sys_TryEnterCriticalSection(CriticalSection critSect);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user