diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /utils/vmpi/threadhelpers.cpp | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'utils/vmpi/threadhelpers.cpp')
| -rw-r--r-- | utils/vmpi/threadhelpers.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/utils/vmpi/threadhelpers.cpp b/utils/vmpi/threadhelpers.cpp new file mode 100644 index 0000000..1205eb8 --- /dev/null +++ b/utils/vmpi/threadhelpers.cpp @@ -0,0 +1,155 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +//=============================================================================// + +#include <windows.h> +#include "threadhelpers.h" +#include "tier0/dbg.h" + + +// -------------------------------------------------------------------------------- // +// CCriticalSection implementation. +// -------------------------------------------------------------------------------- // + +CCriticalSection::CCriticalSection() +{ + Assert( sizeof( CRITICAL_SECTION ) == SIZEOF_CS ); + +#if defined( _DEBUG ) + InitializeCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); +#endif + + InitializeCriticalSection( (CRITICAL_SECTION*)&m_CS ); +} + + +CCriticalSection::~CCriticalSection() +{ + DeleteCriticalSection( (CRITICAL_SECTION*)&m_CS ); + +#if defined( _DEBUG ) + DeleteCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); +#endif +} + + + +void CCriticalSection::Lock() +{ +#if defined( _DEBUG ) + // Check if this one is already locked. + DWORD id = GetCurrentThreadId(); + EnterCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); + Assert( m_Locks.Find( id ) == m_Locks.InvalidIndex() ); + m_Locks.AddToTail( id ); + LeaveCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); +#endif + + EnterCriticalSection( (CRITICAL_SECTION*)&m_CS ); +} + + +void CCriticalSection::Unlock() +{ +#if defined( _DEBUG ) + // Check if this one is already locked. + DWORD id = GetCurrentThreadId(); + EnterCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); + int index = m_Locks.Find( id ); + Assert( index != m_Locks.InvalidIndex() ); + m_Locks.Remove( index ); + LeaveCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect ); +#endif + + LeaveCriticalSection( (CRITICAL_SECTION*)&m_CS ); +} + + + +// -------------------------------------------------------------------------------- // +// CCriticalSectionLock implementation. +// -------------------------------------------------------------------------------- // + +CCriticalSectionLock::CCriticalSectionLock( CCriticalSection *pCS ) +{ + m_pCS = pCS; + m_bLocked = false; +} + + +CCriticalSectionLock::~CCriticalSectionLock() +{ + if ( m_bLocked ) + m_pCS->Unlock(); +} + + +void CCriticalSectionLock::Lock() +{ + Assert( !m_bLocked ); + m_bLocked = true; + m_pCS->Lock(); +} + + +void CCriticalSectionLock::Unlock() +{ + Assert( m_bLocked ); + m_bLocked = false; + m_pCS->Unlock(); +} + + +// -------------------------------------------------------------------------------- // +// CEvent implementation. +// -------------------------------------------------------------------------------- // + +CEvent::CEvent() +{ + m_hEvent = NULL; +} + +CEvent::~CEvent() +{ + Term(); +} + +bool CEvent::Init( bool bManualReset, bool bInitialState ) +{ + Term(); + + m_hEvent = (void*)CreateEvent( NULL, bManualReset, bInitialState, NULL ); + return (m_hEvent != NULL); +} + +void CEvent::Term() +{ + if ( m_hEvent ) + { + CloseHandle( (HANDLE)m_hEvent ); + m_hEvent = NULL; + } +} + +void* CEvent::GetEventHandle() const +{ + Assert( m_hEvent ); + return m_hEvent; +} + +bool CEvent::SetEvent() +{ + Assert( m_hEvent ); + return ::SetEvent( (HANDLE)m_hEvent ) != 0; +} + +bool CEvent::ResetEvent() +{ + Assert( m_hEvent ); + return ::ResetEvent( (HANDLE)m_hEvent ) != 0; +} + + |