[PATCH] D26135: [Support] Generic version of RWMutex

bekket mcclane via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 31 00:40:27 PDT 2016


mshockwave created this revision.
mshockwave added reviewers: resistor, chandlerc.
mshockwave added a subscriber: llvm-commits.

This revision provides similar lock/unlock behavior to `std::unique_lock` in addition to `std::defer_lock`. 
User can choose to lock the mutex upon object construction or do it manually. The lock would be unlocked either within the destructor or by calling `unlock` explicitly. 
The original `SmartScopedReader` and `SmartScopedWriter` are also replaced by this generic version of reader/writer lock.


https://reviews.llvm.org/D26135

Files:
  include/llvm/Support/RWMutex.h


Index: include/llvm/Support/RWMutex.h
===================================================================
--- include/llvm/Support/RWMutex.h
+++ include/llvm/Support/RWMutex.h
@@ -142,35 +142,76 @@
     };
     typedef SmartRWMutex<false> RWMutex;
 
-    /// ScopedReader - RAII acquisition of a reader lock
-    template<bool mt_only>
-    struct SmartScopedReader {
-      SmartRWMutex<mt_only>& mutex;
+    /// SmartReader - Generic variant that allows customized
+    /// lock/unlock policy
+    template<bool mt_only,bool auto_lock>
+    class GenericSmartReader {
+      SmartRWMutex<mt_only>& Mutex;
+      bool IsLocked;
+
+    public:
+      explicit GenericSmartReader(SmartRWMutex<mt_only>& m) :
+        Mutex(m), IsLocked(false) {
+        if(auto_lock) lock();
+      }
 
-      explicit SmartScopedReader(SmartRWMutex<mt_only>& m) : mutex(m) {
-        mutex.lock_shared();
+      void lock() { 
+        if(!IsLocked){
+          Mutex.lock_shared();
+          IsLocked = true;
+        }
       }
 
-      ~SmartScopedReader() {
-        mutex.unlock_shared();
+      void unlock() { 
+        if(IsLocked) Mutex.unlock_shared(); 
+        IsLocked = false;
       }
+
+      ~GenericSmartReader() { unlock(); }
     };
-    typedef SmartScopedReader<false> ScopedReader;
+    template<bool auto_lock>
+    using SmartReader = GenericSmartReader<false,auto_lock>;
 
-    /// ScopedWriter - RAII acquisition of a writer lock
-    template<bool mt_only>
-    struct SmartScopedWriter {
-      SmartRWMutex<mt_only>& mutex;
+    /// SmartWriter - Generic variant that allows customized
+    /// lock/unlock policy
+    template<bool mt_only,bool auto_lock>
+    class GenericSmartWriter {
+      SmartRWMutex<mt_only>& Mutex;
+      bool IsLocked;
 
-      explicit SmartScopedWriter(SmartRWMutex<mt_only>& m) : mutex(m) {
-        mutex.lock();
+    public:
+      explicit GenericSmartWriter(SmartRWMutex<mt_only>& m) :
+        Mutex(m), IsLocked(false){
+        if(auto_lock) lock();
       }
 
-      ~SmartScopedWriter() {
-        mutex.unlock();
+      void lock(){
+        if(!IsLocked) {
+          Mutex.lock();
+          IsLocked = true;
+        }
       }
+
+      void unlock() {
+        if(IsLocked) Mutex.unlock();
+        IsLocked = false;
+      }
+
+      ~GenericSmartWriter() { unlock(); }
     };
-    typedef SmartScopedWriter<false> ScopedWriter;
+    template<bool auto_lock>
+    using SmartWriter = GenericSmartWriter<false,auto_lock>;
+
+    /// ScopedReader - RAII acquisition of a reader lock
+    template<bool mt_only>
+    using SmartScopedReader = GenericSmartReader<mt_only,true>;
+    using ScopedReader = SmartScopedReader<false>;
+
+    /// ScopedWriter - RAII acquisition of a writer lock
+    template<bool mt_only>
+    using SmartScopedWriter = GenericSmartWriter<mt_only,true>;
+    using ScopedWriter = SmartScopedWriter<false>;
+
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26135.76353.patch
Type: text/x-patch
Size: 2906 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161031/cb9f2caa/attachment.bin>


More information about the llvm-commits mailing list