summaryrefslogtreecommitdiff
path: root/utils/vmpi/threadhelpers.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/vmpi/threadhelpers.cpp')
-rw-r--r--utils/vmpi/threadhelpers.cpp155
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;
+}
+
+