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/unstable/readwritemutex.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ src/unstable/readwritemutex.h | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/unstable/readwritemutex.cpp create mode 100644 src/unstable/readwritemutex.h (limited to 'src/unstable') 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