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.

102 lines
1.9 KiB

  1. //============================================================================
  2. //
  3. // CAutoLock.cpp -- Automatic locking class for mutexes and critical sections.
  4. //
  5. // Copyright (c) 1998-2002 Microsoft Corporation, All Rights Reserved
  6. //
  7. // Revisions: 6/26/98 a-kevhu Created
  8. //
  9. //============================================================================
  10. #include "precomp.h"
  11. #include "CAutoLock.h"
  12. /*
  13. CAutoLock::CAutoLock(HANDLE hMutexHandle)
  14. :
  15. m_pcCritSec(NULL),
  16. m_pcMutex(NULL),
  17. m_psCritSec(NULL),
  18. m_hMutexHandle(hMutexHandle),
  19. bExec ( FALSE )
  20. {
  21. ::WaitForSingleObject(m_hMutexHandle, INFINITE);
  22. }
  23. CAutoLock::CAutoLock(CMutex& rCMutex):
  24. m_pcCritSec(NULL),
  25. m_psCritSec(NULL),
  26. m_hMutexHandle(NULL),
  27. m_pcMutex(&rCMutex),
  28. bExec ( FALSE )
  29. {
  30. m_pcMutex->Wait(INFINITE);
  31. }
  32. */
  33. CAutoLock::CAutoLock(CCriticalSec& rCCritSec):
  34. // m_hMutexHandle(NULL),
  35. // m_pcMutex(NULL),
  36. m_pcCritSec(&rCCritSec),
  37. m_psCritSec(NULL),
  38. bExec ( FALSE )
  39. {
  40. m_pcCritSec->Enter();
  41. }
  42. CAutoLock::CAutoLock( CStaticCritSec & rCCriticalSec):
  43. // m_hMutexHandle(NULL),
  44. // m_pcMutex(NULL),
  45. m_pcCritSec(NULL),
  46. m_psCritSec(&rCCriticalSec),
  47. bExec ( FALSE )
  48. {
  49. m_psCritSec->Enter();
  50. };
  51. // destructor...
  52. CAutoLock::~CAutoLock()
  53. {
  54. if ( FALSE == bExec )
  55. {
  56. Exec () ;
  57. }
  58. }
  59. BOOL CAutoLock::Exec ()
  60. {
  61. BOOL bStatus = TRUE;
  62. /*
  63. if (m_hMutexHandle)
  64. {
  65. bStatus = ::ReleaseMutex(m_hMutexHandle);
  66. }
  67. else if (m_pcMutex)
  68. {
  69. bStatus = m_pcMutex->Release();
  70. }
  71. else */
  72. if (m_pcCritSec)
  73. {
  74. m_pcCritSec->Leave();
  75. }
  76. else
  77. {
  78. m_psCritSec->Leave();
  79. }
  80. if (!bStatus)
  81. {
  82. LogMessage2(L"CAutoLock Error: %d", ::GetLastError());
  83. }
  84. else
  85. {
  86. bExec = TRUE ;
  87. }
  88. return bStatus ;
  89. }