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;
46 bool __TBB_EXPORTED_METHOD internal_acquire_writer();
50 void __TBB_EXPORTED_METHOD internal_release_writer();
53 void __TBB_EXPORTED_METHOD internal_acquire_reader();
56 bool __TBB_EXPORTED_METHOD internal_upgrade();
60 void __TBB_EXPORTED_METHOD internal_downgrade();
63 void __TBB_EXPORTED_METHOD internal_release_reader();
66 bool __TBB_EXPORTED_METHOD internal_try_acquire_writer();
69 bool __TBB_EXPORTED_METHOD internal_try_acquire_reader();
75 #if TBB_USE_THREADING_TOOLS 83 __TBB_ASSERT( !state,
"destruction of an acquired mutex");
91 #if __TBB_TSX_AVAILABLE 92 friend class tbb::interface8::internal::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; }
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();
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);
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));
159 __TBB_ASSERT( !
mutex,
"holding mutex already" );
162 result = write? m.internal_try_acquire_writer()
163 : m.internal_try_acquire_reader();
180 static const bool is_rw_mutex =
true;
181 static const bool is_recursive_mutex =
false;
182 static const bool is_fair_mutex =
false;
187 void lock() {internal_acquire_writer();}
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);
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;
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 {
249 #if __TBB_TSX_AVAILABLE bool upgrade_to_writer()
Upgrade reader to become a writer.
Definition: spin_rw_mutex.h:123
~scoped_lock()
Release lock (if lock is held).
Definition: spin_rw_mutex.h:108
void lock_read()
Acquire reader lock.
Definition: spin_rw_mutex.h:207
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
Definition: spin_rw_mutex.h:172
void unlock()
Release lock.
Definition: spin_rw_mutex.h:194
Fast, unfair, spinning reader-writer lock with backoff and writer-preference.
Definition: spin_rw_mutex.h:42
Acquire.
Definition: atomic.h:47
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: spin_rw_mutex.h:100
interface7::internal::padded_mutex< tbb::spin_rw_mutex, true > speculative_spin_rw_mutex
A cross-platform spin reader/writer mutex with speculative lock acquisition.
Definition: spin_rw_mutex.h:252
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock...
Definition: spin_rw_mutex.h:176
spin_rw_mutex_v3()
Construct unacquired mutex.
Definition: spin_rw_mutex.h:74
The scoped locking pattern.
Definition: spin_rw_mutex.h:90
void acquire(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: spin_rw_mutex.h:113
void lock()
Acquire writer lock.
Definition: spin_rw_mutex.h:187
bool try_lock()
Try acquiring writer lock (non-blocking)
Definition: spin_rw_mutex.h:191
Definition: _mutex_padding.h:36
void release()
Release lock.
Definition: spin_rw_mutex.h:131
Definition: _flow_graph_async_msg_impl.h:32
Release.
Definition: atomic.h:49
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
state_t state
State of lock.
Definition: spin_rw_mutex.h:224
scoped_lock(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: spin_rw_mutex.h:103
bool try_acquire(spin_rw_mutex &m, bool write=true)
Try acquire lock on given mutex.
Definition: spin_rw_mutex.h:158
bool try_lock_read()
Try acquiring reader lock (non-blocking)
Definition: spin_rw_mutex.h:211
bool downgrade_to_reader()
Downgrade writer to become a reader.
Definition: spin_rw_mutex.h:145
Wrapper around the platform's native reader-writer lock.
Definition: mutex.h:40