Skip to content

Fix FIXME: thread-safe initialization of DispatchInitializer#844

Open
voyager-jhk wants to merge 4 commits intocompiler-research:mainfrom
voyager-jhk:fix-fixme
Open

Fix FIXME: thread-safe initialization of DispatchInitializer#844
voyager-jhk wants to merge 4 commits intocompiler-research:mainfrom
voyager-jhk:fix-fixme

Conversation

@voyager-jhk
Copy link

Fix FIXME regarding thread-safe initialization of DispatchInitializer.

This change replaces the global static instance with a function-local
static to guarantee thread-safe initialization according to C++11.

The initializer is now created via GetDispatchInitializer().

@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.56%. Comparing base (5b9435c) to head (4ce13f7).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #844   +/-   ##
=======================================
  Coverage   79.56%   79.56%           
=======================================
  Files          11       11           
  Lines        4013     4013           
=======================================
  Hits         3193     3193           
  Misses        820      820           

see 1 file with indirect coverage changes
see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

// FIXME: Make this threadsafe by moving it as a function static.
DispatchInitializer g_dispatch_init;
// Thread-safe initialization using function-local static
static DispatchInitializer& GetDispatchInitializer() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'GetDispatchInitializer' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]

Suggested change
static DispatchInitializer& GetDispatchInitializer() {
DispatchInitializer& GetDispatchInitializer() {

static DispatchInitializer instance;
return instance;
}
static DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'g_dispatch_init' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]

Suggested change
static DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
DispatchInitializer& g_dispatch_init = GetDispatchInitializer();

static DispatchInitializer instance;
return instance;
}
static DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'g_dispatch_init' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

static DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
                            ^

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

static DispatchInitializer instance;
return instance;
}
DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'g_dispatch_init' provides global access to a non-const object; consider making the referenced data 'const' [cppcoreguidelines-avoid-non-const-global-variables]

DispatchInitializer& g_dispatch_init = GetDispatchInitializer();
                     ^

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance for a test?

@voyager-jhk
Copy link
Author

Any chance for a test?

Thanks! I've pushed a fix to restore the initialization trigger.

It looks like CI didn't run for the latest commit. Could you please rerun it?

@mcbarton
Copy link
Collaborator

Any chance for a test?

Thanks! I've pushed a fix to restore the initialization trigger.

It looks like CI didn't run for the latest commit. Could you please rerun it?

@voyager-jhk The ci doesn't run automatically for those who are contributing for the first time. It needs to be manually run. You will need to rebase as well to fix the Emscripten ci.

@aaronj0
Copy link
Collaborator

aaronj0 commented Mar 11, 2026

This is a one-time initialisation for the CppInterOpDispatchTests binary and not thread-safe for the tests (this is not called by each test). We need a single instance creation in Utils.cpp for the API to be loaded at runtime... Unless this fixes some use of gtest_parallel across binaries, I don't see what this patch achieves

@github-actions
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants