Simplify PoolAlloc with use of thread_local.
authorBen Clayton <bclayton@google.com>
Tue, 14 Jul 2020 17:32:12 +0000 (18:32 +0100)
committerBen Clayton <bclayton@google.com>
Tue, 21 Jul 2020 08:40:34 +0000 (09:40 +0100)
glslang is using C++ 11, which has first class support for variables of the `thread_local` storage class.

By dropping the use of the `OS_[GS]etTLSValue`, we can simplify the logic, and have it support a thread-local default allocator if none is provided.

Issue: #2346

glslang/Include/InitializeGlobals.h
glslang/MachineIndependent/PoolAlloc.cpp

index 95d0a40e99a89f9806330d37d8b9d830e8392d62..b7fdd7aabcb0199e157b9cd3b6ddd1416edface7 100644 (file)
@@ -37,7 +37,7 @@
 
 namespace glslang {
 
-bool InitializePoolIndex();
+inline bool InitializePoolIndex() { return true; } // DEPRECATED: No need to call
 
 } // end namespace glslang
 
index 84c40f4e79522aea79a5cf42921434e21e887fd5..c269d7d83c8f1342463d79b93d0abf7a425216af 100644 (file)
 #include "../Include/Common.h"
 #include "../Include/PoolAlloc.h"
 
-#include "../Include/InitializeGlobals.h"
-#include "../OSDependent/osinclude.h"
-
 namespace glslang {
 
-// Process-wide TLS index
-OS_TLSIndex PoolIndex;
+namespace {
+thread_local TPoolAllocator* threadPoolAllocator = nullptr;
+
+TPoolAllocator* GetDefaultThreadPoolAllocator()
+{
+    thread_local TPoolAllocator defaultAllocator;
+    return &defaultAllocator;
+}
+} // anonymous namespace
 
 // Return the thread-specific current pool.
 TPoolAllocator& GetThreadPoolAllocator()
 {
-    return *static_cast<TPoolAllocator*>(OS_GetTLSValue(PoolIndex));
+    return *(threadPoolAllocator ? threadPoolAllocator : GetDefaultThreadPoolAllocator());
 }
 
 // Set the thread-specific current pool.
 void SetThreadPoolAllocator(TPoolAllocator* poolAllocator)
 {
-    OS_SetTLSValue(PoolIndex, poolAllocator);
-}
-
-// Process-wide set up of the TLS pool storage.
-bool InitializePoolIndex()
-{
-    // Allocate a TLS index.
-    if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
-        return false;
-
-    return true;
+    threadPoolAllocator = poolAllocator;
 }
 
 //