Skip to content

Commit 65c3587

Browse files
Vipul-Cariappamcbarton
authored andcommitted
fix thread safety for static locals
Having static locals can be risky and we might want to move them into the InterpreterInfo class
1 parent e1e07e0 commit 65c3587

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,12 +3080,16 @@ int get_wrapper_code(compat::Interpreter& I, const FunctionDecl* FD,
30803080
JitCall::GenericCall make_wrapper(compat::Interpreter& I,
30813081
const FunctionDecl* FD) {
30823082
static std::map<const FunctionDecl*, void*> gWrapperStore;
3083+
static std::recursive_mutex gWrapperStoreMutex;
30833084

30843085
LOCK(getInterpInfo(FD));
30853086

3086-
auto R = gWrapperStore.find(FD);
3087-
if (R != gWrapperStore.end())
3088-
return (JitCall::GenericCall)R->second;
3087+
{
3088+
std::lock_guard<std::recursive_mutex> Lock(gWrapperStoreMutex);
3089+
auto R = gWrapperStore.find(FD);
3090+
if (R != gWrapperStore.end())
3091+
return (JitCall::GenericCall)R->second;
3092+
}
30893093

30903094
std::string wrapper_name;
30913095
std::string wrapper_code;
@@ -3103,6 +3107,7 @@ JitCall::GenericCall make_wrapper(compat::Interpreter& I,
31033107
void* wrapper =
31043108
compile_wrapper(I, wrapper_name, wrapper_code, withAccessControl);
31053109
if (wrapper) {
3110+
std::lock_guard<std::recursive_mutex> Lock(gWrapperStoreMutex);
31063111
gWrapperStore.insert(std::make_pair(FD, wrapper));
31073112
} else {
31083113
llvm::errs() << "TClingCallFunc::make_wrapper"
@@ -3174,12 +3179,16 @@ static JitCall::DestructorCall make_dtor_wrapper(compat::Interpreter& interp,
31743179
//--
31753180

31763181
static map<const Decl*, void*> gDtorWrapperStore;
3182+
static std::recursive_mutex gDtorWrapperStoreMutex;
31773183

31783184
LOCK(getInterpInfo(D));
31793185

3180-
auto I = gDtorWrapperStore.find(D);
3181-
if (I != gDtorWrapperStore.end())
3182-
return (JitCall::DestructorCall)I->second;
3186+
{
3187+
std::lock_guard<std::recursive_mutex> Lock(gDtorWrapperStoreMutex);
3188+
auto I = gDtorWrapperStore.find(D);
3189+
if (I != gDtorWrapperStore.end())
3190+
return (JitCall::DestructorCall)I->second;
3191+
}
31833192

31843193
//
31853194
// Make the wrapper name.
@@ -3279,6 +3288,7 @@ static JitCall::DestructorCall make_dtor_wrapper(compat::Interpreter& interp,
32793288
void* F = compile_wrapper(interp, wrapper_name, wrapper,
32803289
/*withAccessControl=*/false);
32813290
if (F) {
3291+
std::lock_guard<std::recursive_mutex> Lock(gDtorWrapperStoreMutex);
32823292
gDtorWrapperStore.insert(make_pair(D, F));
32833293
} else {
32843294
llvm::errs() << "make_dtor_wrapper"
@@ -3974,14 +3984,14 @@ InstantiateTemplateFunctionFromString(const char* function_template,
39743984
TInterp_t interp /*=nullptr*/) {
39753985
// FIXME: Drop this interface and replace it with the proper overload
39763986
// resolution handling and template instantiation selection.
3987+
auto* I = static_cast<compat::Interpreter*>(interp);
3988+
LOCK(getInterpInfo(I));
39773989

39783990
// Try to force template instantiation and overload resolution.
39793991
static unsigned long long var_count = 0;
39803992
std::string id = "__Cppyy_GetMethTmpl_" + std::to_string(var_count++);
39813993
std::string instance = "auto " + id + " = " + function_template + ";\n";
39823994

3983-
auto* I = static_cast<compat::Interpreter*>(interp);
3984-
LOCK(getInterpInfo(I));
39853995
if (!Cpp::Declare(instance.c_str(), /*silent=*/false, interp)) {
39863996
auto* VD = static_cast<VarDecl*>(Cpp::GetNamed(id, nullptr, interp));
39873997
Expr* E = VD->getInit()->IgnoreImpCasts();

0 commit comments

Comments
 (0)