From: Yehuda Sadeh Date: Sat, 3 May 2014 15:32:19 +0000 (-0700) Subject: common/RWLock: track read/write locks via atomics for assertion checks X-Git-Tag: v0.80.10~30^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=a73a4cb3889a6da21c3cfa4ddfa16d1a7059d20c;p=ceph.git common/RWLock: track read/write locks via atomics for assertion checks Signed-off-by: Yehuda Sadeh (cherry picked from commit 92615ea95a31d9fd22c3d11c860e0f502dc52c26) --- diff --git a/src/common/RWLock.h b/src/common/RWLock.h index f901ac0de7d..3d94969e5b8 100644 --- a/src/common/RWLock.h +++ b/src/common/RWLock.h @@ -19,28 +19,42 @@ #include #include "lockdep.h" +#include "include/atomic.h" class RWLock { mutable pthread_rwlock_t L; const char *name; mutable int id; + mutable atomic_t nrlock, nwlock; public: RWLock(const RWLock& other); const RWLock& operator=(const RWLock& other); - RWLock(const char *n) : name(n), id(-1) { + RWLock(const char *n) : name(n), id(-1), nrlock(0), nwlock(0) { pthread_rwlock_init(&L, NULL); if (g_lockdep) id = lockdep_register(name); } + bool is_locked() const { + return (nrlock.read() > 0) || (nwlock.read() > 0); + } + + bool is_wlocked() const { + return (nwlock.read() > 0); + } virtual ~RWLock() { pthread_rwlock_unlock(&L); pthread_rwlock_destroy(&L); } void unlock() const { + if (nwlock.read() > 0) { + nwlock.dec(); + } else { + nrlock.dec(); + } if (g_lockdep) id = lockdep_will_unlock(name, id); pthread_rwlock_unlock(&L); } @@ -50,9 +64,11 @@ public: if (g_lockdep) id = lockdep_will_lock(name, id); pthread_rwlock_rdlock(&L); if (g_lockdep) id = lockdep_locked(name, id); + nrlock.inc(); } bool try_get_read() const { if (pthread_rwlock_tryrdlock(&L) == 0) { + nrlock.inc(); if (g_lockdep) id = lockdep_locked(name, id); return true; } @@ -67,10 +83,12 @@ public: if (g_lockdep) id = lockdep_will_lock(name, id); pthread_rwlock_wrlock(&L); if (g_lockdep) id = lockdep_locked(name, id); + nwlock.inc(); } bool try_get_write() { if (pthread_rwlock_trywrlock(&L) == 0) { if (g_lockdep) id = lockdep_locked(name, id); + nwlock.inc(); return true; } return false;