2 Copyright 2005-2014 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5 you can redistribute it and/or modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation. Threading Building Blocks is
7 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 See the GNU General Public License for more details. You should have received a copy of
10 the GNU General Public License along with Threading Building Blocks; if not, write to the
11 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
13 As a special exception, you may use this file as part of a free software library without
14 restriction. Specifically, if other files instantiate templates or use macros or inline
15 functions from this file, or you compile this file and link it with other files to produce
16 an executable, this file does not by itself cause the resulting executable to be covered
17 by the GNU General Public License. This exception does not however invalidate any other
18 reasons why the executable file might be covered by the GNU General Public License.
21 #ifndef __TBB_spin_rw_mutex_H
22 #define __TBB_spin_rw_mutex_H
24 #include "tbb_stddef.h"
25 #include "tbb_machine.h"
26 #include "tbb_profiling.h"
27 #include "internal/_mutex_padding.h"
31 #if __TBB_TSX_AVAILABLE
32 namespace interface8 { namespace internal {
33 class x86_rtm_rw_mutex;
37 class spin_rw_mutex_v3;
38 typedef spin_rw_mutex_v3 spin_rw_mutex;
40 //! Fast, unfair, spinning reader-writer lock with backoff and writer-preference
41 /** @ingroup synchronization */
42 class spin_rw_mutex_v3 : internal::mutex_copy_deprecated_and_disabled {
45 //! Internal acquire write lock.
46 bool __TBB_EXPORTED_METHOD internal_acquire_writer();
48 //! Out of line code for releasing a write lock.
49 /** This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler. */
50 void __TBB_EXPORTED_METHOD internal_release_writer();
52 //! Internal acquire read lock.
53 void __TBB_EXPORTED_METHOD internal_acquire_reader();
55 //! Internal upgrade reader to become a writer.
56 bool __TBB_EXPORTED_METHOD internal_upgrade();
58 //! Out of line code for downgrading a writer to a reader.
59 /** This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler. */
60 void __TBB_EXPORTED_METHOD internal_downgrade();
62 //! Internal release read lock.
63 void __TBB_EXPORTED_METHOD internal_release_reader();
65 //! Internal try_acquire write lock.
66 bool __TBB_EXPORTED_METHOD internal_try_acquire_writer();
68 //! Internal try_acquire read lock.
69 bool __TBB_EXPORTED_METHOD internal_try_acquire_reader();
73 //! Construct unacquired mutex.
74 spin_rw_mutex_v3() : state(0) {
75 #if TBB_USE_THREADING_TOOLS
81 //! Destructor asserts if the mutex is acquired, i.e. state is zero.
83 __TBB_ASSERT( !state, "destruction of an acquired mutex");
85 #endif /* TBB_USE_ASSERT */
87 //! The scoped locking pattern
88 /** It helps to avoid the common problem of forgetting to release lock.
89 It also nicely provides the "node" for queuing locks. */
90 class scoped_lock : internal::no_copy {
91 #if __TBB_TSX_AVAILABLE
92 friend class tbb::interface8::internal::x86_rtm_rw_mutex;
93 // helper methods for x86_rtm_rw_mutex
94 spin_rw_mutex *internal_get_mutex() const { return mutex; }
95 void internal_set_mutex(spin_rw_mutex* m) { mutex = m; }
98 //! Construct lock that has not acquired a mutex.
99 /** Equivalent to zero-initialization of *this. */
100 scoped_lock() : mutex(NULL), is_writer(false) {}
102 //! Acquire lock on given mutex.
103 scoped_lock( spin_rw_mutex& m, bool write = true ) : mutex(NULL) {
107 //! Release lock (if lock is held).
109 if( mutex ) release();
112 //! Acquire lock on given mutex.
113 void acquire( spin_rw_mutex& m, bool write = true ) {
114 __TBB_ASSERT( !mutex, "holding mutex already" );
117 if( write ) mutex->internal_acquire_writer();
118 else mutex->internal_acquire_reader();
121 //! Upgrade reader to become a writer.
122 /** Returns whether the upgrade happened without releasing and re-acquiring the lock */
123 bool upgrade_to_writer() {
124 __TBB_ASSERT( mutex, "lock is not acquired" );
125 __TBB_ASSERT( !is_writer, "not a reader" );
127 return mutex->internal_upgrade();
132 __TBB_ASSERT( mutex, "lock is not acquired" );
133 spin_rw_mutex *m = mutex;
135 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
136 if( is_writer ) m->internal_release_writer();
137 else m->internal_release_reader();
139 if( is_writer ) __TBB_AtomicAND( &m->state, READERS );
140 else __TBB_FetchAndAddWrelease( &m->state, -(intptr_t)ONE_READER);
141 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
144 //! Downgrade writer to become a reader.
145 bool downgrade_to_reader() {
146 __TBB_ASSERT( mutex, "lock is not acquired" );
147 __TBB_ASSERT( is_writer, "not a writer" );
148 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
149 mutex->internal_downgrade();
151 __TBB_FetchAndAddW( &mutex->state, ((intptr_t)ONE_READER-WRITER));
152 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
157 //! Try acquire lock on given mutex.
158 bool try_acquire( spin_rw_mutex& m, bool write = true ) {
159 __TBB_ASSERT( !mutex, "holding mutex already" );
162 result = write? m.internal_try_acquire_writer()
163 : m.internal_try_acquire_reader();
171 //! The pointer to the current mutex that is held, or NULL if no mutex is held.
172 spin_rw_mutex* mutex;
174 //! If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.
175 /** Not defined if not holding a lock. */
180 static const bool is_rw_mutex = true;
181 static const bool is_recursive_mutex = false;
182 static const bool is_fair_mutex = false;
184 // ISO C++0x compatibility methods
186 //! Acquire writer lock
187 void lock() {internal_acquire_writer();}
189 //! Try acquiring writer lock (non-blocking)
190 /** Return true if lock acquired; false otherwise. */
191 bool try_lock() {return internal_try_acquire_writer();}
195 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
196 if( state&WRITER ) internal_release_writer();
197 else internal_release_reader();
199 if( state&WRITER ) __TBB_AtomicAND( &state, READERS );
200 else __TBB_FetchAndAddWrelease( &state, -(intptr_t)ONE_READER);
201 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
204 // Methods for reader locks that resemble ISO C++0x compatibility methods.
206 //! Acquire reader lock
207 void lock_read() {internal_acquire_reader();}
209 //! Try acquiring reader lock (non-blocking)
210 /** Return true if reader lock acquired; false otherwise. */
211 bool try_lock_read() {return internal_try_acquire_reader();}
214 typedef intptr_t state_t;
215 static const state_t WRITER = 1;
216 static const state_t WRITER_PENDING = 2;
217 static const state_t READERS = ~(WRITER | WRITER_PENDING);
218 static const state_t ONE_READER = 4;
219 static const state_t BUSY = WRITER | READERS;
221 /** Bit 0 = writer is holding lock
222 Bit 1 = request by a writer to acquire lock (hint to readers to wait)
223 Bit 2..N = number of readers holding lock */
227 void __TBB_EXPORTED_METHOD internal_construct();
230 __TBB_DEFINE_PROFILING_SET_NAME(spin_rw_mutex)
234 #if __TBB_TSX_AVAILABLE
235 #include "internal/_x86_rtm_rw_mutex_impl.h"
239 namespace interface8 {
240 //! A cross-platform spin reader/writer mutex with speculative lock acquisition.
241 /** On platforms with proper HW support, this lock may speculatively execute
242 its critical sections, using HW mechanisms to detect real data races and
243 ensure atomicity of the critical sections. In particular, it uses
244 Intel(R) Transactional Synchronization Extensions (Intel(R) TSX).
245 Without such HW support, it behaves like a spin_rw_mutex.
246 It should be used for locking short critical sections where the lock is
247 contended but the data it protects are not.
248 @ingroup synchronization */
249 #if __TBB_TSX_AVAILABLE
250 typedef interface7::internal::padded_mutex<tbb::interface8::internal::x86_rtm_rw_mutex,true> speculative_spin_rw_mutex;
252 typedef interface7::internal::padded_mutex<tbb::spin_rw_mutex,true> speculative_spin_rw_mutex;
254 } // namespace interface8
256 using interface8::speculative_spin_rw_mutex;
257 __TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_rw_mutex)
259 #endif /* __TBB_spin_rw_mutex_H */