From a58d48de794b6261cdefbd9cf82cead92946c392 Mon Sep 17 00:00:00 2001 From: Dhruba Borthakur Date: Mon, 1 Oct 2012 21:58:36 -0700 Subject: [PATCH] Implement ReadWrite locks for leveldb Summary: Implement ReadWrite locks for leveldb. These will be helpful to implement a read-modify-write operation (e.g. atomic increments). Test Plan: does not modify any existing code Reviewers: heyongqiang Reviewed By: heyongqiang CC: MarkCallaghan Differential Revision: https://reviews.facebook.net/D5787 --- port/port_posix.cc | 10 ++++++++++ port/port_posix.h | 18 ++++++++++++++++++ util/mutexlock.h | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/port/port_posix.cc b/port/port_posix.cc index 5ba127a5b..82cecdf2c 100644 --- a/port/port_posix.cc +++ b/port/port_posix.cc @@ -46,6 +46,16 @@ void CondVar::SignalAll() { PthreadCall("broadcast", pthread_cond_broadcast(&cv_)); } +RWMutex::RWMutex() { PthreadCall("init mutex", pthread_rwlock_init(&mu_, NULL)); } + +RWMutex::~RWMutex() { PthreadCall("destroy mutex", pthread_rwlock_destroy(&mu_)); } + +void RWMutex::ReadLock() { PthreadCall("read lock", pthread_rwlock_rdlock(&mu_)); } + +void RWMutex::WriteLock() { PthreadCall("write lock", pthread_rwlock_wrlock(&mu_)); } + +void RWMutex::Unlock() { PthreadCall("unlock", pthread_rwlock_unlock(&mu_)); } + void InitOnce(OnceType* once, void (*initializer)()) { PthreadCall("once", pthread_once(once, initializer)); } diff --git a/port/port_posix.h b/port/port_posix.h index b68910a03..86a0927d0 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -97,6 +97,24 @@ class Mutex { void operator=(const Mutex&); }; +class RWMutex { + public: + RWMutex(); + ~RWMutex(); + + void ReadLock(); + void WriteLock(); + void Unlock(); + void AssertHeld() { } + + private: + pthread_rwlock_t mu_; // the underlying platform mutex + + // No copying allowed + RWMutex(const RWMutex&); + void operator=(const RWMutex&); +}; + class CondVar { public: explicit CondVar(Mutex* mu); diff --git a/util/mutexlock.h b/util/mutexlock.h index c3f3306d3..8817e55c9 100644 --- a/util/mutexlock.h +++ b/util/mutexlock.h @@ -33,6 +33,45 @@ class MutexLock { void operator=(const MutexLock&); }; +// +// Acquire a ReadLock on the specified RWMutex. +// The Lock will be automatically released then the +// object goes out of scope. +// +class ReadLock { + public: + explicit ReadLock(port::RWMutex *mu) : mu_(mu) { + this->mu_->ReadLock(); + } + ~ReadLock() { this->mu_->Unlock(); } + + private: + port::RWMutex *const mu_; + // No copying allowed + ReadLock(const ReadLock&); + void operator=(const ReadLock&); +}; + + +// +// Acquire a WriteLock on the specified RWMutex. +// The Lock will be automatically released then the +// object goes out of scope. +// +class WriteLock { + public: + explicit WriteLock(port::RWMutex *mu) : mu_(mu) { + this->mu_->WriteLock(); + } + ~WriteLock() { this->mu_->Unlock(); } + + private: + port::RWMutex *const mu_; + // No copying allowed + WriteLock(const WriteLock&); + void operator=(const WriteLock&); +}; + } // namespace leveldb