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_mutex * | mutex
| 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.

: mutex(NULL), is_writer(false) {}
tbb::spin_rw_mutex_v3::scoped_lock::mutex
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:165
tbb::spin_rw_mutex_v3::scoped_lock::is_writer
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:169

## 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.

: mutex(NULL) {
acquire(m, write);
}
tbb::spin_rw_mutex_v3::scoped_lock::acquire
void acquire(spin_rw_mutex &m, bool write=true)
Acquire lock on given mutex.
Definition: spin_rw_mutex.h:106

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.

{
if( mutex ) release();
}
tbb::spin_rw_mutex_v3::scoped_lock::release
void release()
Release lock.
Definition: spin_rw_mutex.h:124

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.

{
__TBB_ASSERT( !mutex, "holding mutex already" );
is_writer = write;
mutex = &m;
if( write ) mutex->internal_acquire_writer();
else mutex->internal_acquire_reader();
}
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::spin_rw_mutex_v3::internal_acquire_writer
bool __TBB_EXPORTED_METHOD internal_acquire_writer()
Internal acquire write lock.
Definition: spin_rw_mutex.cpp:37
tbb::spin_rw_mutex_v3::internal_acquire_reader
void __TBB_EXPORTED_METHOD internal_acquire_reader()
Internal acquire read lock.
Definition: spin_rw_mutex.cpp:62

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.

{
__TBB_ASSERT( mutex, "mutex is not acquired" );
if (!is_writer) return true; // Already a reader
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
mutex->internal_downgrade();
#else
__TBB_FetchAndAddW( &mutex->state, ((intptr_t)ONE_READER-WRITER));
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
is_writer = false;
return true;
}
tbb::spin_rw_mutex_v3::internal_downgrade
void __TBB_EXPORTED_METHOD internal_downgrade()
Out of line code for downgrading a writer to a reader.
Definition: spin_rw_mutex.cpp:108
tbb::spin_rw_mutex_v3::state
state_t state
State of lock.
Definition: spin_rw_mutex.h:217
tbb::spin_rw_mutex_v3::WRITER
static const state_t WRITER
Definition: spin_rw_mutex.h:208
tbb::spin_rw_mutex_v3::ONE_READER
static const state_t ONE_READER
Definition: spin_rw_mutex.h:211

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.

{
__TBB_ASSERT( mutex, "mutex is not acquired" );
spin_rw_mutex *m = mutex;
mutex = NULL;
#if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
if( is_writer ) m->internal_release_writer();
else m->internal_release_reader();
#else
if( is_writer ) __TBB_AtomicAND( &m->state, READERS );
else __TBB_FetchAndAddWrelease( &m->state, -(intptr_t)ONE_READER);
#endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
}
__TBB_AtomicAND
void __TBB_AtomicAND(volatile void *operand, uintptr_t addend)
Definition: tbb_machine.h:891
__TBB_FetchAndAddWrelease
#define __TBB_FetchAndAddWrelease(P, V)
Definition: tbb_machine.h:312
tbb::spin_rw_mutex
spin_rw_mutex_v3 spin_rw_mutex
Definition: spin_rw_mutex.h:33
tbb::spin_rw_mutex_v3::READERS
static const state_t READERS
Definition: spin_rw_mutex.h:210
tbb::spin_rw_mutex_v3::internal_release_writer
void __TBB_EXPORTED_METHOD internal_release_writer()
Out of line code for releasing a write lock.
Definition: spin_rw_mutex.cpp:55

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 tbb::interface5::concurrent_hash_map< Key, T, HashCompare, Allocator >::const_accessor::release(), and ~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.

{
__TBB_ASSERT( !mutex, "holding mutex already" );
bool result;
is_writer = write;
result = write? m.internal_try_acquire_writer()
: m.internal_try_acquire_reader();
if( result )
mutex = &m;
return result;
}

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.

{
__TBB_ASSERT( mutex, "mutex is not acquired" );
if (is_writer) return true; // Already a writer
is_writer = true;
return mutex->internal_upgrade();
}
tbb::spin_rw_mutex_v3::internal_upgrade
bool __TBB_EXPORTED_METHOD internal_upgrade()
Internal upgrade reader to become a writer.
Definition: spin_rw_mutex.cpp:82

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(), tbb::interface5::concurrent_hash_map< Key, T, HashCompare, Allocator >::const_accessor::is_writer(), 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:

- spin_rw_mutex.h

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.