Index: source/Plugins/Platform/POSIX/PlatformPOSIX.cpp =================================================================== --- source/Plugins/Platform/POSIX/PlatformPOSIX.cpp +++ source/Plugins/Platform/POSIX/PlatformPOSIX.cpp @@ -1036,17 +1036,23 @@ thread_sp->CalculateExecutionContext(exe_ctx); Status utility_error; - - // The UtilityFunction is held in the Process. Platforms don't track the - // lifespan of the Targets that use them, we can't put this in the Platform. - UtilityFunction *dlopen_utility_func - = process->GetLoadImageUtilityFunction(this); + UtilityFunction *dlopen_utility_func; ValueList arguments; FunctionCaller *do_dlopen_function = nullptr; - if (!dlopen_utility_func) { - // Make the UtilityFunction: - dlopen_utility_func = MakeLoadImageUtilityFunction(exe_ctx, error); + // Multiple threads could be calling into DoLoadImage concurrently, + // only one should be allowed to create the UtilityFunction. + { + static std::mutex do_dlopen_mutex; + std::lock_guard lock(do_dlopen_mutex); + + // The UtilityFunction is held in the Process. Platforms don't track the + // lifespan of the Targets that use them, we can't put this in the Platform. + dlopen_utility_func = process->GetLoadImageUtilityFunction(this); + if (!dlopen_utility_func) { + // Make the UtilityFunction: + dlopen_utility_func = MakeLoadImageUtilityFunction(exe_ctx, error); + } } // If we couldn't make it, the error will be in error, so we can exit here. if (!dlopen_utility_func)