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.

122 lines
2.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: mmcmt.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef __MMCMT_H_
  11. #define __MMCMT_H_
  12. /*
  13. mmcmt.h
  14. Simple thread synchronization classes
  15. */
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CSyncObject
  18. class CSyncObject
  19. {
  20. friend class CMutex;
  21. public:
  22. CSyncObject();
  23. virtual ~CSyncObject();
  24. operator HANDLE() const { return m_hObject; }
  25. virtual BOOL Lock(DWORD dwTimeout = INFINITE);
  26. virtual BOOL Unlock() = 0;
  27. virtual BOOL Unlock(LONG /* lCount */, LPLONG /* lpPrevCount=NULL */) { return TRUE; }
  28. protected:
  29. HANDLE m_hObject;
  30. };
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CMutex
  33. class CMutex : public CSyncObject
  34. {
  35. public:
  36. CMutex(BOOL bInitiallyOwn = FALSE);
  37. BOOL Unlock();
  38. };
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CCriticalSection
  41. class CCriticalSection : public CSyncObject
  42. {
  43. public:
  44. CCriticalSection();
  45. virtual ~CCriticalSection();
  46. operator CRITICAL_SECTION*() { return &m_sect; }
  47. BOOL Lock();
  48. BOOL Unlock();
  49. protected:
  50. CRITICAL_SECTION m_sect;
  51. };
  52. inline CCriticalSection::CCriticalSection() :
  53. CSyncObject()
  54. {
  55. ::InitializeCriticalSection( &m_sect );
  56. }
  57. inline CCriticalSection::~CCriticalSection()
  58. {
  59. ::DeleteCriticalSection( &m_sect );
  60. }
  61. inline BOOL CCriticalSection::Lock()
  62. {
  63. ::EnterCriticalSection( &m_sect );
  64. return TRUE;
  65. }
  66. inline BOOL CCriticalSection::Unlock()
  67. {
  68. ::LeaveCriticalSection( &m_sect );
  69. return TRUE;
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CSingleLock
  73. class CSingleLock
  74. {
  75. public:
  76. CSingleLock(CSyncObject* pObject, BOOL bInitialLock = FALSE);
  77. ~CSingleLock() { Unlock(); }
  78. BOOL Lock(DWORD dwTimeOut = INFINITE);
  79. BOOL Unlock();
  80. BOOL IsLocked() { return m_bAcquired; }
  81. protected:
  82. CSyncObject* m_pObject;
  83. HANDLE m_hObject;
  84. BOOL m_bAcquired;
  85. };
  86. #endif // __MMCMT_H__