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.

308 lines
6.9 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 __PROVMT_H__
  11. #define __PROVMT_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. // CCriticalSection
  112. class CStaticCriticalSection : public CSyncObject
  113. {
  114. static bool failureSet;
  115. static void RecordFailure(){ failureSet = true;}
  116. bool validCS;
  117. // Constructor
  118. public:
  119. CStaticCriticalSection();
  120. // Attributes
  121. public:
  122. operator CRITICAL_SECTION*();
  123. CRITICAL_SECTION m_sect;
  124. // Operations
  125. public:
  126. static bool AnyFailure() { return failureSet;}
  127. BOOL Unlock();
  128. BOOL Lock();
  129. BOOL Lock(DWORD dwTimeout);
  130. // Implementation
  131. public:
  132. virtual ~CStaticCriticalSection();
  133. };
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CSingleLock
  136. class CSingleLock
  137. {
  138. // Constructors
  139. public:
  140. CSingleLock(CSyncObject* pObject, BOOL bInitialLock = FALSE);
  141. // Operations
  142. public:
  143. BOOL Lock(DWORD dwTimeOut = INFINITE);
  144. BOOL Unlock();
  145. BOOL Unlock(LONG lCount, LPLONG lPrevCount = NULL);
  146. BOOL IsLocked();
  147. // Implementation
  148. public:
  149. ~CSingleLock();
  150. protected:
  151. CSyncObject* m_pObject;
  152. HANDLE m_hObject;
  153. BOOL m_bAcquired;
  154. };
  155. /////////////////////////////////////////////////////////////////////////////
  156. // CMultiLock
  157. class CMultiLock
  158. {
  159. // Constructor
  160. public:
  161. CMultiLock(CSyncObject* ppObjects[], DWORD dwCount, BOOL bInitialLock = FALSE);
  162. // Operations
  163. public:
  164. DWORD Lock(DWORD dwTimeOut = INFINITE, BOOL bWaitForAll = TRUE,
  165. DWORD dwWakeMask = 0);
  166. BOOL Unlock();
  167. BOOL Unlock(LONG lCount, LPLONG lPrevCount = NULL);
  168. BOOL IsLocked(DWORD dwItem);
  169. // Implementation
  170. public:
  171. ~CMultiLock();
  172. protected:
  173. HANDLE m_hPreallocated[8];
  174. BOOL m_bPreallocated[8];
  175. CSyncObject* const * m_ppObjectArray;
  176. HANDLE* m_pHandleArray;
  177. BOOL* m_bLockedArray;
  178. DWORD m_dwCount;
  179. };
  180. /////////////////////////////////////////////////////////////////////////////
  181. // Inline function declarations
  182. inline CSyncObject::operator HANDLE() const
  183. { return m_hObject;}
  184. inline BOOL CSemaphore::Unlock()
  185. { return Unlock(1, NULL); }
  186. inline BOOL CEvent::SetEvent()
  187. { return ::SetEvent(m_hObject); }
  188. inline BOOL CEvent::PulseEvent()
  189. { return ::PulseEvent(m_hObject); }
  190. inline BOOL CEvent::ResetEvent()
  191. { return ::ResetEvent(m_hObject); }
  192. inline CSingleLock::~CSingleLock()
  193. { Unlock(); }
  194. inline BOOL CSingleLock::IsLocked()
  195. { return m_bAcquired; }
  196. inline BOOL CMultiLock::IsLocked(DWORD dwObject)
  197. { return m_bLockedArray[dwObject]; }
  198. inline CCriticalSection::CCriticalSection() : CSyncObject(NULL)
  199. {
  200. if (!::InitializeCriticalSectionAndSpinCount(&m_sect,0))
  201. {
  202. throw Heap_Exception(Heap_Exception::HEAP_ERROR::E_ALLOCATION_ERROR) ;
  203. }
  204. }
  205. inline CCriticalSection::operator CRITICAL_SECTION*()
  206. { return (CRITICAL_SECTION*) &m_sect; }
  207. inline CCriticalSection::~CCriticalSection()
  208. { ::DeleteCriticalSection(&m_sect); }
  209. inline BOOL CCriticalSection::Lock()
  210. { ::EnterCriticalSection(&m_sect); return TRUE; }
  211. inline BOOL CCriticalSection::Lock(DWORD /* dwTimeout */)
  212. { return Lock(); }
  213. inline BOOL CCriticalSection::Unlock()
  214. { ::LeaveCriticalSection(&m_sect); return TRUE; }
  215. inline CStaticCriticalSection::CStaticCriticalSection() : CSyncObject(NULL)
  216. {
  217. if (::InitializeCriticalSectionAndSpinCount(&m_sect,0))
  218. {
  219. validCS = true;
  220. }
  221. else
  222. {
  223. validCS = false;
  224. RecordFailure();
  225. }
  226. }
  227. inline CStaticCriticalSection::~CStaticCriticalSection()
  228. { if (validCS) ::DeleteCriticalSection(&m_sect); }
  229. inline CStaticCriticalSection::operator CRITICAL_SECTION*()
  230. { return (CRITICAL_SECTION*) &m_sect; }
  231. inline BOOL CStaticCriticalSection::Lock()
  232. {
  233. if (validCS)
  234. {
  235. ::EnterCriticalSection(&m_sect); return TRUE;
  236. }
  237. else
  238. return FALSE;
  239. }
  240. inline BOOL CStaticCriticalSection::Lock(DWORD /* dwTimeout */)
  241. { return Lock(); }
  242. inline BOOL CStaticCriticalSection::Unlock()
  243. {
  244. if (validCS)
  245. {
  246. ::LeaveCriticalSection(&m_sect); return TRUE;
  247. }
  248. else
  249. return FALSE;
  250. }
  251. #endif // __AFXMT_H__
  252. /////////////////////////////////////////////////////////////////////////////