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:
parent
ff8e59f510
commit
cb42541e51
@ -38,8 +38,8 @@
|
|||||||
|
|
||||||
#include "InitializeDll.h"
|
#include "InitializeDll.h"
|
||||||
#include "../glslang/Include/InitializeGlobals.h"
|
#include "../glslang/Include/InitializeGlobals.h"
|
||||||
|
|
||||||
#include "../glslang/Public/ShaderLang.h"
|
#include "../glslang/Public/ShaderLang.h"
|
||||||
|
#include "../glslang/Include/PoolAlloc.h"
|
||||||
|
|
||||||
namespace glslang {
|
namespace glslang {
|
||||||
|
|
||||||
@ -105,57 +105,14 @@ bool InitThread()
|
|||||||
if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
|
if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
InitializeMemoryPools();
|
|
||||||
|
|
||||||
if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
|
if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
|
||||||
assert(0 && "InitThread(): Unable to set init flag.");
|
assert(0 && "InitThread(): Unable to set init flag.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glslang::SetThreadPoolAllocator(nullptr);
|
||||||
|
|
||||||
return true;
|
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
|
} // end namespace glslang
|
||||||
|
@ -40,8 +40,6 @@ namespace glslang {
|
|||||||
|
|
||||||
bool InitProcess();
|
bool InitProcess();
|
||||||
bool InitThread();
|
bool InitThread();
|
||||||
bool DetachThread(); // TODO: use this or remove it; ideally make it unneeded
|
|
||||||
bool DetachProcess();
|
|
||||||
|
|
||||||
} // end namespace glslang
|
} // end namespace glslang
|
||||||
|
|
||||||
|
@ -37,11 +37,7 @@
|
|||||||
|
|
||||||
namespace glslang {
|
namespace glslang {
|
||||||
|
|
||||||
void InitializeMemoryPools();
|
|
||||||
void FreeMemoryPools();
|
|
||||||
|
|
||||||
bool InitializePoolIndex();
|
bool InitializePoolIndex();
|
||||||
void FreePoolIndex();
|
|
||||||
|
|
||||||
} // end namespace glslang
|
} // end namespace glslang
|
||||||
|
|
||||||
|
@ -56,11 +56,14 @@ class TUniformMap;
|
|||||||
//
|
//
|
||||||
class TShHandleBase {
|
class TShHandleBase {
|
||||||
public:
|
public:
|
||||||
TShHandleBase() { }
|
TShHandleBase() { pool = new glslang::TPoolAllocator; }
|
||||||
virtual ~TShHandleBase() { }
|
virtual ~TShHandleBase() { delete pool; }
|
||||||
virtual TCompiler* getAsCompiler() { return 0; }
|
virtual TCompiler* getAsCompiler() { return 0; }
|
||||||
virtual TLinker* getAsLinker() { return 0; }
|
virtual TLinker* getAsLinker() { return 0; }
|
||||||
virtual TUniformMap* getAsUniformMap() { return 0; }
|
virtual TUniformMap* getAsUniformMap() { return 0; }
|
||||||
|
virtual glslang::TPoolAllocator* getPool() const { return pool; }
|
||||||
|
private:
|
||||||
|
glslang::TPoolAllocator* pool;
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -43,35 +43,16 @@ namespace glslang {
|
|||||||
// Process-wide TLS index
|
// Process-wide TLS index
|
||||||
OS_TLSIndex PoolIndex;
|
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.
|
// Return the thread-specific current pool.
|
||||||
TPoolAllocator& GetThreadPoolAllocator()
|
TPoolAllocator& GetThreadPoolAllocator()
|
||||||
{
|
{
|
||||||
return *GetThreadMemoryPools()->threadPoolAllocator;
|
return *static_cast<TPoolAllocator*>(OS_GetTLSValue(PoolIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the thread-specific current pool.
|
// Set the thread-specific current pool.
|
||||||
void SetThreadPoolAllocator(TPoolAllocator* poolAllocator)
|
void SetThreadPoolAllocator(TPoolAllocator* poolAllocator)
|
||||||
{
|
{
|
||||||
GetThreadMemoryPools()->threadPoolAllocator = poolAllocator;
|
OS_SetTLSValue(PoolIndex, poolAllocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process-wide set up of the TLS pool storage.
|
// Process-wide set up of the TLS pool storage.
|
||||||
@ -81,39 +62,9 @@ bool InitializePoolIndex()
|
|||||||
if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
|
if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
SetThreadMemoryPools(nullptr);
|
|
||||||
|
|
||||||
return true;
|
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
|
// Implement the functionality of the TPoolAllocator class, which
|
||||||
// is documented in PoolAlloc.h.
|
// is documented in PoolAlloc.h.
|
||||||
|
@ -722,9 +722,6 @@ bool ProcessDeferred(
|
|||||||
const std::string sourceEntryPointName = "",
|
const std::string sourceEntryPointName = "",
|
||||||
const TEnvironment* environment = nullptr) // optional way of fully setting all versions, overriding the above
|
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.
|
// This must be undone (.pop()) by the caller, after it finishes consuming the created tree.
|
||||||
GetThreadPoolAllocator().push();
|
GetThreadPoolAllocator().push();
|
||||||
|
|
||||||
@ -1299,7 +1296,7 @@ int __fastcall ShFinalize()
|
|||||||
glslang::HlslScanContext::deleteKeywordMap();
|
glslang::HlslScanContext::deleteKeywordMap();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return DetachProcess() ? 1 : 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -1332,6 +1329,8 @@ int ShCompile(
|
|||||||
if (compiler == 0)
|
if (compiler == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
SetThreadPoolAllocator(compiler->getPool());
|
||||||
|
|
||||||
compiler->infoSink.info.erase();
|
compiler->infoSink.info.erase();
|
||||||
compiler->infoSink.debug.erase();
|
compiler->infoSink.debug.erase();
|
||||||
|
|
||||||
@ -1389,6 +1388,8 @@ int ShLinkExt(
|
|||||||
TShHandleBase* base = reinterpret_cast<TShHandleBase*>(linkHandle);
|
TShHandleBase* base = reinterpret_cast<TShHandleBase*>(linkHandle);
|
||||||
TLinker* linker = static_cast<TLinker*>(base->getAsLinker());
|
TLinker* linker = static_cast<TLinker*>(base->getAsLinker());
|
||||||
|
|
||||||
|
SetThreadPoolAllocator(linker->getPool());
|
||||||
|
|
||||||
if (linker == 0)
|
if (linker == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1423,9 +1424,6 @@ void ShSetEncryptionMethod(ShHandle handle)
|
|||||||
//
|
//
|
||||||
const char* ShGetInfoLog(const ShHandle handle)
|
const char* ShGetInfoLog(const ShHandle handle)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1449,9 +1447,6 @@ const char* ShGetInfoLog(const ShHandle handle)
|
|||||||
//
|
//
|
||||||
const void* ShGetExecutable(const ShHandle handle)
|
const void* ShGetExecutable(const ShHandle handle)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1474,9 +1469,6 @@ const void* ShGetExecutable(const ShHandle handle)
|
|||||||
//
|
//
|
||||||
int ShSetVirtualAttributeBindings(const ShHandle handle, const ShBindingTable* table)
|
int ShSetVirtualAttributeBindings(const ShHandle handle, const ShBindingTable* table)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1496,9 +1488,6 @@ int ShSetVirtualAttributeBindings(const ShHandle handle, const ShBindingTable* t
|
|||||||
//
|
//
|
||||||
int ShSetFixedAttributeBindings(const ShHandle handle, const ShBindingTable* table)
|
int ShSetFixedAttributeBindings(const ShHandle handle, const ShBindingTable* table)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1517,9 +1506,6 @@ int ShSetFixedAttributeBindings(const ShHandle handle, const ShBindingTable* tab
|
|||||||
//
|
//
|
||||||
int ShExcludeAttributes(const ShHandle handle, int *attributes, int count)
|
int ShExcludeAttributes(const ShHandle handle, int *attributes, int count)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1541,9 +1527,6 @@ int ShExcludeAttributes(const ShHandle handle, int *attributes, int count)
|
|||||||
//
|
//
|
||||||
int ShGetUniformLocation(const ShHandle handle, const char* name)
|
int ShGetUniformLocation(const ShHandle handle, const char* name)
|
||||||
{
|
{
|
||||||
if (!InitThread())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (handle == 0)
|
if (handle == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -1707,8 +1690,8 @@ bool TShader::parse(const TBuiltInResource* builtInResources, int defaultVersion
|
|||||||
{
|
{
|
||||||
if (! InitThread())
|
if (! InitThread())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
SetThreadPoolAllocator(pool);
|
SetThreadPoolAllocator(pool);
|
||||||
|
|
||||||
if (! preamble)
|
if (! preamble)
|
||||||
preamble = "";
|
preamble = "";
|
||||||
|
|
||||||
@ -1730,8 +1713,8 @@ bool TShader::preprocess(const TBuiltInResource* builtInResources,
|
|||||||
{
|
{
|
||||||
if (! InitThread())
|
if (! InitThread())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
SetThreadPoolAllocator(pool);
|
SetThreadPoolAllocator(pool);
|
||||||
|
|
||||||
if (! preamble)
|
if (! preamble)
|
||||||
preamble = "";
|
preamble = "";
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ namespace glslang {
|
|||||||
//
|
//
|
||||||
static void DetachThreadLinux(void *)
|
static void DetachThreadLinux(void *)
|
||||||
{
|
{
|
||||||
DetachThread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user