reader_writer_lock.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "internal/_deprecated_header_message_guard.h"
  14. #if !defined(__TBB_show_deprecation_message_reader_writer_lock_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_reader_writer_lock_H
  16. #pragma message("TBB Warning: tbb/reader_writer_lock.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
  17. #endif
  18. #if defined(__TBB_show_deprecated_header_message)
  19. #undef __TBB_show_deprecated_header_message
  20. #endif
  21. #ifndef __TBB_reader_writer_lock_H
  22. #define __TBB_reader_writer_lock_H
  23. #define __TBB_reader_writer_lock_H_include_area
  24. #include "internal/_warning_suppress_enable_notice.h"
  25. #include "tbb_thread.h"
  26. #include "tbb_allocator.h"
  27. #include "atomic.h"
  28. namespace tbb {
  29. namespace interface5 {
  30. //! Writer-preference reader-writer lock with local-only spinning on readers.
  31. /** Loosely adapted from Mellor-Crummey and Scott pseudocode at
  32. http://www.cs.rochester.edu/research/synchronization/pseudocode/rw.html#s_wp
  33. @ingroup synchronization */
  34. class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::reader_writer_lock is deprecated, use std::shared_mutex")
  35. reader_writer_lock : tbb::internal::no_copy {
  36. public:
  37. friend class scoped_lock;
  38. friend class scoped_lock_read;
  39. //! Status type for nodes associated with lock instances
  40. /** waiting_nonblocking: the wait state for nonblocking lock
  41. instances; for writes, these transition straight to active
  42. states; for reads, these are unused.
  43. waiting: the start and spin state for all lock instances; these will
  44. transition to active state when appropriate. Non-blocking write locks
  45. transition from this state to waiting_nonblocking immediately.
  46. active: the active state means that the lock instance holds
  47. the lock; it will transition to invalid state during node deletion
  48. invalid: the end state for all nodes; this is set in the
  49. destructor so if we encounter this state, we are looking at
  50. memory that has already been freed
  51. The state diagrams below describe the status transitions.
  52. Single arrows indicate that the thread that owns the node is
  53. responsible for the transition; double arrows indicate that
  54. any thread could make the transition.
  55. State diagram for scoped_lock status:
  56. waiting ----------> waiting_nonblocking
  57. | _____________/ |
  58. V V V
  59. active -----------------> invalid
  60. State diagram for scoped_lock_read status:
  61. waiting
  62. |
  63. V
  64. active ----------------->invalid
  65. */
  66. enum status_t { waiting_nonblocking, waiting, active, invalid };
  67. //! Constructs a new reader_writer_lock
  68. reader_writer_lock() {
  69. internal_construct();
  70. }
  71. //! Destructs a reader_writer_lock object
  72. ~reader_writer_lock() {
  73. internal_destroy();
  74. }
  75. //! The scoped lock pattern for write locks
  76. /** Scoped locks help avoid the common problem of forgetting to release the lock.
  77. This type also serves as the node for queuing locks. */
  78. class scoped_lock : tbb::internal::no_copy {
  79. public:
  80. friend class reader_writer_lock;
  81. //! Construct with blocking attempt to acquire write lock on the passed-in lock
  82. scoped_lock(reader_writer_lock& lock) {
  83. internal_construct(lock);
  84. }
  85. //! Destructor, releases the write lock
  86. ~scoped_lock() {
  87. internal_destroy();
  88. }
  89. void* operator new(size_t s) {
  90. return tbb::internal::allocate_via_handler_v3(s);
  91. }
  92. void operator delete(void* p) {
  93. tbb::internal::deallocate_via_handler_v3(p);
  94. }
  95. private:
  96. //! The pointer to the mutex to lock
  97. reader_writer_lock *mutex;
  98. //! The next queued competitor for the mutex
  99. scoped_lock* next;
  100. //! Status flag of the thread associated with this node
  101. atomic<status_t> status;
  102. //! Construct scoped_lock that is not holding lock
  103. scoped_lock();
  104. void __TBB_EXPORTED_METHOD internal_construct(reader_writer_lock&);
  105. void __TBB_EXPORTED_METHOD internal_destroy();
  106. };
  107. //! The scoped lock pattern for read locks
  108. class scoped_lock_read : tbb::internal::no_copy {
  109. public:
  110. friend class reader_writer_lock;
  111. //! Construct with blocking attempt to acquire read lock on the passed-in lock
  112. scoped_lock_read(reader_writer_lock& lock) {
  113. internal_construct(lock);
  114. }
  115. //! Destructor, releases the read lock
  116. ~scoped_lock_read() {
  117. internal_destroy();
  118. }
  119. void* operator new(size_t s) {
  120. return tbb::internal::allocate_via_handler_v3(s);
  121. }
  122. void operator delete(void* p) {
  123. tbb::internal::deallocate_via_handler_v3(p);
  124. }
  125. private:
  126. //! The pointer to the mutex to lock
  127. reader_writer_lock *mutex;
  128. //! The next queued competitor for the mutex
  129. scoped_lock_read *next;
  130. //! Status flag of the thread associated with this node
  131. atomic<status_t> status;
  132. //! Construct scoped_lock_read that is not holding lock
  133. scoped_lock_read();
  134. void __TBB_EXPORTED_METHOD internal_construct(reader_writer_lock&);
  135. void __TBB_EXPORTED_METHOD internal_destroy();
  136. };
  137. //! Acquires the reader_writer_lock for write.
  138. /** If the lock is currently held in write mode by another
  139. context, the writer will block by spinning on a local
  140. variable. Exceptions thrown: improper_lock The context tries
  141. to acquire a reader_writer_lock that it already has write
  142. ownership of.*/
  143. void __TBB_EXPORTED_METHOD lock();
  144. //! Tries to acquire the reader_writer_lock for write.
  145. /** This function does not block. Return Value: True or false,
  146. depending on whether the lock is acquired or not. If the lock
  147. is already held by this acquiring context, try_lock() returns
  148. false. */
  149. bool __TBB_EXPORTED_METHOD try_lock();
  150. //! Acquires the reader_writer_lock for read.
  151. /** If the lock is currently held by a writer, this reader will
  152. block and wait until the writers are done. Exceptions thrown:
  153. improper_lock The context tries to acquire a
  154. reader_writer_lock that it already has write ownership of. */
  155. void __TBB_EXPORTED_METHOD lock_read();
  156. //! Tries to acquire the reader_writer_lock for read.
  157. /** This function does not block. Return Value: True or false,
  158. depending on whether the lock is acquired or not. */
  159. bool __TBB_EXPORTED_METHOD try_lock_read();
  160. //! Releases the reader_writer_lock
  161. void __TBB_EXPORTED_METHOD unlock();
  162. private:
  163. void __TBB_EXPORTED_METHOD internal_construct();
  164. void __TBB_EXPORTED_METHOD internal_destroy();
  165. //! Attempts to acquire write lock
  166. /** If unavailable, spins in blocking case, returns false in non-blocking case. */
  167. bool start_write(scoped_lock *);
  168. //! Sets writer_head to w and attempts to unblock
  169. void set_next_writer(scoped_lock *w);
  170. //! Relinquishes write lock to next waiting writer or group of readers
  171. void end_write(scoped_lock *);
  172. //! Checks if current thread holds write lock
  173. bool is_current_writer();
  174. //! Attempts to acquire read lock
  175. /** If unavailable, spins in blocking case, returns false in non-blocking case. */
  176. void start_read(scoped_lock_read *);
  177. //! Unblocks pending readers
  178. void unblock_readers();
  179. //! Relinquishes read lock by decrementing counter; last reader wakes pending writer
  180. void end_read();
  181. //! The list of pending readers
  182. atomic<scoped_lock_read*> reader_head;
  183. //! The list of pending writers
  184. atomic<scoped_lock*> writer_head;
  185. //! The last node in the list of pending writers
  186. atomic<scoped_lock*> writer_tail;
  187. //! Writer that owns the mutex; tbb_thread::id() otherwise.
  188. tbb_thread::id my_current_writer;
  189. //! Status of mutex
  190. atomic<uintptr_t> rdr_count_and_flags; // used with __TBB_AtomicOR, which assumes uintptr_t
  191. };
  192. } // namespace interface5
  193. using interface5::reader_writer_lock;
  194. } // namespace tbb
  195. #include "internal/_warning_suppress_disable_notice.h"
  196. #undef __TBB_reader_writer_lock_H_include_area
  197. #endif /* __TBB_reader_writer_lock_H */