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.
This commit is contained in:
Arcady Goldmints-Orlov 2023-07-31 14:45:11 -06:00 committed by kd-11
parent 1b37f3a490
commit b4443eb859
5 changed files with 10 additions and 82 deletions

View File

@ -54,6 +54,7 @@ glslang_set_link_args(glslang-standalone)
set(LIBRARIES set(LIBRARIES
glslang glslang
OSDependent
SPIRV SPIRV
glslang-default-resource-limits) glslang-default-resource-limits)

View File

@ -45,6 +45,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <memory> #include <memory>
#include <mutex>
#include "SymbolTable.h" #include "SymbolTable.h"
#include "ParseHelper.h" #include "ParseHelper.h"
#include "Scan.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. // Shared global; access should be protected by a global mutex/critical section.
int NumberOfClients = 0; int NumberOfClients = 0;
// global initialization lock
std::mutex init_lock;
using namespace glslang; using namespace glslang;
// Create a language specific version of parseables. // Create a language specific version of parseables.
@ -417,18 +421,15 @@ void SetupBuiltinSymbolTable(int version, EProfile profile, const SpvVersion& sp
TInfoSink infoSink; TInfoSink infoSink;
// Make sure only one thread tries to do this at a time // Make sure only one thread tries to do this at a time
glslang::GetGlobalLock(); const std::lock_guard<std::mutex> lock(init_lock);
// See if it's already been done for this version/profile combination // See if it's already been done for this version/profile combination
int versionIndex = MapVersionToIndex(version); int versionIndex = MapVersionToIndex(version);
int spvVersionIndex = MapSpvVersionToIndex(spvVersion); int spvVersionIndex = MapSpvVersionToIndex(spvVersion);
int profileIndex = MapProfileToIndex(profile); int profileIndex = MapProfileToIndex(profile);
int sourceIndex = MapSourceToIndex(source); int sourceIndex = MapSourceToIndex(source);
if (CommonSymbolTable[versionIndex][spvVersionIndex][profileIndex][sourceIndex][EPcGeneral]) { if (CommonSymbolTable[versionIndex][spvVersionIndex][profileIndex][sourceIndex][EPcGeneral])
glslang::ReleaseGlobalLock();
return; return;
}
// Switch to a new pool // Switch to a new pool
TPoolAllocator& previousAllocator = GetThreadPoolAllocator(); TPoolAllocator& previousAllocator = GetThreadPoolAllocator();
@ -475,8 +476,6 @@ void SetupBuiltinSymbolTable(int version, EProfile profile, const SpvVersion& sp
delete builtInPoolAllocator; delete builtInPoolAllocator;
SetThreadPoolAllocator(&previousAllocator); SetThreadPoolAllocator(&previousAllocator);
glslang::ReleaseGlobalLock();
} }
// Function to Print all builtins // Function to Print all builtins
@ -1297,12 +1296,10 @@ bool CompileDeferred(
// //
int ShInitialize() int ShInitialize()
{ {
glslang::InitGlobalLock();
if (! InitProcess()) if (! InitProcess())
return 0; return 0;
glslang::GetGlobalLock(); const std::lock_guard<std::mutex> lock(init_lock);
++NumberOfClients; ++NumberOfClients;
if (PerProcessGPA == nullptr) if (PerProcessGPA == nullptr)
@ -1313,7 +1310,6 @@ int ShInitialize()
glslang::HlslScanContext::fillInKeywordMap(); glslang::HlslScanContext::fillInKeywordMap();
#endif #endif
glslang::ReleaseGlobalLock();
return 1; return 1;
} }
@ -1372,14 +1368,11 @@ void ShDestruct(ShHandle handle)
// //
int ShFinalize() int ShFinalize()
{ {
glslang::GetGlobalLock(); const std::lock_guard<std::mutex> lock(init_lock);
--NumberOfClients; --NumberOfClients;
assert(NumberOfClients >= 0); assert(NumberOfClients >= 0);
bool finalize = NumberOfClients == 0; if (NumberOfClients > 0)
if (! finalize) {
glslang::ReleaseGlobalLock();
return 1; return 1;
}
for (int version = 0; version < VersionCount; ++version) { for (int version = 0; version < VersionCount; ++version) {
for (int spvVersion = 0; spvVersion < SpvVersionCount; ++spvVersion) { for (int spvVersion = 0; spvVersion < SpvVersionCount; ++spvVersion) {
@ -1417,7 +1410,6 @@ int ShFinalize()
glslang::HlslScanContext::deleteKeywordMap(); glslang::HlslScanContext::deleteKeywordMap();
#endif #endif
glslang::ReleaseGlobalLock();
return 1; return 1;
} }

View File

@ -36,15 +36,8 @@
// This file contains the Linux-specific functions // This file contains the Linux-specific functions
// //
#include "../osinclude.h" #include "../osinclude.h"
#include "../../../OGLCompilersDLL/InitializeDll.h"
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <cstdio> #include <cstdio>
#include <sys/time.h>
#if !defined(__Fuchsia__) #if !defined(__Fuchsia__)
#include <sys/resource.h> #include <sys/resource.h>
@ -52,34 +45,6 @@
namespace glslang { 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 // #define DUMP_COUNTERS
void OS_DumpMemoryCounters() void OS_DumpMemoryCounters()

View File

@ -37,11 +37,9 @@
#define STRICT #define STRICT
#define VC_EXTRALEAN 1 #define VC_EXTRALEAN 1
#include <windows.h> #include <windows.h>
#include <cassert>
#include <process.h> #include <process.h>
#include <psapi.h> #include <psapi.h>
#include <cstdio> #include <cstdio>
#include <cstdint>
// //
// This file contains the Window-OS-specific functions // This file contains the Window-OS-specific functions
@ -53,28 +51,6 @@
namespace glslang { 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 //#define DUMP_COUNTERS
void OS_DumpMemoryCounters() void OS_DumpMemoryCounters()

View File

@ -37,12 +37,6 @@
namespace glslang { namespace glslang {
void InitGlobalLock();
void GetGlobalLock();
void ReleaseGlobalLock();
typedef unsigned int (*TThreadEntrypoint)(void*);
void OS_DumpMemoryCounters(); void OS_DumpMemoryCounters();
} // end namespace glslang } // end namespace glslang