@@ -473,61 +473,67 @@ void SwiftLanguageRuntimeImpl::SetupReflection() {
473473
474474 // The global ABI bit is read by the Swift runtime library.
475475 SetupABIBit ();
476-
477- std::lock_guard<std::recursive_mutex> lock (m_add_module_mutex);
478- if (m_initialized_reflection_ctx)
479- return ;
476+
477+ // A copy of the modules that should be added, to prevent mutating the list
478+ // while iterating over it.
479+ ModuleList modules_snapshot ;
480480
481481 auto &target = m_process.GetTarget ();
482482 auto exe_module = target.GetExecutableModule ();
483+ {
484+ std::lock_guard<std::recursive_mutex> lock (m_add_module_mutex);
485+ if (m_initialized_reflection_ctx)
486+ return ;
487+
488+ if (!exe_module) {
489+ LLDB_LOGF (GetLog (LLDBLog::Types), " %s: Failed to get executable module" ,
490+ LLVM_PRETTY_FUNCTION);
491+ m_initialized_reflection_ctx = false ;
492+ return ;
493+ }
483494
484- if (!exe_module) {
485- LLDB_LOGF (GetLog (LLDBLog::Types), " %s: Failed to get executable module" ,
486- LLVM_PRETTY_FUNCTION);
487- m_initialized_reflection_ctx = false ;
488- return ;
489- }
495+ bool objc_interop = (bool )findRuntime (m_process, RuntimeKind::ObjC);
496+ const char *objc_interop_msg =
497+ objc_interop ? " with Objective-C interopability" : " Swift only" ;
490498
491- bool objc_interop = (bool )findRuntime (m_process, RuntimeKind::ObjC);
492- const char *objc_interop_msg =
493- objc_interop ? " with Objective-C interopability" : " Swift only" ;
494-
495- auto &triple = exe_module->GetArchitecture ().GetTriple ();
496- if (triple.isArch64Bit ()) {
497- LLDB_LOGF (GetLog (LLDBLog::Types),
498- " Initializing a 64-bit reflection context (%s) for \" %s\" " ,
499- triple.str ().c_str (), objc_interop_msg);
500- m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64 (
501- this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
502- } else if (triple.isArch32Bit ()) {
503- LLDB_LOGF (GetLog (LLDBLog::Types),
504- " Initializing a 32-bit reflection context (%s) for \" %s\" " ,
505- triple.str ().c_str (), objc_interop_msg);
506- m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32 (
507- this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
508- } else {
509- LLDB_LOGF (GetLog (LLDBLog::Types),
510- " Could not initialize reflection context for \" %s\" " ,
511- triple.str ().c_str ());
512- }
499+ auto &triple = exe_module->GetArchitecture ().GetTriple ();
500+ if (triple.isArch64Bit ()) {
501+ LLDB_LOGF (GetLog (LLDBLog::Types),
502+ " Initializing a 64-bit reflection context (%s) for \" %s\" " ,
503+ triple.str ().c_str (), objc_interop_msg);
504+ m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext64 (
505+ this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
506+ } else if (triple.isArch32Bit ()) {
507+ LLDB_LOGF (GetLog (LLDBLog::Types),
508+ " Initializing a 32-bit reflection context (%s) for \" %s\" " ,
509+ triple.str ().c_str (), objc_interop_msg);
510+ m_reflection_ctx = ReflectionContextInterface::CreateReflectionContext32 (
511+ this ->GetMemoryReader (), objc_interop, GetSwiftMetadataCache ());
512+ } else {
513+ LLDB_LOGF (GetLog (LLDBLog::Types),
514+ " Could not initialize reflection context for \" %s\" " ,
515+ triple.str ().c_str ());
516+ }
513517
514- m_initialized_reflection_ctx = true ;
518+ modules_snapshot = std::move (m_modules_to_add);
519+ m_modules_to_add.Clear ();
520+ m_initialized_reflection_ctx = true ;
521+ }
515522
516523 Progress progress (
517524 llvm::formatv (" Setting up Swift reflection for '{0}'" ,
518525 exe_module->GetFileSpec ().GetFilename ().AsCString ()),
519- m_modules_to_add .GetSize ());
526+ modules_snapshot .GetSize ());
520527
521528 size_t completion = 0 ;
522529
523530 // Add all defered modules to reflection context that were added to
524531 // the target since this SwiftLanguageRuntime was created.
525- m_modules_to_add .ForEach ([&](const ModuleSP &module_sp) -> bool {
532+ modules_snapshot .ForEach ([&](const ModuleSP &module_sp) -> bool {
526533 AddModuleToReflectionContext (module_sp);
527534 progress.Increment (++completion);
528535 return true ;
529536 });
530- m_modules_to_add.Clear ();
531537}
532538
533539bool SwiftLanguageRuntimeImpl::IsABIStable () {
0 commit comments