Source code of Windows XP (NT5)
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.

322 lines
5.7 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLSYNC_INL__
  11. #define __ATLSYNC_INL__
  12. #pragma once
  13. #ifndef __ATLSYNC_H__
  14. #error atlsync.inl requires atlsync.h to be included first
  15. #endif
  16. namespace ATL
  17. {
  18. inline CCriticalSection::CCriticalSection()
  19. {
  20. __try
  21. {
  22. ::InitializeCriticalSection( this );
  23. }
  24. __except( EXCEPTION_EXECUTE_HANDLER )
  25. {
  26. AtlThrow( E_OUTOFMEMORY );
  27. }
  28. }
  29. #if (_WIN32_WINNT >= 0x0403)
  30. inline CCriticalSection::CCriticalSection( ULONG nSpinCount )
  31. {
  32. __try
  33. {
  34. ::InitializeCriticalSectionAndSpinCount( this, nSpinCount );
  35. }
  36. __except( EXCEPTION_EXECUTE_HANDLER )
  37. {
  38. AtlThrow( E_OUTOFMEMORY );
  39. }
  40. }
  41. #endif
  42. inline CCriticalSection::~CCriticalSection()
  43. {
  44. ::DeleteCriticalSection( this );
  45. }
  46. inline void CCriticalSection::Enter()
  47. {
  48. __try
  49. {
  50. ::EnterCriticalSection( this );
  51. }
  52. __except( EXCEPTION_EXECUTE_HANDLER )
  53. {
  54. AtlThrow( E_OUTOFMEMORY );
  55. }
  56. }
  57. inline void CCriticalSection::Leave()
  58. {
  59. ::LeaveCriticalSection( this );
  60. }
  61. #if (_WIN32_WINNT >= 0x0403)
  62. inline ULONG CCriticalSection::SetSpinCount( ULONG nSpinCount )
  63. {
  64. return( ::SetCriticalSectionSpinCount( this, nSpinCount ) );
  65. }
  66. #endif
  67. #if(_WIN32_WINNT >= 0x0400)
  68. inline BOOL CCriticalSection::TryEnter()
  69. {
  70. return( ::TryEnterCriticalSection( this ) );
  71. }
  72. #endif
  73. inline CEvent::CEvent()
  74. {
  75. }
  76. inline CEvent::CEvent( CEvent& hEvent ) :
  77. CHandle( hEvent )
  78. {
  79. }
  80. inline CEvent::CEvent( BOOL bManualReset, BOOL bInitialState )
  81. {
  82. BOOL bSuccess;
  83. bSuccess = Create( NULL, bManualReset, bInitialState, NULL );
  84. if( !bSuccess )
  85. {
  86. AtlThrowLastWin32();
  87. }
  88. }
  89. inline CEvent::CEvent( LPSECURITY_ATTRIBUTES pAttributes, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName )
  90. {
  91. BOOL bSuccess;
  92. bSuccess = Create( pAttributes, bManualReset, bInitialState, pszName );
  93. if( !bSuccess )
  94. {
  95. AtlThrowLastWin32();
  96. }
  97. }
  98. inline CEvent::CEvent( HANDLE h ) :
  99. CHandle( h )
  100. {
  101. }
  102. inline BOOL CEvent::Create( LPSECURITY_ATTRIBUTES pSecurity, BOOL bManualReset, BOOL bInitialState, LPCTSTR pszName )
  103. {
  104. ATLASSERT( m_h == NULL );
  105. m_h = ::CreateEvent( pSecurity, bManualReset, bInitialState, pszName );
  106. return( m_h != NULL );
  107. }
  108. inline BOOL CEvent::Open( DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName )
  109. {
  110. ATLASSERT( m_h == NULL );
  111. m_h = ::OpenEvent( dwAccess, bInheritHandle, pszName );
  112. return( m_h != NULL );
  113. }
  114. inline BOOL CEvent::Pulse()
  115. {
  116. ATLASSERT( m_h != NULL );
  117. return( ::PulseEvent( m_h ) );
  118. }
  119. inline BOOL CEvent::Reset()
  120. {
  121. ATLASSERT( m_h != NULL );
  122. return( ::ResetEvent( m_h ) );
  123. }
  124. inline BOOL CEvent::Set()
  125. {
  126. ATLASSERT( m_h != NULL );
  127. return( ::SetEvent( m_h ) );
  128. }
  129. inline CMutex::CMutex()
  130. {
  131. }
  132. inline CMutex::CMutex( CMutex& hMutex ) :
  133. CHandle( hMutex )
  134. {
  135. }
  136. inline CMutex::CMutex( BOOL bInitialOwner )
  137. {
  138. BOOL bSuccess;
  139. bSuccess = Create( NULL, bInitialOwner, NULL );
  140. if( !bSuccess )
  141. {
  142. AtlThrowLastWin32();
  143. }
  144. }
  145. inline CMutex::CMutex( LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName )
  146. {
  147. BOOL bSuccess;
  148. bSuccess = Create( pSecurity, bInitialOwner, pszName );
  149. if( !bSuccess )
  150. {
  151. AtlThrowLastWin32();
  152. }
  153. }
  154. inline CMutex::CMutex( HANDLE h ) :
  155. CHandle( h )
  156. {
  157. }
  158. inline BOOL CMutex::Create( LPSECURITY_ATTRIBUTES pSecurity, BOOL bInitialOwner, LPCTSTR pszName )
  159. {
  160. ATLASSERT( m_h == NULL );
  161. m_h = ::CreateMutex( pSecurity, bInitialOwner, pszName );
  162. return( m_h != NULL );
  163. }
  164. inline BOOL CMutex::Open( DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName )
  165. {
  166. ATLASSERT( m_h == NULL );
  167. m_h = ::OpenMutex( dwAccess, bInheritHandle, pszName );
  168. return( m_h != NULL );
  169. }
  170. inline BOOL CMutex::Release()
  171. {
  172. ATLASSERT( m_h != NULL );
  173. return( ::ReleaseMutex( m_h ) );
  174. }
  175. inline CSemaphore::CSemaphore()
  176. {
  177. }
  178. inline CSemaphore::CSemaphore( CSemaphore& hSemaphore ) :
  179. CHandle( hSemaphore )
  180. {
  181. }
  182. inline CSemaphore::CSemaphore( LONG nInitialCount, LONG nMaxCount )
  183. {
  184. BOOL bSuccess;
  185. bSuccess = Create( NULL, nInitialCount, nMaxCount, NULL );
  186. if( !bSuccess )
  187. {
  188. AtlThrowLastWin32();
  189. }
  190. }
  191. inline CSemaphore::CSemaphore( LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName )
  192. {
  193. BOOL bSuccess;
  194. bSuccess = Create( pSecurity, nInitialCount, nMaxCount, pszName );
  195. if( !bSuccess )
  196. {
  197. AtlThrowLastWin32();
  198. }
  199. }
  200. inline CSemaphore::CSemaphore( HANDLE h ) :
  201. CHandle( h )
  202. {
  203. }
  204. inline BOOL CSemaphore::Create( LPSECURITY_ATTRIBUTES pSecurity, LONG nInitialCount, LONG nMaxCount, LPCTSTR pszName )
  205. {
  206. ATLASSERT( m_h == NULL );
  207. m_h = ::CreateSemaphore( pSecurity, nInitialCount, nMaxCount, pszName );
  208. return( m_h != NULL );
  209. }
  210. inline BOOL CSemaphore::Open( DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName )
  211. {
  212. ATLASSERT( m_h == NULL );
  213. m_h = ::OpenSemaphore( dwAccess, bInheritHandle, pszName );
  214. return( m_h != NULL );
  215. }
  216. inline BOOL CSemaphore::Release( LONG nReleaseCount, LONG* pnOldCount )
  217. {
  218. ATLASSERT( m_h != NULL );
  219. return( ::ReleaseSemaphore( m_h, nReleaseCount, pnOldCount ) );
  220. }
  221. inline CMutexLock::CMutexLock( CMutex& mtx, bool bInitialLock ) :
  222. m_mtx( mtx ),
  223. m_bLocked( false )
  224. {
  225. if( bInitialLock )
  226. {
  227. Lock();
  228. }
  229. }
  230. inline CMutexLock::~CMutexLock()
  231. {
  232. if( m_bLocked )
  233. {
  234. Unlock();
  235. }
  236. }
  237. inline void CMutexLock::Lock()
  238. {
  239. DWORD dwResult;
  240. ATLASSERT( !m_bLocked );
  241. dwResult = ::WaitForSingleObject( m_mtx, INFINITE );
  242. if( dwResult == WAIT_ABANDONED )
  243. {
  244. ATLTRACE(atlTraceSync, 0, _T("Warning: abandoned mutex 0x%x\n"), (HANDLE)m_mtx);
  245. }
  246. m_bLocked = true;
  247. }
  248. inline void CMutexLock::Unlock()
  249. {
  250. ATLASSERT( m_bLocked );
  251. m_mtx.Release();
  252. }
  253. }; // namespace ATL
  254. #endif // __ATLSYNC_INL__