From: <ric...@us...> - 2012-04-02 06:05:11
|
Revision: 1180 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1180&view=rev Author: rich_sposato Date: 2012-04-02 06:05:04 +0000 (Mon, 02 Apr 2012) Log Message: ----------- Overloaded functions for volatile to make better use of Memento and Loki::Checker. Modified Paths: -------------- trunk/include/loki/LevelMutex.h trunk/src/LevelMutex.cpp Modified: trunk/include/loki/LevelMutex.h =================================================================== --- trunk/include/loki/LevelMutex.h 2012-04-02 06:01:51 UTC (rev 1179) +++ trunk/include/loki/LevelMutex.h 2012-04-02 06:05:04 UTC (rev 1180) @@ -272,11 +272,15 @@ { public: - explicit Memento( const volatile LevelMutexInfo & mutex ); + explicit Memento( const LevelMutexInfo & mutex ); - bool operator == ( const volatile LevelMutexInfo & mutex ) const; + bool operator == ( const LevelMutexInfo & mutex ) const; private: + + /// Copy-assignment operator is not implemented. + Memento & operator = ( const Memento & ); + /// Level of this mutex. const unsigned int m_level; @@ -295,7 +299,7 @@ exception. The checkers only get used in debug builds, and get optimized away in release builds. */ - typedef ::Loki::CheckFor< volatile LevelMutexInfo, Memento > CheckFor; + typedef ::Loki::CheckFor< const LevelMutexInfo, Memento > CheckFor; /** @class MutexUndoer Undoes actions by MultiLock if an exception occurs. It keeps track of @@ -362,13 +366,13 @@ /** Returns true if no class invariant broken, otherwise asserts. This function only gets called in debug builds. */ - bool IsValid( void ) const; + bool IsValid2( void ) const; /// Returns true if all pre-conditions for PostLock function are valid. - bool PostLockValidator( void ) const volatile; + bool PostLockValidator( void ) const; /// Returns true if all pre-conditions for PreUnlock function are valid. - bool PreUnlockValidator( void ) const volatile; + bool PreUnlockValidator( void ) const; private: @@ -395,16 +399,22 @@ /// Called only by MultiUnlock to unlock each particular mutex within a container. virtual MutexErrors::Type UnlockThis( void ) volatile = 0; + void PostLock( void ); + /** The actual implementation of IsLockedByCurrentThread. This does not do any invariant checking because the functions which call it already have. */ bool IsLockedByCurrentThreadImpl( void ) const volatile; + bool IsLockedByCurrentThreadImpl( void ) const; + /** Does just the opposite of IsLockedByCurrentThread. Called as a post-condition check by another function. */ bool IsNotLockedByCurrentThread( void ) const volatile; + bool IsNotLockedByCurrentThread( void ) const; + /// Pointer to singly-linked list of mutexes locked by the current thread. static LOKI_THREAD_LOCAL volatile LevelMutexInfo * s_currentMutex; Modified: trunk/src/LevelMutex.cpp =================================================================== --- trunk/src/LevelMutex.cpp 2012-04-02 06:01:51 UTC (rev 1179) +++ trunk/src/LevelMutex.cpp 2012-04-02 06:05:04 UTC (rev 1180) @@ -232,7 +232,7 @@ // ---------------------------------------------------------------------------- -LevelMutexInfo::Memento::Memento( const volatile LevelMutexInfo & mutex ) : +LevelMutexInfo::Memento::Memento( const LevelMutexInfo & mutex ) : m_level( mutex.m_level ), m_count( mutex.m_count ), m_previous( mutex.m_previous ), @@ -243,7 +243,7 @@ // ---------------------------------------------------------------------------- -bool LevelMutexInfo::Memento::operator == ( const volatile LevelMutexInfo & mutex ) const +bool LevelMutexInfo::Memento::operator == ( const LevelMutexInfo & mutex ) const { assert( this != nullptr ); @@ -594,6 +594,14 @@ bool LevelMutexInfo::IsValid( void ) const volatile { + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + return pThis->IsValid2(); +} + +// ---------------------------------------------------------------------------- + +bool LevelMutexInfo::IsValid2( void ) const +{ assert( nullptr != this ); assert( LevelMutexInfo::UnlockedLevel != m_level ); assert( m_previous != this ); @@ -604,14 +612,6 @@ // ---------------------------------------------------------------------------- -bool LevelMutexInfo::IsValid( void ) const -{ - const volatile LevelMutexInfo * pThis = const_cast< const volatile LevelMutexInfo * >( this ); - return pThis->IsValid(); -} - -// ---------------------------------------------------------------------------- - void LevelMutexInfo::IncrementCount( void ) volatile { assert( IsValid() ); @@ -636,7 +636,8 @@ // gets called by various functions that are called to clean up after an exception // is thrown LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoChange checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoChange checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) return IsLockedByCurrentThreadImpl(); @@ -646,6 +647,14 @@ bool LevelMutexInfo::IsLockedByCurrentThreadImpl( void ) const volatile { + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + return pThis->IsLockedByCurrentThreadImpl(); +} + +// ---------------------------------------------------------------------------- + +bool LevelMutexInfo::IsLockedByCurrentThreadImpl( void ) const +{ if ( !IsLocked() ) return false; const volatile LevelMutexInfo * mutex = s_currentMutex; @@ -681,7 +690,8 @@ bool LevelMutexInfo::IsRecentLock( void ) const volatile { LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrowOrChange checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrowOrChange checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) @@ -705,7 +715,8 @@ bool LevelMutexInfo::IsRecentLock( std::size_t count ) const volatile { LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrowOrChange checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrowOrChange checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) @@ -728,7 +739,8 @@ bool LevelMutexInfo::IsLockedByAnotherThread( void ) const volatile { LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrowOrChange checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrowOrChange checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) @@ -743,7 +755,7 @@ // ---------------------------------------------------------------------------- -bool LevelMutexInfo::PostLockValidator( void ) const volatile +bool LevelMutexInfo::PostLockValidator( void ) const { assert( 0 == m_count ); assert( nullptr == m_previous ); @@ -757,8 +769,17 @@ void LevelMutexInfo::PostLock( void ) volatile { + LevelMutexInfo * pThis = const_cast< LevelMutexInfo * >( this ); + pThis->PostLock(); +} + +// ---------------------------------------------------------------------------- + +void LevelMutexInfo::PostLock( void ) +{ LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrow checker( this, &LevelMutexInfo::IsValid, + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrow checker( pThis, &LevelMutexInfo::IsValid2, &LevelMutexInfo::PostLockValidator, &LevelMutexInfo::IsLockedByCurrentThreadImpl ); (void)checker; ) @@ -775,7 +796,7 @@ // ---------------------------------------------------------------------------- -bool LevelMutexInfo::PreUnlockValidator( void ) const volatile +bool LevelMutexInfo::PreUnlockValidator( void ) const { assert( 1 == m_count ); assert( nullptr != s_currentMutex ); @@ -790,9 +811,10 @@ void LevelMutexInfo::PreUnlock( void ) volatile { LOKI_MUTEX_DEBUG_CODE( + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); // This must use CheckFor::Invariants instead of CheckFor::NoThrow because the // function gets called when MultiLock has to clean up after an exception. - CheckFor::Invariants checker( this, &LevelMutexInfo::IsValid, + CheckFor::Invariants checker( pThis, &LevelMutexInfo::IsValid2, &LevelMutexInfo::PreUnlockValidator, &LevelMutexInfo::IsNotLockedByCurrentThread ); (void)checker; ) @@ -812,7 +834,8 @@ MutexErrors::Type LevelMutexInfo::PreLockCheck( bool forTryLock ) volatile { LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrow checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrow checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) @@ -849,7 +872,8 @@ MutexErrors::Type LevelMutexInfo::PreUnlockCheck( void ) volatile { LOKI_MUTEX_DEBUG_CODE( - CheckFor::NoThrow checker( this, &LevelMutexInfo::IsValid ); + const LevelMutexInfo * pThis = const_cast< const LevelMutexInfo * >( this ); + CheckFor::NoThrow checker( pThis, &LevelMutexInfo::IsValid2 ); (void)checker; ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |