diff options
author | Matthew Newhook <matthew@zeroc.com> | 2002-04-18 17:30:42 +0000 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2002-04-18 17:30:42 +0000 |
commit | a44de0d2616828e72e49532b4bd771a233683f84 (patch) | |
tree | c297bc1133ec1473c081b23d62eb76adbc4a4872 /cpp/src/IceUtil/RWRecMutex.cpp | |
parent | fix (diff) | |
download | ice-a44de0d2616828e72e49532b4bd771a233683f84.tar.bz2 ice-a44de0d2616828e72e49532b4bd771a233683f84.tar.xz ice-a44de0d2616828e72e49532b4bd771a233683f84.zip |
Added support for recursive write mutexes. Added read/write mutex upgrade.
Diffstat (limited to 'cpp/src/IceUtil/RWRecMutex.cpp')
-rw-r--r-- | cpp/src/IceUtil/RWRecMutex.cpp | 71 |
1 files changed, 68 insertions, 3 deletions
diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp index dd87a1ae245..b664381df5c 100644 --- a/cpp/src/IceUtil/RWRecMutex.cpp +++ b/cpp/src/IceUtil/RWRecMutex.cpp @@ -1,8 +1,8 @@ // ********************************************************************** // // Copyright (c) 2001 -// IONA Technologies, Inc. -// Waltham, MA, USA +// MutableRealms, Inc. +// Huntsville, AL, USA // // All Rights Reserved // @@ -61,6 +61,16 @@ IceUtil::RWRecMutex::writeLock() const Mutex::Lock lock(_mutex); // + // If the mutex is already write locked by this writer then + // decrement _count, and return. + // + if (_count < 0 && _writerControl == ThreadControl()) + { + --_count; + return; + } + + // // Wait for the lock to become available and increment the number // of waiting writers. // @@ -91,6 +101,16 @@ IceUtil::RWRecMutex::tryWriteLock() const Mutex::Lock lock(_mutex); // + // If the mutex is already write locked by this writer then + // decrement _count, and return. + // + if (_count < 0 && _writerControl == ThreadControl()) + { + --_count; + return; + } + + // // If there are readers or other writers then the call would block. // if (_count != 0) @@ -124,7 +144,15 @@ IceUtil::RWRecMutex::unlock() const // // Writer called unlock // - _count = 0; + ++_count; + + // + // If the write lock wasn't totally released we're done. + // + if (_count != 0) + { + return; + } } else { @@ -163,3 +191,40 @@ IceUtil::RWRecMutex::unlock() const _readers.broadcast(); } } + +void +IceUtil::RWRecMutex::upgrade() const +{ + Mutex::Lock lock(_mutex); + + // + // Reader called unlock + // + assert(_count > 0); + --_count; + + // + // Wait to acquire the write lock. + // + while (_count != 0) + { + _waitingWriters++; + try + { + _writers.wait(lock); + } + catch(...) + { + --_waitingWriters; + throw; + } + _waitingWriters--; + + + } + + // + // Got the lock, indicate it's held by a writer. + // + _count = -1; +} |