From cb42541e513a3d2d023730e07c5a05a0c608062c Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Sun, 12 Nov 2017 23:12:57 -0700 Subject: [PATCH] Memory: Remove the need for per-thread tear down. Make key objects using the memory pool own their own pool and delete it, such that there is not generic per-thread pool to manage. --- OGLCompilersDLL/InitializeDll.cpp | 49 ++------------------- OGLCompilersDLL/InitializeDll.h | 2 - glslang/Include/InitializeGlobals.h | 4 -- glslang/Include/ShHandle.h | 7 ++- glslang/MachineIndependent/PoolAlloc.cpp | 53 +---------------------- glslang/MachineIndependent/ShaderLang.cpp | 31 +++---------- glslang/OSDependent/Unix/ossource.cpp | 1 - 7 files changed, 17 insertions(+), 130 deletions(-) diff --git a/OGLCompilersDLL/InitializeDll.cpp b/OGLCompilersDLL/InitializeDll.cpp index 67bf15f9..d4e1b9f5 100644 --- a/OGLCompilersDLL/InitializeDll.cpp +++ b/OGLCompilersDLL/InitializeDll.cpp @@ -38,8 +38,8 @@ #include "InitializeDll.h" #include "../glslang/Include/InitializeGlobals.h" - #include "../glslang/Public/ShaderLang.h" +#include "../glslang/Include/PoolAlloc.h" namespace glslang { @@ -105,57 +105,14 @@ bool InitThread() if (OS_GetTLSValue(ThreadInitializeIndex) != 0) return true; - InitializeMemoryPools(); - if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) { assert(0 && "InitThread(): Unable to set init flag."); return false; } + glslang::SetThreadPoolAllocator(nullptr); + return true; } -// Thread-scoped tear down. Needs to be done by all but the last -// thread, which calls DetachProcess() instead. -// NB. TODO: Not currently being executed by each thread. -bool DetachThread() -{ - bool success = true; - - if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) - return true; - - // - // Function is re-entrant and this thread may not have been initialized. - // - if (OS_GetTLSValue(ThreadInitializeIndex) != 0) { - if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) { - assert(0 && "DetachThread(): Unable to clear init flag."); - success = false; - } - - FreeMemoryPools(); - } - - return success; -} - -// Process-scoped tear down. Needs to be done by final thread in process. -bool DetachProcess() -{ - bool success = true; - - if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) - return true; - - success = DetachThread(); - - FreePoolIndex(); - - OS_FreeTLSIndex(ThreadInitializeIndex); - ThreadInitializeIndex = OS_INVALID_TLS_INDEX; - - return success; -} - } // end namespace glslang diff --git a/OGLCompilersDLL/InitializeDll.h b/OGLCompilersDLL/InitializeDll.h index 281f3b1a..3f27ce9b 100644 --- a/OGLCompilersDLL/InitializeDll.h +++ b/OGLCompilersDLL/InitializeDll.h @@ -40,8 +40,6 @@ namespace glslang { bool InitProcess(); bool InitThread(); -bool DetachThread(); // TODO: use this or remove it; ideally make it unneeded -bool DetachProcess(); } // end namespace glslang diff --git a/glslang/Include/InitializeGlobals.h b/glslang/Include/InitializeGlobals.h index dd7554b6..95d0a40e 100644 --- a/glslang/Include/InitializeGlobals.h +++ b/glslang/Include/InitializeGlobals.h @@ -37,11 +37,7 @@ namespace glslang { -void InitializeMemoryPools(); -void FreeMemoryPools(); - bool InitializePoolIndex(); -void FreePoolIndex(); } // end namespace glslang diff --git a/glslang/Include/ShHandle.h b/glslang/Include/ShHandle.h index 64ba6d63..df07bd8e 100644 --- a/glslang/Include/ShHandle.h +++ b/glslang/Include/ShHandle.h @@ -56,11 +56,14 @@ class TUniformMap; // class TShHandleBase { public: - TShHandleBase() { } - virtual ~TShHandleBase() { } + TShHandleBase() { pool = new glslang::TPoolAllocator; } + virtual ~TShHandleBase() { delete pool; } virtual TCompiler* getAsCompiler() { return 0; } virtual TLinker* getAsLinker() { return 0; } virtual TUniformMap* getAsUniformMap() { return 0; } + virtual glslang::TPoolAllocator* getPool() const { return pool; } +private: + glslang::TPoolAllocator* pool; }; // diff --git a/glslang/MachineIndependent/PoolAlloc.cpp b/glslang/MachineIndependent/PoolAlloc.cpp index 57943b1f..c42057c2 100644 --- a/glslang/MachineIndependent/PoolAlloc.cpp +++ b/glslang/MachineIndependent/PoolAlloc.cpp @@ -43,35 +43,16 @@ namespace glslang { // Process-wide TLS index OS_TLSIndex PoolIndex; -// Per-thread structure holding pool pointers. -struct TThreadMemoryPools -{ - TPoolAllocator* threadPoolAllocator; // the current pool - TPoolAllocator* initialMemoryPool; // the original pool owned by this thread (this file), to be freed here as well -}; - -// Return the thread-specific pool pointers. -TThreadMemoryPools* GetThreadMemoryPools() -{ - return static_cast(OS_GetTLSValue(PoolIndex)); -} - -// Set the thread-specific pool pointers. -void SetThreadMemoryPools(TThreadMemoryPools* pools) -{ - OS_SetTLSValue(PoolIndex, pools); -} - // Return the thread-specific current pool. TPoolAllocator& GetThreadPoolAllocator() { - return *GetThreadMemoryPools()->threadPoolAllocator; + return *static_cast(OS_GetTLSValue(PoolIndex)); } // Set the thread-specific current pool. void SetThreadPoolAllocator(TPoolAllocator* poolAllocator) { - GetThreadMemoryPools()->threadPoolAllocator = poolAllocator; + OS_SetTLSValue(PoolIndex, poolAllocator); } // Process-wide set up of the TLS pool storage. @@ -81,39 +62,9 @@ bool InitializePoolIndex() if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX) return false; - SetThreadMemoryPools(nullptr); - return true; } -// Process-wide tear down of the TLS pool storage. -void FreePoolIndex() -{ - // Release the TLS index. - OS_FreeTLSIndex(PoolIndex); -} - -// Per-thread set up of the memory pools. -void InitializeMemoryPools() -{ - if (GetThreadMemoryPools() == nullptr) { - SetThreadMemoryPools(new TThreadMemoryPools()); - GetThreadMemoryPools()->initialMemoryPool = new TPoolAllocator(); - SetThreadPoolAllocator(GetThreadMemoryPools()->initialMemoryPool); - } -} - -// Per-thread tear down of the memory pools. -void FreeMemoryPools() -{ - if (GetThreadMemoryPools() != nullptr) { - if (GetThreadMemoryPools()->initialMemoryPool != nullptr) - delete GetThreadMemoryPools()->initialMemoryPool; - delete GetThreadMemoryPools(); - SetThreadMemoryPools(nullptr); - } -} - // // Implement the functionality of the TPoolAllocator class, which // is documented in PoolAlloc.h. diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index 43a94b79..ba6e8c44 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -722,9 +722,6 @@ bool ProcessDeferred( const std::string sourceEntryPointName = "", const TEnvironment* environment = nullptr) // optional way of fully setting all versions, overriding the above { - if (! InitThread()) - return false; - // This must be undone (.pop()) by the caller, after it finishes consuming the created tree. GetThreadPoolAllocator().push(); @@ -1299,7 +1296,7 @@ int __fastcall ShFinalize() glslang::HlslScanContext::deleteKeywordMap(); #endif - return DetachProcess() ? 1 : 0; + return 1; } // @@ -1332,6 +1329,8 @@ int ShCompile( if (compiler == 0) return 0; + SetThreadPoolAllocator(compiler->getPool()); + compiler->infoSink.info.erase(); compiler->infoSink.debug.erase(); @@ -1389,6 +1388,8 @@ int ShLinkExt( TShHandleBase* base = reinterpret_cast(linkHandle); TLinker* linker = static_cast(base->getAsLinker()); + SetThreadPoolAllocator(linker->getPool()); + if (linker == 0) return 0; @@ -1423,9 +1424,6 @@ void ShSetEncryptionMethod(ShHandle handle) // const char* ShGetInfoLog(const ShHandle handle) { - if (!InitThread()) - return 0; - if (handle == 0) return 0; @@ -1449,9 +1447,6 @@ const char* ShGetInfoLog(const ShHandle handle) // const void* ShGetExecutable(const ShHandle handle) { - if (!InitThread()) - return 0; - if (handle == 0) return 0; @@ -1474,9 +1469,6 @@ const void* ShGetExecutable(const ShHandle handle) // int ShSetVirtualAttributeBindings(const ShHandle handle, const ShBindingTable* table) { - if (!InitThread()) - return 0; - if (handle == 0) return 0; @@ -1496,9 +1488,6 @@ int ShSetVirtualAttributeBindings(const ShHandle handle, const ShBindingTable* t // int ShSetFixedAttributeBindings(const ShHandle handle, const ShBindingTable* table) { - if (!InitThread()) - return 0; - if (handle == 0) return 0; @@ -1517,9 +1506,6 @@ int ShSetFixedAttributeBindings(const ShHandle handle, const ShBindingTable* tab // int ShExcludeAttributes(const ShHandle handle, int *attributes, int count) { - if (!InitThread()) - return 0; - if (handle == 0) return 0; @@ -1541,9 +1527,6 @@ int ShExcludeAttributes(const ShHandle handle, int *attributes, int count) // int ShGetUniformLocation(const ShHandle handle, const char* name) { - if (!InitThread()) - return 0; - if (handle == 0) return -1; @@ -1707,8 +1690,8 @@ bool TShader::parse(const TBuiltInResource* builtInResources, int defaultVersion { if (! InitThread()) return false; - SetThreadPoolAllocator(pool); + if (! preamble) preamble = ""; @@ -1730,8 +1713,8 @@ bool TShader::preprocess(const TBuiltInResource* builtInResources, { if (! InitThread()) return false; - SetThreadPoolAllocator(pool); + if (! preamble) preamble = ""; diff --git a/glslang/OSDependent/Unix/ossource.cpp b/glslang/OSDependent/Unix/ossource.cpp index 24b77e16..9c16dc40 100644 --- a/glslang/OSDependent/Unix/ossource.cpp +++ b/glslang/OSDependent/Unix/ossource.cpp @@ -56,7 +56,6 @@ namespace glslang { // static void DetachThreadLinux(void *) { - DetachThread(); } //