Skip to content

Commit

Permalink
Tweak critical section initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
brianferguson committed Sep 16, 2022
1 parent d91c144 commit f751215
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
11 changes: 8 additions & 3 deletions Library/MeasureRecycleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,22 @@ enum class MeasureRecycleManager::Type
MeasureRecycleManager::MeasureRecycleManager(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_Type(Type::None)
{
static bool s_Init = [] {
if (g_InstanceCount <= 0)
{
System::InitializeCriticalSection(&g_CriticalSection);
return true;
} ();
}

++g_InstanceCount;
}

MeasureRecycleManager::~MeasureRecycleManager()
{
--g_InstanceCount;

if (g_InstanceCount <= 0)
{
DeleteCriticalSection(&g_CriticalSection);
}
}

void MeasureRecycleManager::ReadOptions(ConfigParser& parser, const WCHAR* section)
Expand Down
4 changes: 2 additions & 2 deletions Library/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,14 +1078,14 @@ void System::ResetWorkingDirectory()
*/
void System::InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{
if (InitializeCriticalSectionEx(lpCriticalSection, 0UL, CRITICAL_SECTION_NO_DEBUG_INFO))
if (InitializeCriticalSectionEx(lpCriticalSection, 0UL, CRITICAL_SECTION_NO_DEBUG_INFO) == TRUE)
{
return;
}

// The following should "always succeed" according to:
// https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializecriticalsectionandspincount
if (InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0UL))
if (InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0UL) == TRUE)
{
return;
}
Expand Down
9 changes: 8 additions & 1 deletion Plugins/PluginFileView/PluginFileView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
InitializeCriticalSection(&g_CriticalSection);
// See |Library\System.cpp:InitialCriticalSection| for details
if (InitializeCriticalSectionEx(&g_CriticalSection, 0UL, CRITICAL_SECTION_NO_DEBUG_INFO) == FALSE)
{
if (InitializeCriticalSectionAndSpinCount(&g_CriticalSection, 0UL) == FALSE)
{
// This should never be reached
}
}

// Disable DLL_THREAD_ATTACH and DLL_THREAD_DETACH notification calls.
DisableThreadLibraryCalls(hinstDLL);
Expand Down
9 changes: 8 additions & 1 deletion Plugins/PluginPing/Ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
InitializeCriticalSection(&g_CriticalSection);
// See |Library\System.cpp:InitialCriticalSection| for details
if (InitializeCriticalSectionEx(&g_CriticalSection, 0UL, CRITICAL_SECTION_NO_DEBUG_INFO) == FALSE)
{
if (InitializeCriticalSectionAndSpinCount(&g_CriticalSection, 0UL) == FALSE)
{
// This should never be reached
}
}

// Disable DLL_THREAD_ATTACH and DLL_THREAD_DETACH notification calls.
DisableThreadLibraryCalls(hinstDLL);
Expand Down

0 comments on commit f751215

Please sign in to comment.