Browse Source

Implement Critical section constructor

pull/88/head
galaxyhaxz 7 years ago committed by Anders Jenbo
parent
commit
cef65ea0b6
  1. 23
      3rdParty/Storm/Source/storm.h
  2. 56
      Source/dthread.cpp
  3. 3
      Source/dthread.h
  4. 56
      Source/dx.cpp
  5. 3
      Source/dx.h
  6. 48
      Source/engine.cpp
  7. 3
      Source/engine.h
  8. 48
      Source/logging.cpp
  9. 3
      Source/logging.h
  10. 58
      Source/nthread.cpp
  11. 3
      Source/nthread.h

23
3rdParty/Storm/Source/storm.h vendored

@ -51,6 +51,29 @@ typedef struct _WSIZE
WORD cy;
} WSIZE, *PWSIZE;
#ifdef __cplusplus
struct CCritSect {
CRITICAL_SECTION m_critsect;
CCritSect()
{
InitializeCriticalSection(&m_critsect);
}
~CCritSect()
{
DeleteCriticalSection(&m_critsect);
}
void Enter()
{
EnterCriticalSection(&m_critsect);
}
void Leave()
{
LeaveCriticalSection(&m_critsect);
}
};
#endif
// Game states

56
Source/dthread.cpp

@ -2,7 +2,9 @@
#include "../types.h"
static CRITICAL_SECTION sgMemCrit; // idb
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
unsigned int glpDThreadId; // idb
TMegaPkt *sgpInfoHead; /* may not be right struct */
BOOLEAN dthread_running;
@ -11,44 +13,20 @@ HANDLE sghWorkToDoEvent;
/* rdata */
static HANDLE sghThread = INVALID_HANDLE_VALUE;
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
dthread_c_init(void)
{
dthread_init_mutex();
dthread_cleanup_mutex_atexit();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV dthread_c_init_funcs[] = { &dthread_c_init };
void dthread_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void dthread_cleanup_mutex_atexit()
{
atexit(dthread_cleanup_mutex);
}
void __cdecl dthread_cleanup_mutex()
{
DeleteCriticalSection(&sgMemCrit);
}
void dthread_remove_player(int pnum)
{
TMegaPkt *pkt;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
for (pkt = sgpInfoHead; pkt; pkt = pkt->pNext) {
if (pkt->dwSpaceLeft == pnum)
pkt->dwSpaceLeft = 4;
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
void dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen)
@ -66,7 +44,9 @@ void dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen)
pkt->data[0] = cmd;
*(_DWORD *)&pkt->data[4] = dwLen;
memcpy(&pkt->data[8], pbSrc, dwLen);
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
p = (TMegaPkt *)&sgpInfoHead;
while (p->pNext) {
p = p->pNext;
@ -74,7 +54,9 @@ void dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen)
p->pNext = pkt;
SetEvent(sghWorkToDoEvent);
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
void dthread_start()
@ -112,13 +94,17 @@ unsigned int __stdcall dthread_handler(void *unused)
app_fatal("dthread4:\n%s", error_buf);
}
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
pkt = sgpInfoHead;
if (sgpInfoHead)
sgpInfoHead = sgpInfoHead->pNext;
else
ResetEvent(sghWorkToDoEvent);
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
if (pkt) {
if (pkt->dwSpaceLeft != 4)

3
Source/dthread.h

@ -5,9 +5,6 @@
extern unsigned int glpDThreadId; // idb
extern BOOLEAN dthread_running;
void dthread_init_mutex();
void dthread_cleanup_mutex_atexit();
void __cdecl dthread_cleanup_mutex(void);
void dthread_remove_player(int pnum);
void dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen);
void dthread_start();

56
Source/dx.cpp

