Skip to content

Commit 928b7b2

Browse files
authored
Merge pull request KhronosGroup#2361 from ben-clayton/revert-thread-local
Revert changes that migrate to `thread_local`.
2 parents 7ab4564 + 2a44064 commit 928b7b2

File tree

8 files changed

+161
-35
lines changed

8 files changed

+161
-35
lines changed

CMakeLists.txt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,23 +279,17 @@ function(glslang_add_build_info_dependency target)
279279
add_dependencies(${target} glslang-build-info)
280280
endfunction()
281281

282-
# glslang_default_to_hidden_visibility() makes the symbol visibility hidden by
283-
# default for <target>.
284-
function(glslang_default_to_hidden_visibility target)
285-
if(NOT WIN32)
286-
target_compile_options(${target} PRIVATE "-fvisibility=hidden")
287-
endif()
288-
endfunction()
289-
290282
# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
291-
# default for <target>, and sets the GLSLANG_IS_SHARED_LIBRARY define, and
292-
# GLSLANG_EXPORTING to 1 when specifically building <target>.
283+
# default for <target> when building shared libraries, and sets the
284+
# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
285+
# building <target>.
293286
function(glslang_only_export_explicit_symbols target)
294-
glslang_default_to_hidden_visibility(${target})
295287
if(BUILD_SHARED_LIBS)
296288
target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
297289
if(WIN32)
298290
target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
291+
else()
292+
target_compile_options(${target} PRIVATE "-fvisibility=hidden")
299293
endif()
300294
endif()
301295
endfunction()

OGLCompilersDLL/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ set(SOURCES InitializeDll.cpp InitializeDll.h)
3636
add_library(OGLCompiler STATIC ${SOURCES})
3737
set_property(TARGET OGLCompiler PROPERTY FOLDER glslang)
3838
set_property(TARGET OGLCompiler PROPERTY POSITION_INDEPENDENT_CODE ON)
39-
glslang_default_to_hidden_visibility(OGLCompiler)
4039

4140
if(WIN32)
4241
source_group("Source" FILES ${SOURCES})

OGLCompilersDLL/InitializeDll.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,134 @@
3232
// POSSIBILITY OF SUCH DAMAGE.
3333
//
3434

35+
#define SH_EXPORTING
36+
37+
#include <cassert>
38+
39+
#include "InitializeDll.h"
40+
#include "../glslang/Include/InitializeGlobals.h"
41+
#include "../glslang/Public/ShaderLang.h"
42+
#include "../glslang/Include/PoolAlloc.h"
43+
3544
namespace glslang {
3645

46+
OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
47+
48+
// Per-process initialization.
49+
// Needs to be called at least once before parsing, etc. is done.
50+
// Will also do thread initialization for the calling thread; other
51+
// threads will need to do that explicitly.
52+
bool InitProcess()
53+
{
54+
glslang::GetGlobalLock();
55+
56+
if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
57+
//
58+
// Function is re-entrant.
59+
//
60+
61+
glslang::ReleaseGlobalLock();
62+
return true;
63+
}
64+
65+
ThreadInitializeIndex = OS_AllocTLSIndex();
66+
67+
if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
68+
assert(0 && "InitProcess(): Failed to allocate TLS area for init flag");
69+
70+
glslang::ReleaseGlobalLock();
71+
return false;
72+
}
73+
74+
if (! InitializePoolIndex()) {
75+
assert(0 && "InitProcess(): Failed to initialize global pool");
76+
77+
glslang::ReleaseGlobalLock();
78+
return false;
79+
}
80+
81+
if (! InitThread()) {
82+
assert(0 && "InitProcess(): Failed to initialize thread");
83+
84+
glslang::ReleaseGlobalLock();
85+
return false;
86+
}
87+
88+
glslang::ReleaseGlobalLock();
89+
return true;
90+
}
91+
92+
// Per-thread scoped initialization.
93+
// Must be called at least once by each new thread sharing the
94+
// symbol tables, etc., needed to parse.
95+
bool InitThread()
96+
{
97+
//
98+
// This function is re-entrant
99+
//
100+
if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
101+
assert(0 && "InitThread(): Process hasn't been initalised.");
102+
return false;
103+
}
104+
105+
if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
106+
return true;
107+
108+
if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
109+
assert(0 && "InitThread(): Unable to set init flag.");
110+
return false;
111+
}
112+
113+
glslang::SetThreadPoolAllocator(nullptr);
114+
115+
return true;
116+
}
117+
118+
// Not necessary to call this: InitThread() is reentrant, and the need
119+
// to do per thread tear down has been removed.
120+
//
121+
// This is kept, with memory management removed, to satisfy any exiting
122+
// calls to it that rely on it.
123+
bool DetachThread()
124+
{
125+
bool success = true;
126+
127+
if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
128+
return true;
129+
130+
//
131+
// Function is re-entrant and this thread may not have been initialized.
132+
//
133+
if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
134+
if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
135+
assert(0 && "DetachThread(): Unable to clear init flag.");
136+
success = false;
137+
}
138+
}
139+
140+
return success;
141+
}
142+
143+
// Not necessary to call this: InitProcess() is reentrant.
144+
//
145+
// This is kept, with memory management removed, to satisfy any exiting
146+
// calls to it that rely on it.
147+
//
148+
// Users of glslang should call shFinalize() or glslang::FinalizeProcess() for
149+
// process-scoped memory tear down.
150+
bool DetachProcess()
151+
{
152+
bool success = true;
153+
154+
if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
155+
return true;
156+
157+
success = DetachThread();
158+
159+
OS_FreeTLSIndex(ThreadInitializeIndex);
160+
ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
161+
162+
return success;
163+
}
164+
37165
} // end namespace glslang

