CriticalSection.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class CriticalSection
  7. #ifndef _WIN32
  8. : public CCLock
  9. {
  10. public:
  11. CriticalSection(DWORD spincount = 0): CCLock(true) { }
  12. };
  13. #else // _WIN32
  14. {
  15. public:
  16. CriticalSection(DWORD spincount = 0)
  17. {
  18. #pragma prefast(suppress:6031, "InitializeCriticalSectionAndSpinCount always succeed since Vista. No need to check return value");
  19. ::InitializeCriticalSectionAndSpinCount(&cs, spincount);
  20. }
  21. ~CriticalSection() { ::DeleteCriticalSection(&cs); }
  22. BOOL TryEnter() { return ::TryEnterCriticalSection(&cs); }
  23. void Enter() { ::EnterCriticalSection(&cs); }
  24. void Leave() { ::LeaveCriticalSection(&cs); }
  25. #if DBG
  26. bool IsLocked() const { return cs.OwningThread == (HANDLE)::GetCurrentThreadId(); }
  27. #endif
  28. private:
  29. CRITICAL_SECTION cs;
  30. };
  31. #endif
  32. //FakeCriticalSection mimics CriticalSection apis
  33. class FakeCriticalSection
  34. {
  35. public:
  36. FakeCriticalSection(DWORD spincount = 0) { /*do nothing*/spincount++; }
  37. ~FakeCriticalSection() {}
  38. BOOL TryEnter() { return true; }
  39. void Enter() {}
  40. void Leave() {}
  41. #if DBG
  42. bool IsLocked() const { return true; }
  43. #endif
  44. };
  45. class AutoCriticalSection
  46. {
  47. public:
  48. AutoCriticalSection(CriticalSection * cs) : cs(cs) { cs->Enter(); }
  49. ~AutoCriticalSection() { cs->Leave(); }
  50. private:
  51. CriticalSection * cs;
  52. };
  53. class AutoOptionalCriticalSection
  54. {
  55. public:
  56. AutoOptionalCriticalSection(CriticalSection * cs) : cs(cs)
  57. {
  58. if (cs)
  59. {
  60. cs->Enter();
  61. }
  62. }
  63. ~AutoOptionalCriticalSection()
  64. {
  65. if (cs)
  66. {
  67. cs->Leave();
  68. }
  69. }
  70. private:
  71. CriticalSection * cs;
  72. };
  73. template <class SyncObject = FakeCriticalSection >
  74. class AutoRealOrFakeCriticalSection
  75. {
  76. public:
  77. AutoRealOrFakeCriticalSection(SyncObject * cs) : cs(cs) { cs->Enter(); }
  78. ~AutoRealOrFakeCriticalSection() { cs->Leave(); }
  79. private:
  80. SyncObject * cs;
  81. };
  82. template <class SyncObject = FakeCriticalSection >
  83. class AutoOptionalRealOrFakeCriticalSection
  84. {
  85. public:
  86. AutoOptionalRealOrFakeCriticalSection(SyncObject * cs) : cs(cs)
  87. {
  88. if (cs)
  89. {
  90. cs->Enter();
  91. }
  92. }
  93. ~AutoOptionalRealOrFakeCriticalSection()
  94. {
  95. if (cs)
  96. {
  97. cs->Leave();
  98. }
  99. }
  100. private:
  101. SyncObject * cs;
  102. };