@ -12,39 +12,13 @@ IDirectDrawSurface *lpDDSPrimary;
#ifdef _DEBUG
int locktbl[256];
#endif
static CRITICAL_SECTION sgMemCrit;
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
char gbBackBuf; // weak
char gbEmulate; // weak
HMODULE ghDiabMod; // idb
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
dx_c_init(void)
{
dx_init_mutex();
dx_cleanup_mutex_atexit();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV dx_c_init_funcs[] = { &dx_c_init };
void dx_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void dx_cleanup_mutex_atexit()
{
atexit(dx_cleanup_mutex);
}
void __cdecl dx_cleanup_mutex(void)
{
DeleteCriticalSection(&sgMemCrit);
}
void dx_init(HWND hWnd)
{
HWND v1; // esi
@ -219,7 +193,9 @@ void lock_buf_priv()
DDSURFACEDESC ddsd;
HRESULT error_code;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (sgpBackBuf != NULL) {
gpBuffer = sgpBackBuf;
sgdwLockCount++;
@ -284,7 +260,9 @@ void unlock_buf_priv()
DDErrMsg(error_code, 273, "C:\\Src\\Diablo\\Source\\dx.cpp");
}
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
void dx_cleanup()
@ -294,7 +272,9 @@ void dx_cleanup()
if (ghMainWnd)
ShowWindow(ghMainWnd, SW_HIDE);
SDrawDestroy();
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (sgpBackBuf != NULL) {
v0 = sgpBackBuf;
sgpBackBuf = 0;
@ -309,7 +289,9 @@ void dx_cleanup()
}
sgdwLockCount = 0;
gpBuffer = 0;
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
if (lpDDSPrimary) {
#ifdef __cplusplus
lpDDSPrimary->Release();
@ -340,7 +322,9 @@ void dx_reinit()
{
int lockCount;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
ClearCursor();
lockCount = sgdwLockCount;
@ -358,5 +342,7 @@ void dx_reinit()
lockCount--;
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}

3
Source/dx.h

@ -11,9 +11,6 @@ extern char gbBackBuf; // weak
extern char gbEmulate; // weak
extern HMODULE ghDiabMod; // idb
void dx_init_mutex();
void dx_cleanup_mutex_atexit();
void __cdecl dx_cleanup_mutex(void);
void dx_init(HWND hWnd);
void dx_create_back_buffer();
void dx_create_primary_surface();

48
Source/engine.cpp

@ -11,7 +11,9 @@ int dword_52B970; // BOOLEAN flip - if y < x
int orgseed; // weak
int sgnWidth;
int sglGameSeed; // weak
static CRITICAL_SECTION sgMemCrit;
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
int SeedCount; // weak
int dword_52B99C; // BOOLEAN valid - if x/y are in bounds
@ -2389,41 +2391,17 @@ int random(BYTE idx, int v)
return (GetRndSeed() >> 16) % v;
}
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
engine_c_init(void)
{
mem_init_mutex();
mem_atexit_mutex();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV engine_c_init_funcs[] = { &engine_c_init };
void mem_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void mem_atexit_mutex()
{
atexit(mem_free_mutex);
}
void __cdecl mem_free_mutex(void)
{
DeleteCriticalSection(&sgMemCrit);
}
unsigned char *DiabloAllocPtr(int dwBytes)
{
BYTE *buf;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
buf = (BYTE *)SMemAlloc(dwBytes, "C:\\Src\\Diablo\\Source\\ENGINE.CPP", 2236, 0);
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
if (buf == NULL) {
ErrDlg(IDD_DIALOG2, GetLastError(), "C:\\Src\\Diablo\\Source\\ENGINE.CPP", 2269);
@ -2435,9 +2413,13 @@ unsigned char *DiabloAllocPtr(int dwBytes)
void mem_free_dbg(void *p)
{
if (p) {
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
SMemFree(p, "C:\\Src\\Diablo\\Source\\ENGINE.CPP", 2317, 0);
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
}

3
Source/engine.h

@ -40,9 +40,6 @@ int GetDirection(int x1, int y1, int x2, int y2);
void SetRndSeed(int s);
int GetRndSeed();
int random(BYTE idx, int v);
void mem_init_mutex();
void mem_atexit_mutex();
void __cdecl mem_free_mutex(void);
unsigned char *DiabloAllocPtr(int dwBytes);
void mem_free_dbg(void *p);
BYTE *LoadFileInMem(char *pszName, int *pdwFileLen);

48
Source/logging.cpp

@ -2,7 +2,9 @@
#include "../types.h"
static CRITICAL_SECTION sgMemCrit;
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
CHAR FileName[MAX_PATH]; // idb
char log_buffer[388];
LPCVOID lpAddress; // idb
@ -13,39 +15,13 @@ DWORD nNumberOfBytesToWrite; // idb
int log_not_created = 1; // weak
HANDLE log_file = (HANDLE)0xFFFFFFFF; // idb
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
log_c_init(void)
{
log_init_mutex();
j_log_cleanup_mutex();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV log_c_init_funcs[] = { &log_c_init };
void log_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void j_log_cleanup_mutex()
{
atexit(log_cleanup_mutex);
}
void __cdecl log_cleanup_mutex(void)
{
DeleteCriticalSection(&sgMemCrit);
}
void __cdecl log_flush(BOOL force_close)
{
DWORD NumberOfBytesWritten;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (nNumberOfBytesToWrite) {
if (log_file == INVALID_HANDLE_VALUE) {
log_file = log_create();
@ -62,7 +38,9 @@ void __cdecl log_flush(BOOL force_close)
CloseHandle(log_file);
log_file = INVALID_HANDLE_VALUE;
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
HANDLE log_create()
@ -155,7 +133,9 @@ void __cdecl log_printf(const char *pszFmt, ...)
va_list va; // [esp+218h] [ebp+Ch]
va_start(va, pszFmt);
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
_vsnprintf(v3, 0x200u, pszFmt, va);
va_end(va);
v3[511] = 0;
@ -170,7 +150,9 @@ void __cdecl log_printf(const char *pszFmt, ...)
memcpy(&v2[nNumberOfBytesToWrite], v3, v1);
nNumberOfBytesToWrite += v1;
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
void log_dump_computer_info()

3
Source/logging.h

@ -7,9 +7,6 @@ extern char log_buffer[388];
extern LPCVOID lpAddress; // idb
extern DWORD nNumberOfBytesToWrite; // idb
void log_init_mutex();
void j_log_cleanup_mutex();
void __cdecl log_cleanup_mutex(void);
void __cdecl log_flush(BOOL force_close);
HANDLE log_create(); // should be HANDLE
void log_get_version(VS_FIXEDFILEINFO *file_info);

58
Source/nthread.cpp

@ -4,7 +4,9 @@
char byte_679704; // weak
int gdwMsgLenTbl[MAX_PLRS];
static CRITICAL_SECTION sgMemCrit;
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
int gdwDeltaBytesSec; // weak
char nthread_should_run; // weak
DWORD gdwTurnsInTransit; // weak
@ -22,34 +24,6 @@ int last_tick; // weak
/* data */
static HANDLE sghThread = (HANDLE)0xFFFFFFFF; // idb
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
nthread_c_init(void)
{
nthread_init_mutex();
nthread_cleanup_mutex_atexit();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV nthread_c_init_funcs[] = { &nthread_c_init };
void nthread_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void nthread_cleanup_mutex_atexit()
{
atexit(nthread_cleanup_mutex);
}
void __cdecl nthread_cleanup_mutex(void)
{
DeleteCriticalSection(&sgMemCrit);
}
void nthread_terminate_game(const char *pszFcn)
{
DWORD sErr; // eax
@ -195,7 +169,9 @@ void nthread_start(BOOL set_turn_upper_bit)
gdwNormalMsgSize = largestMsgSize;
if ((unsigned char)gbMaxPlayers > 1u) {
sgbThreadIsRunning = 0;
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
nthread_should_run = 1;
sghThread = (HANDLE)_beginthreadex(NULL, 0, nthread_handler, NULL, 0, &glpNThreadId);
if (sghThread == (HANDLE)-1) {
@ -226,7 +202,9 @@ unsigned int __stdcall nthread_handler(void *a1)
if (nthread_should_run) {
while (1) {
EnterCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (!nthread_should_run)
break;
nthread_send_and_recv_turn(0, 0);
@ -234,13 +212,17 @@ unsigned int __stdcall nthread_handler(void *a1)
delta = last_tick - GetTickCount();
else
delta = 50;
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
if (delta > 0)
Sleep(delta);
if (!nthread_should_run)
return 0;
}
LeaveCriticalSection(&sgMemCrit);
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
return 0;
}
@ -254,8 +236,10 @@ void nthread_cleanup()
gdwNormalMsgSize = 0;
gdwLargestMsgSize = 0;
if (sghThread != (HANDLE)-1 && glpNThreadId != GetCurrentThreadId()) {
#ifdef __cplusplus
if (!sgbThreadIsRunning)
LeaveCriticalSection(&sgMemCrit);
sgMemCrit.Leave();
#endif
if (WaitForSingleObject(sghThread, 0xFFFFFFFF) == -1) {
app_fatal("nthread3:\n(%s)", TraceLastError());
}
@ -272,10 +256,12 @@ void nthread_cleanup()
void nthread_ignore_mutex(BOOL bStart)
{
if (sghThread != (HANDLE)-1) {
#ifdef __cplusplus
if (bStart)
LeaveCriticalSection(&sgMemCrit);
sgMemCrit.Leave();
else
EnterCriticalSection(&sgMemCrit);
sgMemCrit.Enter();
#endif
sgbThreadIsRunning = bStart;
}
}

3
Source/nthread.h

@ -15,9 +15,6 @@ extern int gdwLargestMsgSize; // weak
extern int gdwNormalMsgSize; // weak
extern int last_tick; // weak
void nthread_init_mutex();
void nthread_cleanup_mutex_atexit();
void __cdecl nthread_cleanup_mutex(void);
void nthread_terminate_game(const char *pszFcn);
int nthread_send_and_recv_turn(int cur_turn, int turn_delta);
int nthread_recv_turns(int *pfSendAsync);

Loading…
Cancel
Save