Skip to content

Commit c78e65c

Browse files
authored
[lldb][plugin] Use counter directly for number of readers (#139252)
Here we were initializing & locking a shared_mutex in a thread, while releasing it in the parent which may/often turned out to be a different thread (shared_mutex::unlock_shared is undefined behavior if called from a thread that doesn't hold the lock). Switch to counter to more simply keep track of number of readers and simply lock/unlock rather than utilizing reader mutex to verify last freed (and so requiring this matching thread init/destroy behavior).
1 parent ba2dacd commit c78e65c

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,20 @@ DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() {
189189
}
190190

191191
DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) {
192-
m_cu->m_die_array_scoped_mutex.lock_shared();
192+
llvm::sys::ScopedLock lock(m_cu->m_die_array_scoped_mutex);
193+
++m_cu->m_die_array_scoped_count;
193194
}
194195

195196
DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
196197
if (!m_cu)
197198
return;
198-
m_cu->m_die_array_scoped_mutex.unlock_shared();
199-
if (!m_clear_dies || m_cu->m_cancel_scopes)
200-
return;
201-
// Be sure no other ScopedExtractDIEs is running anymore.
202-
llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
203-
llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
204-
if (m_cu->m_cancel_scopes)
205-
return;
206-
m_cu->ClearDIEsRWLocked();
199+
llvm::sys::ScopedLock lock(m_cu->m_die_array_scoped_mutex);
200+
--m_cu->m_die_array_scoped_count;
201+
if (m_cu->m_die_array_scoped_count == 0 && m_clear_dies &&
202+
!m_cu->m_cancel_scopes) {
203+
llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
204+
m_cu->ClearDIEsRWLocked();
205+
}
207206
}
208207

209208
DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs)

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
1818
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
1919
#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
20+
#include "llvm/Support/Mutex.h"
2021
#include "llvm/Support/RWMutex.h"
2122
#include <atomic>
2223
#include <optional>
@@ -328,7 +329,8 @@ class DWARFUnit : public DWARFExpression::Delegate, public UserID {
328329
DWARFDebugInfoEntry::collection m_die_array;
329330
mutable llvm::sys::RWMutex m_die_array_mutex;
330331
// It is used for tracking of ScopedExtractDIEs instances.
331-
mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
332+
mutable llvm::sys::Mutex m_die_array_scoped_mutex;
333+
mutable int m_die_array_scoped_count = 0;
332334
// ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
333335
// as someone called ExtractDIEsIfNeeded().
334336
std::atomic<bool> m_cancel_scopes;

lldb/test/API/tools/lldb-dap/console/TestDAP_console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_diagnositcs(self):
176176
f"target create --core {core}", context="repl"
177177
)
178178

179-
output = self.get_important()
179+
output = self.get_important(timeout=2.0)
180180
self.assertIn(
181181
"warning: unable to retrieve process ID from minidump file",
182182
output,

0 commit comments

Comments
 (0)