From cef65ea0b6faf73e4833002587500b158fa54fd2 Mon Sep 17 00:00:00 2001 From: galaxyhaxz Date: Sun, 14 Apr 2019 17:06:18 -0500 Subject: [PATCH] Implement Critical section constructor --- 3rdParty/Storm/Source/storm.h | 23 ++++++++++++++ Source/dthread.cpp | 56 +++++++++++++-------------------- Source/dthread.h | 3 -- Source/dx.cpp | 56 +++++++++++++-------------------- Source/dx.h | 3 -- Source/engine.cpp | 48 +++++++++-------------------- Source/engine.h | 3 -- Source/logging.cpp | 48 +++++++++-------------------- Source/logging.h | 3 -- Source/nthread.cpp | 58 +++++++++++++---------------------- Source/nthread.h | 3 -- 11 files changed, 117 insertions(+), 187 deletions(-) diff --git a/3rdParty/Storm/Source/storm.h b/3rdParty/Storm/Source/storm.h index b44c09688..d765bf160 100644 --- a/3rdParty/Storm/Source/storm.h +++ b/3rdParty/Storm/Source/storm.h @@ -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 diff --git a/Source/dthread.cpp b/Source/dthread.cpp index 03b23deda..34c7199c1 100644 --- a/Source/dthread.cpp +++ b/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) diff --git a/Source/dthread.h b/Source/dthread.h index a92b0ee89..b204f723f 100644 --- a/Source/dthread.h +++ b/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(); diff --git a/Source/dx.cpp b/Source/dx.cpp index efcb888e6..f14ab8b04 100644 --- a/Source/dx.cpp +++ b/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 } diff --git a/Source/dx.h b/Source/dx.h index 0b9dd9622..59ee5bb9e 100644 --- a/Source/dx.h +++ b/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(); diff --git a/Source/engine.cpp b/Source/engine.cpp index 2419e987f..d3b616c80 100644 --- a/Source/engine.cpp +++ b/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 } } diff --git a/Source/engine.h b/Source/engine.h index f518ecdce..ba1095ef1 100644 --- a/Source/engine.h +++ b/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); diff --git a/Source/logging.cpp b/Source/logging.cpp index dd35d04df..cb8844216 100644 --- a/Source/logging.cpp +++ b/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() diff --git a/Source/logging.h b/Source/logging.h index e23cecfa4..de6289aea 100644 --- a/Source/logging.h +++ b/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); diff --git a/Source/nthread.cpp b/Source/nthread.cpp index ed07094b6..52f528090 100644 --- a/Source/nthread.cpp +++ b/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; } } diff --git a/Source/nthread.h b/Source/nthread.h index c374059d9..de39ea0f4 100644 --- a/Source/nthread.h +++ b/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);