//===================================================================== BOOL SimpleRWLock::TryEnterWrite() { STATIC_CONTRACT_NOTHROW; STATIC_CONTRACT_CAN_TAKE_LOCK; #ifdef _DEBUG PreEnter(); #endif //_DEBUG LONG RWLock = InterlockedCompareExchange( &m_RWLock, -1, 0 ); _ASSERTE (RWLock >= 0 || RWLock == -1); if( RWLock ) { return FALSE; } INCTHREADLOCKCOUNT(); EE_LOCK_TAKEN(this); #ifdef _DEBUG PostEnter(); #endif //_DEBUG ResetWriterWaiting(); return TRUE; }
//----------------------------------------------------------------- // Acquire the lock. //----------------------------------------------------------------- void CrstBase::Enter() { #ifdef _DEBUG PreEnter (); #endif _ASSERTE(IsSafeToTake() || g_fEEShutDown); #ifdef _DEBUG char buffer[100]; sprintf(buffer, "Enter in crst.h - %s", m_tag); CRSTBLOCKCOUNTINCL(); LOCKCOUNTINCL(buffer); #endif EnterCriticalSection(&m_criticalsection); CRSTELOCKCOUNTINCL(); #ifdef _DEBUG m_holderthreadid = GetCurrentThreadId(); m_entercount++; PostEnter (); #endif }
BOOL SimpleRWLock::TryEnterRead() { STATIC_CONTRACT_NOTHROW; STATIC_CONTRACT_CAN_TAKE_LOCK; #ifdef _DEBUG PreEnter(); #endif //_DEBUG LONG RWLock; do { RWLock = m_RWLock; if( RWLock == -1 ) return FALSE; _ASSERTE (RWLock >= 0); } while( RWLock != InterlockedCompareExchange( &m_RWLock, RWLock+1, RWLock )); INCTHREADLOCKCOUNT(); EE_LOCK_TAKEN(this); #ifdef _DEBUG PostEnter(); #endif //_DEBUG return TRUE; }