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.

157 lines
4.4 KiB

  1. #ifndef _INC_CSCVIEW_THDSYNC_H
  2. #define _INC_CSCVIEW_THDSYNC_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /* File: thdsync.h
  5. Description: Contains classes for managing thread synchronization in
  6. Win32 programs. Most of the work is to provide automatic unlocking
  7. of synchronization primities on object destruction. The work on
  8. monitors and condition variables is strongly patterned after
  9. work in "Multithreaded Programming with Windows NT" by Pham and Garg.
  10. Revision History:
  11. Date Description Programmer
  12. -------- --------------------------------------------------- ----------
  13. 09/22/97 Initial creation. BrianAu
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef _WINDOWS_
  17. # include <windows.h>
  18. #endif
  19. #ifndef _INC_DSKQUOTA_DEBUG_H
  20. # include "debug.h"
  21. #endif
  22. class CCriticalSection
  23. {
  24. public:
  25. CCriticalSection(void)
  26. { if (!InitializeCriticalSectionAndSpinCount(&m_cs, 0))
  27. throw CSyncException(CSyncException::critsect, CSyncException::create);
  28. }
  29. ~CCriticalSection(void)
  30. { DeleteCriticalSection(&m_cs); }
  31. void Enter(void)
  32. { EnterCriticalSection(&m_cs); }
  33. void Leave(void)
  34. { LeaveCriticalSection(&m_cs); }
  35. operator CRITICAL_SECTION& ()
  36. { return m_cs; }
  37. private:
  38. CRITICAL_SECTION m_cs;
  39. //
  40. // Prevent copy.
  41. //
  42. CCriticalSection(const CCriticalSection& rhs);
  43. CCriticalSection& operator = (const CCriticalSection& rhs);
  44. };
  45. class CWin32SyncObj
  46. {
  47. public:
  48. explicit CWin32SyncObj(HANDLE handle)
  49. : m_handle(handle) { }
  50. virtual ~CWin32SyncObj(void)
  51. { if (NULL != m_handle) CloseHandle(m_handle); }
  52. HANDLE Handle(void)
  53. { return m_handle; }
  54. protected:
  55. HANDLE m_handle;
  56. };
  57. class CMutex : public CWin32SyncObj
  58. {
  59. public:
  60. explicit CMutex(BOOL InitialOwner = FALSE);
  61. ~CMutex(void) { };
  62. DWORD Wait(DWORD dwTimeout = INFINITE)
  63. { return WaitForSingleObject(m_handle, dwTimeout); }
  64. void Release(void)
  65. { ReleaseMutex(m_handle); }
  66. private:
  67. //
  68. // Prevent copy.
  69. //
  70. CMutex(const CMutex& rhs);
  71. CMutex& operator = (const CMutex& rhs);
  72. };
  73. //
  74. // An "auto lock" object based on a Win32 critical section.
  75. // The constructor automatically calls EnterCriticalSection for the
  76. // specified critical section. The destructor automatically calls
  77. // LeaveCriticalSection. Note that the critical section object may
  78. // be specified as a Win32 CRITICAL_SECTION or a CCriticalSection object.
  79. // If using a CRITICAL_SECTION object, initialization and deletion of
  80. // the CRITICALS_SECTION is the responsibility of the caller.
  81. //
  82. class AutoLockCs
  83. {
  84. public:
  85. explicit AutoLockCs(CRITICAL_SECTION& cs)
  86. : m_cLock(0),
  87. m_pCS(&cs) { Lock(); }
  88. void Lock(void)
  89. { DBGASSERT((0 <= m_cLock)); EnterCriticalSection(m_pCS); m_cLock++; }
  90. void Release(void)
  91. { m_cLock--; LeaveCriticalSection(m_pCS); }
  92. ~AutoLockCs(void) { if (0 < m_cLock) Release(); }
  93. private:
  94. CRITICAL_SECTION *m_pCS;
  95. int m_cLock;
  96. };
  97. //
  98. // An "auto lock" object based on a Win32 Mutex object.
  99. // The constructor automatically calls WaitForSingleObject for the
  100. // specified mutex. The destructor automatically calls
  101. // ReleaseMutex.
  102. //
  103. class AutoLockMutex
  104. {
  105. public:
  106. //
  107. // Attaches to an already-owned mutex to ensure release.
  108. //
  109. explicit AutoLockMutex(HANDLE hMutex)
  110. : m_hMutex(hMutex) { }
  111. explicit AutoLockMutex(CMutex& mutex)
  112. : m_hMutex(mutex.Handle()) { }
  113. AutoLockMutex(HANDLE hMutex, DWORD dwTimeout)
  114. : m_hMutex(hMutex) { Wait(dwTimeout); }
  115. AutoLockMutex(CMutex& mutex, DWORD dwTimeout)
  116. : m_hMutex(mutex.Handle()) { Wait(dwTimeout); }
  117. ~AutoLockMutex(void) { ReleaseMutex(m_hMutex); }
  118. private:
  119. HANDLE m_hMutex;
  120. void Wait(DWORD dwTimeout = INFINITE);
  121. };
  122. #endif // _INC_CSCVIEW_THDSYNC_H