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: Condition.cpp 00010 00011 Description: Implementation of a condition class. 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/Condition.h> 00030 00031 namespace AVT { 00032 namespace VmbAPI { 00033 00034 Condition::Condition() 00035 : m_nWaiterNumber( 0 ) 00036 , m_nReleaseNumber( 0 ) 00037 , m_bLocked( true ) 00038 { 00039 SP_SET( m_Semaphore, new Semaphore() ); 00040 } 00041 00042 void Condition::Wait( const BasicLockable &rLockable ) 00043 { 00044 Wait( rLockable.GetMutex() ); 00045 } 00046 00047 void Condition::Wait( const MutexPtr &rMutex ) 00048 { 00049 m_nWaiterNumber++; 00050 00051 SP_ACCESS( rMutex )->Unlock(); 00052 00053 SP_ACCESS( m_Semaphore )->Acquire(); 00054 00055 SP_ACCESS( rMutex) ->Lock(); 00056 00057 if ( m_nWaiterNumber > 0 ) 00058 { 00059 m_nWaiterNumber--; 00060 } 00061 00062 if ( m_nReleaseNumber > 0 ) 00063 { 00064 m_nReleaseNumber--; 00065 } 00066 00067 if( m_nWaiterNumber > 0 00068 && m_nReleaseNumber > 0 ) 00069 { 00070 SP_ACCESS( m_Semaphore )->Release(); 00071 m_bLocked = false; 00072 } 00073 else 00074 { 00075 m_bLocked = true; 00076 } 00077 00078 if( m_nReleaseNumber > m_nWaiterNumber ) 00079 { 00080 m_nReleaseNumber = m_nWaiterNumber; 00081 } 00082 } 00083 00084 void Condition::Signal( bool bSingle ) 00085 { 00086 if( m_nWaiterNumber > m_nReleaseNumber ) 00087 { 00088 if( true == bSingle ) 00089 { 00090 m_nReleaseNumber++; 00091 } 00092 else 00093 { 00094 m_nReleaseNumber = m_nWaiterNumber; 00095 } 00096 00097 if( true == m_bLocked ) 00098 { 00099 SP_ACCESS( m_Semaphore )->Release(); 00100 m_bLocked = false; 00101 } 00102 } 00103 } 00104 00105 }} // namespace AVT::VmbAPI