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.

88 lines
1.4 KiB

  1. //
  2. // auto_cs.h CRITICAL_SECTION
  3. //
  4. #pragma once
  5. class auto_leave;
  6. class auto_cs
  7. {
  8. public:
  9. auto_cs()
  10. { InitializeCriticalSection(&m_cs); }
  11. ~auto_cs()
  12. {
  13. DeleteCriticalSection(&m_cs);
  14. };
  15. // return value of current dumb pointer
  16. LPCRITICAL_SECTION get()
  17. { return &m_cs; };
  18. LPCRITICAL_SECTION get() const
  19. { return (LPCRITICAL_SECTION)&m_cs; };
  20. protected:
  21. CRITICAL_SECTION m_cs;
  22. };
  23. class auto_leave
  24. {
  25. public:
  26. auto_leave(auto_cs& cs)
  27. : m_ulCount(0), m_pcs(cs.get()) {}
  28. auto_leave(const auto_cs& cs)
  29. {
  30. m_ulCount =0;
  31. m_pcs = cs.get();
  32. }
  33. ~auto_leave()
  34. {
  35. reset();
  36. }
  37. auto_leave& operator=(auto_cs& cs)
  38. {
  39. reset();
  40. m_pcs = cs.get();
  41. return *this;
  42. }
  43. void EnterCriticalSection()
  44. { ::EnterCriticalSection(m_pcs); m_ulCount++; }
  45. void LeaveCriticalSection()
  46. {
  47. if (m_ulCount)
  48. {
  49. m_ulCount--;
  50. ::LeaveCriticalSection(m_pcs);
  51. }
  52. }
  53. //Note: Win95 doesn't support TryEnterCriticalSection.
  54. //Commenting out this since we don't use it. [mgorti]
  55. //BOOL TryEnterCriticalSection()
  56. //{
  57. // if (::TryEnterCriticalSection(m_pcs))
  58. // {
  59. // m_ulCount++;
  60. // return TRUE;
  61. // }
  62. // return FALSE;
  63. //}
  64. protected:
  65. void reset()
  66. {
  67. while (m_ulCount)
  68. {
  69. LeaveCriticalSection();
  70. }
  71. m_pcs = 0;
  72. }
  73. ULONG m_ulCount;
  74. LPCRITICAL_SECTION m_pcs;
  75. };