diff options
author | megamage <none@none> | 2009-02-12 17:09:15 -0600 |
---|---|---|
committer | megamage <none@none> | 2009-02-12 17:09:15 -0600 |
commit | 6aee5fcbe7473a3cbac12b7e8482a7b98bef8be3 (patch) | |
tree | 91ec91d5c19eba9c2fe0e84b1c9dc7047a3de80e /dep/src/zthread/MutexImpl.h | |
parent | 2d2f433b4de1c35b22aaf07854fc0ee11fcb350d (diff) | |
parent | f385747164c3fb278c92ef46fbd6c3da6590bbf0 (diff) |
*Merge.
--HG--
branch : trunk
Diffstat (limited to 'dep/src/zthread/MutexImpl.h')
-rw-r--r-- | dep/src/zthread/MutexImpl.h | 148 |
1 files changed, 74 insertions, 74 deletions
diff --git a/dep/src/zthread/MutexImpl.h b/dep/src/zthread/MutexImpl.h index 10a9160ce5a..21338ba289c 100644 --- a/dep/src/zthread/MutexImpl.h +++ b/dep/src/zthread/MutexImpl.h @@ -58,14 +58,14 @@ protected: * @version 2.2.11 * @class MutexImpl * - * The MutexImpl template allows how waiter lists are sorted, and + * The MutexImpl template allows how waiter lists are sorted, and * what actions are taken when a thread interacts with the mutex * to be parametized. */ -template <typename List, typename Behavior> +template <typename List, typename Behavior> class MutexImpl : Behavior { - //! List of Events that are waiting for notification + //! List of Events that are waiting for notification List _waiters; //! Serialize access to this Mutex @@ -75,7 +75,7 @@ class MutexImpl : Behavior { volatile ThreadImpl* _owner; public: - + /** * Create a new MutexImpl @@ -84,11 +84,11 @@ class MutexImpl : Behavior { * properly allocated */ MutexImpl() : _owner(0) { } - + ~MutexImpl(); void acquire(); - + void release(); bool tryAcquire(unsigned long timeout); @@ -98,20 +98,20 @@ class MutexImpl : Behavior { /** * Destroy this MutexImpl and release its resources */ -template<typename List, typename Behavior> +template<typename List, typename Behavior> MutexImpl<List, Behavior>::~MutexImpl() { #ifndef NDEBUG // It is an error to destroy a mutex that has not been released - if(_owner != 0) { + if(_owner != 0) { ZTDEBUG("** You are destroying a mutex which was never released. **\n"); assert(0); // Destroyed mutex while in use } - if(!_waiters.empty()) { + if(!_waiters.empty()) { ZTDEBUG("** You are destroying a mutex which is blocking %d threads. **\n", _waiters.size()); assert(0); // Destroyed mutex while in use @@ -125,7 +125,7 @@ MutexImpl<List, Behavior>::~MutexImpl() { /** * Acquire a lock on the mutex. If this operation succeeds the calling - * thread holds an exclusive lock on this mutex, otherwise it is blocked + * thread holds an exclusive lock on this mutex, otherwise it is blocked * until the lock can be acquired. * * @exception Deadlock_Exception thrown when the caller attempts to acquire() more @@ -133,7 +133,7 @@ MutexImpl<List, Behavior>::~MutexImpl() { * @exception Interrupted_Exception thrown when the caller status is interrupted * @exception Synchronization_Exception thrown if there is some other error. */ -template<typename List, typename Behavior> +template<typename List, typename Behavior> void MutexImpl<List, Behavior>::acquire() { ThreadImpl* self = ThreadImpl::current(); @@ -142,41 +142,41 @@ void MutexImpl<List, Behavior>::acquire() { Monitor::STATE state; Guard<FastLock> g1(_lock); - - // Deadlock will occur if the current thread is the owner + + // Deadlock will occur if the current thread is the owner // and there is no entry count. - if(_owner == self) + if(_owner == self) throw Deadlock_Exception(); - + // Acquire the lock if it is free and there are no waiting threads if(_owner == 0 && _waiters.empty()) { _owner = self; this->ownerAcquired(self); - + } // Otherwise, wait for a signal from a thread releasing its // ownership of the lock - else { - + else { + _waiters.insert(self); m.acquire(); this->waiterArrived(self); - { - + { + Guard<FastLock, UnlockedScope> g2(g1); state = m.wait(); - + } this->waiterDeparted(self); m.release(); - + // Remove from waiter list, regardless of wether release() is called or // not. The monitor is sticky, so its possible a state 'stuck' from a // previous operation and will leave the wait() w/o release() having @@ -185,26 +185,26 @@ void MutexImpl<List, Behavior>::acquire() { if(i != _waiters.end()) _waiters.erase(i); - // If awoke due to a notify(), take ownership. + // If awoke due to a notify(), take ownership. switch(state) { case Monitor::SIGNALED: assert(_owner == 0); - _owner = self; + _owner = self; this->ownerAcquired(self); break; - + case Monitor::INTERRUPTED: throw Interrupted_Exception(); - + default: throw Synchronization_Exception(); - } - + } + } - + } @@ -218,17 +218,17 @@ void MutexImpl<List, Behavior>::acquire() { * @exception Interrupted_Exception thrown when the caller status is interrupted * @exception Synchronization_Exception thrown if there is some other error. */ -template<typename List, typename Behavior> +template<typename List, typename Behavior> bool MutexImpl<List, Behavior>::tryAcquire(unsigned long timeout) { - + ThreadImpl* self = ThreadImpl::current(); Monitor& m = self->getMonitor(); Guard<FastLock> g1(_lock); - - // Deadlock will occur if the current thread is the owner + + // Deadlock will occur if the current thread is the owner // and there is no entry count. - if(_owner == self) + if(_owner == self) throw Deadlock_Exception(); // Acquire the lock if it is free and there are no waiting threads @@ -237,38 +237,38 @@ bool MutexImpl<List, Behavior>::tryAcquire(unsigned long timeout) { _owner = self; this->ownerAcquired(self); - + } // Otherwise, wait for a signal from a thread releasing its // ownership of the lock else { - + _waiters.insert(self); - + Monitor::STATE state = Monitor::TIMEDOUT; - + // Don't bother waiting if the timeout is 0 if(timeout) { - + m.acquire(); this->waiterArrived(self); - + { - + Guard<FastLock, UnlockedScope> g2(g1); state = m.wait(timeout); - + } this->waiterDeparted(self); - + m.release(); - + } - - + + // Remove from waiter list, regarless of weather release() is called or // not. The monitor is sticky, so its possible a state 'stuck' from a // previous operation and will leave the wait() w/o release() having @@ -276,50 +276,50 @@ bool MutexImpl<List, Behavior>::tryAcquire(unsigned long timeout) { typename List::iterator i = std::find(_waiters.begin(), _waiters.end(), self); if(i != _waiters.end()) _waiters.erase(i); - - // If awoke due to a notify(), take ownership. + + // If awoke due to a notify(), take ownership. switch(state) { case Monitor::SIGNALED: - + assert(0 == _owner); _owner = self; this->ownerAcquired(self); - + break; - + case Monitor::INTERRUPTED: throw Interrupted_Exception(); - + case Monitor::TIMEDOUT: return false; - + default: throw Synchronization_Exception(); - } - + } + } - + return true; - + } /** * Release a lock on the mutex. If this operation succeeds the calling - * thread no longer holds an exclusive lock on this mutex. If there are - * waiting threads, one will be selected, assigned ownership and specifically + * thread no longer holds an exclusive lock on this mutex. If there are + * waiting threads, one will be selected, assigned ownership and specifically * awakened. * - * @exception InvalidOp_Exception - thrown if an attempt is made to + * @exception InvalidOp_Exception - thrown if an attempt is made to * release a mutex not owned by the calling thread. */ -template<typename List, typename Behavior> +template<typename List, typename Behavior> void MutexImpl<List, Behavior>::release() { ThreadImpl* impl = ThreadImpl::current(); Guard<FastLock> g1(_lock); - + // Make sure the operation is valid if(_owner != impl) throw InvalidOp_Exception(); @@ -327,45 +327,45 @@ void MutexImpl<List, Behavior>::release() { _owner = 0; this->ownerReleased(impl); - + // Try to find a waiter with a backoff & retry scheme for(;;) { - + // Go through the list, attempt to notify() a waiter. for(typename List::iterator i = _waiters.begin(); i != _waiters.end();) { - + // Try the monitor lock, if it cant be locked skip to the next waiter impl = *i; Monitor& m = impl->getMonitor(); if(m.tryAcquire()) { - - // If notify() is not sucessful, it is because the wait() has already + + // If notify() is not sucessful, it is because the wait() has already // been ended (killed/interrupted/notify'd) bool woke = m.notify(); - + m.release(); - + // Once notify() succeeds, return if(woke) return; - + } else ++i; - + } - + if(_waiters.empty()) return; { // Backoff and try again - + Guard<FastLock, UnlockedScope> g2(g1); ThreadImpl::yield(); } } - + } } // namespace ZThread |