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: MutexGuard.cpp 00010 00011 Description: Implementation of a mutex helper class for locking and unlocking. 00012 (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/MutexGuard.h> 00030 00031 #include <VimbaCPP/Include/VimbaSystem.h> 00032 00033 namespace AVT { 00034 namespace VmbAPI { 00035 00036 MutexGuard::MutexGuard() 00037 { 00038 } 00039 00040 MutexGuard::MutexGuard( MutexPtr pMutex ) 00041 { 00042 if ( SP_ISNULL( pMutex )) 00043 { 00044 LOG_FREE_TEXT( "No mutex passed." ); 00045 } 00046 else 00047 { 00048 Protect( pMutex ); 00049 } 00050 } 00051 00052 MutexGuard::MutexGuard( BasicLockablePtr pLockable ) 00053 { 00054 if ( SP_ISNULL( pLockable )) 00055 { 00056 LOG_FREE_TEXT( "No mutex passed." ); 00057 } 00058 else 00059 { 00060 Protect( pLockable ); 00061 } 00062 } 00063 00064 MutexGuard::MutexGuard( const BasicLockable &rLockable ) 00065 { 00066 Protect( rLockable ); 00067 } 00068 00069 MutexGuard::~MutexGuard() 00070 { 00071 Release(); 00072 } 00073 00074 void MutexGuard::Protect( MutexPtr pMutex ) 00075 { 00076 if( SP_ISNULL( pMutex )) 00077 { 00078 LOG_FREE_TEXT( "No mutex passed." ); 00079 } 00080 00081 else if( SP_ISEQUAL( pMutex, m_pMutex )) 00082 { 00083 return; 00084 } 00085 00086 Release(); 00087 00088 SP_ACCESS( pMutex )->Lock(); 00089 m_pMutex = pMutex; 00090 } 00091 00092 void MutexGuard::Protect( BasicLockablePtr pLockable ) 00093 { 00094 if( SP_ISNULL( SP_ACCESS( pLockable )->GetMutex() )) 00095 { 00096 LOG_FREE_TEXT( "No mutex passed." ); 00097 } 00098 else 00099 { 00100 Protect( SP_ACCESS( pLockable )->GetMutex() ); 00101 } 00102 } 00103 00104 void MutexGuard::Protect( const BasicLockable &rLockable ) 00105 { 00106 Protect( rLockable.GetMutex() ); 00107 } 00108 00109 bool MutexGuard::Release() 00110 { 00111 if( SP_ISNULL( m_pMutex )) 00112 { 00113 return false; 00114 } 00115 00116 SP_ACCESS( m_pMutex )->Unlock(); 00117 SP_RESET( m_pMutex ); 00118 00119 return true; 00120 } 00121 00122 }} //namespace AVT::VmbAPI