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

The scoped locking pattern. More...

#include <spin_rw_mutex.h>

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

Public Member Functions

 scoped_lock ()
 Construct lock that has not acquired a mutex. More...
 
 scoped_lock (spin_rw_mutex &m, bool write=true)
 Acquire lock on given mutex. More...
 
 ~scoped_lock ()
 Release lock (if lock is held). More...
 
void acquire (spin_rw_mutex &m, bool write=true)
 Acquire lock on given mutex. More...
 
bool upgrade_to_writer ()
 Upgrade reader to become a writer. More...
 
void release ()
 Release lock. More...
 
bool downgrade_to_reader ()
 Downgrade writer to become a reader. More...
 
bool try_acquire (spin_rw_mutex &m, bool write=true)
 Try acquire lock on given mutex. More...
 

Protected Attributes

spin_rw_mutexmutex
 The pointer to the current mutex that is held, or NULL if no mutex is held. More...
 
bool is_writer
 If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock. More...
 

Additional Inherited Members

- Private Member Functions inherited from tbb::internal::no_copy
 no_copy (const no_copy &)=delete
 
 no_copy ()=default
 

Detailed Description

The scoped locking pattern.

It helps to avoid the common problem of forgetting to release lock. It also nicely provides the "node" for queuing locks.

Definition at line 86 of file spin_rw_mutex.h.

Constructor & Destructor Documentation

◆ scoped_lock() [1/2]

tbb::spin_rw_mutex_v3::scoped_lock::scoped_lock ( )
inline

Construct lock that has not acquired a mutex.

Equivalent to zero-initialization of *this.

Definition at line 93 of file spin_rw_mutex.h.

93 : mutex(NULL), is_writer(false) {}
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

◆ scoped_lock() [2/2]

tbb::spin_rw_mutex_v3::scoped_lock::scoped_lock ( spin_rw_mutex m,
bool  write = true 
)
inline

Acquire lock on given mutex.

Definition at line 96 of file spin_rw_mutex.h.

