#include <string>
#include <include/assert.h>
#include "lockdep.h"
-#include "include/atomic.h"
#include "common/valgrind.h"
+#include <atomic>
+
class RWLock final
{
mutable pthread_rwlock_t L;
std::string name;
mutable int id;
- mutable atomic_t nrlock, nwlock;
+ mutable std::atomic<unsigned> nrlock = { 0 }, nwlock = { 0 };
bool track, lockdep;
std::string unique_name(const char* name) const;
const RWLock& operator=(const RWLock& other) = delete;
RWLock(const std::string &n, bool track_lock=true, bool ld=true, bool prioritize_write=false)
- : name(n), id(-1), nrlock(0), nwlock(0), track(track_lock),
+ : name(n), id(-1), track(track_lock),
lockdep(ld) {
#if defined(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
if (prioritize_write) {
bool is_locked() const {
assert(track);
- return (nrlock.read() > 0) || (nwlock.read() > 0);
+ return (nrlock > 0) || (nwlock > 0);
}
bool is_wlocked() const {
assert(track);
- return (nwlock.read() > 0);
+ return (nwlock > 0);
}
~RWLock() {
// The following check is racy but we are about to destroy
void unlock(bool lockdep=true) const {
if (track) {
- if (nwlock.read() > 0) {
- nwlock.dec();
+ if (nwlock > 0) {
+ nwlock--;
} else {
- assert(nrlock.read() > 0);
- nrlock.dec();
+ assert(nrlock > 0);
+ nrlock--;
}
}
if (lockdep && this->lockdep && g_lockdep)
assert(r == 0);
if (lockdep && g_lockdep) id = lockdep_locked(name.c_str(), id);
if (track)
- nrlock.inc();
+ nrlock++;
}
bool try_get_read() const {
if (pthread_rwlock_tryrdlock(&L) == 0) {
if (track)
- nrlock.inc();
+ nrlock++;
if (lockdep && g_lockdep) id = lockdep_locked(name.c_str(), id);
return true;
}
if (lockdep && this->lockdep && g_lockdep)
id = lockdep_locked(name.c_str(), id);
if (track)
- nwlock.inc();
+ nwlock++;
}
bool try_get_write(bool lockdep=true) {
if (lockdep && this->lockdep && g_lockdep)
id = lockdep_locked(name.c_str(), id);
if (track)
- nwlock.inc();
+ nwlock++;
return true;
}
return false;