From fae1e4eee5a478175ec96c35bccc523adb16ef9c Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Tue, 20 Jul 2021 15:09:23 +0300 Subject: [PATCH] Replace CCritSect with SdlMutex --- Source/dx.cpp | 11 ++++++----- Source/nthread.cpp | 17 +++++++++-------- Source/storm/storm.h | 30 ------------------------------ 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/Source/dx.cpp b/Source/dx.cpp index 3b49dc7d5..c6275fd58 100644 --- a/Source/dx.cpp +++ b/Source/dx.cpp @@ -11,6 +11,7 @@ #include "options.h" #include "storm/storm.h" #include "utils/display.h" +#include "utils/sdl_mutex.h" #include "utils/log.hpp" #ifdef __3DS__ @@ -42,7 +43,7 @@ int sgdwLockCount; #ifdef _DEBUG int locktbl[256]; #endif -CCritSect sgMemCrit; +SdlMutex MemCrit; bool CanRenderDirectlyToOutputSurface() { @@ -111,7 +112,7 @@ void CreatePrimarySurface() void LockBufPriv() { - sgMemCrit.Enter(); + MemCrit.lock(); if (sgdwLockCount != 0) { sgdwLockCount++; return; @@ -126,7 +127,7 @@ void UnlockBufPriv() app_fatal("draw main unlock error"); sgdwLockCount--; - sgMemCrit.Leave(); + MemCrit.unlock(); } /** @@ -194,9 +195,9 @@ void dx_cleanup() if (ghMainWnd != nullptr) SDL_HideWindow(ghMainWnd); #endif - sgMemCrit.Enter(); + MemCrit.lock(); sgdwLockCount = 0; - sgMemCrit.Leave(); + MemCrit.unlock(); if (pal_surface == nullptr) return; diff --git a/Source/nthread.cpp b/Source/nthread.cpp index cc9f703f3..f68056503 100644 --- a/Source/nthread.cpp +++ b/Source/nthread.cpp @@ -9,6 +9,7 @@ #include "nthread.h" #include "storm/storm.h" #include "utils/thread.h" +#include "utils/sdl_mutex.h" namespace devilution { @@ -22,7 +23,7 @@ float gfProgressToNextGameTick = 0.0; namespace { -CCritSect sgMemCrit; +SdlMutex MemCrit; DWORD gdwDeltaBytesSec; bool nthread_should_run; SDL_threadID glpNThreadId; @@ -41,16 +42,16 @@ void NthreadHandler() } while (true) { - sgMemCrit.Enter(); + MemCrit.lock(); if (!nthread_should_run) { - sgMemCrit.Leave(); + MemCrit.unlock(); break; } nthread_send_and_recv_turn(0, 0); int delta = gnTickDelay; if (nthread_recv_turns()) delta = last_tick - SDL_GetTicks(); - sgMemCrit.Leave(); + MemCrit.unlock(); if (delta > 0) SDL_Delay(delta); if (!nthread_should_run) @@ -179,7 +180,7 @@ void nthread_start(bool setTurnUpperBit) gdwNormalMsgSize = largestMsgSize; if (gbIsMultiplayer) { sgbThreadIsRunning = false; - sgMemCrit.Enter(); + MemCrit.lock(); nthread_should_run = true; sghThread = CreateThread(NthreadHandler, &glpNThreadId); if (sghThread == nullptr) { @@ -197,7 +198,7 @@ void nthread_cleanup() gdwLargestMsgSize = 0; if (sghThread != nullptr && glpNThreadId != SDL_GetThreadID(nullptr)) { if (!sgbThreadIsRunning) - sgMemCrit.Leave(); + MemCrit.unlock(); SDL_WaitThread(sghThread, nullptr); sghThread = nullptr; } @@ -207,9 +208,9 @@ void nthread_ignore_mutex(bool bStart) { if (sghThread != nullptr) { if (bStart) - sgMemCrit.Leave(); + MemCrit.unlock(); else - sgMemCrit.Enter(); + MemCrit.lock(); sgbThreadIsRunning = bStart; } } diff --git a/Source/storm/storm.h b/Source/storm/storm.h index 18670a201..29553ef48 100644 --- a/Source/storm/storm.h +++ b/Source/storm/storm.h @@ -72,36 +72,6 @@ struct _SNETEVENT { #define WINAPI #endif -#ifdef __cplusplus -struct CCritSect { - SDL_mutex *m_critsect; - - CCritSect() - { - m_critsect = SDL_CreateMutex(); - if (m_critsect == NULL) { - ErrSdl(); - } - } - ~CCritSect() - { - SDL_DestroyMutex(m_critsect); - } - void Enter() - { - if (SDL_LockMutex(m_critsect) < 0) { - ErrSdl(); - } - } - void Leave() - { - if (SDL_UnlockMutex(m_critsect) < 0) { - ErrSdl(); - } - } -}; -#endif - // Game states #define GAMESTATE_PRIVATE 0x01 #define GAMESTATE_FULL 0x02