1
0
mirror of https://github.com/narzoul/DDrawCompat synced 2024-12-30 08:55:36 +01:00

Introduced a common RAII class for critical sections

This commit is contained in:
narzoul 2016-04-13 20:58:30 +02:00
parent 0a05ef1174
commit 2937bde6a4
9 changed files with 59 additions and 49 deletions

View File

@ -14,10 +14,10 @@
#include "CompatPrimarySurface.h" #include "CompatPrimarySurface.h"
#include "DDrawProcs.h" #include "DDrawProcs.h"
#include "RealPrimarySurface.h" #include "RealPrimarySurface.h"
#include "ScopedCriticalSection.h"
namespace namespace
{ {
CRITICAL_SECTION g_gdiCriticalSection;
std::atomic<DWORD> g_renderingRefCount = 0; std::atomic<DWORD> g_renderingRefCount = 0;
DWORD g_ddLockThreadRenderingRefCount = 0; DWORD g_ddLockThreadRenderingRefCount = 0;
DWORD g_ddLockThreadId = 0; DWORD g_ddLockThreadId = 0;
@ -128,25 +128,6 @@ namespace CompatGdi
{ {
CRITICAL_SECTION g_gdiCriticalSection; CRITICAL_SECTION g_gdiCriticalSection;
GdiScopedThreadLock::GdiScopedThreadLock() : m_isLocked(true)
{
EnterCriticalSection(&g_gdiCriticalSection);
}
GdiScopedThreadLock::~GdiScopedThreadLock()
{
unlock();
}
void GdiScopedThreadLock::unlock()
{
if (m_isLocked)
{
LeaveCriticalSection(&g_gdiCriticalSection);
m_isLocked = false;
}
}
bool beginGdiRendering() bool beginGdiRendering()
{ {
if (!RealPrimarySurface::isFullScreen()) if (!RealPrimarySurface::isFullScreen())
@ -182,7 +163,7 @@ namespace CompatGdi
void endGdiRendering() void endGdiRendering()
{ {
CompatGdi::GdiScopedThreadLock gdiLock; Compat::ScopedCriticalSection gdiLock(CompatGdi::g_gdiCriticalSection);
if (GetCurrentThreadId() == g_ddLockThreadId) if (GetCurrentThreadId() == g_ddLockThreadId)
{ {
@ -257,7 +238,7 @@ namespace CompatGdi
void updatePalette(DWORD startingEntry, DWORD count) void updatePalette(DWORD startingEntry, DWORD count)
{ {
GdiScopedThreadLock gdiLock; Compat::ScopedCriticalSection gdiLock(g_gdiCriticalSection);
CompatGdiDcCache::clear(); CompatGdiDcCache::clear();
if (CompatPrimarySurface::palette) if (CompatPrimarySurface::palette)

View File

@ -6,16 +6,6 @@
namespace CompatGdi namespace CompatGdi
{ {
class GdiScopedThreadLock
{
public:
GdiScopedThreadLock();
~GdiScopedThreadLock();
void unlock();
private:
bool m_isLocked;
};
bool beginGdiRendering(); bool beginGdiRendering();
void endGdiRendering(); void endGdiRendering();
@ -23,4 +13,6 @@ namespace CompatGdi
void installHooks(); void installHooks();
void invalidate(const RECT* rect); void invalidate(const RECT* rect);
void updatePalette(DWORD startingEntry, DWORD count); void updatePalette(DWORD startingEntry, DWORD count);
extern CRITICAL_SECTION g_gdiCriticalSection;
}; };

View File

@ -6,6 +6,7 @@
#include "CompatGdiCaret.h" #include "CompatGdiCaret.h"
#include "CompatGdiDc.h" #include "CompatGdiDc.h"
#include "Hook.h" #include "Hook.h"
#include "ScopedCriticalSection.h"
namespace namespace
{ {
@ -91,11 +92,11 @@ namespace
void updateCaret() void updateCaret()
{ {
EnterCriticalSection(&g_caretCriticalSection); Compat::ScopedCriticalSection lock(g_caretCriticalSection);
CaretData newCaret = getCaretData(); CaretData newCaret = getCaretData();
if (newCaret == g_caret) if (newCaret == g_caret)
{ {
LeaveCriticalSection(&g_caretCriticalSection);
return; return;
} }
@ -107,7 +108,6 @@ namespace
} }
g_caret = newCaret; g_caret = newCaret;
LeaveCriticalSection(&g_caretCriticalSection);
} }
} }

View File

@ -6,6 +6,7 @@
#include "CompatGdiDcCache.h" #include "CompatGdiDcCache.h"
#include "DDrawLog.h" #include "DDrawLog.h"
#include "Hook.h" #include "Hook.h"
#include "ScopedCriticalSection.h"
namespace namespace
{ {
@ -157,7 +158,7 @@ namespace CompatGdiDc
return nullptr; return nullptr;
} }
CompatGdi::GdiScopedThreadLock gdiLock; Compat::ScopedCriticalSection gdiLock(CompatGdi::g_gdiCriticalSection);
auto it = g_origDcToCompatDc.find(origDc); auto it = g_origDcToCompatDc.find(origDc);
if (it != g_origDcToCompatDc.end()) if (it != g_origDcToCompatDc.end())
@ -202,7 +203,7 @@ namespace CompatGdiDc
void releaseDc(HDC origDc) void releaseDc(HDC origDc)
{ {
CompatGdi::GdiScopedThreadLock gdiLock; Compat::ScopedCriticalSection gdiLock(CompatGdi::g_gdiCriticalSection);
auto it = g_origDcToCompatDc.find(origDc); auto it = g_origDcToCompatDc.find(origDc);
if (it == g_origDcToCompatDc.end()) if (it == g_origDcToCompatDc.end())

View File

@ -12,6 +12,7 @@
#include "CompatGdiTitleBar.h" #include "CompatGdiTitleBar.h"
#include "CompatGdiWinProc.h" #include "CompatGdiWinProc.h"
#include "DDrawLog.h" #include "DDrawLog.h"
#include "ScopedCriticalSection.h"
namespace namespace
{ {
@ -37,7 +38,7 @@ namespace
} }
else if (WM_DESTROY == ret->message) else if (WM_DESTROY == ret->message)
{ {
CompatGdi::GdiScopedThreadLock lock; Compat::ScopedCriticalSection lock(CompatGdi::g_gdiCriticalSection);
g_prevWindowRect.erase(ret->hwnd); g_prevWindowRect.erase(ret->hwnd);
} }
else if (WM_WINDOWPOSCHANGED == ret->message) else if (WM_WINDOWPOSCHANGED == ret->message)
@ -148,7 +149,7 @@ namespace
void onWindowPosChanged(HWND hwnd) void onWindowPosChanged(HWND hwnd)
{ {
CompatGdi::GdiScopedThreadLock lock; Compat::ScopedCriticalSection lock(CompatGdi::g_gdiCriticalSection);
const auto it = g_prevWindowRect.find(hwnd); const auto it = g_prevWindowRect.find(hwnd);
if (it != g_prevWindowRect.end()) if (it != g_prevWindowRect.end())

View File

@ -10,6 +10,7 @@
#include "DDrawTypes.h" #include "DDrawTypes.h"
#include "Hook.h" #include "Hook.h"
#include "RealPrimarySurface.h" #include "RealPrimarySurface.h"
#include "ScopedCriticalSection.h"
namespace namespace
{ {
@ -121,11 +122,10 @@ namespace CompatPaletteConverter
return false; return false;
} }
EnterCriticalSection(&g_criticalSection); Compat::ScopedCriticalSection lock(g_criticalSection);
g_oldBitmap = SelectObject(dc, dib); g_oldBitmap = SelectObject(dc, dib);
g_dc = dc; g_dc = dc;
g_surface = surface; g_surface = surface;
LeaveCriticalSection(&g_criticalSection);
return true; return true;
} }
@ -149,11 +149,10 @@ namespace CompatPaletteConverter
void release() void release()
{ {
EnterCriticalSection(&g_criticalSection); Compat::ScopedCriticalSection lock(g_criticalSection);
if (!g_surface) if (!g_surface)
{ {
LeaveCriticalSection(&g_criticalSection);
return; return;
} }
@ -163,13 +162,11 @@ namespace CompatPaletteConverter
DeleteObject(SelectObject(g_dc, g_oldBitmap)); DeleteObject(SelectObject(g_dc, g_oldBitmap));
DeleteDC(g_dc); DeleteDC(g_dc);
g_dc = nullptr; g_dc = nullptr;
LeaveCriticalSection(&g_criticalSection);
} }
void setClipper(IDirectDrawClipper* clipper) void setClipper(IDirectDrawClipper* clipper)
{ {
EnterCriticalSection(&g_criticalSection); Compat::ScopedCriticalSection lock(g_criticalSection);
if (g_surface) if (g_surface)
{ {
HRESULT result = CompatDirectDrawSurface<IDirectDrawSurface7>::s_origVtable.SetClipper( HRESULT result = CompatDirectDrawSurface<IDirectDrawSurface7>::s_origVtable.SetClipper(
@ -179,7 +176,6 @@ namespace CompatPaletteConverter
LOG_ONCE("Failed to set a clipper on the palette converter surface: " << result); LOG_ONCE("Failed to set a clipper on the palette converter surface: " << result);
} }
} }
LeaveCriticalSection(&g_criticalSection);
} }
void setHalftonePalette() void setHalftonePalette()
@ -191,7 +187,7 @@ namespace CompatPaletteConverter
void setPrimaryPalette(DWORD startingEntry, DWORD count) void setPrimaryPalette(DWORD startingEntry, DWORD count)
{ {
EnterCriticalSection(&g_criticalSection); Compat::ScopedCriticalSection lock(g_criticalSection);
if (g_dc) if (g_dc)
{ {
if (CompatPrimarySurface::palette) if (CompatPrimarySurface::palette)
@ -207,7 +203,6 @@ namespace CompatPaletteConverter
SetDIBColorTable(g_dc, 0, 256, g_halftonePalette); SetDIBColorTable(g_dc, 0, 256, g_halftonePalette);
} }
} }
LeaveCriticalSection(&g_criticalSection);
} }
void unlockDc() void unlockDc()

View File

@ -174,6 +174,7 @@
<ClInclude Include="Hook.h" /> <ClInclude Include="Hook.h" />
<ClInclude Include="IReleaseNotifier.h" /> <ClInclude Include="IReleaseNotifier.h" />
<ClInclude Include="RealPrimarySurface.h" /> <ClInclude Include="RealPrimarySurface.h" />
<ClInclude Include="ScopedCriticalSection.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="CompatDirectDraw.cpp" /> <ClCompile Include="CompatDirectDraw.cpp" />

View File

@ -105,6 +105,9 @@
<ClInclude Include="CompatPaletteConverter.h"> <ClInclude Include="CompatPaletteConverter.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ScopedCriticalSection.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="DllMain.cpp"> <ClCompile Include="DllMain.cpp">

View File

@ -0,0 +1,36 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
namespace Compat
{
class ScopedCriticalSection
{
public:
ScopedCriticalSection(CRITICAL_SECTION& cs)
: m_cs(cs), m_isLocked(true)
{
EnterCriticalSection(&m_cs);
}
~ScopedCriticalSection()
{
unlock();
}
void unlock()
{
if (m_isLocked)
{
LeaveCriticalSection(&m_cs);
m_isLocked = false;
}
}
private:
CRITICAL_SECTION& m_cs;
bool m_isLocked;
};
};