From 9dd51c94844bc7e5f8b1552511d31ed2c9bac05c Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 21 Feb 2013 03:55:56 +0000 Subject: Added the Bu::ReadWriteMutex, which is super awesome. Also made the Bu::RandomBase::rand functions visible in the Bu::RandomCmwc class. --- src/stable/randomcmwc.h | 1 + src/tests/readwritemutex.cpp | 106 ++++++++++++++++++++++++++++++++++++++++ src/unstable/readwritemutex.cpp | 78 +++++++++++++++++++++++++++++ src/unstable/readwritemutex.h | 75 ++++++++++++++++++++++++++++ 4 files changed, 260 insertions(+) create mode 100644 src/tests/readwritemutex.cpp create mode 100644 src/unstable/readwritemutex.cpp create mode 100644 src/unstable/readwritemutex.h (limited to 'src') diff --git a/src/stable/randomcmwc.h b/src/stable/randomcmwc.h index 0508381..5e89404 100644 --- a/src/stable/randomcmwc.h +++ b/src/stable/randomcmwc.h @@ -26,6 +26,7 @@ namespace Bu virtual void seed( int32_t iSeed ); virtual int32_t rand(); + using RandomBase::rand; private: uint32_t *q, c, i; diff --git a/src/tests/readwritemutex.cpp b/src/tests/readwritemutex.cpp new file mode 100644 index 0000000..d00956d --- /dev/null +++ b/src/tests/readwritemutex.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +using namespace Bu; + +ReadWriteMutex mRW; +bool bRunning; + +class Writer : public Thread +{ +public: + Writer( int iId ) : + iId( iId ), + rand( iId ) + { + } + + virtual ~Writer() + { + } + +protected: + virtual void run() + { + while( bRunning ) + { + mRW.lockWrite(); + println("Writer %1 locking.").arg( iId ); + usleep( rand.rand(5,10)*100000 ); + println("Writer %1 unlocking.").arg( iId ); + mRW.unlockWrite(); + usleep( rand.rand(5,10)*10000 ); + } + } + +private: + int iId; + RandomCmwc rand; +}; + +class Reader : public Thread +{ +public: + Reader( int iId ) : + iId( iId ), + rand( -iId ) + { + } + + virtual ~Reader() + { + } + +protected: + virtual void run() + { + while( bRunning ) + { + mRW.lockRead(); + println("Reader %1 locking.").arg( iId ); + usleep( rand.rand(5,10)*100000 ); + println("Reader %1 unlocking.").arg( iId ); + mRW.unlockRead(); + usleep( rand.rand(5,10)*10000 ); + } + } + +private: + int iId; + RandomCmwc rand; +}; + +#define CNT 5 + +int main() +{ + bRunning = true; + + Thread **threads = new Thread*[CNT*2]; + for( int j = 0; j < CNT; j++ ) + { + threads[j] = new Reader( j+1 ); + threads[j+CNT] = new Writer( j+1 ); + } + + println("Starting."); + for( int j = 0; j < CNT*2; j++ ) + threads[j]->start(); + + sleep( 10 ); + bRunning = false; + + for( int j = 0; j < CNT*2; j++ ) + { + threads[j]->join(); + delete threads[j]; + } + + delete[] threads; + + return 0; +} + + diff --git a/src/unstable/readwritemutex.cpp b/src/unstable/readwritemutex.cpp new file mode 100644 index 0000000..b0a3b77 --- /dev/null +++ b/src/unstable/readwritemutex.cpp @@ -0,0 +1,78 @@ +#include "bu/readwritemutex.h" + +Bu::ReadWriteMutex::ReadWriteMutex() : + iCounter( 0 ), + bWantWrite( false ) +{ +} + +Bu::ReadWriteMutex::~ReadWriteMutex() +{ +} + +void Bu::ReadWriteMutex::lockRead() +{ + // Check to see if someone wants to write + cWrite.lock(); + if( bWantWrite ) + { + // If so, wait patiently for them to finish + cWrite.wait(); + } + cWrite.unlock(); + + // Now lock the read counter + mRead.lock(); + iCounter++; + // If the lock counter is one, we just locked for the first time, + // so we lock the writer. + if( iCounter == 1 ) + mWrite.lock(); + mRead.unlock(); +} + +void Bu::ReadWriteMutex::unlockRead() +{ + // Lock the read counter + mRead.lock(); + iCounter--; + // If we just decremented the counter back to zero then we can + // release the write lock + if( iCounter == 0 ) + mWrite.unlock(); + mRead.unlock(); +} + +void Bu::ReadWriteMutex::lockWrite() +{ + // Lock the read counter + mRead.lock(); + if( iCounter > 0 ) + { + // If there is an active read in progress then we set the bWantWrite + // flag to make sure no more readers start working. + cWrite.lock(); + bWantWrite = true; + cWrite.unlock(); + } + mRead.unlock(); + + // Lock the write lock + mWrite.lock(); +} + +void Bu::ReadWriteMutex::unlockWrite() +{ + // Just always set the bWantWrite flag to false at this point, as long + // as we're locked. + cWrite.lock(); + bWantWrite = false; + cWrite.unlock(); + + // Release all waiting readers, they won't actually do much until we + // unlock the write lock though + cWrite.broadcast(); + + // Unlock the write lock + mWrite.unlock(); +} diff --git a/src/unstable/readwritemutex.h b/src/unstable/readwritemutex.h new file mode 100644 index 0000000..9e07047 --- /dev/null +++ b/src/unstable/readwritemutex.h @@ -0,0 +1,75 @@ +#ifndef BU_READ_WRITE_MUTEX_H +#define BU_READ_WRITE_MUTEX_H + +#include "bu/mutex.h" +#include "bu/condition.h" + +namespace Bu +{ + /** + * Mutex designed for situations where overlapped reading is safe, but + * overlapped writing isn't. There are many, many good examples of this + * including most data structures, streams, etc. etc. + * + * Use this just like a normal mutex except that you use the + * lockRead/unlockRead and lockWrite/unlockWrite functions depending on + * weather the section of code your locking is reading data or changing + * data. + * + * This particular mutex is designed so that while a read operation is + * happening other read operations can also happen, but no write operations + * can occur. While a write is happening, no other write or read operation + * can continue. There is an extra feature to ensure writes get a chance + * to complete, when a lockWrite is issued, all current read operations + * continue, but future read operations block until the write is complete. + */ + class ReadWriteMutex + { + public: + ReadWriteMutex(); + virtual ~ReadWriteMutex(); + + /** + * Lock the mutex for reading. Multiple code sections can hold a read + * lock at the same time, but write locks will wait for all read locks + * to be released before continuing. Read locks will also wait if + * there is an active write lock. + * + * It is very important to not make any changes to your data within + * a read lock. + */ + void lockRead(); + + /** + * Release a read lock. + */ + void unlockRead(); + + /** + * Lock the mutex for writing. Only one code section can have a write + * lock at any given time. No code sections can be in a locked read + * section while a write lock is held. When a write lock is requested + * all following read locks will block until the write operation is + * started, ensuring writes always get a chance to execute. + * + * Within a write locked code section feel free to change your data + * and read your data. It is imparative to spend as little time as + * possible in a write-locked section. + */ + void lockWrite(); + + /** + * Release a write lock. + */ + void unlockWrite(); + + private: + Bu::Mutex mRead; + int iCounter; + Bu::Mutex mWrite; + Bu::Condition cWrite; + bool bWantWrite; + }; +}; + +#endif -- cgit v1.2.3