Skip to content

Commit

Permalink
Simplify PoolAlloc with use of thread_local.
Browse files Browse the repository at this point in the history
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: KhronosGroup#2346
  • Loading branch information
ben-clayton authored and kd-11 committed Dec 11, 2023
1 parent f56d219 commit f527002
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion glslang/Include/InitializeGlobals.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

namespace glslang {

bool InitializePoolIndex();
inline bool InitializePoolIndex() { return true; } // DEPRECATED: No need to call

} // end namespace glslang

Expand Down
28 changes: 11 additions & 17 deletions glslang/MachineIndependent/PoolAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,28 @@
#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;
}

//
Expand Down

0 comments on commit f527002

Please sign in to comment.