summaryrefslogtreecommitdiffstats
path: root/source/slang
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-09-25 11:52:18 -0400
committerGitHub <noreply@github.com>2023-09-25 11:52:18 -0400
commit2e761512add35fc719b5e5f5ef3315577777124c (patch)
treec91f9e02c8f8607ace76ad9a60744fe2cfd8748f /source/slang
parentab04bd0dd7dd6a818bbac8c5fef9372c4f597352 (diff)
Fix for threading issues around global session & epoch ids. (#3232)
* Fix for threading issues around global session & epoch ids. * Make m_epochId atomic for thread visibility.
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/slang-ast-base.cpp2
-rw-r--r--source/slang/slang-ast-builder.cpp10
-rwxr-xr-xsource/slang/slang-compiler.h6
3 files changed, 9 insertions, 9 deletions
diff --git a/source/slang/slang-ast-base.cpp b/source/slang/slang-ast-base.cpp
index 5b4a4ea0c..7fa0a8886 100644
--- a/source/slang/slang-ast-base.cpp
+++ b/source/slang/slang-ast-base.cpp
@@ -23,7 +23,7 @@ void NodeBase::_initDebug(ASTNodeType inAstNodeType, ASTBuilder* inAstBuilder)
DeclRefBase* Decl::getDefaultDeclRef()
{
auto astBuilder = getCurrentASTBuilder();
- if (astBuilder->getEpoch() != m_defaultDeclRefEpoch || !m_defaultDeclRef)
+ if (astBuilder && astBuilder->getEpoch() != m_defaultDeclRefEpoch || !m_defaultDeclRef)
{
m_defaultDeclRef = astBuilder->getOrCreate<DirectDeclRef>(this);
m_defaultDeclRefEpoch = astBuilder->getEpoch();
diff --git a/source/slang/slang-ast-builder.cpp b/source/slang/slang-ast-builder.cpp
index 4a3756c1f..b2d1e5c09 100644
--- a/source/slang/slang-ast-builder.cpp
+++ b/source/slang/slang-ast-builder.cpp
@@ -221,12 +221,6 @@ Decl* SharedASTBuilder::tryFindMagicDecl(const String& name)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ASTBuilder !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-Index& _getGlobalASTEpochId()
-{
- static thread_local Index epochId = 1;
- return epochId;
-}
-
ASTBuilder::ASTBuilder(SharedASTBuilder* sharedASTBuilder, const String& name):
m_sharedASTBuilder(sharedASTBuilder),
m_name(name),
@@ -260,12 +254,12 @@ ASTBuilder::~ASTBuilder()
Index ASTBuilder::getEpoch()
{
- return _getGlobalASTEpochId();
+ return getSharedASTBuilder()->m_session->m_epochId;
}
void ASTBuilder::incrementEpoch()
{
- _getGlobalASTEpochId()++;
+ getSharedASTBuilder()->m_session->m_epochId++;
}
NodeBase* ASTBuilder::createByNodeType(ASTNodeType nodeType)
diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h
index 95995349a..aae18d08f 100755
--- a/source/slang/slang-compiler.h
+++ b/source/slang/slang-compiler.h
@@ -3008,6 +3008,12 @@ namespace Slang
/// Get the downstream compiler for a transition
IDownstreamCompiler* getDownstreamCompiler(CodeGenTarget source, CodeGenTarget target);
+ // This needs to be atomic not because of contention between threads as `Session` is
+ // *not* multithreaded, but can be used exclusively on one thread at a time.
+ // The need for atomic is purely for visibility. If the session is used on a different
+ // thread we need to be sure any changes to m_epochId are visible to this thread.
+ std::atomic<Index> m_epochId = 1;
+
Scope* baseLanguageScope = nullptr;
Scope* coreLanguageScope = nullptr;
Scope* hlslLanguageScope = nullptr;