aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Haas <git@haasn.dev>2021-11-08 14:47:32 +0100
committerNiklas Haas <git@haasn.dev>2021-11-08 14:47:32 +0100
commit3971424207e27a662d4416eedfb68e18ff287350 (patch)
treee3e8e5bca34e8f32c3e4e1f53e933cedd5b3ab1d
parenteb92526d5e04572fdf1d15d2f3ae10a967c2f46f (diff)
downloadglslang-3971424207e27a662d4416eedfb68e18ff287350.tar.gz
Initialize global mutex in a thread-safe manner
Currently, ShInitialize() and friends call glslang::InitGlobalLock() which *overwrites* the global mutex. As such, even though it ostensibly takes a mutex, this function is actually completely thread-unsafe. Fix it by using pthread_once to ensure the mutex is only initialized once, and then never again.
-rw-r--r--glslang/OSDependent/Unix/ossource.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/glslang/OSDependent/Unix/ossource.cpp b/glslang/OSDependent/Unix/ossource.cpp
index 3f029f02..81da99c2 100644
--- a/glslang/OSDependent/Unix/ossource.cpp
+++ b/glslang/OSDependent/Unix/ossource.cpp
@@ -172,7 +172,7 @@ namespace {
pthread_mutex_t gMutex;
}
-void InitGlobalLock()
+static void InitMutex(void)
{
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
@@ -180,6 +180,12 @@ void InitGlobalLock()
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);