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.

131 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. bsconcur.hxx
  5. Abstract:
  6. Definition of CBsXXXX classes that wrap Win32 concurrency controls.
  7. Author:
  8. Stefan R. Steiner [SSteiner] 14-Apr-1998
  9. Revision History:
  10. SSteiner 7/27/2000 - Added safe critical section handling.
  11. --*/
  12. #ifndef _H_BSCONCUR
  13. #define _H_BSCONCUR
  14. ////////////////////////////////////////////////////////////////////////
  15. // Standard foo for file name aliasing. This code block must be after
  16. // all includes of VSS header files.
  17. //
  18. #ifdef VSS_FILE_ALIAS
  19. #undef VSS_FILE_ALIAS
  20. #endif
  21. #define VSS_FILE_ALIAS "INCBSCRH"
  22. //
  23. ////////////////////////////////////////////////////////////////////////
  24. class CBsCritSec
  25. {
  26. public:
  27. CBsCritSec()
  28. {
  29. try
  30. {
  31. m_bInitialized = TRUE;
  32. // May throw STATUS_NO_MEMORY if memory is low.
  33. ::InitializeCriticalSection( &m_cs );
  34. }
  35. catch( ... )
  36. {
  37. m_bInitialized = FALSE;
  38. }
  39. }
  40. virtual ~CBsCritSec()
  41. {
  42. if ( m_bInitialized )
  43. ::DeleteCriticalSection( &m_cs );
  44. }
  45. // Enter the critical section
  46. void Enter()
  47. {
  48. if ( !m_bInitialized )
  49. throw E_OUTOFMEMORY;
  50. try
  51. {
  52. // May throw STATUS_INVALID_HANDLE if memory is low
  53. ::EnterCriticalSection( &m_cs );
  54. }
  55. catch( ... )
  56. {
  57. throw E_OUTOFMEMORY;
  58. }
  59. }
  60. // Leave the critical section
  61. void Leave()
  62. {
  63. if ( !m_bInitialized )
  64. throw E_OUTOFMEMORY;
  65. ::LeaveCriticalSection( &m_cs );
  66. }
  67. // Return a pointer to the internal critical section
  68. CRITICAL_SECTION *GetCritSec()
  69. {
  70. return &m_cs;
  71. }
  72. private:
  73. CRITICAL_SECTION m_cs;
  74. BOOL m_bInitialized;
  75. };
  76. class CBsAutoLock
  77. {
  78. public:
  79. CBsAutoLock( HANDLE hMutexHandle )
  80. : m_type( BS_AUTO_MUTEX_HANDLE ),
  81. m_hMutexHandle( hMutexHandle )
  82. {
  83. ::WaitForSingleObject( m_hMutexHandle, INFINITE );
  84. }
  85. CBsAutoLock( CRITICAL_SECTION *pCritSec )
  86. : m_type( BS_AUTO_CRIT_SEC ),
  87. m_pCritSec( pCritSec )
  88. {
  89. ::EnterCriticalSection( m_pCritSec );
  90. }
  91. CBsAutoLock( CBsCritSec &rCBsCritSec )
  92. : m_type( BS_AUTO_CRIT_SEC_CLASS ),
  93. m_pcCritSec( &rCBsCritSec )
  94. {
  95. m_pcCritSec->Enter();
  96. }
  97. virtual ~CBsAutoLock();
  98. private:
  99. enum LockType { BS_AUTO_MUTEX_HANDLE, BS_AUTO_CRIT_SEC, BS_AUTO_CRIT_SEC_CLASS };
  100. HANDLE m_hMutexHandle;
  101. CRITICAL_SECTION *m_pCritSec;
  102. CBsCritSec *m_pcCritSec;
  103. LockType m_type;
  104. };
  105. #endif // _H_BSCONCUR