Counter Strike : Global Offensive Source Code
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.

110 lines
1.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef THREADHELPERS_H
  8. #define THREADHELPERS_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/utllinkedlist.h"
  13. #define SIZEOF_CS 24 // sizeof( CRITICAL_SECTION )
  14. class CCriticalSection
  15. {
  16. public:
  17. CCriticalSection();
  18. ~CCriticalSection();
  19. protected:
  20. friend class CCriticalSectionLock;
  21. void Lock();
  22. void Unlock();
  23. public:
  24. char m_CS[SIZEOF_CS];
  25. // Used to protect against deadlock in debug mode.
  26. //#if defined( _DEBUG )
  27. CUtlLinkedList<unsigned long,int> m_Locks;
  28. char m_DeadlockProtect[SIZEOF_CS];
  29. //#endif
  30. };
  31. // Use this to lock a critical section.
  32. class CCriticalSectionLock
  33. {
  34. public:
  35. CCriticalSectionLock( CCriticalSection *pCS );
  36. ~CCriticalSectionLock();
  37. void Lock();
  38. void Unlock();
  39. private:
  40. CCriticalSection *m_pCS;
  41. bool m_bLocked;
  42. };
  43. template< class T >
  44. class CCriticalSectionData : private CCriticalSection
  45. {
  46. public:
  47. // You only have access to the data between Lock() and Unlock().
  48. T* Lock()
  49. {
  50. CCriticalSection::Lock();
  51. return &m_Data;
  52. }
  53. void Unlock()
  54. {
  55. CCriticalSection::Unlock();
  56. }
  57. private:
  58. T m_Data;
  59. };
  60. // ------------------------------------------------------------------------------------------------ //
  61. // CEvent.
  62. // ------------------------------------------------------------------------------------------------ //
  63. class CEvent
  64. {
  65. public:
  66. CEvent();
  67. ~CEvent();
  68. bool Init( bool bManualReset, bool bInitialState );
  69. void Term();
  70. void* GetEventHandle() const;
  71. // Signal the event.
  72. bool SetEvent();
  73. // Unset the event's signalled status.
  74. bool ResetEvent();
  75. private:
  76. void *m_hEvent;
  77. };
  78. #endif // THREADHELPERS_H