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 ()
 Allow default construction. More...
 

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 90 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 97 of file spin_rw_mutex.h.

97 : 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 100 of file spin_rw_mutex.h.

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

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 105 of file spin_rw_mutex.h.

105  {
106  if( mutex ) release();
107  }
spin_rw_mutex * mutex
The pointer to the current mutex that is held, or NULL if no mutex is held.

References 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 110 of file spin_rw_mutex.h.

110  {
111  __TBB_ASSERT( !mutex, "holding mutex already" );
112  is_writer = write;
113  mutex = &m;
114  if( write ) mutex->internal_acquire_writer();
116  }
void __TBB_EXPORTED_METHOD internal_acquire_reader()
Internal acquire read 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:169
bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire 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, and is_writer.

Referenced by scoped_lock().

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 142 of file spin_rw_mutex.h.

142  {
143  __TBB_ASSERT( mutex, "mutex is not acquired" );
144  if (!is_writer) return true; // Already a reader
145 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
147 #else
148  __TBB_FetchAndAddW( &mutex->state, ((intptr_t)ONE_READER-WRITER));
149 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
150  is_writer = false;
151  return true;
152  }
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:169
void __TBB_EXPORTED_METHOD internal_downgrade()
Out of line code for downgrading a writer to a reader.
static const state_t ONE_READER
static const state_t WRITER
state_t state
State of 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, is_writer, tbb::spin_rw_mutex_v3::ONE_READER, and tbb::spin_rw_mutex_v3::WRITER.

◆ release()

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

Release lock.

Definition at line 128 of file spin_rw_mutex.h.

128  {
129  __TBB_ASSERT( mutex, "mutex is not acquired" );
130  spin_rw_mutex *m = mutex;
131  mutex = NULL;
132 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
134  else m->internal_release_reader();
135 #else
136  if( is_writer ) __TBB_AtomicAND( &m->state, READERS );
137  else __TBB_FetchAndAddWrelease( &m->state, -(intptr_t)ONE_READER);
138 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
139  }
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:169
#define __TBB_FetchAndAddWrelease(P, V)
Definition: tbb_machine.h:313
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:892
static const state_t ONE_READER
spin_rw_mutex_v3 spin_rw_mutex
Definition: spin_rw_mutex.h:37
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.
static const state_t READERS

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 155 of file spin_rw_mutex.h.

155  {
156  __TBB_ASSERT( !mutex, "holding mutex already" );
157  bool result;
158  is_writer = write;
159  result = write? m.internal_try_acquire_writer()
160  : m.internal_try_acquire_reader();
161  if( result )
162  mutex = &m;
163  return result;
164  }
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:169
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(), and is_writer.

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 120 of file spin_rw_mutex.h.

120  {
121  __TBB_ASSERT( mutex, "mutex is not acquired" );
122  if (is_writer) return true; // Already a writer
123  is_writer = true;
124  return mutex->internal_upgrade();
125  }
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:169
bool is_writer
If mutex!=NULL, then is_writer is true if holding a writer lock, false if holding a reader lock.
bool __TBB_EXPORTED_METHOD internal_upgrade()
Internal upgrade reader to become a writer.

References __TBB_ASSERT, and is_writer.

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 173 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 169 of file spin_rw_mutex.h.

Referenced by release().


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

Copyright © 2005-2019 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.