[Lldb-commits] [lldb] r357276 - Use the multi-lockable form of std::lock for operator=

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Fri Mar 29 10:07:30 PDT 2019


Author: jingham
Date: Fri Mar 29 10:07:30 2019
New Revision: 357276

URL: http://llvm.org/viewvc/llvm-project?rev=357276&view=rev
Log:
Use the multi-lockable form of std::lock for operator=

For = operators for lists that have mutexes, we were either
just taking the locks sequentially or hand-rolling a trick
to try to avoid lock inversion.  Use the std::lock mechanism
for this instead.

Differential Revision: https://reviews.llvm.org/D59957

Modified:
    lldb/trunk/include/lldb/Core/ModuleSpec.h
    lldb/trunk/include/lldb/Utility/StreamTee.h
    lldb/trunk/source/Breakpoint/BreakpointLocationCollection.cpp
    lldb/trunk/source/Core/ModuleList.cpp
    lldb/trunk/source/Target/SectionLoadList.cpp
    lldb/trunk/source/Target/ThreadList.cpp

Modified: lldb/trunk/include/lldb/Core/ModuleSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ModuleSpec.h?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/ModuleSpec.h (original)
+++ lldb/trunk/include/lldb/Core/ModuleSpec.h Fri Mar 29 10:07:30 2019
@@ -311,8 +311,10 @@ public:
 
   ModuleSpecList &operator=(const ModuleSpecList &rhs) {
     if (this != &rhs) {
-      std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex);
-      std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex);
+      std::lock(m_mutex, rhs.m_mutex);
+      std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex, std::adopt_lock);
+      std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex, 
+                                                      std::adopt_lock);
       m_specs = rhs.m_specs;
     }
     return *this;

Modified: lldb/trunk/include/lldb/Utility/StreamTee.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/StreamTee.h?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/StreamTee.h (original)
+++ lldb/trunk/include/lldb/Utility/StreamTee.h Fri Mar 29 10:07:30 2019
@@ -49,8 +49,11 @@ public:
   StreamTee &operator=(const StreamTee &rhs) {
     if (this != &rhs) {
       Stream::operator=(rhs);
-      std::lock_guard<std::recursive_mutex> lhs_locker(m_streams_mutex);
-      std::lock_guard<std::recursive_mutex> rhs_locker(rhs.m_streams_mutex);
+      std::lock(m_streams_mutex, rhs.m_streams_mutex);
+      std::lock_guard<std::recursive_mutex> lhs_locker(m_streams_mutex,
+                                                       std::adopt_lock);
+      std::lock_guard<std::recursive_mutex> rhs_locker(rhs.m_streams_mutex,
+                                                       std::adopt_lock);
       m_streams = rhs.m_streams;
     }
     return *this;

Modified: lldb/trunk/source/Breakpoint/BreakpointLocationCollection.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/BreakpointLocationCollection.cpp?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/BreakpointLocationCollection.cpp (original)
+++ lldb/trunk/source/Breakpoint/BreakpointLocationCollection.cpp Fri Mar 29 10:07:30 2019
@@ -181,17 +181,11 @@ void BreakpointLocationCollection::GetDe
 
 BreakpointLocationCollection &BreakpointLocationCollection::operator=(
     const BreakpointLocationCollection &rhs) {
-  // Same trick as in ModuleList to avoid lock inversion.
   if (this != &rhs) {
-    if (uintptr_t(this) > uintptr_t(&rhs)) {
-      std::lock_guard<std::mutex> lhs_guard(m_collection_mutex);
-      std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex);
+      std::lock(m_collection_mutex, rhs.m_collection_mutex);
+      std::lock_guard<std::mutex> lhs_guard(m_collection_mutex, std::adopt_lock);
+      std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex, std::adopt_lock);
       m_break_loc_collection = rhs.m_break_loc_collection;
-    } else {
-      std::lock_guard<std::mutex> lhs_guard(m_collection_mutex);
-      std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex);
-      m_break_loc_collection = rhs.m_break_loc_collection;
-    }
   }
   return *this;
 }

Modified: lldb/trunk/source/Core/ModuleList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ModuleList.cpp?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/source/Core/ModuleList.cpp (original)
+++ lldb/trunk/source/Core/ModuleList.cpp Fri Mar 29 10:07:30 2019
@@ -130,25 +130,12 @@ ModuleList::ModuleList(ModuleList::Notif
 
 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
   if (this != &rhs) {
-    // That's probably me nit-picking, but in theoretical situation:
-    //
-    // * that two threads A B and
-    // * two ModuleList's x y do opposite assignments ie.:
-    //
-    //  in thread A: | in thread B:
-    //    x = y;     |   y = x;
-    //
-    // This establishes correct(same) lock taking order and thus avoids
-    // priority inversion.
-    if (uintptr_t(this) > uintptr_t(&rhs)) {
-      std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
-      std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
-      m_modules = rhs.m_modules;
-    } else {
-      std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
-      std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
-      m_modules = rhs.m_modules;
-    }
+    std::lock(m_modules_mutex, rhs.m_modules_mutex);
+    std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex, 
+                                                    std::adopt_lock);
+    std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex, 
+                                                    std::adopt_lock);
+    m_modules = rhs.m_modules;
   }
   return *this;
 }

Modified: lldb/trunk/source/Target/SectionLoadList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/SectionLoadList.cpp?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/source/Target/SectionLoadList.cpp (original)
+++ lldb/trunk/source/Target/SectionLoadList.cpp Fri Mar 29 10:07:30 2019
@@ -27,8 +27,9 @@ SectionLoadList::SectionLoadList(const S
 }
 
 void SectionLoadList::operator=(const SectionLoadList &rhs) {
-  std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex);
-  std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex);
+  std::lock(m_mutex, rhs.m_mutex);
+  std::lock_guard<std::recursive_mutex> lhs_guard(m_mutex, std::adopt_lock);
+  std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_mutex, std::adopt_lock);
   m_addr_to_sect = rhs.m_addr_to_sect;
   m_sect_to_addr = rhs.m_sect_to_addr;
 }

Modified: lldb/trunk/source/Target/ThreadList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/ThreadList.cpp?rev=357276&r1=357275&r2=357276&view=diff
==============================================================================
--- lldb/trunk/source/Target/ThreadList.cpp (original)
+++ lldb/trunk/source/Target/ThreadList.cpp Fri Mar 29 10:07:30 2019
@@ -37,8 +37,10 @@ const ThreadList &ThreadList::operator=(
   if (this != &rhs) {
     // Lock both mutexes to make sure neither side changes anyone on us while
     // the assignment occurs
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex());
+    std::lock(GetMutex(), rhs.GetMutex());
+    std::lock_guard<std::recursive_mutex> guard(GetMutex(), std::adopt_lock);
+    std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex(), 
+                                                    std::adopt_lock);
 
     m_process = rhs.m_process;
     m_stop_id = rhs.m_stop_id;




More information about the lldb-commits mailing list