RWAutoRelease.cppGo to the documentation of this file.00001 /* 00002 * Copyright (c) 2000-2003 Barak Weichselbaum <barak@komodia.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 * 00026 * Contact info: 00027 * ------------- 00028 * 00029 * Site: http://www.komodia.com 00030 * Main contact: barak@komodia.com 00031 * For custom projects, 00032 * consulting, or other 00033 * paid services: sales@komodia.com 00034 */ 00035 00036 #include "stdafx.h" 00037 #include "RWAutoRelease.h" 00038 00039 #include "GenericReadWriteLock.h" 00040 00041 #ifdef _MEMORY_DEBUG 00042 #define new DEBUG_NEW 00043 #define malloc DEBUG_MALLOC 00044 static char THIS_FILE[] = __FILE__; 00045 #endif 00046 00047 KOMODIA_NAMESPACE_START 00048 00049 CRWAutoRelease::CRWAutoRelease(CGenericReadWriteLock* pLock, 00050 BOOL bRead, 00051 BOOL bLock) : m_pLock(pLock), 00052 m_bLockedRead(FALSE), 00053 m_bLockedWrite(FALSE), 00054 m_bEscalated(FALSE) 00055 { 00056 //Do we need to lock 00057 if (bLock) 00058 if (bRead) 00059 if (LockRead()) 00060 m_bLockedRead=TRUE; 00061 else 00062 ; 00063 else 00064 if (LockWrite()) 00065 m_bLockedWrite=TRUE; 00066 } 00067 00068 CRWAutoRelease::~CRWAutoRelease() 00069 { 00070 //Are we locked some how 00071 if (m_bLockedRead) 00072 UnlockRead(); 00073 else if (m_bLockedWrite) 00074 UnlockWrite(); 00075 00076 //Are we escalated 00077 if (m_bEscalated) 00078 StopEscalation(); 00079 } 00080 00081 BOOL CRWAutoRelease::LockRead(unsigned long ulMSTimeout) 00082 { 00083 if (m_pLock) 00084 if (m_pLock->LockRead(ulMSTimeout)) 00085 { 00086 //We are locked for read 00087 m_bLockedRead=TRUE; 00088 00089 //Exit 00090 return FALSE; 00091 } 00092 else 00093 return FALSE; 00094 else 00095 return FALSE; 00096 } 00097 00098 void CRWAutoRelease::UnlockRead() 00099 { 00100 if (m_pLock && m_bLockedRead) 00101 { 00102 //Unlock us 00103 m_pLock->UnlockRead(); 00104 00105 //Not locked anymore 00106 m_bLockedRead=FALSE; 00107 } 00108 } 00109 00110 BOOL CRWAutoRelease::LockWrite() 00111 { 00112 if (m_pLock) 00113 if (m_pLock->LockWrite()) 00114 { 00115 //We are locked for write 00116 m_bLockedWrite=TRUE; 00117 00118 //Exit 00119 return TRUE; 00120 } 00121 else 00122 return FALSE; 00123 else 00124 return FALSE; 00125 } 00126 00127 void CRWAutoRelease::UnlockWrite() 00128 { 00129 //Are we locked 00130 if (m_pLock && m_bLockedWrite) 00131 { 00132 //Unlock us 00133 m_pLock->UnlockWrite(); 00134 00135 //Set the flag 00136 m_bLockedWrite=FALSE; 00137 00138 //Not escalated 00139 m_bEscalated=FALSE; 00140 } 00141 } 00142 00143 BOOL CRWAutoRelease::Escalate(unsigned long ulMSTimeout, 00144 unsigned long ulIterations) 00145 { 00146 if (m_pLock) 00147 if (m_pLock->Escalate(ulMSTimeout,ulIterations)) 00148 { 00149 //We are locked for write 00150 m_bLockedWrite=TRUE; 00151 00152 //And for escalation 00153 m_bEscalated=TRUE; 00154 00155 //Exit 00156 return TRUE; 00157 } 00158 else 00159 return FALSE; 00160 else 00161 return FALSE; 00162 } 00163 00164 void CRWAutoRelease::StopEscalation() 00165 { 00166 if (m_pLock && m_bEscalated) 00167 { 00168 //Stop it 00169 m_pLock->StopEscalation(); 00170 00171 //Set the flag 00172 m_bEscalated=FALSE; 00173 } 00174 } 00175 00176 KOMODIA_NAMESPACE_END |