Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MultiThreading] Avoid Static Initialization Order Fiasco #4307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
namespace multithreading
{

sofa::type::vector<ParallelImplementationsRegistry::Implementation> ParallelImplementationsRegistry::s_implementations;
sofa::type::vector<ParallelImplementationsRegistry::Implementation>& getStaticImplementations()
{
static sofa::type::vector<ParallelImplementationsRegistry::Implementation> s_implementations;
return s_implementations;
}

bool ParallelImplementationsRegistry::addEquivalentImplementations(
const std::string& sequentialImplementation, const std::string& parallelImplementation)
Expand All @@ -34,9 +38,9 @@ bool ParallelImplementationsRegistry::addEquivalentImplementations(

const auto it = findParallelImplementationImpl(sequentialImplementation);

if (it == s_implementations.end())
if (it == getStaticImplementations().end())
{
s_implementations.push_back(implementation);
getStaticImplementations().push_back(implementation);
return true;
}

Expand All @@ -62,7 +66,7 @@ std::string ParallelImplementationsRegistry::findParallelImplementation(
{
const auto it = findParallelImplementationImpl(sequentialImplementation);

if (it != s_implementations.end())
if (it != getImplementations().end())
{
return it->parallel;
}
Expand All @@ -72,14 +76,14 @@ std::string ParallelImplementationsRegistry::findParallelImplementation(
const sofa::type::vector<ParallelImplementationsRegistry::Implementation>&
ParallelImplementationsRegistry::getImplementations()
{
return s_implementations;
return getStaticImplementations();
}

sofa::type::vector<ParallelImplementationsRegistry::Implementation>::const_iterator
ParallelImplementationsRegistry::findParallelImplementationImpl(
const std::string& sequentialImplementation)
{
return std::find_if(s_implementations.begin(), s_implementations.end(),
return std::find_if(getStaticImplementations().begin(), getStaticImplementations().end(),
[&sequentialImplementation](const Implementation& i)
{
return i.sequential == sequentialImplementation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class SOFA_MULTITHREADING_PLUGIN_API ParallelImplementationsRegistry
static const sofa::type::vector<Implementation>& getImplementations();

private:
static sofa::type::vector<Implementation> s_implementations;

static sofa::type::vector<Implementation>::const_iterator findParallelImplementationImpl(const std::string& sequentialImplementation);
};
Expand Down
Loading