Synchronization.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Windows/Synchronization.h
  2. #ifndef __WINDOWS_SYNCHRONIZATION_H
  3. #define __WINDOWS_SYNCHRONIZATION_H
  4. #include "Defs.h"
  5. extern "C"
  6. {
  7. #include "../../C/Threads.h"
  8. }
  9. #ifdef _WIN32
  10. #include "Handle.h"
  11. #endif
  12. namespace NWindows {
  13. namespace NSynchronization {
  14. class CBaseEvent
  15. {
  16. protected:
  17. ::CEvent _object;
  18. public:
  19. bool IsCreated() { return Event_IsCreated(&_object) != 0; }
  20. operator HANDLE() { return _object.handle; }
  21. CBaseEvent() { Event_Construct(&_object); }
  22. ~CBaseEvent() { Close(); }
  23. HRes Close() { return Event_Close(&_object); }
  24. #ifdef _WIN32
  25. HRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL,
  26. LPSECURITY_ATTRIBUTES securityAttributes = NULL)
  27. {
  28. _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset),
  29. BoolToBOOL(initiallyOwn), name);
  30. if (_object.handle != 0)
  31. return 0;
  32. return ::GetLastError();
  33. }
  34. HRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
  35. {
  36. _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name);
  37. if (_object.handle != 0)
  38. return 0;
  39. return ::GetLastError();
  40. }
  41. #endif
  42. HRes Set() { return Event_Set(&_object); }
  43. // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); }
  44. HRes Reset() { return Event_Reset(&_object); }
  45. HRes Lock() { return Event_Wait(&_object); }
  46. };
  47. class CManualResetEvent: public CBaseEvent
  48. {
  49. public:
  50. HRes Create(bool initiallyOwn = false)
  51. {
  52. return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0);
  53. }
  54. HRes CreateIfNotCreated()
  55. {
  56. if (IsCreated())
  57. return 0;
  58. return ManualResetEvent_CreateNotSignaled(&_object);
  59. }
  60. #ifdef _WIN32
  61. HRes CreateWithName(bool initiallyOwn, LPCTSTR name)
  62. {
  63. return CBaseEvent::Create(true, initiallyOwn, name);
  64. }
  65. #endif
  66. };
  67. class CAutoResetEvent: public CBaseEvent
  68. {
  69. public:
  70. HRes Create()
  71. {
  72. return AutoResetEvent_CreateNotSignaled(&_object);
  73. }
  74. HRes CreateIfNotCreated()
  75. {
  76. if (IsCreated())
  77. return 0;
  78. return AutoResetEvent_CreateNotSignaled(&_object);
  79. }
  80. };
  81. #ifdef _WIN32
  82. class CObject: public CHandle
  83. {
  84. public:
  85. HRes Lock(DWORD timeoutInterval = INFINITE)
  86. { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); }
  87. };
  88. class CMutex: public CObject
  89. {
  90. public:
  91. HRes Create(bool initiallyOwn, LPCTSTR name = NULL,
  92. LPSECURITY_ATTRIBUTES securityAttributes = NULL)
  93. {
  94. _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name);
  95. if (_handle != 0)
  96. return 0;
  97. return ::GetLastError();
  98. }
  99. HRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name)
  100. {
  101. _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name);
  102. if (_handle != 0)
  103. return 0;
  104. return ::GetLastError();
  105. }
  106. HRes Release()
  107. {
  108. return ::ReleaseMutex(_handle) ? 0 : ::GetLastError();
  109. }
  110. };
  111. class CMutexLock
  112. {
  113. CMutex *_object;
  114. public:
  115. CMutexLock(CMutex &object): _object(&object) { _object->Lock(); }
  116. ~CMutexLock() { _object->Release(); }
  117. };
  118. #endif
  119. class CSemaphore
  120. {
  121. ::CSemaphore _object;
  122. public:
  123. CSemaphore() { Semaphore_Construct(&_object); }
  124. ~CSemaphore() { Close(); }
  125. HRes Close() { return Semaphore_Close(&_object); }
  126. operator HANDLE() { return _object.handle; }
  127. HRes Create(UInt32 initiallyCount, UInt32 maxCount)
  128. {
  129. return Semaphore_Create(&_object, initiallyCount, maxCount);
  130. }
  131. HRes Release() { return Semaphore_Release1(&_object); }
  132. HRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); }
  133. HRes Lock() { return Semaphore_Wait(&_object); }
  134. };
  135. class CCriticalSection
  136. {
  137. ::CCriticalSection _object;
  138. public:
  139. CCriticalSection() { CriticalSection_Init(&_object); }
  140. ~CCriticalSection() { CriticalSection_Delete(&_object); }
  141. void Enter() { CriticalSection_Enter(&_object); }
  142. void Leave() { CriticalSection_Leave(&_object); }
  143. };
  144. class CCriticalSectionLock
  145. {
  146. CCriticalSection *_object;
  147. void Unlock() { _object->Leave(); }
  148. public:
  149. CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); }
  150. ~CCriticalSectionLock() { Unlock(); }
  151. };
  152. }}
  153. #endif