Source code of Windows XP (NT5)
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.

147 lines
2.0 KiB

  1. // sync.h -- defines the CSyncWith class
  2. #ifndef __SYNC_H__
  3. #define __SYNC_H__
  4. class CITCriticalSection
  5. {
  6. public:
  7. CITCriticalSection();
  8. ~CITCriticalSection();
  9. void Enter();
  10. void Leave();
  11. #ifdef _DEBUG
  12. LONG LockCount();
  13. #endif // _DEBUG
  14. private:
  15. void Start();
  16. void Stop();
  17. CRITICAL_SECTION m_cs;
  18. #ifdef _DEBUG
  19. LONG m_ulOwningThread;
  20. LONG m_cLockRecursion;
  21. #endif // _DEBUG
  22. };
  23. inline CITCriticalSection::CITCriticalSection()
  24. {
  25. Start();
  26. }
  27. inline void CITCriticalSection::Start()
  28. {
  29. InitializeCriticalSection(&m_cs);
  30. #ifdef _DEBUG
  31. m_ulOwningThread = ~0;
  32. m_cLockRecursion = 0;
  33. #endif // _DEBUG
  34. }
  35. inline void CITCriticalSection::Stop()
  36. {
  37. DeleteCriticalSection(&m_cs);
  38. RonM_ASSERT(m_ulOwningThread == ~0);
  39. RonM_ASSERT(m_cLockRecursion == 0);
  40. }
  41. inline CITCriticalSection::~CITCriticalSection()
  42. {
  43. Stop();
  44. }
  45. inline void CITCriticalSection::Enter()
  46. {
  47. ::EnterCriticalSection(&m_cs);
  48. #ifdef _DEBUG
  49. RonM_ASSERT(m_cLockRecursion || m_ulOwningThread == ~0);
  50. if (!m_cLockRecursion++)
  51. m_ulOwningThread = GetCurrentThreadId();
  52. #endif // _DEBUG
  53. }
  54. #ifdef _DEBUG
  55. inline LONG CITCriticalSection::LockCount()
  56. {
  57. return (m_ulOwningThread == GetCurrentThreadId())? m_cLockRecursion : 0;
  58. }
  59. #endif // _DEBUG
  60. inline void CITCriticalSection::Leave()
  61. {
  62. #ifdef _DEBUG
  63. RonM_ASSERT(m_cLockRecursion > 0);
  64. if (!--m_cLockRecursion)
  65. m_ulOwningThread = ~0;
  66. #endif // _DEBUG
  67. ::LeaveCriticalSection(&m_cs);
  68. }
  69. class CSyncWith
  70. {
  71. public:
  72. CSyncWith(CITCriticalSection &refcs);
  73. ~CSyncWith();
  74. private:
  75. CITCriticalSection *m_pcs;
  76. #ifdef _DEBUG
  77. LONG m_cLocksPrevious;
  78. #endif // _DEBUG
  79. };
  80. inline CSyncWith::CSyncWith(CITCriticalSection &refcs)
  81. {
  82. m_pcs = &refcs;
  83. #ifdef _DEBUG
  84. m_cLocksPrevious = m_pcs->LockCount();
  85. #endif // _DEBUG
  86. m_pcs->Enter();
  87. }
  88. inline CSyncWith::~CSyncWith()
  89. {
  90. m_pcs->Leave();
  91. #ifdef _DEBUG
  92. LONG cLocks = m_pcs->LockCount();
  93. RonM_ASSERT(cLocks == m_cLocksPrevious);
  94. #endif // _DEBUG
  95. }
  96. #ifdef _DEBUG
  97. inline BOOL IsUnlocked(CITCriticalSection &refcs)
  98. {
  99. return refcs.LockCount() == 0;
  100. }
  101. #endif // _DEBUG;
  102. #endif // __SYNC_H__