userver: engine::SharedMutex Class Reference
Loading...
Searching...
No Matches
engine::SharedMutex Class Referencefinal

#include <userver/engine/shared_mutex.hpp>

Detailed Description

std::shared_mutex replacement for asynchronous tasks.

Ignores task cancellations (succeeds even if the current task is cancelled).

Writers (unique locks) have priority over readers (shared locks), thus new shared lock waits for the pending writes to finish, which in turn waits for existing existing shared locks to unlock first.

Example usage:

constexpr auto kTestString = "123";
std::string data;
{
std::lock_guard<engine::SharedMutex> lock(mutex);
// accessing the data under the mutex for writing
data = kTestString;
}
{
std::shared_lock<engine::SharedMutex> lock(mutex);
// accessing the data under the mutex for reading,
// data cannot be changed
const auto& x = data;
ASSERT_EQ(x, kTestString);
}
See also
Synchronization Primitives

Definition at line 29 of file shared_mutex.hpp.

Public Member Functions

 SharedMutex (const SharedMutex &)=delete
 
 SharedMutex (SharedMutex &&)=delete
 
SharedMutexoperator= (const SharedMutex &)=delete
 
SharedMutexoperator= (SharedMutex &&)=delete
 
void lock ()
 
void unlock ()
 
bool try_lock ()
 
template<typename Rep , typename Period >
bool try_lock_for (const std::chrono::duration< Rep, Period > &)
 
template<typename Clock , typename Duration >
bool try_lock_until (const std::chrono::time_point< Clock, Duration > &)
 
bool try_lock_until (Deadline deadline)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void lock_shared ()
 
void unlock_shared ()
 
bool try_lock_shared ()
 
template<typename Rep , typename Period >
bool try_lock_shared_for (const std::chrono::duration< Rep, Period > &)
 
template<typename Clock , typename Duration >
bool try_lock_shared_until (const std::chrono::time_point< Clock, Duration > &)
 
bool try_lock_shared_until (Deadline deadline)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 

Member Function Documentation

◆ lock()

void engine::SharedMutex::lock ( )

Locks the mutex for unique ownership. Blocks current coroutine if the mutex is locked by another coroutine for reading or writing.

Note
The method waits for the mutex even if the current task is cancelled.

◆ lock_shared()

void engine::SharedMutex::lock_shared ( )

Locks the mutex for shared ownership. Blocks current coroutine if the mutex is locked by another coroutine for reading or writing.

Note
The method waits for the mutex even if the current task is cancelled.

◆ try_lock()

bool engine::SharedMutex::try_lock ( )

Tries to lock the mutex for unique ownership without blocking the coroutine, returns true if succeeded.

◆ try_lock_for()

template<typename Rep , typename Period >
bool engine::SharedMutex::try_lock_for ( const std::chrono::duration< Rep, Period > & duration)

Tries to lock the mutex for unique ownership in specified duration. Blocks current coroutine if the mutex is locked by another coroutine up to the provided duration.

Returns
true if the locking succeeded

Definition at line 139 of file shared_mutex.hpp.

◆ try_lock_shared()

bool engine::SharedMutex::try_lock_shared ( )

Tries to lock the mutex for shared ownership without blocking the coroutine, returns true if succeeded.

◆ try_lock_shared_for()

template<typename Rep , typename Period >
bool engine::SharedMutex::try_lock_shared_for ( const std::chrono::duration< Rep, Period > & duration)

Tries to lock the mutex for shared ownership in specified duration. Blocks current coroutine if the mutex is locked by another coroutine up to the provided duration.

Returns
true if the locking succeeded

Definition at line 144 of file shared_mutex.hpp.

◆ try_lock_shared_until()

template<typename Clock , typename Duration >
bool engine::SharedMutex::try_lock_shared_until ( const std::chrono::time_point< Clock, Duration > & until)

Tries to lock the mutex for shared ownership till specified time point. Blocks current coroutine if the mutex is locked by another coroutine up to the provided duration.

Returns
true if the locking succeeded

Definition at line 154 of file shared_mutex.hpp.

◆ try_lock_until()

template<typename Clock , typename Duration >
bool engine::SharedMutex::try_lock_until ( const std::chrono::time_point< Clock, Duration > & until)

Tries to lock the mutex for unique ownership till specified time point. Blocks current coroutine if the mutex is locked by another coroutine up to the provided duration.

Returns
true if the locking succeeded

Definition at line 149 of file shared_mutex.hpp.

◆ unlock()

void engine::SharedMutex::unlock ( )

Unlocks the mutex for unique ownership. Before calling this method the the mutex should be locked for unique ownership by current coroutine.

Note
the order of coroutines to unblock is unspecified. Any code assuming any specific order (e.g. FIFO) is incorrect and should be fixed.

◆ unlock_shared()

void engine::SharedMutex::unlock_shared ( )

Unlocks the mutex for shared ownership. Before calling this method the mutex should be locked for shared ownership by current coroutine.

Note
the order of coroutines to unblock is unspecified. Any code assuming any specific order (e.g. FIFO) is incorrect and should be fixed.

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