Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm/Support] Make llvm::sys::RWMutex Lockable #90667

Merged
merged 1 commit into from
May 1, 2024

Conversation

medismailben
Copy link
Member

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 30, 2024

@llvm/pr-subscribers-llvm-support

Author: Med Ismail Bennani (medismailben)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/90667.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Support/RWMutex.h (+6)
  • (modified) llvm/lib/Support/RWMutex.cpp (+13)
diff --git a/llvm/include/llvm/Support/RWMutex.h b/llvm/include/llvm/Support/RWMutex.h
index 32987c3b98f1cb..ac9c542625bd79 100644
--- a/llvm/include/llvm/Support/RWMutex.h
+++ b/llvm/include/llvm/Support/RWMutex.h
@@ -75,6 +75,10 @@ class RWMutexImpl {
   /// Unconditionally release the lock in write mode.
   bool unlock();
 
+  /// Attempts to acquire the lock. Returns immediately.
+  /// @returns true on successful lock acquisition, false otherwise.
+  bool try_lock();
+
   //@}
   /// @name Platform Dependent Data
   /// @{
@@ -148,6 +152,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..d93c7a22271c57 100644
--- a/llvm/lib/Support/RWMutex.cpp
+++ b/llvm/lib/Support/RWMutex.cpp
@@ -28,6 +28,7 @@ bool RWMutexImpl::lock_shared() { return true; }
 bool RWMutexImpl::unlock_shared() { return true; }
 bool RWMutexImpl::lock() { return true; }
 bool RWMutexImpl::unlock() { return true; }
+bool RWMutexImpl::try_lock() { return true; }
 
 #else
 
@@ -107,6 +108,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_tryrdlock(rwlock);
+  return errorcode == 0;
+}
+
 #else
 
 RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
@@ -131,6 +140,10 @@ bool RWMutexImpl::unlock() {
   return static_cast<MutexImpl *>(data_)->release();
 }
 
+bool RWMutexImpl::try_lock() {
+  return static_cast<MutexImpl *>(data_)->tryacquire();
+}
+
 #endif
 #endif
 #endif

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@bennani.ma>
Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is trying to implement something like is_locked. try_lock has fairly well defined semantics for std::mutex and pthreads. In both cases this acquires the lock if it succeeds. Our implementation shouldn't behave differently.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM!

@medismailben medismailben merged commit bf447e2 into llvm:main May 1, 2024
6 of 7 checks passed
@medismailben medismailben deleted the lockable-rwlock branch May 1, 2024 22:32
medismailben added a commit to medismailben/llvm-project that referenced this pull request May 1, 2024
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@bennani.ma>
(cherry picked from commit bf447e2)
JDevlieghere added a commit to apple/llvm-project that referenced this pull request May 2, 2024
[llvm/Support] Make `llvm::sys::RWMutex` Lockable (llvm#90667)
@dwblaikie
Copy link
Collaborator

as an aside - per the instructions here ( https://llvm.org/docs/GitHub.html#updating-pull-requests ) please don't force-push to pull request reviews, as it makes it difficult to trace back comments and discussion through the revisions of a patch (I think, especially, inline comments can get lost/hidden/hard to find when force pushing)

@medismailben
Copy link
Member Author

as an aside - per the instructions here ( https://llvm.org/docs/GitHub.html#updating-pull-requests ) please don't force-push to pull request reviews, as it makes it difficult to trace back comments and discussion through the revisions of a patch (I think, especially, inline comments can get lost/hidden/hard to find when force pushing)

Sure! I'll keep that in mind next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants