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