00001 /*============================================================================= 00002 Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved. 00003 00004 Redistribution of this file, in original or modified form, without 00005 prior written consent of Allied Vision Technologies is prohibited. 00006 00007 ------------------------------------------------------------------------------- 00008 00009 File: ConditionHelper.cpp 00010 00011 Description: Implementation of helper class for conditions. 00012 (This include file is for internal use only.) 00013 00014 ------------------------------------------------------------------------------- 00015 00016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 00017 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, 00018 NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00023 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 00024 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 =============================================================================*/ 00028 00029 #include <VimbaCPP/Source/ConditionHelper.h> 00030 00031 #include <VimbaCPP/Source/MutexGuard.h> 00032 00033 namespace AVT { 00034 namespace VmbAPI { 00035 00036 ConditionHelper::ConditionHelper() 00037 : m_nNumListReads( 0 ) 00038 , m_bIsWritingList( false ) 00039 , m_bExclusive( false ) 00040 { 00041 } 00042 00043 bool ConditionHelper::EnterReadLock( BasicLockable &rLockable ) 00044 { 00045 return EnterReadLock( rLockable.GetMutex() ); 00046 } 00047 bool ConditionHelper::EnterReadLock( MutexPtr pMutex ) 00048 { 00049 MutexGuard guard( pMutex ); 00050 if ( true == m_bExclusive ) 00051 { 00052 guard.Release(); 00053 return false; 00054 } 00055 while ( true == m_bIsWritingList ) 00056 { 00057 m_WriteCondition.Wait( pMutex ); 00058 } 00059 ++m_nNumListReads; 00060 guard.Release(); 00061 00062 return true; 00063 } 00064 00065 void ConditionHelper::ExitReadLock( BasicLockable &rLockable ) 00066 { 00067 ExitReadLock( rLockable.GetMutex() ); 00068 } 00069 void ConditionHelper::ExitReadLock( MutexPtr pMutex ) 00070 { 00071 MutexGuard guard( pMutex ); 00072 if ( 0 == --m_nNumListReads ) 00073 { 00074 m_ReadCondition.Signal(); 00075 } 00076 guard.Release(); 00077 } 00078 00079 bool ConditionHelper::EnterWriteLock( BasicLockable &rLockable, bool bExclusive ) 00080 { 00081 return EnterWriteLock( rLockable.GetMutex(), bExclusive ); 00082 } 00083 bool ConditionHelper::EnterWriteLock( MutexPtr pMutex, bool bExclusive ) 00084 { 00085 MutexGuard guard( pMutex ); 00086 if ( true == m_bExclusive ) 00087 { 00088 guard.Release(); 00089 return false; 00090 } 00091 while ( true == m_bIsWritingList ) 00092 { 00093 m_WriteCondition.Wait( pMutex ); 00094 } 00095 m_bIsWritingList = true; 00096 m_bExclusive = bExclusive; 00097 while ( 0 < m_nNumListReads ) 00098 { 00099 m_ReadCondition.Wait( pMutex ); 00100 } 00101 guard.Release(); 00102 00103 return true; 00104 } 00105 00106 void ConditionHelper::ExitWriteLock( BasicLockable &rLockable ) 00107 { 00108 ExitWriteLock( rLockable.GetMutex() ); 00109 } 00110 void ConditionHelper::ExitWriteLock( MutexPtr pMutex ) 00111 { 00112 MutexGuard guard( pMutex ); 00113 m_bIsWritingList = false; 00114 m_bExclusive = false; 00115 m_WriteCondition.Signal(); 00116 guard.Release(); 00117 } 00118 00119 }} // namespace AV::VimbaAPI