StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
StormByte::Safe::SharedLock Class Reference

Owns at most one shared hold of a SharedMutex. More...

#include <StormByte/safe/shared_lock.hxx>

Public Member Functions

 SharedLock () noexcept
 Empty lock.
 SharedLock (SharedMutex &mutex)
 Take a shared hold of mutex.
 SharedLock (SharedMutex &mutex, DeferLock tag) noexcept
 Associate mutex without locking it.
 SharedLock (SharedMutex &mutex, TryToLock tag) noexcept
 Try to take a shared hold of mutex.
 SharedLock (SharedMutex &mutex, AdoptLock tag) noexcept
 Assume the calling thread already holds mutex shared.
 SharedLock (const SharedLock &)=delete
 SharedLock (SharedLock &&other) noexcept
 Take a lock.
 ~SharedLock () noexcept
 Drop the shared hold if this lock owns it.
SharedLock & operator= (const SharedLock &)=delete
SharedLock & operator= (SharedLock &&other) noexcept
 Take a lock.
void lock ()
 Take a shared hold of the associated mutex.
bool try_lock () noexcept
 Try to take a shared hold of the associated mutex.
void unlock () noexcept
 Drop the shared hold.
SharedMutex * release () noexcept
 Drop the association without unlocking.
SharedMutex * mutex () const noexcept
 Associated mutex.
bool owns_lock () const noexcept
 Whether this lock owns the shared hold.
 operator bool () const noexcept
 Whether this lock owns the shared hold.
void swap (SharedLock &other) noexcept
 Exchange two locks.

Detailed Description

Owns at most one shared hold of a SharedMutex.

Header-only. The object is a pointer and an ownership flag. It does not contain a standard lock. lock and unlock take and drop the shared hold, so ConditionVariableAny can wait on it.

Constructor & Destructor Documentation

◆ SharedLock() [1/7]

StormByte::Safe::SharedLock::SharedLock ( )
inlinenoexcept

Empty lock.

Owns nothing.

◆ SharedLock() [2/7]

StormByte::Safe::SharedLock::SharedLock ( SharedMutex & mutex)
inlineexplicit

Take a shared hold of mutex.

Parameters
mutexShared mutex to acquire.
Exceptions
OperationErrorThe shared lock translated a foreign failure.

◆ SharedLock() [3/7]

StormByte::Safe::SharedLock::SharedLock ( SharedMutex & mutex,
DeferLock tag )
inlinenoexcept

Associate mutex without locking it.

Parameters
mutexShared mutex to lock later.
tagDeferred-lock tag.

◆ SharedLock() [4/7]

StormByte::Safe::SharedLock::SharedLock ( SharedMutex & mutex,
TryToLock tag )
inlinenoexcept

Try to take a shared hold of mutex.

Parameters
mutexShared mutex to try.
tagTry-lock tag.

◆ SharedLock() [5/7]

StormByte::Safe::SharedLock::SharedLock ( SharedMutex & mutex,
AdoptLock tag )
inlinenoexcept

Assume the calling thread already holds mutex shared.

Parameters
mutexShared mutex already held by this thread.
tagAdopt tag.

◆ SharedLock() [6/7]

StormByte::Safe::SharedLock::SharedLock ( const SharedLock & )
delete

◆ SharedLock() [7/7]

StormByte::Safe::SharedLock::SharedLock ( SharedLock && other)
inlinenoexcept

Take a lock.

other owns nothing afterwards.

Parameters
otherSource.

◆ ~SharedLock()

StormByte::Safe::SharedLock::~SharedLock ( )
inlinenoexcept

Drop the shared hold if this lock owns it.

Member Function Documentation

◆ lock()

void StormByte::Safe::SharedLock::lock ( )
inline

Take a shared hold of the associated mutex.

Precondition
A mutex is associated and this lock does not own it.
Exceptions
OperationErrorThe shared lock translated a foreign failure.

◆ mutex()

SharedMutex * StormByte::Safe::SharedLock::mutex ( ) const
inlinenoexcept

Associated mutex.

Returns
The mutex, or null.

◆ operator bool()

StormByte::Safe::SharedLock::operator bool ( ) const
inlineexplicitnoexcept

Whether this lock owns the shared hold.

Returns
Ownership flag.

◆ operator=() [1/2]

SharedLock & StormByte::Safe::SharedLock::operator= ( const SharedLock & )
delete

◆ operator=() [2/2]

SharedLock & StormByte::Safe::SharedLock::operator= ( SharedLock && other)
inlinenoexcept

Take a lock.

Drops the previous shared hold when owned.

Parameters
otherSource. Left not owning.
Returns
This lock.

◆ owns_lock()

bool StormByte::Safe::SharedLock::owns_lock ( ) const
inlinenoexcept

Whether this lock owns the shared hold.

Returns
Ownership flag.

◆ release()

SharedMutex * StormByte::Safe::SharedLock::release ( )
inlinenoexcept

Drop the association without unlocking.

Returns
The previous mutex, or null.

◆ swap()

void StormByte::Safe::SharedLock::swap ( SharedLock & other)
inlinenoexcept

Exchange two locks.

Parameters
otherOther lock.

◆ try_lock()

bool StormByte::Safe::SharedLock::try_lock ( )
inlinenoexcept

Try to take a shared hold of the associated mutex.

Returns
Whether this lock now owns it. False when no mutex is associated.

◆ unlock()

void StormByte::Safe::SharedLock::unlock ( )
inlinenoexcept

Drop the shared hold.

Precondition
This lock owns the shared hold.

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