Merge pull request #2801 from haasn/thread_safety

Thread safety fixes
diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp
index d02eae6..a2dd71c 100644
--- a/glslang/MachineIndependent/ShaderLang.cpp
+++ b/glslang/MachineIndependent/ShaderLang.cpp
@@ -1343,7 +1343,6 @@
 
     glslang::GetGlobalLock();
     ++NumberOfClients;
-    glslang::ReleaseGlobalLock();
 
     if (PerProcessGPA == nullptr)
         PerProcessGPA = new TPoolAllocator();
@@ -1353,6 +1352,7 @@
     glslang::HlslScanContext::fillInKeywordMap();
 #endif
 
+    glslang::ReleaseGlobalLock();
     return 1;
 }
 
@@ -1415,9 +1415,10 @@
     --NumberOfClients;
     assert(NumberOfClients >= 0);
     bool finalize = NumberOfClients == 0;
-    glslang::ReleaseGlobalLock();
-    if (! finalize)
+    if (! finalize) {
+        glslang::ReleaseGlobalLock();
         return 1;
+    }
 
     for (int version = 0; version < VersionCount; ++version) {
         for (int spvVersion = 0; spvVersion < SpvVersionCount; ++spvVersion) {
@@ -1455,6 +1456,7 @@
     glslang::HlslScanContext::deleteKeywordMap();
 #endif
 
+    glslang::ReleaseGlobalLock();
     return 1;
 }
 
diff --git a/glslang/OSDependent/Unix/ossource.cpp b/glslang/OSDependent/Unix/ossource.cpp
index 3f029f0..81da99c 100644
--- a/glslang/OSDependent/Unix/ossource.cpp
+++ b/glslang/OSDependent/Unix/ossource.cpp
@@ -172,7 +172,7 @@
     pthread_mutex_t gMutex;
 }
 
-void InitGlobalLock()
+static void InitMutex(void)
 {
   pthread_mutexattr_t mutexattr;
   pthread_mutexattr_init(&mutexattr);
@@ -180,6 +180,12 @@
   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);