[llvm] bf447e2 - [llvm/Support] Make `llvm::sys::RWMutex` Lockable (#90667)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 15:32:40 PDT 2024


Author: Med Ismail Bennani
Date: 2024-05-01T15:32:36-07:00
New Revision: bf447e27d2ac7a81ea714ceca932eecaac6db77b

URL: https://github.com/llvm/llvm-project/commit/bf447e27d2ac7a81ea714ceca932eecaac6db77b
DIFF: https://github.com/llvm/llvm-project/commit/bf447e27d2ac7a81ea714ceca932eecaac6db77b.diff

LOG: [llvm/Support] Make `llvm::sys::RWMutex` Lockable (#90667)

This patch extends the `llvm::sys::RWMutex` class to fullfill the
`Lockable` requirement to include attempted locking, by implementing a
`bool try_lock` member function.

As the name suggests, this method will try to acquire to lock in a
non-blocking fashion and release it immediately. If it managed to
acquire the lock, returns `true`, otherwise returns `false`.

Signed-off-by: Med Ismail Bennani <ismail at bennani.ma>

Added: 
    

Modified: 
    llvm/include/llvm/Support/RWMutex.h
    llvm/lib/Support/RWMutex.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/RWMutex.h b/llvm/include/llvm/Support/RWMutex.h
index 32987c3b98f1cb..8d221aaab9ab96 100644
--- a/llvm/include/llvm/Support/RWMutex.h
+++ b/llvm/include/llvm/Support/RWMutex.h
@@ -63,6 +63,10 @@ class RWMutexImpl {
   /// Unconditionally release the lock in reader mode.
   bool unlock_shared();
 
+  /// Attempts to acquire the lock in reader mode. Returns immediately.
+  /// @returns true on successful lock acquisition, false otherwise.
+  bool try_lock_shared();
+
   /// Attempts to unconditionally acquire the lock in reader mode. If the
   /// lock is held by any readers, this method will wait until it can
   /// acquire the lock.
@@ -75,6 +79,10 @@ class RWMutexImpl {
   /// Unconditionally release the lock in write mode.
   bool unlock();
 
+  /// Attempts to acquire the lock in writer mode. Returns immediately.
+  /// @returns true on successful lock acquisition, false otherwise.
+  bool try_lock();
+
   //@}
   /// @name Platform Dependent Data
   /// @{
@@ -123,6 +131,8 @@ template <bool mt_only> class SmartRWMutex {
     return true;
   }
 
+  bool try_lock_shared() { return impl.try_lock_shared(); }
+
   bool lock() {
     if (!mt_only || llvm_is_multithreaded()) {
       impl.lock();
@@ -148,6 +158,8 @@ template <bool mt_only> class SmartRWMutex {
     --writers;
     return true;
   }
+
+  bool try_lock() { return impl.try_lock(); }
 };
 
 typedef SmartRWMutex<false> RWMutex;

diff  --git a/llvm/lib/Support/RWMutex.cpp b/llvm/lib/Support/RWMutex.cpp
index 5accf73e5f9404..d6fa956da634dd 100644
--- a/llvm/lib/Support/RWMutex.cpp
+++ b/llvm/lib/Support/RWMutex.cpp
@@ -26,8 +26,10 @@ RWMutexImpl::~RWMutexImpl() = default;
 
 bool RWMutexImpl::lock_shared() { return true; }
 bool RWMutexImpl::unlock_shared() { return true; }
+bool RWMutexImpl::try_lock_shared() { return true; }
 bool RWMutexImpl::lock() { return true; }
 bool RWMutexImpl::unlock() { return true; }
+bool RWMutexImpl::try_lock() { return true; }
 
 #else
 
@@ -87,6 +89,14 @@ RWMutexImpl::unlock_shared()
   return errorcode == 0;
 }
 
+bool RWMutexImpl::try_lock_shared() {
+  pthread_rwlock_t *rwlock = static_cast<pthread_rwlock_t *>(data_);
+  assert(rwlock != nullptr);
+
+  int errorcode = pthread_rwlock_tryrdlock(rwlock);
+  return errorcode == 0;
+}
+
 bool
 RWMutexImpl::lock()
 {
@@ -107,6 +117,14 @@ RWMutexImpl::unlock()
   return errorcode == 0;
 }
 
+bool RWMutexImpl::try_lock() {
+  pthread_rwlock_t *rwlock = static_cast<pthread_rwlock_t *>(data_);
+  assert(rwlock != nullptr);
+
+  int errorcode = pthread_rwlock_trywrlock(rwlock);
+  return errorcode == 0;
+}
+
 #else
 
 RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
@@ -123,6 +141,10 @@ bool RWMutexImpl::unlock_shared() {
   return static_cast<MutexImpl *>(data_)->release();
 }
 
+bool RWMutexImpl::try_lock_shared() {
+  return static_cast<MutexImpl *>(data_)->tryacquire();
+}
+
 bool RWMutexImpl::lock() {
   return static_cast<MutexImpl *>(data_)->acquire();
 }
@@ -131,6 +153,10 @@ bool RWMutexImpl::unlock() {
   return static_cast<MutexImpl *>(data_)->release();
 }
 
+bool RWMutexImpl::try_lock() {
+  return static_cast<MutexImpl *>(data_)->tryacquire();
+}
+
 #endif
 #endif
 #endif


        


More information about the llvm-commits mailing list