diff options
Diffstat (limited to 'dep/include/zthread/BiasedReadWriteLock.h')
-rw-r--r-- | dep/include/zthread/BiasedReadWriteLock.h | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/dep/include/zthread/BiasedReadWriteLock.h b/dep/include/zthread/BiasedReadWriteLock.h index b1de74292cf..1fcff74e032 100644 --- a/dep/include/zthread/BiasedReadWriteLock.h +++ b/dep/include/zthread/BiasedReadWriteLock.h @@ -36,21 +36,21 @@ namespace ZThread { * @author Eric Crahen <http://www.code-foo.com> * @date <2003-07-16T10:22:34-0400> * @version 2.2.7 - * - * A BiasedReadWriteLock has a bias toward writers. It will prefer read-write access over + * + * A BiasedReadWriteLock has a bias toward writers. It will prefer read-write access over * read-only access when many threads are contending for access to either Lockable this * ReadWriteLock provides. * - * @see ReadWriteLock + * @see ReadWriteLock */ class BiasedReadWriteLock : public ReadWriteLock { FastMutex _lock; Condition _condRead; Condition _condWrite; - + volatile int _activeWriters; - volatile int _activeReaders; + volatile int _activeReaders; volatile int _waitingReaders; volatile int _waitingWriters; @@ -70,7 +70,7 @@ namespace ZThread { _rwlock.beforeRead(); } - virtual bool tryAcquire(unsigned long timeout) { + virtual bool tryAcquire(unsigned long timeout) { return _rwlock.beforeReadAttempt(timeout); } @@ -96,7 +96,7 @@ namespace ZThread { _rwlock.beforeWrite(); } - virtual bool tryAcquire(unsigned long timeout) { + virtual bool tryAcquire(unsigned long timeout) { return _rwlock.beforeWriteAttempt(timeout); } @@ -113,18 +113,18 @@ namespace ZThread { WriteLock _wlock; public: - + /** * Create a BiasedReadWriteLock * - * @exception Initialization_Exception thrown if resources could not be + * @exception Initialization_Exception thrown if resources could not be * allocated for this object. */ BiasedReadWriteLock() : _condRead(_lock), _condWrite(_lock), _rlock(*this), _wlock(*this) { _activeWriters = 0; _activeReaders = 0; - + _waitingReaders = 0; _waitingWriters = 0; @@ -143,64 +143,64 @@ namespace ZThread { */ virtual Lockable& getWriteLock() { return _wlock; } - + protected: void beforeRead() { - - Guard<FastMutex> guard(_lock); - - ++_waitingReaders; - + + Guard<FastMutex> guard(_lock); + + ++_waitingReaders; + while(!allowReader()) { - + try { - + // wait _condRead.wait(); - + } catch(...) { - - --_waitingReaders; + + --_waitingReaders; throw; } - + } - + --_waitingReaders; ++_activeReaders; - + } bool beforeReadAttempt(unsigned long timeout) { - - Guard<FastMutex> guard(_lock); + + Guard<FastMutex> guard(_lock); bool result = false; - ++_waitingReaders; + ++_waitingReaders; while(!allowReader()) { - + try { - + result = _condRead.wait(timeout); - + } catch(...) { - - --_waitingReaders; + + --_waitingReaders; throw; } } - + --_waitingReaders; ++_activeReaders; return result; - } - + } + void afterRead() { @@ -210,7 +210,7 @@ namespace ZThread { { Guard<FastMutex> guard(_lock); - + --_activeReaders; wakeReader = (_waitingReaders > 0); @@ -220,62 +220,62 @@ namespace ZThread { if(wakeWriter) _condWrite.signal(); - + else if(wakeReader) _condRead.signal(); } void beforeWrite() { - + Guard<FastMutex> guard(_lock); - + ++_waitingWriters; while(!allowWriter()) { - + try { _condWrite.wait(); - + } catch(...) { --_waitingWriters; throw; } - + } - + --_waitingWriters; - ++_activeWriters; + ++_activeWriters; } bool beforeWriteAttempt(unsigned long timeout) { - + Guard<FastMutex> guard(_lock); bool result = false; ++_waitingWriters; while(!allowWriter()) { - + try { result = _condWrite.wait(timeout); - + } catch(...) { --_waitingWriters; throw; } - + } - + --_waitingWriters; ++_activeWriters; - + return result; } @@ -288,9 +288,9 @@ namespace ZThread { { Guard<FastMutex> guard(_lock); - + --_activeWriters; - + wakeReader = (_waitingReaders > 0); wakeWriter = (_waitingWriters > 0); @@ -298,14 +298,14 @@ namespace ZThread { if(wakeWriter) _condWrite.signal(); - + else if(wakeReader) _condRead.signal(); - + } bool allowReader() { - return (_activeWriters == 0); + return (_activeWriters == 0); } bool allowWriter() { |