96  : mutex(NULL) {
97  acquire(m, write);
98  }
void acquire(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.

References acquire().

Here is the call graph for this function:

◆ ~scoped_lock()

tbb::spin_rw_mutex_v3::scoped_lock::~scoped_lock ( )
inline

Release lock (if lock is held).

Definition at line 101 of file spin_rw_mutex.h.

101  {
102  if( mutex ) release();
103  }
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.

References mutex, and release().

Here is the call graph for this function:

Member Function Documentation

◆ acquire()

void tbb::spin_rw_mutex_v3::scoped_lock::acquire ( spin_rw_mutex m,
bool  write = true 
)
inline

Acquire lock on given mutex.

Definition at line 106 of file spin_rw_mutex.h.

106  {
107  __TBB_ASSERT( !mutex, "holding mutex already" );
108  is_writer = write;
109  mutex = &m;
110  if( write ) mutex->internal_acquire_writer();
112  }
bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire write lock.
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
void __TBB_EXPORTED_METHOD internal_acquire_reader()
Internal acquire read lock.
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

References __TBB_ASSERT, tbb::spin_rw_mutex_v3::internal_acquire_reader(), tbb::spin_rw_mutex_v3::internal_acquire_writer(), is_writer, and mutex.

Referenced by scoped_lock().

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

◆ downgrade_to_reader()

bool tbb::spin_rw_mutex_v3::scoped_lock::downgrade_to_reader ( )
inline

Downgrade writer to become a reader.

Definition at line 138 of file spin_rw_mutex.h.

138  {
139  __TBB_ASSERT( mutex, "mutex is not acquired" );
140  if (!is_writer) return true; // Already a reader
141 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
143 #else
144  __TBB_FetchAndAddW( &mutex->state, ((intptr_t)ONE_READER-WRITER));
145 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
146  is_writer = false;
147  return true;
148  }
static const state_t ONE_READER
state_t state
State of lock.
static const state_t WRITER
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.
void __TBB_EXPORTED_METHOD internal_downgrade()
Out of line code for downgrading a writer to a reader.

References __TBB_ASSERT, tbb::spin_rw_mutex_v3::internal_downgrade(), is_writer, mutex, tbb::spin_rw_mutex_v3::ONE_READER, tbb::spin_rw_mutex_v3::state, and tbb::spin_rw_mutex_v3::WRITER.

Here is the call graph for this function:

◆ release()

void tbb::spin_rw_mutex_v3::scoped_lock::release ( )
inline

Release lock.

Definition at line 124 of file spin_rw_mutex.h.

124  {
125  __TBB_ASSERT( mutex, "mutex is not acquired" );
126  spin_rw_mutex *m = mutex;
127  mutex = NULL;
128 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
130  else m->internal_release_reader();
131 #else
132  if( is_writer ) __TBB_AtomicAND( &m->state, READERS );
133  else __TBB_FetchAndAddWrelease( &m->state, -(intptr_t)ONE_READER);
134 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
135  }
spin_rw_mutex_v3 spin_rw_mutex
Definition: spin_rw_mutex.h:33
static const state_t ONE_READER
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:888
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
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
void __TBB_EXPORTED_METHOD internal_release_writer()
Out of line code for releasing a write lock.
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

References __TBB_ASSERT, __TBB_AtomicAND(), __TBB_FetchAndAddWrelease, tbb::spin_rw_mutex_v3::internal_release_reader(), tbb::spin_rw_mutex_v3::internal_release_writer(), is_writer, mutex, tbb::spin_rw_mutex_v3::ONE_READER, tbb::spin_rw_mutex_v3::READERS, and tbb::spin_rw_mutex_v3::state.

Referenced by ~scoped_lock().

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

◆ try_acquire()

bool tbb::spin_rw_mutex_v3::scoped_lock::try_acquire ( spin_rw_mutex m,
bool  write = true 
)
inline

Try acquire lock on given mutex.

Definition at line 151 of file spin_rw_mutex.h.

151  {
152  __TBB_ASSERT( !mutex, "holding mutex already" );
153  bool result;
154  is_writer = write;
155  result = write? m.internal_try_acquire_writer()
156  : m.internal_try_acquire_reader();
157  if( result )
158  mutex = &m;
159  return result;
160  }
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

References __TBB_ASSERT, tbb::spin_rw_mutex_v3::internal_try_acquire_reader(), tbb::spin_rw_mutex_v3::internal_try_acquire_writer(), is_writer, and mutex.

Here is the call graph for this function:

◆ upgrade_to_writer()

bool tbb::spin_rw_mutex_v3::scoped_lock::upgrade_to_writer ( )
inline

Upgrade reader to become a writer.

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

Definition at line 116 of file spin_rw_mutex.h.

116  {
117  __TBB_ASSERT( mutex, "mutex is not acquired" );
118  if (is_writer) return true; // Already a writer
119  is_writer = true;
120  return mutex->internal_upgrade();
121  }
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
bool __TBB_EXPORTED_METHOD internal_upgrade()
Internal upgrade reader to become a writer.
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

References __TBB_ASSERT, tbb::spin_rw_mutex_v3::internal_upgrade(), is_writer, and mutex.

Here is the call graph for this function:

Member Data Documentation

◆ is_writer

bool tbb::spin_rw_mutex_v3::scoped_lock::is_writer
protected

If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.

Not defined if not holding a lock.

Definition at line 169 of file spin_rw_mutex.h.

Referenced by acquire(), downgrade_to_reader(), release(), try_acquire(), and upgrade_to_writer().

◆ mutex

spin_rw_mutex* tbb::spin_rw_mutex_v3::scoped_lock::mutex
protected

The pointer to the current mutex that is held, or NULL if no mutex is held.

Definition at line 165 of file spin_rw_mutex.h.

Referenced by acquire(), downgrade_to_reader(), release(), try_acquire(), upgrade_to_writer(), and ~scoped_lock().


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

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.