Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <windows.h>
  8. #include "threadhelpers.h"
  9. #include "tier0/dbg.h"
  10. // -------------------------------------------------------------------------------- //
  11. // CCriticalSection implementation.
  12. // -------------------------------------------------------------------------------- //
  13. CCriticalSection::CCriticalSection()
  14. {
  15. Assert( sizeof( CRITICAL_SECTION ) == SIZEOF_CS );
  16. #if defined( _DEBUG )
  17. InitializeCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  18. #endif
  19. InitializeCriticalSection( (CRITICAL_SECTION*)&m_CS );
  20. }
  21. CCriticalSection::~CCriticalSection()
  22. {
  23. DeleteCriticalSection( (CRITICAL_SECTION*)&m_CS );
  24. #if defined( _DEBUG )
  25. DeleteCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  26. #endif
  27. }
  28. void CCriticalSection::Lock()
  29. {
  30. #if defined( _DEBUG )
  31. // Check if this one is already locked.
  32. DWORD id = GetCurrentThreadId();
  33. EnterCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  34. Assert( m_Locks.Find( id ) == m_Locks.InvalidIndex() );
  35. m_Locks.AddToTail( id );
  36. LeaveCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  37. #endif
  38. EnterCriticalSection( (CRITICAL_SECTION*)&m_CS );
  39. }
  40. void CCriticalSection::Unlock()
  41. {
  42. #if defined( _DEBUG )
  43. // Check if this one is already locked.
  44. DWORD id = GetCurrentThreadId();
  45. EnterCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  46. int index = m_Locks.Find( id );
  47. Assert( index != m_Locks.InvalidIndex() );
  48. m_Locks.Remove( index );
  49. LeaveCriticalSection( (CRITICAL_SECTION*)&m_DeadlockProtect );
  50. #endif
  51. LeaveCriticalSection( (CRITICAL_SECTION*)&m_CS );
  52. }
  53. // -------------------------------------------------------------------------------- //
  54. // CCriticalSectionLock implementation.
  55. // -------------------------------------------------------------------------------- //
  56. CCriticalSectionLock::CCriticalSectionLock( CCriticalSection *pCS )
  57. {
  58. m_pCS = pCS;
  59. m_bLocked = false;
  60. }
  61. CCriticalSectionLock::~CCriticalSectionLock()
  62. {
  63. if ( m_bLocked )
  64. m_pCS->Unlock();
  65. }
  66. void CCriticalSectionLock::Lock()
  67. {
  68. Assert( !m_bLocked );
  69. m_bLocked = true;
  70. m_pCS->Lock();
  71. }
  72. void CCriticalSectionLock::Unlock()
  73. {
  74. Assert( m_bLocked );
  75. m_bLocked = false;
  76. m_pCS->Unlock();
  77. }
  78. // -------------------------------------------------------------------------------- //
  79. // CEvent implementation.
  80. // -------------------------------------------------------------------------------- //
  81. CEvent::CEvent()
  82. {
  83. m_hEvent = NULL;
  84. }
  85. CEvent::~CEvent()
  86. {
  87. Term();
  88. }
  89. bool CEvent::Init( bool bManualReset, bool bInitialState )
  90. {
  91. Term();
  92. m_hEvent = (void*)CreateEvent( NULL, bManualReset, bInitialState, NULL );
  93. return (m_hEvent != NULL);
  94. }
  95. void CEvent::Term()
  96. {
  97. if ( m_hEvent )
  98. {
  99. CloseHandle( (HANDLE)m_hEvent );
  100. m_hEvent = NULL;
  101. }
  102. }
  103. void* CEvent::GetEventHandle() const
  104. {
  105. Assert( m_hEvent );
  106. return m_hEvent;
  107. }
  108. bool CEvent::SetEvent()
  109. {
  110. Assert( m_hEvent );
  111. return ::SetEvent( (HANDLE)m_hEvent ) != 0;
  112. }
  113. bool CEvent::ResetEvent()
  114. {
  115. Assert( m_hEvent );
  116. return ::ResetEvent( (HANDLE)m_hEvent ) != 0;
  117. }