[Lldb-commits] [lldb] Allow multiple destroy callbacks in `SBDebugger::SetDestroyCallback()` (PR #89868)

via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 26 14:04:33 PDT 2024


================
@@ -743,9 +743,19 @@ DebuggerSP Debugger::CreateInstance(lldb::LogOutputCallback log_callback,
 }
 
 void Debugger::HandleDestroyCallback() {
-  if (m_destroy_callback) {
-    m_destroy_callback(GetID(), m_destroy_callback_baton);
-    m_destroy_callback = nullptr;
+  std::lock_guard<std::recursive_mutex> guard(m_destroy_callback_mutex);
+  const lldb::user_id_t user_id = GetID();
+  // In case one destroy callback adds or removes other destroy callbacks
+  // which aren't taken care of in the same inner loop.
+  while (m_destroy_callback_and_baton.size()) {
+    auto iter = m_destroy_callback_and_baton.begin();
+    while (iter != m_destroy_callback_and_baton.end()) {
+      // Invoke the callback and remove the entry from the map
+      const auto &callback = iter->second.first;
+      const auto &baton = iter->second.second;
+      callback(user_id, baton);
+      iter = m_destroy_callback_and_baton.erase(iter);
+    }
----------------
royitaqi wrote:

Readability discussion (ignore if you think this particular one is too small a topic):

I thought about writing the inner loop as `for (begin; end; ) { callback(); iter = map.erase(); }`. However, I felt it's basically the same as the above. So I just left it as is. LMK if you think the `for` loop will look better.

FWIW, if we do use the `for` loop, I didn't like the idea of moving the `iter = map.erase()` part into the 3rd clause of the `for` line. LMK if you think differently

https://github.com/llvm/llvm-project/pull/89868


More information about the lldb-commits mailing list