00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "Win32ReadWriteLock.h"
00038
00039 #include "OSManager.h"
00040 #include "GenericMutex.h"
00041 #include "GenericSemaphore.h"
00042
00043 #ifdef _MEMORY_DEBUG
00044 #define new DEBUG_NEW
00045 #define malloc DEBUG_MALLOC
00046 static char THIS_FILE[] = __FILE__;
00047 #endif
00048
00049 KOMODIA_NAMESPACE_START
00050
00051 CWin32ReadWriteLock::CWin32ReadWriteLock(int iMaximumReaders) : CGenericReadWriteLock(iMaximumReaders),
00052 m_pSemaphore(NULL),
00053 m_pMutex(NULL),
00054 m_bWriter(FALSE),
00055 m_bEscalation(FALSE),
00056 m_iReaders(0)
00057 {
00058
00059 m_pMutex=COSManager::CreateMutex();
00060 m_pSemaphore=COSManager::CreateSemaphore(iMaximumReaders,iMaximumReaders);
00061 }
00062
00063 CWin32ReadWriteLock::~CWin32ReadWriteLock()
00064 {
00065
00066 delete m_pMutex;
00067 delete m_pSemaphore;
00068 }
00069
00070 BOOL CWin32ReadWriteLock::LockRead(unsigned long ulMSTimeout)
00071 {
00072
00073 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00074
00075
00076 if (m_bEscalation || m_bWriter)
00077
00078 return FALSE;
00079
00080
00081 if (m_iReaders==GetMaximumReaders())
00082 {
00083
00084 aRelease.Release();
00085
00086
00087 if (m_pSemaphore->Aquire(ulMSTimeout))
00088 return FALSE;
00089 else
00090 {
00091
00092 aRelease.Aquire();
00093
00094
00095 if (m_bEscalation || m_bWriter)
00096 {
00097
00098
00099 m_pSemaphore->Release();
00100
00101
00102 return FALSE;
00103 }
00104 else
00105
00106 ++m_iReaders;
00107 }
00108 }
00109 else if (m_pSemaphore->Aquire(ulMSTimeout))
00110 return FALSE;
00111 else
00112
00113 ++m_iReaders;
00114
00115
00116 return TRUE;
00117 }
00118
00119 void CWin32ReadWriteLock::UnlockRead()
00120 {
00121
00122 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00123
00124
00125 if (m_iReaders)
00126 {
00127
00128 m_pSemaphore->Release();
00129
00130
00131 --m_iReaders;
00132 }
00133 }
00134
00135 BOOL CWin32ReadWriteLock::LockWrite()
00136 {
00137
00138 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00139
00140
00141 if (m_iReaders || m_bWriter)
00142
00143 return FALSE;
00144
00145
00146 m_bWriter=TRUE;
00147
00148
00149 return TRUE;
00150 }
00151
00152 void CWin32ReadWriteLock::UnlockWrite()
00153 {
00154
00155 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00156
00157
00158 if (m_bWriter)
00159 {
00160
00161 m_bWriter=FALSE;
00162
00163
00164 m_bEscalation=FALSE;
00165 }
00166 }
00167
00168 BOOL CWin32ReadWriteLock::Escalate(unsigned long ulMSTimeout,
00169 unsigned long ulIterations)
00170 {
00171
00172 if (LockWrite())
00173 return TRUE;
00174
00175
00176 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00177
00178
00179 m_bEscalation=TRUE;
00180
00181
00182 aRelease.Release();
00183
00184
00185 for (unsigned long ulCounter=0;ulCounter<ulIterations;++ulCounter)
00186 {
00187
00188 if (LockWrite())
00189 return TRUE;
00190
00191
00192 Sleep(ulMSTimeout);
00193 }
00194
00195
00196 return FALSE;
00197 }
00198
00199 void CWin32ReadWriteLock::StopEscalation()
00200 {
00201
00202 CMutexAutoRelease aRelease(m_pMutex,TRUE);
00203
00204
00205 m_bEscalation=FALSE;
00206 }
00207
00208 KOMODIA_NAMESPACE_END