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.

95 lines
1.9 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. rss_cs.hxx
  5. Abstract:
  6. CS wrapper class.
  7. Author:
  8. Ran Kalach
  9. Revision History:
  10. 04/22/2002 rankala Copying with some modifications from vss project nt\drivers\storage\volsnap\vss\server\inc\vs_types.hxx
  11. --*/
  12. #ifndef _RSSCS_
  13. #define _RSSCS_
  14. class CRssCriticalSection
  15. {
  16. CRssCriticalSection(const CRssCriticalSection&);
  17. public:
  18. // Creates and initializes the critical section
  19. CRssCriticalSection(
  20. IN BOOL bThrowOnError = TRUE
  21. ):
  22. m_bInitialized(FALSE),
  23. m_lLockCount(0),
  24. m_bThrowOnError(bThrowOnError)
  25. {
  26. HRESULT hr = S_OK;
  27. try
  28. {
  29. // May throw STATUS_NO_MEMORY if memory is low.
  30. WsbAffirmStatus(InitializeCriticalSectionAndSpinCount(&m_sec, 1000));
  31. }
  32. WsbCatch(hr)
  33. m_bInitialized = SUCCEEDED(hr);
  34. }
  35. // Destroys the critical section
  36. ~CRssCriticalSection()
  37. {
  38. if (m_bInitialized)
  39. DeleteCriticalSection(&m_sec);
  40. }
  41. // Locks the critical section
  42. void Lock()
  43. {
  44. if (!m_bInitialized)
  45. if (m_bThrowOnError)
  46. WsbThrow( E_OUTOFMEMORY );
  47. EnterCriticalSection(&m_sec);
  48. InterlockedIncrement((LPLONG)&m_lLockCount);
  49. }
  50. // Unlocks the critical section
  51. void Unlock()
  52. {
  53. if (!m_bInitialized)
  54. if (m_bThrowOnError)
  55. WsbThrow( E_OUTOFMEMORY );
  56. InterlockedDecrement((LPLONG) &m_lLockCount);
  57. LeaveCriticalSection(&m_sec);
  58. }
  59. BOOL IsLocked() const { return (m_lLockCount > 0); };
  60. BOOL IsInitialized() const { return m_bInitialized; };
  61. private:
  62. CRITICAL_SECTION m_sec;
  63. BOOL m_bInitialized;
  64. BOOL m_bThrowOnError;
  65. LONG volatile m_lLockCount;
  66. };
  67. #endif // _RSSCS_