CriticalSection.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // CriticalSection.h ///////////////////////////////////////////////////////
  24. // Utility class to use critical sections in areas of code.
  25. // Author: JohnM And MattC, August 13, 2002
  26. #pragma once
  27. #ifndef __CRITICALSECTION_H__
  28. #define __CRITICALSECTION_H__
  29. #include "Common/PerfTimer.h"
  30. #ifdef PERF_TIMERS
  31. extern PerfGather TheCritSecPerfGather;
  32. #endif
  33. class CriticalSection
  34. {
  35. CRITICAL_SECTION m_windowsCriticalSection;
  36. public:
  37. CriticalSection()
  38. {
  39. #ifdef PERF_TIMERS
  40. AutoPerfGather a(TheCritSecPerfGather);
  41. #endif
  42. InitializeCriticalSection( &m_windowsCriticalSection );
  43. }
  44. virtual ~CriticalSection()
  45. {
  46. #ifdef PERF_TIMERS
  47. AutoPerfGather a(TheCritSecPerfGather);
  48. #endif
  49. DeleteCriticalSection( &m_windowsCriticalSection );
  50. }
  51. public: // Use these when entering/exiting a critical section.
  52. void enter( void )
  53. {
  54. #ifdef PERF_TIMERS
  55. AutoPerfGather a(TheCritSecPerfGather);
  56. #endif
  57. EnterCriticalSection( &m_windowsCriticalSection );
  58. }
  59. void exit( void )
  60. {
  61. #ifdef PERF_TIMERS
  62. AutoPerfGather a(TheCritSecPerfGather);
  63. #endif
  64. LeaveCriticalSection( &m_windowsCriticalSection );
  65. }
  66. };
  67. class ScopedCriticalSection
  68. {
  69. private:
  70. CriticalSection *m_cs;
  71. public:
  72. ScopedCriticalSection( CriticalSection *cs ) : m_cs(cs)
  73. {
  74. if (m_cs)
  75. m_cs->enter();
  76. }
  77. virtual ~ScopedCriticalSection( )
  78. {
  79. if (m_cs)
  80. m_cs->exit();
  81. }
  82. };
  83. #include "mutex.h"
  84. // These should be NULL on creation then non-NULL in WinMain or equivalent.
  85. // This allows us to be silently non-threadsafe for WB and other single-threaded apps.
  86. extern FastCriticalSectionClass TheAsciiStringCriticalSection;
  87. extern CriticalSection *TheUnicodeStringCriticalSection;
  88. extern CriticalSection *TheDmaCriticalSection;
  89. extern CriticalSection *TheMemoryPoolCriticalSection;
  90. extern CriticalSection *TheDebugLogCriticalSection;
  91. #endif /* __CRITICALSECTION_H__ */