RWLock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // RWLock.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/RWLock.h#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: RWLock
  9. //
  10. // Definition of the RWLock class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_RWLock_INCLUDED
  18. #define Foundation_RWLock_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #if defined(POCO_OS_FAMILY_WINDOWS)
  22. #if defined(_WIN32_WCE)
  23. #include "Poco/RWLock_WINCE.h"
  24. #else
  25. #include "Poco/RWLock_WIN32.h"
  26. #endif
  27. #elif defined(POCO_ANDROID)
  28. #include "Poco/RWLock_Android.h"
  29. #elif defined(POCO_VXWORKS)
  30. #include "Poco/RWLock_VX.h"
  31. #else
  32. #include "Poco/RWLock_POSIX.h"
  33. #endif
  34. namespace Poco {
  35. class ScopedRWLock;
  36. class ScopedReadRWLock;
  37. class ScopedWriteRWLock;
  38. class Foundation_API RWLock: private RWLockImpl
  39. /// A reader writer lock allows multiple concurrent
  40. /// readers or one exclusive writer.
  41. {
  42. public:
  43. typedef ScopedRWLock ScopedLock;
  44. typedef ScopedReadRWLock ScopedReadLock;
  45. typedef ScopedWriteRWLock ScopedWriteLock;
  46. RWLock();
  47. /// Creates the Reader/Writer lock.
  48. ~RWLock();
  49. /// Destroys the Reader/Writer lock.
  50. void readLock();
  51. /// Acquires a read lock. If another thread currently holds a write lock,
  52. /// waits until the write lock is released.
  53. bool tryReadLock();
  54. /// Tries to acquire a read lock. Immediately returns true if successful, or
  55. /// false if another thread currently holds a write lock.
  56. void writeLock();
  57. /// Acquires a write lock. If one or more other threads currently hold
  58. /// locks, waits until all locks are released. The results are undefined
  59. /// if the same thread already holds a read or write lock
  60. bool tryWriteLock();
  61. /// Tries to acquire a write lock. Immediately returns true if successful,
  62. /// or false if one or more other threads currently hold
  63. /// locks. The result is undefined if the same thread already
  64. /// holds a read or write lock.
  65. void unlock();
  66. /// Releases the read or write lock.
  67. private:
  68. RWLock(const RWLock&);
  69. RWLock& operator = (const RWLock&);
  70. };
  71. class Foundation_API ScopedRWLock
  72. /// A variant of ScopedLock for reader/writer locks.
  73. {
  74. public:
  75. ScopedRWLock(RWLock& rwl, bool write = false);
  76. ~ScopedRWLock();
  77. private:
  78. RWLock& _rwl;
  79. ScopedRWLock();
  80. ScopedRWLock(const ScopedRWLock&);
  81. ScopedRWLock& operator = (const ScopedRWLock&);
  82. };
  83. class Foundation_API ScopedReadRWLock : public ScopedRWLock
  84. /// A variant of ScopedLock for reader locks.
  85. {
  86. public:
  87. ScopedReadRWLock(RWLock& rwl);
  88. ~ScopedReadRWLock();
  89. };
  90. class Foundation_API ScopedWriteRWLock : public ScopedRWLock
  91. /// A variant of ScopedLock for writer locks.
  92. {
  93. public:
  94. ScopedWriteRWLock(RWLock& rwl);
  95. ~ScopedWriteRWLock();
  96. };
  97. //
  98. // inlines
  99. //
  100. inline void RWLock::readLock()
  101. {
  102. readLockImpl();
  103. }
  104. inline bool RWLock::tryReadLock()
  105. {
  106. return tryReadLockImpl();
  107. }
  108. inline void RWLock::writeLock()
  109. {
  110. writeLockImpl();
  111. }
  112. inline bool RWLock::tryWriteLock()
  113. {
  114. return tryWriteLockImpl();
  115. }
  116. inline void RWLock::unlock()
  117. {
  118. unlockImpl();
  119. }
  120. inline ScopedRWLock::ScopedRWLock(RWLock& rwl, bool write): _rwl(rwl)
  121. {
  122. if (write)
  123. _rwl.writeLock();
  124. else
  125. _rwl.readLock();
  126. }
  127. inline ScopedRWLock::~ScopedRWLock()
  128. {
  129. try
  130. {
  131. _rwl.unlock();
  132. }
  133. catch (...)
  134. {
  135. poco_unexpected();
  136. }
  137. }
  138. inline ScopedReadRWLock::ScopedReadRWLock(RWLock& rwl): ScopedRWLock(rwl, false)
  139. {
  140. }
  141. inline ScopedReadRWLock::~ScopedReadRWLock()
  142. {
  143. }
  144. inline ScopedWriteRWLock::ScopedWriteRWLock(RWLock& rwl): ScopedRWLock(rwl, true)
  145. {
  146. }
  147. inline ScopedWriteRWLock::~ScopedWriteRWLock()
  148. {
  149. }
  150. } // namespace Poco
  151. #endif // Foundation_RWLock_INCLUDED