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.
This commit is contained in:
John Kessenich 2017-11-12 23:12:57 -07:00
parent ff8e59f510
commit cb42541e51
7 changed files with 17 additions and 130 deletions

View File

@ -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

View File

@ -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

View File

@ -37,11 +37,7 @@
namespace glslang {
void InitializeMemoryPools();
void FreeMemoryPools();
bool InitializePoolIndex();
void FreePoolIndex();
} // end namespace glslang

View File

@ -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;
};
//

View File

@ -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<TThreadMemoryPools*>(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<TPoolAllocator*>(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.

View File

@ -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<TShHandleBase*>(linkHandle);
TLinker* linker = static_cast<TLinker*>(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 = "";

View File

@ -56,7 +56,6 @@ namespace glslang {
//
static void DetachThreadLinux(void *)
{
DetachThread();
}
//