OGLCompilersDLL/InitializeDll.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838

3939
namespace glslang {
4040

41-
inline bool InitProcess() { return true; } // DEPRECATED
42-
inline bool InitThread() { return true; } // DEPRECATED
43-
inline bool DetachThread() { return true; } // DEPRECATED
44-
inline bool DetachProcess() { return true; } // DEPRECATED
41+
bool InitProcess();
42+
bool InitThread();
43+
bool DetachThread(); // not called from standalone, perhaps other tools rely on parts of it
44+
bool DetachProcess(); // not called from standalone, perhaps other tools rely on parts of it
4545

4646
} // end namespace glslang
4747

SPIRV/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ set(SOURCES
4343
CInterface/spirv_c_interface.cpp)
4444

4545
set(SPVREMAP_SOURCES
46-
SPVRemapper.cpp)
46+
SPVRemapper.cpp
47+
doc.cpp)
4748

4849
set(HEADERS
4950
bitutils.h
@@ -68,7 +69,6 @@ set(SPVREMAP_HEADERS
6869
doc.h)
6970

7071
add_library(SPIRV ${LIB_TYPE} ${SOURCES} ${HEADERS})
71-
target_link_libraries(SPIRV PRIVATE MachineIndependent)
7272
set_property(TARGET SPIRV PROPERTY FOLDER glslang)
7373
set_property(TARGET SPIRV PROPERTY POSITION_INDEPENDENT_CODE ON)
7474
target_include_directories(SPIRV PUBLIC
@@ -79,7 +79,6 @@ glslang_add_build_info_dependency(SPIRV)
7979

8080
if (ENABLE_SPVREMAPPER)
8181
add_library(SPVRemapper ${LIB_TYPE} ${SPVREMAP_SOURCES} ${SPVREMAP_HEADERS})
82-
target_link_libraries(SPVRemapper PRIVATE SPIRV)
8382
set_property(TARGET SPVRemapper PROPERTY FOLDER glslang)
8483
set_property(TARGET SPVRemapper PROPERTY POSITION_INDEPENDENT_CODE ON)
8584
endif()
@@ -96,10 +95,12 @@ if(ENABLE_OPT)
9695
PRIVATE ${spirv-tools_SOURCE_DIR}/include
9796
PRIVATE ${spirv-tools_SOURCE_DIR}/source
9897
)
99-
target_link_libraries(SPIRV PRIVATE SPIRV-Tools-opt)
98+
target_link_libraries(SPIRV PRIVATE MachineIndependent SPIRV-Tools-opt)
10099
target_include_directories(SPIRV PUBLIC
101100
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../External>
102101
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/External>)
102+
else()
103+
target_link_libraries(SPIRV PRIVATE MachineIndependent)
103104
endif(ENABLE_OPT)
104105

105106
if(WIN32)

glslang/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ add_library(GenericCodeGen STATIC
5252
GenericCodeGen/Link.cpp)
5353
set_property(TARGET GenericCodeGen PROPERTY POSITION_INDEPENDENT_CODE ON)
5454
set_property(TARGET GenericCodeGen PROPERTY FOLDER glslang)
55-
glslang_default_to_hidden_visibility(GenericCodeGen)
5655

5756
################################################################################
5857
# MachineIndependent
@@ -134,7 +133,6 @@ endif(ENABLE_HLSL)
134133
add_library(MachineIndependent STATIC ${MACHINEINDEPENDENT_SOURCES} ${MACHINEINDEPENDENT_HEADERS})
135134
set_property(TARGET MachineIndependent PROPERTY POSITION_INDEPENDENT_CODE ON)
136135
set_property(TARGET MachineIndependent PROPERTY FOLDER glslang)
137-
glslang_only_export_explicit_symbols(MachineIndependent)
138136

139137
glslang_add_build_info_dependency(MachineIndependent)
140138

@@ -170,7 +168,7 @@ set_target_properties(glslang PROPERTIES
170168
POSITION_INDEPENDENT_CODE ON
171169
VERSION "${GLSLANG_VERSION}"
172170
SOVERSION "${GLSLANG_VERSION_MAJOR}")
173-
target_link_libraries(glslang PRIVATE OGLCompiler MachineIndependent)
171+
target_link_libraries(glslang PRIVATE OGLCompiler OSDependent MachineIndependent)
174172
target_include_directories(glslang PUBLIC
175173
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
176174
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

glslang/Include/InitializeGlobals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
namespace glslang {
3939

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

4242
} // end namespace glslang
4343

glslang/MachineIndependent/PoolAlloc.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,34 @@
3535
#include "../Include/Common.h"
3636
#include "../Include/PoolAlloc.h"
3737

38-
namespace glslang {
38+
#include "../Include/InitializeGlobals.h"
39+
#include "../OSDependent/osinclude.h"
3940

40-
namespace {
41-
thread_local TPoolAllocator* threadPoolAllocator = nullptr;
41+
namespace glslang {
4242

43-
TPoolAllocator* GetDefaultThreadPoolAllocator()
44-
{
45-
thread_local TPoolAllocator defaultAllocator;
46-
return &defaultAllocator;
47-
}
48-
} // anonymous namespace
43+
// Process-wide TLS index
44+
OS_TLSIndex PoolIndex;
4945

5046
// Return the thread-specific current pool.
5147
TPoolAllocator& GetThreadPoolAllocator()
5248
{
53-
return *(threadPoolAllocator ? threadPoolAllocator : GetDefaultThreadPoolAllocator());
49+
return *static_cast<TPoolAllocator*>(OS_GetTLSValue(PoolIndex));
5450
}
5551

5652
// Set the thread-specific current pool.
5753
void SetThreadPoolAllocator(TPoolAllocator* poolAllocator)
5854
{
59-
threadPoolAllocator = poolAllocator;
55+
OS_SetTLSValue(PoolIndex, poolAllocator);
56+
}
57+
58+
// Process-wide set up of the TLS pool storage.
59+
bool InitializePoolIndex()
60+
{
61+
// Allocate a TLS index.
62+
if ((PoolIndex = OS_AllocTLSIndex()) == OS_INVALID_TLS_INDEX)
63+
return false;
64+
65+
return true;
6066
}
6167

6268
//

0 commit comments

Comments
 (0)