From f751215c5e2669c54c64dd921a7f2e52c5fbee69 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 16 Sep 2022 10:49:19 -0600 Subject: [PATCH] Tweak critical section initialization --- Library/MeasureRecycleManager.cpp | 11 ++++++++--- Library/System.cpp | 4 ++-- Plugins/PluginFileView/PluginFileView.cpp | 9 ++++++++- Plugins/PluginPing/Ping.cpp | 9 ++++++++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Library/MeasureRecycleManager.cpp b/Library/MeasureRecycleManager.cpp index 6c4ad27bc..297fe761e 100644 --- a/Library/MeasureRecycleManager.cpp +++ b/Library/MeasureRecycleManager.cpp @@ -130,10 +130,10 @@ 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; } @@ -141,6 +141,11 @@ MeasureRecycleManager::MeasureRecycleManager(Skin* skin, const WCHAR* name) : Me MeasureRecycleManager::~MeasureRecycleManager() { --g_InstanceCount; + + if (g_InstanceCount <= 0) + { + DeleteCriticalSection(&g_CriticalSection); + } } void MeasureRecycleManager::ReadOptions(ConfigParser& parser, const WCHAR* section) diff --git a/Library/System.cpp b/Library/System.cpp index 9d0479dfe..7331797fa 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -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; } diff --git a/Plugins/PluginFileView/PluginFileView.cpp b/Plugins/PluginFileView/PluginFileView.cpp index 6c53bd0ae..3cfe9b4ce 100644 --- a/Plugins/PluginFileView/PluginFileView.cpp +++ b/Plugins/PluginFileView/PluginFileView.cpp @@ -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); diff --git a/Plugins/PluginPing/Ping.cpp b/Plugins/PluginPing/Ping.cpp index 9c8c3f21a..c566c49aa 100644 --- a/Plugins/PluginPing/Ping.cpp +++ b/Plugins/PluginPing/Ping.cpp @@ -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);