queuing_rw_mutex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef __TBB_queuing_rw_mutex_H
  14. #define __TBB_queuing_rw_mutex_H
  15. #define __TBB_queuing_rw_mutex_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include <cstring>
  18. #include "atomic.h"
  19. #include "tbb_profiling.h"
  20. namespace tbb {
  21. //! Queuing reader-writer mutex with local-only spinning.
  22. /** Adapted from Krieger, Stumm, et al. pseudocode at
  23. http://www.eecg.toronto.edu/parallel/pubs_abs.html#Krieger_etal_ICPP93
  24. @ingroup synchronization */
  25. class queuing_rw_mutex : internal::mutex_copy_deprecated_and_disabled {
  26. public:
  27. //! Construct unacquired mutex.
  28. queuing_rw_mutex() {
  29. q_tail = NULL;
  30. #if TBB_USE_THREADING_TOOLS
  31. internal_construct();
  32. #endif
  33. }
  34. //! Destructor asserts if the mutex is acquired, i.e. q_tail is non-NULL
  35. ~queuing_rw_mutex() {
  36. #if TBB_USE_ASSERT
  37. __TBB_ASSERT( !q_tail, "destruction of an acquired mutex");
  38. #endif
  39. }
  40. //! The scoped locking pattern
  41. /** It helps to avoid the common problem of forgetting to release lock.
  42. It also nicely provides the "node" for queuing locks. */
  43. class scoped_lock: internal::no_copy {
  44. //! Initialize fields to mean "no lock held".
  45. void initialize() {
  46. my_mutex = NULL;
  47. my_internal_lock = 0;
  48. my_going = 0;
  49. #if TBB_USE_ASSERT
  50. my_state = 0xFF; // Set to invalid state
  51. internal::poison_pointer(my_next);
  52. internal::poison_pointer(my_prev);
  53. #endif /* TBB_USE_ASSERT */
  54. }
  55. public:
  56. //! Construct lock that has not acquired a mutex.
  57. /** Equivalent to zero-initialization of *this. */
  58. scoped_lock() {initialize();}
  59. //! Acquire lock on given mutex.
  60. scoped_lock( queuing_rw_mutex& m, bool write=true ) {
  61. initialize();
  62. acquire(m,write);
  63. }
  64. //! Release lock (if lock is held).
  65. ~scoped_lock() {
  66. if( my_mutex ) release();
  67. }
  68. //! Acquire lock on given mutex.
  69. void acquire( queuing_rw_mutex& m, bool write=true );
  70. //! Acquire lock on given mutex if free (i.e. non-blocking)
  71. bool try_acquire( queuing_rw_mutex& m, bool write=true );
  72. //! Release lock.
  73. void release();
  74. //! Upgrade reader to become a writer.
  75. /** Returns whether the upgrade happened without releasing and re-acquiring the lock */
  76. bool upgrade_to_writer();
  77. //! Downgrade writer to become a reader.
  78. bool downgrade_to_reader();
  79. private:
  80. //! The pointer to the mutex owned, or NULL if not holding a mutex.
  81. queuing_rw_mutex* my_mutex;
  82. //! The pointer to the previous and next competitors for a mutex
  83. scoped_lock *__TBB_atomic my_prev, *__TBB_atomic my_next;
  84. typedef unsigned char state_t;
  85. //! State of the request: reader, writer, active reader, other service states
  86. atomic<state_t> my_state;
  87. //! The local spin-wait variable
  88. /** Corresponds to "spin" in the pseudocode but inverted for the sake of zero-initialization */
  89. unsigned char __TBB_atomic my_going;
  90. //! A tiny internal lock
  91. unsigned char my_internal_lock;
  92. //! Acquire the internal lock
  93. void acquire_internal_lock();
  94. //! Try to acquire the internal lock
  95. /** Returns true if lock was successfully acquired. */
  96. bool try_acquire_internal_lock();
  97. //! Release the internal lock
  98. void release_internal_lock();
  99. //! Wait for internal lock to be released
  100. void wait_for_release_of_internal_lock();
  101. //! A helper function
  102. void unblock_or_wait_on_internal_lock( uintptr_t );
  103. };
  104. void __TBB_EXPORTED_METHOD internal_construct();
  105. // Mutex traits
  106. static const bool is_rw_mutex = true;
  107. static const bool is_recursive_mutex = false;
  108. static const bool is_fair_mutex = true;
  109. private:
  110. //! The last competitor requesting the lock
  111. atomic<scoped_lock*> q_tail;
  112. };
  113. __TBB_DEFINE_PROFILING_SET_NAME(queuing_rw_mutex)
  114. } // namespace tbb
  115. #include "internal/_warning_suppress_disable_notice.h"
  116. #undef __TBB_queuing_rw_mutex_H_include_area
  117. #endif /* __TBB_queuing_rw_mutex_H */