Skip to content

Commit 71afa88

Browse files
committed
[llvm/Support] Make llvm::sys::RWMutex Lockable (llvm#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 <[email protected]> (cherry picked from commit bf447e2)
1 parent e2c7bdb commit 71afa88

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

llvm/include/llvm/Support/RWMutex.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class RWMutexImpl {
6666
/// Unconditionally release the lock in reader mode.
6767
bool unlock_shared();
6868

69+
/// Attempts to acquire the lock in reader mode. Returns immediately.
70+
/// @returns true on successful lock acquisition, false otherwise.
71+
bool try_lock_shared();
72+
6973
/// Attempts to unconditionally acquire the lock in reader mode. If the
7074
/// lock is held by any readers, this method will wait until it can
7175
/// acquire the lock.
@@ -78,6 +82,10 @@ class RWMutexImpl {
7882
/// Unconditionally release the lock in write mode.
7983
bool unlock();
8084

85+
/// Attempts to acquire the lock in writer mode. Returns immediately.
86+
/// @returns true on successful lock acquisition, false otherwise.
87+
bool try_lock();
88+
8189
//@}
8290
/// @name Platform Dependent Data
8391
/// @{
@@ -126,6 +134,8 @@ template <bool mt_only> class SmartRWMutex {
126134
return true;
127135
}
128136

137+
bool try_lock_shared() { return impl.try_lock_shared(); }
138+
129139
bool lock() {
130140
if (!mt_only || llvm_is_multithreaded()) {
131141
impl.lock();
@@ -151,6 +161,8 @@ template <bool mt_only> class SmartRWMutex {
151161
--writers;
152162
return true;
153163
}
164+
165+
bool try_lock() { return impl.try_lock(); }
154166
};
155167

156168
typedef SmartRWMutex<false> RWMutex;

llvm/lib/Support/RWMutex.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ RWMutexImpl::~RWMutexImpl() = default;
2626

2727
bool RWMutexImpl::lock_shared() { return true; }
2828
bool RWMutexImpl::unlock_shared() { return true; }
29+
bool RWMutexImpl::try_lock_shared() { return true; }
2930
bool RWMutexImpl::lock() { return true; }
3031
bool RWMutexImpl::unlock() { return true; }
32+
bool RWMutexImpl::try_lock() { return true; }
3133

3234
#else
3335

@@ -87,6 +89,14 @@ RWMutexImpl::unlock_shared()
8789
return errorcode == 0;
8890
}
8991

92+
bool RWMutexImpl::try_lock_shared() {
93+
pthread_rwlock_t *rwlock = static_cast<pthread_rwlock_t *>(data_);
94+
assert(rwlock != nullptr);
95+
96+
int errorcode = pthread_rwlock_tryrdlock(rwlock);
97+
return errorcode == 0;
98+
}
99+
90100
bool
91101
RWMutexImpl::lock()
92102
{
@@ -107,6 +117,14 @@ RWMutexImpl::unlock()
107117
return errorcode == 0;
108118
}
109119

120+
bool RWMutexImpl::try_lock() {
121+
pthread_rwlock_t *rwlock = static_cast<pthread_rwlock_t *>(data_);
122+
assert(rwlock != nullptr);
123+
124+
int errorcode = pthread_rwlock_trywrlock(rwlock);
125+
return errorcode == 0;
126+
}
127+
110128
#else
111129

112130
RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
@@ -123,6 +141,10 @@ bool RWMutexImpl::unlock_shared() {
123141
return static_cast<MutexImpl *>(data_)->release();
124142
}
125143

144+
bool RWMutexImpl::try_lock_shared() {
145+
return static_cast<MutexImpl *>(data_)->tryacquire();
146+
}
147+
126148
bool RWMutexImpl::lock() {
127149
return static_cast<MutexImpl *>(data_)->acquire();
128150
}
@@ -131,6 +153,10 @@ bool RWMutexImpl::unlock() {
131153
return static_cast<MutexImpl *>(data_)->release();
132154
}
133155

156+
bool RWMutexImpl::try_lock() {
157+
return static_cast<MutexImpl *>(data_)->tryacquire();
158+
}
159+
134160
#endif
135161
#endif
136162
#endif

0 commit comments

Comments
 (0)