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.

233 lines
5.3 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (c) 1992-2001 Microsoft Corporation, All Rights Reserved
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #ifndef __SNMPMT_H__
  11. #define __SNMPMT_H__
  12. #ifdef UNICODE
  13. #define LPCTSTR wchar_t *
  14. #else
  15. #define LPCTSTR char *
  16. #endif
  17. class CSyncObject;
  18. class CSemaphore;
  19. class CMutex;
  20. class CEvent;
  21. class CCriticalSection;
  22. class CSingleLock;
  23. class CMultiLock;
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Basic synchronization object
  26. class CSyncObject
  27. {
  28. public:
  29. CSyncObject(LPCTSTR pstrName);
  30. // Attributes
  31. public:
  32. operator HANDLE() const;
  33. HANDLE m_hObject;
  34. // Operations
  35. virtual BOOL Lock(DWORD dwTimeout = INFINITE);
  36. virtual BOOL Unlock() = 0;
  37. virtual BOOL Unlock(LONG /* lCount */, LPLONG /* lpPrevCount=NULL */)
  38. { return TRUE; }
  39. // Implementation
  40. public:
  41. virtual ~CSyncObject();
  42. friend class CSingleLock;
  43. friend class CMultiLock;
  44. };
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSemaphore
  47. class CSemaphore : public CSyncObject
  48. {
  49. // Constructor
  50. public:
  51. CSemaphore(LONG lInitialCount = 1, LONG lMaxCount = 1,
  52. LPCTSTR pstrName=NULL, LPSECURITY_ATTRIBUTES lpsaAttributes = NULL);
  53. // Implementation
  54. public:
  55. virtual ~CSemaphore();
  56. virtual BOOL Unlock();
  57. virtual BOOL Unlock(LONG lCount, LPLONG lprevCount = NULL);
  58. };
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMutex
  61. class CMutex : public CSyncObject
  62. {
  63. // Constructor
  64. public:
  65. CMutex(BOOL bInitiallyOwn = FALSE, LPCTSTR lpszName = NULL,
  66. LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);
  67. // Implementation
  68. public:
  69. virtual ~CMutex();
  70. BOOL Unlock();
  71. };
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CEvent
  74. class CEvent : public CSyncObject
  75. {
  76. // Constructor
  77. public:
  78. CEvent(BOOL bInitiallyOwn = FALSE, BOOL bManualReset = FALSE,
  79. LPCTSTR lpszNAme = NULL, LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);
  80. // Operations
  81. public:
  82. BOOL SetEvent();
  83. BOOL PulseEvent();
  84. BOOL ResetEvent();
  85. BOOL Unlock();
  86. // Implementation
  87. public:
  88. virtual ~CEvent();
  89. };
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CCriticalSection
  92. class CCriticalSection : public CSyncObject
  93. {
  94. // Constructor
  95. public:
  96. CCriticalSection();
  97. // Attributes
  98. public:
  99. operator CRITICAL_SECTION*();
  100. CRITICAL_SECTION m_sect;
  101. // Operations
  102. public:
  103. BOOL Unlock();
  104. BOOL Lock();
  105. BOOL Lock(DWORD dwTimeout);
  106. // Implementation
  107. public:
  108. virtual ~CCriticalSection();
  109. };
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CSingleLock
  112. class CSingleLock
  113. {
  114. // Constructors
  115. public:
  116. CSingleLock(CSyncObject* pObject, BOOL bInitialLock = FALSE);
  117. // Operations
  118. public:
  119. BOOL Lock(DWORD dwTimeOut = INFINITE);
  120. BOOL Unlock();
  121. BOOL Unlock(LONG lCount, LPLONG lPrevCount = NULL);
  122. BOOL IsLocked();
  123. // Implementation
  124. public:
  125. ~CSingleLock();
  126. protected:
  127. CSyncObject* m_pObject;
  128. HANDLE m_hObject;
  129. BOOL m_bAcquired;
  130. };
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMultiLock
  133. class CMultiLock
  134. {
  135. // Constructor
  136. public:
  137. CMultiLock(CSyncObject* ppObjects[], DWORD dwCount, BOOL bInitialLock = FALSE);
  138. // Operations
  139. public:
  140. DWORD Lock(DWORD dwTimeOut = INFINITE, BOOL bWaitForAll = TRUE,
  141. DWORD dwWakeMask = 0);
  142. BOOL Unlock();
  143. BOOL Unlock(LONG lCount, LPLONG lPrevCount = NULL);
  144. BOOL IsLocked(DWORD dwItem);
  145. // Implementation
  146. public:
  147. ~CMultiLock();
  148. protected:
  149. HANDLE m_hPreallocated[8];
  150. BOOL m_bPreallocated[8];
  151. CSyncObject* const * m_ppObjectArray;
  152. HANDLE* m_pHandleArray;
  153. BOOL* m_bLockedArray;
  154. DWORD m_dwCount;
  155. };
  156. /////////////////////////////////////////////////////////////////////////////
  157. // Inline function declarations
  158. inline CSyncObject::operator HANDLE() const
  159. { return m_hObject;}
  160. inline BOOL CSemaphore::Unlock()
  161. { return Unlock(1, NULL); }
  162. inline BOOL CEvent::SetEvent()
  163. { return ::SetEvent(m_hObject); }
  164. inline BOOL CEvent::PulseEvent()
  165. { return ::PulseEvent(m_hObject); }
  166. inline BOOL CEvent::ResetEvent()
  167. { return ::ResetEvent(m_hObject); }
  168. inline CSingleLock::~CSingleLock()
  169. { Unlock(); }
  170. inline BOOL CSingleLock::IsLocked()
  171. { return m_bAcquired; }
  172. inline BOOL CMultiLock::IsLocked(DWORD dwObject)
  173. { return m_bLockedArray[dwObject]; }
  174. inline CCriticalSection::CCriticalSection() : CSyncObject(NULL)
  175. { ::InitializeCriticalSection(&m_sect); }
  176. inline CCriticalSection::operator CRITICAL_SECTION*()
  177. { return (CRITICAL_SECTION*) &m_sect; }
  178. inline CCriticalSection::~CCriticalSection()
  179. { ::DeleteCriticalSection(&m_sect); }
  180. inline BOOL CCriticalSection::Lock()
  181. { ::EnterCriticalSection(&m_sect); return TRUE; }
  182. inline BOOL CCriticalSection::Lock(DWORD /* dwTimeout */)
  183. { return Lock(); }
  184. inline BOOL CCriticalSection::Unlock()
  185. { ::LeaveCriticalSection(&m_sect); return TRUE; }
  186. #endif // __AFXMT_H__
  187. /////////////////////////////////////////////////////////////////////////////