| 1 | /* |
| 2 | Copyright (c) 2005-2019 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __TBB_spin_rw_mutex_H |
| 18 | #define __TBB_spin_rw_mutex_H |
| 19 | |
| 20 | #include "tbb/tbb_stddef.h" |
| 21 | |
| 22 | namespace tbb { |
| 23 | |
| 24 | //! Fast, unfair, spinning reader-writer lock with backoff and writer-preference |
| 25 | /** @ingroup synchronization */ |
| 26 | class spin_rw_mutex { |
| 27 | //! @cond INTERNAL |
| 28 | |
| 29 | //! Present so that 1.0 headers work with 1.1 dynamic library. |
| 30 | static void __TBB_EXPORTED_FUNC internal_itt_releasing(spin_rw_mutex *); |
| 31 | |
| 32 | //! Internal acquire write lock. |
| 33 | static bool __TBB_EXPORTED_FUNC internal_acquire_writer(spin_rw_mutex *); |
| 34 | |
| 35 | //! Out of line code for releasing a write lock. |
| 36 | /** This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler. */ |
| 37 | static void __TBB_EXPORTED_FUNC internal_release_writer(spin_rw_mutex *); |
| 38 | |
| 39 | //! Internal acquire read lock. |
| 40 | static void __TBB_EXPORTED_FUNC internal_acquire_reader(spin_rw_mutex *); |
| 41 | |
| 42 | //! Internal upgrade reader to become a writer. |
| 43 | static bool __TBB_EXPORTED_FUNC internal_upgrade(spin_rw_mutex *); |
| 44 | |
| 45 | //! Out of line code for downgrading a writer to a reader. |
| 46 | /** This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler. */ |
| 47 | static void __TBB_EXPORTED_FUNC internal_downgrade(spin_rw_mutex *); |
| 48 | |
| 49 | //! Internal release read lock. |
| 50 | static void __TBB_EXPORTED_FUNC internal_release_reader(spin_rw_mutex *); |
| 51 | |
| 52 | //! Internal try_acquire write lock. |
| 53 | static bool __TBB_EXPORTED_FUNC internal_try_acquire_writer(spin_rw_mutex *); |
| 54 | |
| 55 | //! Internal try_acquire read lock. |
| 56 | static bool __TBB_EXPORTED_FUNC internal_try_acquire_reader(spin_rw_mutex *); |
| 57 | |
| 58 | //! @endcond |
| 59 | public: |
| 60 | //! Construct unacquired mutex. |
| 61 | spin_rw_mutex() : state(0) {} |
| 62 | |
| 63 | #if TBB_USE_ASSERT |
| 64 | //! Destructor asserts if the mutex is acquired, i.e. state is zero. |
| 65 | ~spin_rw_mutex() { |
| 66 | __TBB_ASSERT( !state, "destruction of an acquired mutex" ); |
| 67 | }; |
| 68 | #endif /* TBB_USE_ASSERT */ |
| 69 | |
| 70 | //! The scoped locking pattern |
| 71 | /** It helps to avoid the common problem of forgetting to release lock. |
| 72 | It also nicely provides the "node" for queuing locks. */ |
| 73 | class scoped_lock : internal::no_copy { |
| 74 | public: |
| 75 | //! Construct lock that has not acquired a mutex. |
| 76 | /** Equivalent to zero-initialization of *this. */ |
| 77 | scoped_lock() : mutex(NULL) {} |
| 78 | |
| 79 | //! Construct and acquire lock on given mutex. |
| 80 | scoped_lock( spin_rw_mutex& m, bool write = true ) : mutex(NULL) { |
| 81 | acquire(m, write); |
| 82 | } |
| 83 | |
| 84 | //! Release lock (if lock is held). |
| 85 | ~scoped_lock() { |
| 86 | if( mutex ) release(); |
| 87 | } |
| 88 | |
| 89 | //! Acquire lock on given mutex. |
| 90 | void acquire( spin_rw_mutex& m, bool write = true ) { |
| 91 | __TBB_ASSERT( !mutex, "holding mutex already" ); |
| 92 | mutex = &m; |
| 93 | is_writer = write; |
| 94 | if( write ) internal_acquire_writer(mutex); |
| 95 | else internal_acquire_reader(mutex); |
| 96 | } |
| 97 | |
| 98 | //! Upgrade reader to become a writer. |
| 99 | /** Returns whether the upgrade happened without releasing and re-acquiring the lock */ |
| 100 | bool upgrade_to_writer() { |
| 101 | __TBB_ASSERT( mutex, "lock is not acquired" ); |
| 102 | __TBB_ASSERT( !is_writer, "not a reader" ); |
| 103 | is_writer = true; |
| 104 | return internal_upgrade(mutex); |
| 105 | } |
| 106 | |
| 107 | //! Release lock. |
| 108 | void release() { |
| 109 | __TBB_ASSERT( mutex, "lock is not acquired" ); |
| 110 | spin_rw_mutex *m = mutex; |
| 111 | mutex = NULL; |
| 112 | if( is_writer ) { |
| 113 | #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT |
| 114 | internal_release_writer(m); |
| 115 | #else |
| 116 | m->state = 0; |
| 117 | #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */ |
| 118 | } else { |
| 119 | internal_release_reader(m); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | //! Downgrade writer to become a reader. |
| 124 | bool downgrade_to_reader() { |
| 125 | __TBB_ASSERT( mutex, "lock is not acquired" ); |
| 126 | __TBB_ASSERT( is_writer, "not a writer" ); |
| 127 | #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT |
| 128 | internal_downgrade(mutex); |
| 129 | #else |
| 130 | mutex->state = 4; // Bit 2 - reader, 00..00100 |
| 131 | #endif |
| 132 | is_writer = false; |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | //! Try acquire lock on given mutex. |
| 137 | bool try_acquire( spin_rw_mutex& m, bool write = true ) { |
| 138 | __TBB_ASSERT( !mutex, "holding mutex already" ); |
| 139 | bool result; |
| 140 | is_writer = write; |
| 141 | result = write? internal_try_acquire_writer(&m) |
| 142 | : internal_try_acquire_reader(&m); |
| 143 | if( result ) mutex = &m; |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | //! The pointer to the current mutex that is held, or NULL if no mutex is held. |
| 149 | spin_rw_mutex* mutex; |
| 150 | |
| 151 | //! If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock. |
| 152 | /** Not defined if not holding a lock. */ |
| 153 | bool is_writer; |
| 154 | }; |
| 155 | |
| 156 | private: |
| 157 | typedef uintptr_t state_t; |
| 158 | static const state_t WRITER = 1; |
| 159 | static const state_t WRITER_PENDING = 2; |
| 160 | static const state_t READERS = ~(WRITER | WRITER_PENDING); |
| 161 | static const state_t ONE_READER = 4; |
| 162 | static const state_t BUSY = WRITER | READERS; |
| 163 | /** Bit 0 = writer is holding lock |
| 164 | Bit 1 = request by a writer to acquire lock (hint to readers to wait) |
| 165 | Bit 2..N = number of readers holding lock */ |
| 166 | volatile state_t state; |
| 167 | }; |
| 168 | |
| 169 | } // namespace tbb |
| 170 | |
| 171 | #endif /* __TBB_spin_rw_mutex_H */ |
| 172 | |