Seastar
High performance C++ framework for concurrent servers
|
Implements a read-write lock mechanism. Beware: this is not a cross-CPU lock, due to seastar's sharded architecture. Instead, it can be used to achieve rwlock semantics between two (or more) fibers running in the same CPU that may use the same resource. Acquiring the write lock will effectively cause all readers not to be executed until the write part is done.
#include <seastar/core/rwlock.hh>
Public Types | |
using | holder = semaphore_units< semaphore_default_exception_factory, Clock > |
Public Member Functions | |
rwlock_for_read< Clock > & | for_read () |
rwlock_for_write< Clock > & | for_write () |
future | read_lock (typename semaphore_type::time_point timeout=semaphore_type::time_point::max()) |
future | read_lock (abort_source &as) |
void | read_unlock () |
future | write_lock (typename semaphore_type::time_point timeout=semaphore_type::time_point::max()) |
future | write_lock (abort_source &as) |
void | write_unlock () |
bool | try_read_lock () |
Tries to acquire the lock in read mode iff this can be done without waiting. | |
bool | try_write_lock () |
Tries to acquire the lock in write mode iff this can be done without waiting. | |
future< holder > | hold_read_lock (typename semaphore_type::time_point timeout=semaphore_type::time_point::max()) |
future< holder > | hold_read_lock (abort_source &as) |
std::optional< holder > | try_hold_read_lock () noexcept |
future< holder > | hold_write_lock (typename semaphore_type::time_point timeout=semaphore_type::time_point::max()) |
future< holder > | hold_write_lock (abort_source &as) |
std::optional< holder > | try_hold_write_lock () noexcept |
bool | locked () const |
Checks if any read or write locks are currently held. | |
|
inline |
Cast this rwlock into read lock object with lock semantics appropriate to be used by "with_lock". The resulting object will have lock / unlock calls that, when called, will acquire / release the lock in read mode.
|
inline |
Cast this rwlock into write lock object with lock semantics appropriate to be used by "with_lock". The resulting object will have lock / unlock calls that, when called, will acquire / release the lock in write mode.
|
inline |
hold_read_lock() waits for a read lock and returns an object which, when destroyed, releases the lock. This makes it easy to ensure that the lock is eventually undone, at any circumstance (even including exceptions). The release() method can be used on the returned object to release its ownership of the lock and avoid the automatic unlock. Note that both hold_read_lock() and hold_write_lock() return an object of the same type, rwlock::holder.
hold_read_lock() may throw an exception (or, in other implementations, return an exceptional future) when it failed to obtain the lock - e.g., on allocation failure.
|
inline |
hold_write_lock() waits for a write lock and returns an object which, when destroyed, releases the lock. This makes it easy to ensure that the lock is eventually undone, at any circumstance (even including exceptions). The release() method can be used on the returned object to release its ownership of the lock and avoid the automatic unlock. Note that both hold_read_lock() and hold_write_lock() return an object of the same type, rwlock::holder.
hold_read_lock() may throw an exception (or, in other implementations, return an exceptional future) when it failed to obtain the lock - e.g., on allocation failure.
|
inline |
Acquires this lock in read mode. Many readers are allowed, but when this future returns, and until read_unlock is called, all fibers waiting on write_lock are guaranteed not to execute.
|
inline |
Releases the lock, which must have been taken in read mode. After this is called, one of the fibers waiting on write_lock will be allowed to proceed.
|
inlinenoexcept |
try_hold_read_lock() synchronously tries to get a read lock and, if successful, returns an optional object which, when destroyed, releases the lock.
|
inlinenoexcept |
try_hold_write_lock() synchronously tries to get a write lock and, if successful, returns an optional object which, when destroyed, releases the lock.
|
inline |
Acquires this lock in write mode. Only one writer is allowed. When this future returns, and until write_unlock is called, all other fibers waiting on either read_lock or write_lock are guaranteed not to execute.
|
inline |
Releases the lock, which must have been taken in write mode. After this is called, one of the other fibers waiting on write_lock or the fibers waiting on read_lock will be allowed to proceed.