![]() |
mutable
A Database System for Research and Fast Prototyping
|
Implements a many-readers, single-writer locking concept. More...
#include <reader_writer_lock.hpp>
Public Member Functions | |
void | notify_readers () |
void | notify_writer () |
void | lock_read () |
Acquire a read lock. | |
void | lock_write () |
Acquire the write lock. | |
bool | try_lock_read () |
Attempts to immediatly claim a read lock. | |
bool | try_lock_write () |
Attempts to immediatly claim the exclusive write lock. | |
bool | upgrade () |
Tries to upgrade a read lock to the write lock. | |
void | unlock_read () |
void | unlock_write () |
Private Attributes | |
std::mutex | mutex_ |
The mutex to guard operations on members of this class. | |
detail::reader_writer_mutex_internal | rw_ |
Represents the internal state of the reader-writer mutex. | |
std::condition_variable | cv_readers_ |
Used to signal the respective class of clients that the resource has become available. | |
std::condition_variable | cv_writers_ |
Implements a many-readers, single-writer locking concept.
NOTE: This class is thread-safe.
Definition at line 117 of file reader_writer_lock.hpp.
|
inline |
Acquire a read lock.
This call blocks the calling thread until a read lock was acquired.
Definition at line 143 of file reader_writer_lock.hpp.
References m::detail::reader_writer_mutex_internal::acquire_read_lock(), m::detail::reader_writer_mutex_internal::can_acquire_read_lock(), cv_readers_, M_insist, mutex_, m::detail::reader_writer_mutex_internal::request_read_lock(), and rw_.
|
inline |
Acquire the write lock.
This call blocks the calling thread until the write lock was acquired.
Definition at line 152 of file reader_writer_lock.hpp.
References m::detail::reader_writer_mutex_internal::acquire_write_lock(), m::detail::reader_writer_mutex_internal::can_acquire_write_lock(), cv_writers_, M_insist, mutex_, m::detail::reader_writer_mutex_internal::request_write_lock(), and rw_.
|
inline |
Definition at line 139 of file reader_writer_lock.hpp.
References cv_readers_.
Referenced by unlock_write().
|
inline |
Definition at line 140 of file reader_writer_lock.hpp.
References cv_writers_.
Referenced by unlock_read(), and unlock_write().
|
inline |
Attempts to immediatly claim a read lock.
Returns true
on success, false
otherwise.
Definition at line 161 of file reader_writer_lock.hpp.
References m::detail::reader_writer_mutex_internal::acquire_read_lock(), m::detail::reader_writer_mutex_internal::can_acquire_read_lock(), mutex_, m::detail::reader_writer_mutex_internal::request_read_lock(), and rw_.
|
inline |
Attempts to immediatly claim the exclusive write lock.
Returns true
on success, false
otherwise.
Definition at line 171 of file reader_writer_lock.hpp.
References m::detail::reader_writer_mutex_internal::acquire_write_lock(), m::detail::reader_writer_mutex_internal::can_acquire_write_lock(), mutex_, m::detail::reader_writer_mutex_internal::request_write_lock(), and rw_.
|
inline |
Definition at line 205 of file reader_writer_lock.hpp.
References m::and, M_insist, mutex_, notify_writer(), m::detail::reader_writer_mutex_internal::reader_wants_upgrade(), m::detail::reader_writer_mutex_internal::readers_active(), m::detail::reader_writer_mutex_internal::release_read_lock(), rw_, and m::detail::reader_writer_mutex_internal::writers_waiting().
|
inline |
Definition at line 221 of file reader_writer_lock.hpp.
References m::and, M_insist, mutex_, notify_readers(), notify_writer(), m::detail::reader_writer_mutex_internal::readers_active(), m::detail::reader_writer_mutex_internal::readers_waiting(), m::detail::reader_writer_mutex_internal::release_write_lock(), rw_, and m::detail::reader_writer_mutex_internal::writers_waiting().
|
inline |
Tries to upgrade a read lock to the write lock.
This operation fails if another thread has already requested an upgrade.
WARNING: This operation is dangerous, as misuse can easily lead to a deadlock!
If two threads both hold a read lock and then both want to upgrade, one thread will succeed and one will fail. The succeeding thread will start to wait for the upgrade, i.e., until it is the single last active reader. If the other thread, that failed to upgrade, holds on to its read lock, the two threads deadlock.
A fair solution to this problem is to have the reader that failed to upgrade give up its read lock. This behavior is implemented by reader_writer_lock::upgrade()
.
Definition at line 193 of file reader_writer_lock.hpp.
References m::detail::reader_writer_mutex_internal::can_upgrade_lock(), cv_writers_, M_insist, mutex_, m::detail::reader_writer_mutex_internal::reader_wants_upgrade(), rw_, m::detail::reader_writer_mutex_internal::try_request_upgrade(), and m::detail::reader_writer_mutex_internal::upgrade_lock().
|
private |
Used to signal the respective class of clients that the resource has become available.
NOTE: Clients are not waiting for mutex_
to become available, but for the reader_writer_mutex
to become available. These are two different things.
Definition at line 136 of file reader_writer_lock.hpp.
Referenced by lock_read(), and notify_readers().
|
private |
Definition at line 136 of file reader_writer_lock.hpp.
Referenced by lock_write(), notify_writer(), and upgrade().
|
private |
The mutex to guard operations on members of this class.
NOTE: This is actually not the mutex that the user of reader_writer_mutex
holds. The mutex held by a user of reader_writer_mutex
is virtual.
Definition at line 125 of file reader_writer_lock.hpp.
Referenced by lock_read(), lock_write(), try_lock_read(), try_lock_write(), unlock_read(), unlock_write(), and upgrade().
|
private |
Represents the internal state of the reader-writer mutex.
Definition at line 128 of file reader_writer_lock.hpp.
Referenced by lock_read(), lock_write(), try_lock_read(), try_lock_write(), unlock_read(), unlock_write(), and upgrade().