RWLock_WIN32.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // RWLock_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/RWLock_WIN32.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: RWLock
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/RWLock_WIN32.h"
  16. namespace Poco {
  17. RWLockImpl::RWLockImpl(): _readers(0), _writersWaiting(0), _writers(0)
  18. {
  19. _mutex = CreateMutexW(NULL, FALSE, NULL);
  20. if (_mutex == NULL)
  21. throw SystemException("cannot create reader/writer lock");
  22. _readEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
  23. if (_readEvent == NULL)
  24. throw SystemException("cannot create reader/writer lock");
  25. _writeEvent = CreateEventW(NULL, TRUE, TRUE, NULL);
  26. if (_writeEvent == NULL)
  27. throw SystemException("cannot create reader/writer lock");
  28. }
  29. RWLockImpl::~RWLockImpl()
  30. {
  31. CloseHandle(_mutex);
  32. CloseHandle(_readEvent);
  33. CloseHandle(_writeEvent);
  34. }
  35. inline void RWLockImpl::addWriter()
  36. {
  37. switch (WaitForSingleObject(_mutex, INFINITE))
  38. {
  39. case WAIT_OBJECT_0:
  40. if (++_writersWaiting == 1) ResetEvent(_readEvent);
  41. ReleaseMutex(_mutex);
  42. break;
  43. default:
  44. throw SystemException("cannot lock reader/writer lock");
  45. }
  46. }
  47. inline void RWLockImpl::removeWriter()
  48. {
  49. switch (WaitForSingleObject(_mutex, INFINITE))
  50. {
  51. case WAIT_OBJECT_0:
  52. if (--_writersWaiting == 0 && _writers == 0) SetEvent(_readEvent);
  53. ReleaseMutex(_mutex);
  54. break;
  55. default:
  56. throw SystemException("cannot lock reader/writer lock");
  57. }
  58. }
  59. void RWLockImpl::readLockImpl()
  60. {
  61. HANDLE h[2];
  62. h[0] = _mutex;
  63. h[1] = _readEvent;
  64. switch (WaitForMultipleObjects(2, h, TRUE, INFINITE))
  65. {
  66. case WAIT_OBJECT_0:
  67. case WAIT_OBJECT_0 + 1:
  68. ++_readers;
  69. ResetEvent(_writeEvent);
  70. ReleaseMutex(_mutex);
  71. poco_assert_dbg(_writers == 0);
  72. break;
  73. default:
  74. throw SystemException("cannot lock reader/writer lock");
  75. }
  76. }
  77. bool RWLockImpl::tryReadLockImpl()
  78. {
  79. for (;;)
  80. {
  81. if (_writers != 0 || _writersWaiting != 0)
  82. return false;
  83. DWORD result = tryReadLockOnce();
  84. switch (result)
  85. {
  86. case WAIT_OBJECT_0:
  87. case WAIT_OBJECT_0 + 1:
  88. return true;
  89. case WAIT_TIMEOUT:
  90. continue; // try again
  91. default:
  92. throw SystemException("cannot lock reader/writer lock");
  93. }
  94. }
  95. }
  96. void RWLockImpl::writeLockImpl()
  97. {
  98. addWriter();
  99. HANDLE h[2];
  100. h[0] = _mutex;
  101. h[1] = _writeEvent;
  102. switch (WaitForMultipleObjects(2, h, TRUE, INFINITE))
  103. {
  104. case WAIT_OBJECT_0:
  105. case WAIT_OBJECT_0 + 1:
  106. --_writersWaiting;
  107. ++_readers;
  108. ++_writers;
  109. ResetEvent(_readEvent);
  110. ResetEvent(_writeEvent);
  111. ReleaseMutex(_mutex);
  112. poco_assert_dbg(_writers == 1);
  113. break;
  114. default:
  115. removeWriter();
  116. throw SystemException("cannot lock reader/writer lock");
  117. }
  118. }
  119. bool RWLockImpl::tryWriteLockImpl()
  120. {
  121. addWriter();
  122. HANDLE h[2];
  123. h[0] = _mutex;
  124. h[1] = _writeEvent;
  125. switch (WaitForMultipleObjects(2, h, TRUE, 1))
  126. {
  127. case WAIT_OBJECT_0:
  128. case WAIT_OBJECT_0 + 1:
  129. --_writersWaiting;
  130. ++_readers;
  131. ++_writers;
  132. ResetEvent(_readEvent);
  133. ResetEvent(_writeEvent);
  134. ReleaseMutex(_mutex);
  135. poco_assert_dbg(_writers == 1);
  136. return true;
  137. case WAIT_TIMEOUT:
  138. removeWriter();
  139. return false;
  140. default:
  141. removeWriter();
  142. throw SystemException("cannot lock reader/writer lock");
  143. }
  144. }
  145. void RWLockImpl::unlockImpl()
  146. {
  147. switch (WaitForSingleObject(_mutex, INFINITE))
  148. {
  149. case WAIT_OBJECT_0:
  150. _writers = 0;
  151. if (_writersWaiting == 0) SetEvent(_readEvent);
  152. if (--_readers == 0) SetEvent(_writeEvent);
  153. ReleaseMutex(_mutex);
  154. break;
  155. default:
  156. throw SystemException("cannot unlock reader/writer lock");
  157. }
  158. }
  159. DWORD RWLockImpl::tryReadLockOnce()
  160. {
  161. HANDLE h[2];
  162. h[0] = _mutex;
  163. h[1] = _readEvent;
  164. DWORD result = WaitForMultipleObjects(2, h, TRUE, 1);
  165. switch (result)
  166. {
  167. case WAIT_OBJECT_0:
  168. case WAIT_OBJECT_0 + 1:
  169. ++_readers;
  170. ResetEvent(_writeEvent);
  171. ReleaseMutex(_mutex);
  172. poco_assert_dbg(_writers == 0);
  173. return result;
  174. case WAIT_TIMEOUT:
  175. return result;
  176. default:
  177. throw SystemException("cannot lock reader/writer lock");
  178. }
  179. }
  180. } // namespace Poco