mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes
m::reader_writer_mutex Class Reference

Implements a many-readers, single-writer locking concept. More...

#include <reader_writer_lock.hpp>

Collaboration diagram for m::reader_writer_mutex:
[legend]

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_
 

Detailed Description

Implements a many-readers, single-writer locking concept.

NOTE: This class is thread-safe.

Definition at line 117 of file reader_writer_lock.hpp.

Member Function Documentation

◆ lock_read()

void m::reader_writer_mutex::lock_read ( )
inline

◆ lock_write()

void m::reader_writer_mutex::lock_write ( )
inline

◆ notify_readers()

void m::reader_writer_mutex::notify_readers ( )
inline

Definition at line 139 of file reader_writer_lock.hpp.

References cv_readers_.

Referenced by unlock_write().

◆ notify_writer()

void m::reader_writer_mutex::notify_writer ( )
inline

Definition at line 140 of file reader_writer_lock.hpp.

References cv_writers_.

Referenced by unlock_read(), and unlock_write().

◆ try_lock_read()

bool m::reader_writer_mutex::try_lock_read ( )
inline

◆ try_lock_write()

bool m::reader_writer_mutex::try_lock_write ( )
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_.

◆ unlock_read()

void m::reader_writer_mutex::unlock_read ( )
inline

◆ unlock_write()

void m::reader_writer_mutex::unlock_write ( )
inline

◆ upgrade()

bool m::reader_writer_mutex::upgrade ( )
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().

Field Documentation

◆ cv_readers_

std::condition_variable m::reader_writer_mutex::cv_readers_
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().

◆ cv_writers_

std::condition_variable m::reader_writer_mutex::cv_writers_
private

Definition at line 136 of file reader_writer_lock.hpp.

Referenced by lock_write(), notify_writer(), and upgrade().

◆ mutex_

std::mutex m::reader_writer_mutex::mutex_
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().

◆ rw_

detail::reader_writer_mutex_internal m::reader_writer_mutex::rw_
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().


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