Skip to content

Commit 2739972

Browse files
committed
[llvm/Support] Make llvm::sys::RWMutex Lockable
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]>
1 parent 4cd11c9 commit 2739972

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
@@ -63,6 +63,10 @@ class RWMutexImpl {
6363
/// Unconditionally release the lock in reader mode.
6464
bool unlock_shared();
6565

66+
/// Attempts to acquire the lock in reader mode. Returns immediately.
67+
/// @returns true on successful lock acquisition, false otherwise.
68+
bool try_lock_shared();
69+
6670
/// Attempts to unconditionally acquire the lock in reader mode. If the
6771
/// lock is held by any readers, this method will wait until it can
6872
/// acquire the lock.
@@ -75,6 +79,10 @@ class RWMutexImpl {
7579
/// Unconditionally release the lock in write mode.
7680
bool unlock();
7781

82+
/// Attempts to acquire the lock in writer mode. Returns immediately.
83+
/// @returns true on successful lock acquisition, false otherwise.
84+
bool try_lock();
85+
7886
//@}
7987
/// @name Platform Dependent Data
8088
/// @{
@@ -123,6 +131,8 @@ template <bool mt_only> class SmartRWMutex {
123131
return true;
124132
}
125133

134+
bool try_lock_shared() { return impl.try_lock_shared(); }
135+
126136
bool lock() {
127137
if (!mt_only || llvm_is_multithreaded()) {
128138
impl.lock();
@@ -148,6 +158,8 @@ template <bool mt_only> class SmartRWMutex {
148158
--writers;
149159
return true;
150160
}
161+
162+
bool try_lock() { return impl.try_lock(); }
151163
};
152164

153165
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)