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.

213 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. #include "precomp.h"
  11. #include <provexpt.h>
  12. #include <snmpmt.h>
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CSemaphore
  15. CSemaphore::CSemaphore(LONG lInitialCount, LONG lMaxCount,
  16. LPCTSTR pstrName, LPSECURITY_ATTRIBUTES lpsaAttributes)
  17. : CSyncObject(pstrName)
  18. {
  19. m_hObject = ::CreateSemaphore(lpsaAttributes, lInitialCount, lMaxCount,
  20. pstrName);
  21. }
  22. CSemaphore::~CSemaphore()
  23. {
  24. }
  25. BOOL CSemaphore::Unlock(LONG lCount, LPLONG lpPrevCount /* =NULL */)
  26. {
  27. return ::ReleaseSemaphore(m_hObject, lCount, lpPrevCount);
  28. }
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CMutex
  31. CMutex::CMutex(BOOL bInitiallyOwn, LPCTSTR pstrName,
  32. LPSECURITY_ATTRIBUTES lpsaAttribute /* = NULL */)
  33. : CSyncObject(pstrName)
  34. {
  35. m_hObject = ::CreateMutex(lpsaAttribute, bInitiallyOwn, pstrName);
  36. }
  37. CMutex::~CMutex()
  38. {
  39. }
  40. BOOL CMutex::Unlock()
  41. {
  42. return ::ReleaseMutex(m_hObject);
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CEvent
  46. CEvent::CEvent(BOOL bInitiallyOwn, BOOL bManualReset, LPCTSTR pstrName,
  47. LPSECURITY_ATTRIBUTES lpsaAttribute)
  48. : CSyncObject(pstrName)
  49. {
  50. m_hObject = ::CreateEvent(lpsaAttribute, bManualReset,
  51. bInitiallyOwn, pstrName);
  52. }
  53. CEvent::~CEvent()
  54. {
  55. }
  56. BOOL CEvent::Unlock()
  57. {
  58. return TRUE;
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CSingleLock
  62. CSingleLock::CSingleLock(CSyncObject* pObject, BOOL bInitialLock)
  63. {
  64. m_pObject = pObject;
  65. m_hObject = pObject->m_hObject;
  66. m_bAcquired = FALSE;
  67. if (bInitialLock)
  68. Lock();
  69. }
  70. BOOL CSingleLock::Lock(DWORD dwTimeOut /* = INFINITE */)
  71. {
  72. m_bAcquired = m_pObject->Lock(dwTimeOut);
  73. return m_bAcquired;
  74. }
  75. BOOL CSingleLock::Unlock()
  76. {
  77. if (m_bAcquired)
  78. m_bAcquired = !m_pObject->Unlock();
  79. // successfully unlocking means it isn't acquired
  80. return !m_bAcquired;
  81. }
  82. BOOL CSingleLock::Unlock(LONG lCount, LPLONG lpPrevCount /* = NULL */)
  83. {
  84. if (m_bAcquired)
  85. m_bAcquired = !m_pObject->Unlock(lCount, lpPrevCount);
  86. // successfully unlocking means it isn't acquired
  87. return !m_bAcquired;
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CMultiLock
  91. #define _countof(array) (sizeof(array)/sizeof(array[0]))
  92. CMultiLock::CMultiLock(CSyncObject* pObjects[], DWORD dwCount,
  93. BOOL bInitialLock): m_pHandleArray ( NULL )
  94. {
  95. m_ppObjectArray = pObjects;
  96. m_dwCount = dwCount;
  97. // as an optimization, skip alloacating array if
  98. // we can use a small, predeallocated bunch of handles
  99. if (m_dwCount > _countof(m_hPreallocated))
  100. {
  101. m_pHandleArray = new HANDLE[m_dwCount];
  102. m_bLockedArray = new BOOL[m_dwCount];
  103. }
  104. else
  105. {
  106. m_pHandleArray = m_hPreallocated;
  107. m_bLockedArray = m_bPreallocated;
  108. }
  109. // get list of handles from array of objects passed
  110. for (DWORD i = 0; i <m_dwCount; i++)
  111. {
  112. m_pHandleArray[i] = pObjects[i]->m_hObject;
  113. m_bLockedArray[i] = FALSE;
  114. }
  115. if (bInitialLock)
  116. Lock();
  117. }
  118. CMultiLock::~CMultiLock()
  119. {
  120. Unlock();
  121. if (m_pHandleArray != m_hPreallocated)
  122. {
  123. delete[] m_bLockedArray;
  124. delete[] m_pHandleArray;
  125. }
  126. }
  127. DWORD CMultiLock::Lock(DWORD dwTimeOut /* = INFINITE */,
  128. BOOL bWaitForAll /* = TRUE */, DWORD dwWakeMask /* = 0 */)
  129. {
  130. DWORD dwResult;
  131. if (dwWakeMask == 0)
  132. dwResult = ::WaitForMultipleObjects(m_dwCount,
  133. m_pHandleArray, bWaitForAll, dwTimeOut);
  134. else
  135. dwResult = ::MsgWaitForMultipleObjects(m_dwCount,
  136. m_pHandleArray, bWaitForAll, dwTimeOut, dwWakeMask);
  137. if (dwResult < (WAIT_OBJECT_0 + m_dwCount))
  138. {
  139. if (bWaitForAll)
  140. {
  141. for (DWORD i = 0; i < m_dwCount; i++)
  142. m_bLockedArray[i] = TRUE;
  143. }
  144. else
  145. {
  146. m_bLockedArray[dwResult - WAIT_OBJECT_0] = TRUE;
  147. }
  148. }
  149. return dwResult;
  150. }
  151. BOOL CMultiLock::Unlock()
  152. {
  153. for (DWORD i=0; i < m_dwCount; i++)
  154. {
  155. if (m_bLockedArray[i])
  156. m_bLockedArray[i] = !m_ppObjectArray[i]->Unlock();
  157. }
  158. return TRUE;
  159. }
  160. BOOL CMultiLock::Unlock(LONG lCount, LPLONG lpPrevCount /* =NULL */)
  161. {
  162. BOOL bGotOne = FALSE;
  163. for (DWORD i=0; i < m_dwCount; i++)
  164. {
  165. if (m_bLockedArray[i])
  166. {
  167. CSemaphore* pSemaphore = ( CSemaphore *) m_ppObjectArray[i];
  168. if (pSemaphore != NULL)
  169. {
  170. bGotOne = TRUE;
  171. m_bLockedArray[i] = !m_ppObjectArray[i]->Unlock(lCount, lpPrevCount);
  172. }
  173. }
  174. }
  175. return bGotOne;
  176. }
  177. /////////////////////////////////////////////////////////////////////////////