Leaked source code of windows server 2003
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.

148 lines
3.6 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CriticalSection.h
  7. //
  8. // Implementation Files:
  9. // CriticalSection.cpp
  10. //
  11. // Description:
  12. // This file contains the declaration of the CCriticalSection
  13. // class.
  14. //
  15. // The class CCriticalSection is a simple wrapper around Platform SDK
  16. // spinlock objects.
  17. //
  18. // Maintained By:
  19. // John Franco (jfranco) 03-Oct-2001
  20. //
  21. //////////////////////////////////////////////////////////////////////////////
  22. // Make sure that this file is included only once per compile path.
  23. #pragma once
  24. //////////////////////////////////////////////////////////////////////////////
  25. // Include Files
  26. //////////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////////////////////////////////////////////////////
  28. // Constant Declarations
  29. //////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////
  31. //++
  32. //
  33. // class CCriticalSection
  34. //
  35. // Description:
  36. // The class CCriticalSection is a simple wrapper around Platform SDK
  37. // spinlock objects.
  38. //
  39. //--
  40. //////////////////////////////////////////////////////////////////////////////
  41. class CCriticalSection
  42. {
  43. public:
  44. CCriticalSection( DWORD cSpinsIn = RECOMMENDED_SPIN_COUNT );
  45. ~CCriticalSection( void );
  46. HRESULT HrInitialized( void ) const;
  47. void Enter( void );
  48. void Leave( void );
  49. private:
  50. CCriticalSection( const CCriticalSection & );
  51. CCriticalSection & operator=( const CCriticalSection & );
  52. CRITICAL_SECTION m_csSpinlock;
  53. HRESULT m_hrInitialization;
  54. }; //*** class CCriticalSection
  55. //////////////////////////////////////////////////////////////////////////
  56. //++
  57. //
  58. // CCriticalSection::HrInitialized
  59. //
  60. // Description:
  61. // Find out whether the critical section initialized itself.
  62. //
  63. // Arguments:
  64. // None.
  65. //
  66. // Return Value:
  67. // S_OK: the critical section initialized itself successfully.
  68. // Failure: initialization failed; the critical section is unusable.
  69. //
  70. //--
  71. //////////////////////////////////////////////////////////////////////////
  72. inline
  73. HRESULT
  74. CCriticalSection::HrInitialized( void ) const
  75. {
  76. return m_hrInitialization;
  77. } //*** CCriticalSection::HrInitialized
  78. //////////////////////////////////////////////////////////////////////////
  79. //++
  80. //
  81. // CCriticalSection::Enter
  82. //
  83. // Description:
  84. // Acquire the spin lock, blocking if necessary until
  85. // it becomes available.
  86. //
  87. // Arguments:
  88. // None.
  89. //
  90. // Return Value:
  91. // None.
  92. //
  93. //--
  94. //////////////////////////////////////////////////////////////////////////
  95. inline
  96. void
  97. CCriticalSection::Enter( void )
  98. {
  99. Assert( SUCCEEDED( m_hrInitialization ) );
  100. EnterCriticalSection( &m_csSpinlock );
  101. } //*** CCriticalSection::Enter
  102. //////////////////////////////////////////////////////////////////////////
  103. //++
  104. //
  105. // CCriticalSection::Leave
  106. //
  107. // Description:
  108. // Release the spin lock.
  109. //
  110. // Arguments:
  111. // None.
  112. //
  113. // Return Value:
  114. // None.
  115. //
  116. // Remarks:
  117. // This thread must own the lock from calling CCriticalSection::Enter.
  118. //
  119. //--
  120. //////////////////////////////////////////////////////////////////////////
  121. inline void
  122. CCriticalSection::Leave( void )
  123. {
  124. Assert( SUCCEEDED( m_hrInitialization ) );
  125. LeaveCriticalSection( &m_csSpinlock );
  126. } //*** CCriticalSection::Leave