From 396596ca4a5fa12872783505568aac9c6bdd9d1d Mon Sep 17 00:00:00 2001 From: Arcady Goldmints-Orlov Date: Mon, 31 Jul 2023 14:45:11 -0600 Subject: [PATCH] Replace GlobalLock functions with std::mutex The usage of GetGlobalLock/ReleaseGlobalLock/InitGlobalLock is replaced by std::lock_guard which is available as of c++11, and the functions are removed from the OSDependent ossource.cpp files. The standalone glslang binary now explicitly depends on OSDependent, as nothing in in the glslang library uses those functions anymore and they are not implicitly picked up by the linker. --- StandAlone/CMakeLists.txt | 1 + glslang/MachineIndependent/ShaderLang.cpp | 26 ++++++----------- glslang/OSDependent/Unix/ossource.cpp | 35 ----------------------- glslang/OSDependent/Windows/ossource.cpp | 24 ---------------- glslang/OSDependent/osinclude.h | 6 ---- 5 files changed, 10 insertions(+), 82 deletions(-) diff --git a/StandAlone/CMakeLists.txt b/StandAlone/CMakeLists.txt index b0966545..e25f5db8 100644 --- a/StandAlone/CMakeLists.txt +++ b/StandAlone/CMakeLists.txt @@ -54,6 +54,7 @@ glslang_set_link_args(glslang-standalone) set(LIBRARIES glslang + OSDependent SPIRV glslang-default-resource-limits) diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index 400a5bde..51c524bb 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include "SymbolTable.h" #include "ParseHelper.h" #include "Scan.h" @@ -81,6 +82,9 @@ namespace { // anonymous namespace for file-local functions and symbols // Shared global; access should be protected by a global mutex/critical section. int NumberOfClients = 0; +// global initialization lock +std::mutex init_lock; + using namespace glslang; // Create a language specific version of parseables. @@ -417,18 +421,15 @@ void SetupBuiltinSymbolTable(int version, EProfile profile, const SpvVersion& sp TInfoSink infoSink; // Make sure only one thread tries to do this at a time - glslang::GetGlobalLock(); + const std::lock_guard lock(init_lock); // See if it's already been done for this version/profile combination int versionIndex = MapVersionToIndex(version); int spvVersionIndex = MapSpvVersionToIndex(spvVersion); int profileIndex = MapProfileToIndex(profile); int sourceIndex = MapSourceToIndex(source); - if (CommonSymbolTable[versionIndex][spvVersionIndex][profileIndex][sourceIndex][EPcGeneral]) { - glslang::ReleaseGlobalLock(); - + if (CommonSymbolTable[versionIndex][spvVersionIndex][profileIndex][sourceIndex][EPcGeneral]) return; - } // Switch to a new pool TPoolAllocator& previousAllocator = GetThreadPoolAllocator(); @@ -475,8 +476,6 @@ void SetupBuiltinSymbolTable(int version, EProfile profile, const SpvVersion& sp delete builtInPoolAllocator; SetThreadPoolAllocator(&previousAllocator); - - glslang::ReleaseGlobalLock(); } // Function to Print all builtins @@ -1297,12 +1296,10 @@ bool CompileDeferred( // int ShInitialize() { - glslang::InitGlobalLock(); - if (! InitProcess()) return 0; - glslang::GetGlobalLock(); + const std::lock_guard lock(init_lock); ++NumberOfClients; if (PerProcessGPA == nullptr) @@ -1313,7 +1310,6 @@ int ShInitialize() glslang::HlslScanContext::fillInKeywordMap(); #endif - glslang::ReleaseGlobalLock(); return 1; } @@ -1372,14 +1368,11 @@ void ShDestruct(ShHandle handle) // int ShFinalize() { - glslang::GetGlobalLock(); + const std::lock_guard lock(init_lock); --NumberOfClients; assert(NumberOfClients >= 0); - bool finalize = NumberOfClients == 0; - if (! finalize) { - glslang::ReleaseGlobalLock(); + if (NumberOfClients > 0) return 1; - } for (int version = 0; version < VersionCount; ++version) { for (int spvVersion = 0; spvVersion < SpvVersionCount; ++spvVersion) { @@ -1417,7 +1410,6 @@ int ShFinalize() glslang::HlslScanContext::deleteKeywordMap(); #endif - glslang::ReleaseGlobalLock(); return 1; } diff --git a/glslang/OSDependent/Unix/ossource.cpp b/glslang/OSDependent/Unix/ossource.cpp index 2b20349d..fbb51f7b 100644 --- a/glslang/OSDependent/Unix/ossource.cpp +++ b/glslang/OSDependent/Unix/ossource.cpp @@ -36,15 +36,8 @@ // This file contains the Linux-specific functions // #include "../osinclude.h" -#include "../../../OGLCompilersDLL/InitializeDll.h" -#include -#include -#include -#include -#include #include -#include #if !defined(__Fuchsia__) #include @@ -52,34 +45,6 @@ namespace glslang { -namespace { - pthread_mutex_t gMutex; -} - -static void InitMutex(void) -{ - pthread_mutexattr_t mutexattr; - pthread_mutexattr_init(&mutexattr); - pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&gMutex, &mutexattr); -} - -void InitGlobalLock() -{ - static pthread_once_t once = PTHREAD_ONCE_INIT; - pthread_once(&once, InitMutex); -} - -void GetGlobalLock() -{ - pthread_mutex_lock(&gMutex); -} - -void ReleaseGlobalLock() -{ - pthread_mutex_unlock(&gMutex); -} - // #define DUMP_COUNTERS void OS_DumpMemoryCounters() diff --git a/glslang/OSDependent/Windows/ossource.cpp b/glslang/OSDependent/Windows/ossource.cpp index 64d0b174..d7f89f71 100644 --- a/glslang/OSDependent/Windows/ossource.cpp +++ b/glslang/OSDependent/Windows/ossource.cpp @@ -37,11 +37,9 @@ #define STRICT #define VC_EXTRALEAN 1 #include -#include #include #include #include -#include // // This file contains the Window-OS-specific functions @@ -53,28 +51,6 @@ namespace glslang { -HANDLE GlobalLock; - -void InitGlobalLock() -{ - GlobalLock = CreateMutex(nullptr, false, nullptr); -} - -void GetGlobalLock() -{ - WaitForSingleObject(GlobalLock, INFINITE); -} - -void ReleaseGlobalLock() -{ - ReleaseMutex(GlobalLock); -} - -unsigned int __stdcall EnterGenericThread (void* entry) -{ - return ((TThreadEntrypoint)entry)(nullptr); -} - //#define DUMP_COUNTERS void OS_DumpMemoryCounters() diff --git a/glslang/OSDependent/osinclude.h b/glslang/OSDependent/osinclude.h index c118b553..0d677e4a 100644 --- a/glslang/OSDependent/osinclude.h +++ b/glslang/OSDependent/osinclude.h @@ -37,12 +37,6 @@ namespace glslang { -void InitGlobalLock(); -void GetGlobalLock(); -void ReleaseGlobalLock(); - -typedef unsigned int (*TThreadEntrypoint)(void*); - void OS_DumpMemoryCounters(); } // end namespace glslang