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

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

#include <StormByte/safe/exclusive_lock.hxx>

Public Member Functions

 ExclusiveLock () noexcept
 Empty lock.
 ExclusiveLock (SharedMutex &mutex)
 Take the exclusive hold of mutex.
 ExclusiveLock (SharedMutex &mutex, DeferLock tag) noexcept
 Associate mutex without locking it.
 ExclusiveLock (SharedMutex &mutex, TryToLock tag) noexcept
 Try to take the exclusive hold of mutex.
 ExclusiveLock (SharedMutex &mutex, AdoptLock tag) noexcept
 Assume the calling thread already holds mutex exclusively.
 ExclusiveLock (const ExclusiveLock &)=delete
 ExclusiveLock (ExclusiveLock &&other) noexcept
 Take a lock.
 ~ExclusiveLock () noexcept
 Drop the exclusive hold if this lock owns it.
ExclusiveLock & operator= (const ExclusiveLock &)=delete
ExclusiveLock & operator= (ExclusiveLock &&other) noexcept
 Take a lock.
void lock ()
 Take the exclusive hold of the associated mutex.
bool try_lock () noexcept
 Try to take the exclusive hold of the associated mutex.
void unlock () noexcept
 Drop the exclusive 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 exclusive hold.
 operator bool () const noexcept
 Whether this lock owns the exclusive hold.
void swap (ExclusiveLock &other) noexcept
 Exchange two locks.

Detailed Description

Owns at most one exclusive 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 exclusive hold, so ConditionVariableAny can wait on it. It does not take the shared hold. That is SharedLock.

Constructor & Destructor Documentation

◆ ExclusiveLock() [1/7]

StormByte::Safe::ExclusiveLock::ExclusiveLock ( )
inlinenoexcept

Empty lock.

Owns nothing.

◆ ExclusiveLock() [2/7]

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

Take the exclusive hold of mutex.

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

◆ ExclusiveLock() [3/7]

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

Associate mutex without locking it.

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

◆ ExclusiveLock() [4/7]

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

Try to take the exclusive hold of mutex.

Parameters
mutexShared mutex to try.
tagTry-lock tag.

◆ ExclusiveLock() [5/7]

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

Assume the calling thread already holds mutex exclusively.

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

◆ ExclusiveLock() [6/7]

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

◆ ExclusiveLock() [7/7]

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

Take a lock.

other owns nothing afterwards.

Parameters
otherSource.

◆ ~ExclusiveLock()

StormByte::Safe::ExclusiveLock::~ExclusiveLock ( )
inlinenoexcept

Drop the exclusive hold if this lock owns it.

Member Function Documentation

◆ lock()

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

Take the exclusive hold of the associated mutex.

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

◆ mutex()

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

Associated mutex.

Returns
The mutex, or null.

◆ operator bool()

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

Whether this lock owns the exclusive hold.

Returns
Ownership flag.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

Take a lock.

Drops the previous exclusive hold when owned.

Parameters
otherSource. Left not owning.
Returns
This lock.

◆ owns_lock()

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

Whether this lock owns the exclusive hold.

Returns
Ownership flag.

◆ release()

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

Drop the association without unlocking.

Returns
The previous mutex, or null.

◆ swap()

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

Exchange two locks.

Parameters
otherOther lock.

◆ try_lock()

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

Try to take the exclusive hold of the associated mutex.

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

◆ unlock()

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

Drop the exclusive hold.

Precondition
This lock owns the exclusive hold.

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