[Lldb-commits] [PATCH] D109013: [lldb] Tighten lock in Language::ForEach

Alex Langford via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 31 11:53:10 PDT 2021

bulbazord created this revision.
bulbazord added reviewers: jingham, teemperor, JDevlieghere, clayborg, aprantl.
bulbazord requested review of this revision.
Herald added a project: LLDB.

It is easy to accidentally introduce a deadlock by having the callback
passed to Language::ForEach also attempt to acquire the same lock. It
is easy enough to disallow the callback from calling anything in
Language directly, but it may happen through a series of other
function/method calls.

The solution I am proposing is to tighten the lock in Language::ForEach
so that it is only held as we gather the currently loaded language
plugins. We store them in a vector and then iterate through them with
the callback so that the callback can't introduce a callback.

  rG LLVM Github Monorepo



Index: lldb/source/Target/Language.cpp
--- lldb/source/Target/Language.cpp
+++ lldb/source/Target/Language.cpp
@@ -108,10 +108,18 @@
-  std::lock_guard<std::mutex> guard(GetLanguagesMutex());
-  LanguagesMap &map(GetLanguagesMap());
-  for (const auto &entry : map) {
-    if (!callback(entry.second.get()))
+  std::vector<Language *> loaded_plugins;
+  {
+    std::lock_guard<std::mutex> guard(GetLanguagesMutex());
+    LanguagesMap &map(GetLanguagesMap());
+    for (const auto &entry : map) {
+      if (entry.second)
+        loaded_plugins.push_back(entry.second.get());
+    }
+  }
+  for (auto *lang : loaded_plugins) {
+    if (!callback(lang))

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109013.369758.patch
Type: text/x-patch
Size: 762 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210831/4bebcfc1/attachment.bin>

More information about the lldb-commits mailing list