Browse Source

Replace CCritSect with SdlMutex

pull/2439/head
Vladimir Olteanu 5 years ago committed by Anders Jenbo
parent
commit
fae1e4eee5
  1. 11
      Source/dx.cpp
  2. 17
      Source/nthread.cpp
  3. 30
      Source/storm/storm.h

11
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;

17
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;
}
}

30
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

Loading…
Cancel
Save