Home ⌂Doc Index ◂Up ▴
Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::spin_rw_mutex_v3 Class Reference

Fast, unfair, spinning reader-writer lock with backoff and writer-preference. More...

#include <spin_rw_mutex.h>

Inheritance diagram for tbb::spin_rw_mutex_v3:
Collaboration diagram for tbb::spin_rw_mutex_v3:

Classes

class  scoped_lock
 The scoped locking pattern. More...
 

Public Member Functions

 spin_rw_mutex_v3 ()
 Construct unacquired mutex. More...
 
void lock ()
 Acquire writer lock. More...
 
bool try_lock ()
 Try acquiring writer lock (non-blocking) More...
 
void unlock ()
 Release lock. More...
 
void lock_read ()
 Acquire reader lock. More...
 
bool try_lock_read ()
 Try acquiring reader lock (non-blocking) More...
 

Static Public Attributes

static const bool is_rw_mutex = true
 
static const bool is_recursive_mutex = false
 
static const bool is_fair_mutex = false
 

Protected Types

typedef intptr_t state_t
 

Protected Attributes

state_t state
 State of lock. More...
 

Static Protected Attributes

static const state_t WRITER = 1
 
static const state_t WRITER_PENDING = 2
 
static const state_t READERS = ~(WRITER | WRITER_PENDING)
 
static const state_t ONE_READER = 4
 
static const state_t BUSY = WRITER | READERS
 

Private Member Functions

bool __TBB_EXPORTED_METHOD internal_acquire_writer ()
 Internal acquire write lock. More...
 
void __TBB_EXPORTED_METHOD internal_release_writer ()
 Out of line code for releasing a write lock. More...
 
void __TBB_EXPORTED_METHOD internal_acquire_reader ()
 Internal acquire read lock. More...
 
bool __TBB_EXPORTED_METHOD internal_upgrade ()
 Internal upgrade reader to become a writer. More...
 
void __TBB_EXPORTED_METHOD internal_downgrade ()
 Out of line code for downgrading a writer to a reader. More...
 
void __TBB_EXPORTED_METHOD internal_release_reader ()
 Internal release read lock. More...
 
bool __TBB_EXPORTED_METHOD internal_try_acquire_writer ()
 Internal try_acquire write lock. More...
 
bool __TBB_EXPORTED_METHOD internal_try_acquire_reader ()
 Internal try_acquire read lock. More...
 
void __TBB_EXPORTED_METHOD internal_construct ()
 

Detailed Description

Fast, unfair, spinning reader-writer lock with backoff and writer-preference.

Definition at line 38 of file spin_rw_mutex.h.

Member Typedef Documentation

◆ state_t

typedef intptr_t tbb::spin_rw_mutex_v3::state_t
protected

Definition at line 207 of file spin_rw_mutex.h.

Constructor & Destructor Documentation

◆ spin_rw_mutex_v3()

tbb::spin_rw_mutex_v3::spin_rw_mutex_v3 ( )
inline

Construct unacquired mutex.

Definition at line 70 of file spin_rw_mutex.h.

70  : state(0) {
71 #if TBB_USE_THREADING_TOOLS
73 #endif
74  }
state_t state
State of lock.
void __TBB_EXPORTED_METHOD internal_construct()

References internal_construct().

Here is the call graph for this function:

Member Function Documentation

◆ internal_acquire_reader()

void tbb::spin_rw_mutex_v3::internal_acquire_reader ( )
private

Internal acquire read lock.

Acquire read lock on given mutex.

Definition at line 62 of file spin_rw_mutex.cpp.

63 {
64  ITT_NOTIFY(sync_prepare, this);
65  for( internal::atomic_backoff b;;b.pause() ){
66  state_t s = const_cast<volatile state_t&>(state); // ensure reloading
67  if( !(s & (WRITER|WRITER_PENDING)) ) { // no writer or write requests
68  state_t t = (state_t)__TBB_FetchAndAddW( &state, (intptr_t) ONE_READER );
69  if( !( t&WRITER ))
70  break; // successfully stored increased number of readers
71  // writer got there first, undo the increment
72  __TBB_FetchAndAddW( &state, -(intptr_t)ONE_READER );
73  }
74  }
75 
76  ITT_NOTIFY(sync_acquired, this);
77  __TBB_ASSERT( state & READERS, "invalid state of a read lock: no readers" );
78 }
static const state_t ONE_READER
state_t state
State of lock.
static const state_t WRITER_PENDING
static const state_t WRITER
static const state_t READERS
void const char const char int ITT_FORMAT __itt_group_sync s
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_ASSERT, ITT_NOTIFY, ONE_READER, tbb::internal::atomic_backoff::pause(), READERS, s, state, WRITER, and WRITER_PENDING.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::acquire(), and lock_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_acquire_writer()

