4#include <condition_variable>
144 std::unique_lock lock{
mutex_};
153 std::unique_lock lock{
mutex_};
162 std::unique_lock lock{
mutex_};
172 std::unique_lock lock{
mutex_};
194 std::unique_lock lock{
mutex_};
206 std::unique_lock lock{
mutex_};
209 "if a reader wants to upgrade, it must be waiting to claim the write lock");
222 std::unique_lock lock{
mutex_};
NOTE: This class is not thread-safe.
void upgrade_lock()
Upgrades an already held read lock to a write lock, after previously requesting to upgrade.
bool writer_active() const
bool writer_active_
As there can be at most one active writer, we track this information with a bool.
void release_write_lock()
uint16_t readers_active() const
uint16_t writers_waiting_
The number of writers waiting to acquire the singular write lock.
void release_read_lock()
Releases a previously acquired read lock.
bool reader_wants_upgrade_
Whether a single reader wants to upgrade to a writer.
uint16_t readers_waiting_
The number of readers waiting to acquire a read lock.
uint16_t readers_active_
The number of currently active readers, i.e.
void acquire_write_lock()
uint16_t readers_waiting() const
bool try_request_upgrade()
Try to request an upgrade.
uint16_t writers_waiting() const
void acquire_read_lock()
Acquires a previously requested read lock.
bool can_acquire_write_lock() const
The mutex is in a state where a writer can acquire a write (exclusive) lock.
bool can_upgrade_lock() const
The mutex is in a state where a reader can claim the write (exclusive) lock.
void request_write_lock()
Inform others that we want to acquire the write lock.
bool reader_wants_upgrade() const
void request_read_lock()
Inform others that we want to acquire a read lock.
bool can_acquire_read_lock() const
The mutex is in a state where a reader can acquire a reader (shared) lock.
Implements a many-readers, single-writer locking concept.
void lock_write()
Acquire the write lock.
std::condition_variable cv_readers_
Used to signal the respective class of clients that the resource has become available.
bool try_lock_read()
Attempts to immediatly claim a read lock.
detail::reader_writer_mutex_internal rw_
Represents the internal state of the reader-writer mutex.
std::condition_variable cv_writers_
void lock_read()
Acquire 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.
std::mutex mutex_
The mutex to guard operations on members of this class.
#define M_unreachable(MSG)
read_lock(read_lock &&other)
friend void swap(read_lock &first, read_lock &second)
read_lock(reader_writer_mutex &rw_mutex)
read_lock & operator=(read_lock &&other)
read_lock(reader_writer_mutex &rw_mutex, std::defer_lock_t)
std::reference_wrapper< reader_writer_mutex > rw_mutex_
read_lock(const read_lock &)=delete
read_lock(reader_writer_mutex &rw_mutex, std::adopt_lock_t)
This is a helper class that helps managing reader and writer locks claimed from reader_writer_mutex.
@ LOCK_WRITE
holds a write (exclusive) lock
@ LOCK_READ
holds a read (shared) lock
bool owns_write_lock() const
void lock_write()
Acquire the write lock.
bool upgrade()
Tries to upgrade a held read lock to the write lock.
reader_writer_lock & operator=(reader_writer_lock &&other)=default
reader_writer_lock(reader_writer_mutex &rw_mutex)
reader_writer_lock(reader_writer_lock &&other)
reader_writer_lock(const reader_writer_lock &)=delete
bool owns_read_lock() const
std::reference_wrapper< reader_writer_mutex > rw_mutex_
void lock_read()
Acquire a read lock.
enum m::reader_writer_lock::@4 LOCK_NONE
upgrade_lock(const upgrade_lock &)=delete
upgrade_lock(reader_writer_mutex &rw_mutex, std::defer_lock_t)
upgrade_lock(reader_writer_mutex &rw_mutex, std::adopt_lock_t)
write_lock upgrade()
Attempts to upgrade the read lock to a write lock.
upgrade_lock(upgrade_lock &&other)=default
upgrade_lock(reader_writer_mutex &rw_mutex)
bool owns_read_lock() const
upgrade_lock & operator=(upgrade_lock &&)=default
friend void swap(write_lock &first, write_lock &second)
std::reference_wrapper< reader_writer_mutex > rw_mutex_
write_lock(reader_writer_mutex &rw_mutex, std::adopt_lock_t)
write_lock(write_lock &&other)
write_lock(const write_lock &)=delete
write_lock(reader_writer_mutex &rw_mutex)
write_lock & operator=(write_lock &&other)
write_lock(reader_writer_mutex &rw_mutex, std::defer_lock_t)