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.

115 lines
2.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: mmcmt.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. /*
  11. mmcmt.cpp
  12. Implementation of thread synchronization classes
  13. */
  14. #include "stdafx.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CSyncObject
  17. CSyncObject::CSyncObject() :
  18. m_hObject( NULL )
  19. {
  20. }
  21. CSyncObject::~CSyncObject()
  22. {
  23. if( m_hObject != NULL )
  24. {
  25. ::CloseHandle( m_hObject );
  26. m_hObject = NULL;
  27. }
  28. }
  29. BOOL CSyncObject::Lock( DWORD dwTimeout )
  30. {
  31. // this is a band-aid fix. Whis locking architecture is not working at all.
  32. // Raid #374770 ( Windows Bugs ntraid9 4/23/2001 )
  33. // fixes need to be made to:
  34. // a) remove m_hObject member from this class
  35. // b) remove CMutex - not used anywhere
  36. // c) make Lock a pure virtual method and require everyone to override it.
  37. // d) remove this locking from context menu - it is not needed there
  38. if( m_hObject && ::WaitForSingleObject( m_hObject, dwTimeout) == WAIT_OBJECT_0 )
  39. return TRUE;
  40. else
  41. return FALSE;
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CMutex
  45. CMutex::CMutex( BOOL bInitiallyOwn ) :
  46. CSyncObject()
  47. {
  48. m_hObject = ::CreateMutex( NULL, bInitiallyOwn, NULL );
  49. ASSERT( m_hObject );
  50. }
  51. BOOL CMutex::Unlock()
  52. {
  53. return ::ReleaseMutex( m_hObject );
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CSingleLock
  57. CSingleLock::CSingleLock( CSyncObject* pObject, BOOL bInitialLock )
  58. {
  59. ASSERT( pObject != NULL );
  60. m_pObject = pObject;
  61. m_hObject = *pObject;
  62. m_bAcquired = FALSE;
  63. if (bInitialLock)
  64. Lock();
  65. }
  66. BOOL CSingleLock::Lock( DWORD dwTimeOut )
  67. {
  68. ASSERT( m_pObject != NULL || m_hObject != NULL );
  69. ASSERT( !m_bAcquired );
  70. m_bAcquired = m_pObject->Lock( dwTimeOut );
  71. return m_bAcquired;
  72. }
  73. BOOL CSingleLock::Unlock()
  74. {
  75. ASSERT( m_pObject != NULL );
  76. if (m_bAcquired)
  77. m_bAcquired = !m_pObject->Unlock();
  78. // successfully unlocking means it isn't acquired
  79. return !m_bAcquired;
  80. }