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.

133 lines
2.6 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. lock.cxx
  5. Abstract:
  6. A simple CRITICAL_SECTION wrapper.
  7. Author:
  8. Michael Courage (MCourage) 15-Feb-1999
  9. Revision History:
  10. --*/
  11. #ifndef _LOCK_HXX_
  12. #define _LOCK_HXX_
  13. #define PREALLOCATE_EVENT_MASK 0x80000000
  14. #define IIS_DEFAULT_SPIN_COUNT (1000)
  15. //
  16. // class LOCK
  17. //
  18. // A simple wrapper for a CRITICAL_SECTION
  19. //
  20. #define LOCK_SIGNATURE CREATE_SIGNATURE( 'LOCK' )
  21. #define LOCK_SIGNATURE_FREED CREATE_SIGNATURE( 'xloc' )
  22. class LOCK
  23. {
  24. public:
  25. LOCK()
  26. {
  27. m_dwSignature = LOCK_SIGNATURE;
  28. m_fValid = FALSE;
  29. }
  30. ~LOCK()
  31. {
  32. DBG_ASSERT( m_dwSignature == LOCK_SIGNATURE );
  33. DBG_ASSERT( !m_fValid );
  34. m_dwSignature = LOCK_SIGNATURE_FREED;
  35. }
  36. HRESULT
  37. Initialize(
  38. DWORD dwSpinCount = IIS_DEFAULT_SPIN_COUNT,
  39. BOOL fPreallocateEvent = TRUE
  40. )
  41. {
  42. DBG_ASSERT( m_dwSignature == LOCK_SIGNATURE );
  43. DBG_ASSERT( !m_fValid );
  44. HRESULT hr = S_OK;
  45. if (fPreallocateEvent) {
  46. //
  47. // preallocating the event prevents
  48. // EnterCriticalSection from throwing
  49. // an exception, although you may end
  50. // up using more memory.
  51. //
  52. dwSpinCount |= PREALLOCATE_EVENT_MASK;
  53. }
  54. __try {
  55. if (InitializeCriticalSectionAndSpinCount(
  56. &m_csLock,
  57. dwSpinCount
  58. ) ) {
  59. m_fValid = TRUE;
  60. } else {
  61. hr = HRESULT_FROM_WIN32(GetLastError());
  62. }
  63. } __except (EXCEPTION_EXECUTE_HANDLER) {
  64. hr = HRESULT_FROM_NT(_exception_code());
  65. }
  66. return hr;
  67. }
  68. VOID
  69. Terminate(
  70. VOID
  71. )
  72. {
  73. DBG_ASSERT( m_dwSignature == LOCK_SIGNATURE );
  74. if (m_fValid) {
  75. DeleteCriticalSection(&m_csLock);
  76. m_fValid = FALSE;
  77. }
  78. }
  79. VOID
  80. Lock(
  81. VOID
  82. )
  83. {
  84. DBG_ASSERT( m_dwSignature == LOCK_SIGNATURE );
  85. DBG_ASSERT( m_fValid );
  86. EnterCriticalSection(&m_csLock);
  87. }
  88. VOID
  89. Unlock(
  90. VOID
  91. )
  92. {
  93. DBG_ASSERT( m_dwSignature == LOCK_SIGNATURE );
  94. DBG_ASSERT( m_fValid );
  95. LeaveCriticalSection(&m_csLock);
  96. }
  97. private:
  98. DWORD m_dwSignature;
  99. BOOL m_fValid;
  100. CRITICAL_SECTION m_csLock;
  101. };
  102. #endif // !_LOCK_HXX_