bool tbb::spin_rw_mutex_v3::internal_acquire_writer ( )
private

Internal acquire write lock.

Acquire write lock on the given mutex.

Definition at line 37 of file spin_rw_mutex.cpp.

38 {
39  ITT_NOTIFY(sync_prepare, this);
40  for( internal::atomic_backoff backoff;;backoff.pause() ){
41  state_t s = const_cast<volatile state_t&>(state); // ensure reloading
42  if( !(s & BUSY) ) { // no readers, no writers
43  if( CAS(state, WRITER, s)==s )
44  break; // successfully stored writer flag
45  backoff.reset(); // we could be very close to complete op.
46  } else if( !(s & WRITER_PENDING) ) { // no pending writers
48  }
49  }
50  ITT_NOTIFY(sync_acquired, this);
51  return false;
52 }
static T CAS(volatile T &addr, T newv, T oldv)
void __TBB_AtomicOR(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:878
state_t state
State of lock.
static const state_t BUSY
static const state_t WRITER_PENDING
static const state_t WRITER
void const char const char int ITT_FORMAT __itt_group_sync s
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_AtomicOR(), BUSY, tbb::CAS(), ITT_NOTIFY, tbb::internal::atomic_backoff::pause(), s, state, WRITER, and WRITER_PENDING.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::acquire(), internal_upgrade(), and lock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_construct()

void tbb::spin_rw_mutex_v3::internal_construct ( )
private

Definition at line 152 of file spin_rw_mutex.cpp.

152  {
153  ITT_SYNC_CREATE(this, _T("tbb::spin_rw_mutex"), _T(""));
154 }
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:59
#define ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:115

References _T, and ITT_SYNC_CREATE.

Referenced by spin_rw_mutex_v3().

Here is the caller graph for this function:

◆ internal_downgrade()

void tbb::spin_rw_mutex_v3::internal_downgrade ( )
private

Out of line code for downgrading a writer to a reader.

Downgrade writer to a reader.

This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler.

Definition at line 108 of file spin_rw_mutex.cpp.

108  {
109  ITT_NOTIFY(sync_releasing, this);
110  __TBB_FetchAndAddW( &state, (intptr_t)(ONE_READER-WRITER));
111  __TBB_ASSERT( state & READERS, "invalid state after downgrade: no readers" );
112 }
static const state_t ONE_READER
state_t state
State of lock.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p sync_releasing
static const state_t WRITER
static const state_t READERS
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_ASSERT, ITT_NOTIFY, ONE_READER, READERS, state, sync_releasing, and WRITER.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::downgrade_to_reader().

Here is the caller graph for this function:

◆ internal_release_reader()

void tbb::spin_rw_mutex_v3::internal_release_reader ( )
private

Internal release read lock.

Release read lock on the given mutex.

Definition at line 115 of file spin_rw_mutex.cpp.

116 {
117  __TBB_ASSERT( state & READERS, "invalid state of a read lock: no readers" );
118  ITT_NOTIFY(sync_releasing, this); // release reader
120 }
static const state_t ONE_READER
state_t state
State of lock.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p sync_releasing
static const state_t READERS
#define __TBB_FetchAndAddWrelease(P, V)
Definition: tbb_machine.h:309
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_ASSERT, __TBB_FetchAndAddWrelease, ITT_NOTIFY, ONE_READER, READERS, state, and sync_releasing.

Referenced by internal_upgrade(), tbb::spin_rw_mutex_v3::scoped_lock::release(), and unlock().

Here is the caller graph for this function:

◆ internal_release_writer()

void tbb::spin_rw_mutex_v3::internal_release_writer ( )
private

Out of line code for releasing a write lock.

Release writer lock on the given mutex.

This code has debug checking and instrumentation for Intel(R) Thread Checker and Intel(R) Thread Profiler.

Definition at line 55 of file spin_rw_mutex.cpp.

56 {
59 }
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:888
state_t state
State of lock.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p sync_releasing
static const state_t READERS
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_AtomicAND(), ITT_NOTIFY, READERS, state, and sync_releasing.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::release(), and unlock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_try_acquire_reader()

bool tbb::spin_rw_mutex_v3::internal_try_acquire_reader ( )
private

Internal try_acquire read lock.

Try to acquire read lock on the given mutex.

Definition at line 136 of file spin_rw_mutex.cpp.

137 {
138  // for a reader: acquire if no active or waiting writers
139  state_t s = state;
140  if( !(s & (WRITER|WRITER_PENDING)) ) { // no writers
141  state_t t = (state_t)__TBB_FetchAndAddW( &state, (intptr_t) ONE_READER );
142  if( !( t&WRITER )) { // got the lock
143  ITT_NOTIFY(sync_acquired, this);
144  return true; // successfully stored increased number of readers
145  }
146  // writer got there first, undo the increment
147  __TBB_FetchAndAddW( &state, -(intptr_t)ONE_READER );
148  }
149  return false;
150 }
static const state_t ONE_READER
state_t state
State of lock.
static const state_t WRITER_PENDING
static const state_t WRITER
void const char const char int ITT_FORMAT __itt_group_sync s
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References ITT_NOTIFY, ONE_READER, s, state, WRITER, and WRITER_PENDING.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::try_acquire(), and try_lock_read().

Here is the caller graph for this function:

◆ internal_try_acquire_writer()

bool tbb::spin_rw_mutex_v3::internal_try_acquire_writer ( )
private

Internal try_acquire write lock.

Try to acquire write lock on the given mutex.

Definition at line 123 of file spin_rw_mutex.cpp.

124 {
125  // for a writer: only possible to acquire if no active readers or writers
126  state_t s = state;
127  if( !(s & BUSY) ) // no readers, no writers; mask is 1..1101
128  if( CAS(state, WRITER, s)==s ) {
129  ITT_NOTIFY(sync_acquired, this);
130  return true; // successfully stored writer flag
131  }
132  return false;
133 }
static T CAS(volatile T &addr, T newv, T oldv)
state_t state
State of lock.
static const state_t BUSY
static const state_t WRITER
void const char const char int ITT_FORMAT __itt_group_sync s
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References BUSY, tbb::CAS(), ITT_NOTIFY, s, state, and WRITER.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::try_acquire(), and try_lock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_upgrade()

bool tbb::spin_rw_mutex_v3::internal_upgrade ( )
private

Internal upgrade reader to become a writer.

Upgrade reader to become a writer.

Returns whether the upgrade happened without releasing and re-acquiring the lock

Definition at line 82 of file spin_rw_mutex.cpp.

83 {
84  state_t s = state;
85  __TBB_ASSERT( s & READERS, "invalid state before upgrade: no readers " );
86  // check and set writer-pending flag
87  // required conditions: either no pending writers, or we are the only reader
88  // (with multiple readers and pending writer, another upgrade could have been requested)
89  while( (s & READERS)==ONE_READER || !(s & WRITER_PENDING) ) {
90  state_t old_s = s;
91  if( (s=CAS(state, s | WRITER | WRITER_PENDING, s))==old_s ) {
92  ITT_NOTIFY(sync_prepare, this);
93  internal::atomic_backoff backoff;
94  while( (state & READERS) != ONE_READER ) backoff.pause();
95  __TBB_ASSERT((state&(WRITER_PENDING|WRITER))==(WRITER_PENDING|WRITER),"invalid state when upgrading to writer");
96  // both new readers and writers are blocked at this time
97  __TBB_FetchAndAddW( &state, - (intptr_t)(ONE_READER+WRITER_PENDING));
98  ITT_NOTIFY(sync_acquired, this);
99  return true; // successfully upgraded
100  }
101  }
102  // slow reacquire
104  return internal_acquire_writer(); // always returns false
105 }
void __TBB_EXPORTED_METHOD internal_release_reader()
Internal release read lock.
bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire write lock.
static T CAS(volatile T &addr, T newv, T oldv)
static const state_t ONE_READER
state_t state
State of lock.
static const state_t WRITER_PENDING
static const state_t WRITER
static const state_t READERS
void const char const char int ITT_FORMAT __itt_group_sync s
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define ITT_NOTIFY(name, obj)
Definition: itt_notify.h:112

References __TBB_ASSERT, tbb::CAS(), internal_acquire_writer(), internal_release_reader(), ITT_NOTIFY, ONE_READER, tbb::internal::atomic_backoff::pause(), READERS, s, state, WRITER, and WRITER_PENDING.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::upgrade_to_writer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

void tbb::spin_rw_mutex_v3::lock ( )
inline

Acquire writer lock.

Definition at line 180 of file spin_rw_mutex.h.

bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire write lock.

References internal_acquire_writer().

Referenced by tbb::internal::market::adjust_demand(), and tbb::internal::market::try_destroy_arena().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_read()

void tbb::spin_rw_mutex_v3::lock_read ( )
inline

Acquire reader lock.

Definition at line 200 of file spin_rw_mutex.h.

void __TBB_EXPORTED_METHOD internal_acquire_reader()
Internal acquire read lock.

References internal_acquire_reader().

Here is the call graph for this function:

◆ try_lock()

bool tbb::spin_rw_mutex_v3::try_lock ( )
inline

Try acquiring writer lock (non-blocking)

Return true if lock acquired; false otherwise.

Definition at line 184 of file spin_rw_mutex.h.

184 {return internal_try_acquire_writer();}
bool __TBB_EXPORTED_METHOD internal_try_acquire_writer()
Internal try_acquire write lock.

References internal_try_acquire_writer().

Here is the call graph for this function:

◆ try_lock_read()

bool tbb::spin_rw_mutex_v3::try_lock_read ( )
inline

Try acquiring reader lock (non-blocking)

Return true if reader lock acquired; false otherwise.

Definition at line 204 of file spin_rw_mutex.h.

204 {return internal_try_acquire_reader();}
bool __TBB_EXPORTED_METHOD internal_try_acquire_reader()
Internal try_acquire read lock.

References internal_try_acquire_reader().

Here is the call graph for this function:

◆ unlock()

void tbb::spin_rw_mutex_v3::unlock ( )
inline

Release lock.

Definition at line 187 of file spin_rw_mutex.h.

187  {
188 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
191 #else
193  else __TBB_FetchAndAddWrelease( &state, -(intptr_t)ONE_READER);
194 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
195  }
void __TBB_EXPORTED_METHOD internal_release_reader()
Internal release read lock.
static const state_t ONE_READER
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:888
state_t state
State of lock.
static const state_t WRITER
static const state_t READERS
#define __TBB_FetchAndAddWrelease(P, V)
Definition: tbb_machine.h:309
void __TBB_EXPORTED_METHOD internal_release_writer()
Out of line code for releasing a write lock.

References __TBB_AtomicAND(), __TBB_FetchAndAddWrelease, internal_release_reader(), internal_release_writer(), ONE_READER, READERS, state, and WRITER.

Referenced by tbb::internal::market::adjust_demand(), and tbb::internal::market::try_destroy_arena().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ BUSY

const state_t tbb::spin_rw_mutex_v3::BUSY = WRITER | READERS
staticprotected

Definition at line 212 of file spin_rw_mutex.h.

Referenced by internal_acquire_writer(), and internal_try_acquire_writer().

◆ is_fair_mutex

const bool tbb::spin_rw_mutex_v3::is_fair_mutex = false
static

Definition at line 175 of file spin_rw_mutex.h.

◆ is_recursive_mutex

const bool tbb::spin_rw_mutex_v3::is_recursive_mutex = false
static

Definition at line 174 of file spin_rw_mutex.h.

◆ is_rw_mutex

const bool tbb::spin_rw_mutex_v3::is_rw_mutex = true
static

Definition at line 173 of file spin_rw_mutex.h.

◆ ONE_READER

◆ READERS

◆ state

state_t tbb::spin_rw_mutex_v3::state
protected

State of lock.

Bit 0 = writer is holding lock Bit 1 = request by a writer to acquire lock (hint to readers to wait) Bit 2..N = number of readers holding lock

Definition at line 217 of file spin_rw_mutex.h.

Referenced by tbb::spin_rw_mutex_v3::scoped_lock::downgrade_to_reader(), internal_acquire_reader(), internal_acquire_writer(), internal_downgrade(), internal_release_reader(), internal_release_writer(), internal_try_acquire_reader(), internal_try_acquire_writer(), internal_upgrade(), tbb::spin_rw_mutex_v3::scoped_lock::release(), and unlock().

◆ WRITER

◆ WRITER_PENDING

const state_t tbb::spin_rw_mutex_v3::WRITER_PENDING = 2
staticprotected

The documentation for this class was generated from the following files:

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.