Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/include/llvm/Support/RWMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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.
Expand All @@ -78,6 +82,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
/// @{
Expand Down Expand Up @@ -126,6 +134,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();
Expand All @@ -151,6 +161,8 @@ template <bool mt_only> class SmartRWMutex {
--writers;
return true;
}

bool try_lock() { return impl.try_lock(); }
};

typedef SmartRWMutex<false> RWMutex;
Expand Down
26 changes: 26 additions & 0 deletions llvm/lib/Support/RWMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
{
Expand All @@ -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)) { }
Expand All @@ -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();
}
Expand All @@ -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