EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
UniqueLockPtr< T, LockType > Class Template Reference

Overview

template<typename T, typename LockType>
class etcpal::UniqueLockPtr< T, LockType >

An RAII handle for mutable access whose lock may or may not be held.

Like ConstUniqueLockPtr, but hands out mutable access. Returned by SynchronizedValue::TryToSynchronize() and the *Synchronize() factories, or constructed directly. Move-only.

Template Parameters
TThe type of the guarded value.
LockTypeThe type of lock guarding the value.
Inheritance diagram for UniqueLockPtr< T, LockType >:
ConstUniqueLockPtr< T, LockType >

Public Member Functions

 UniqueLockPtr (SynchronizedValue< T, LockType > &sync)
 Lock the given SynchronizedValue for mutable access, blocking until the lock is acquired. More...
 
 UniqueLockPtr (SynchronizedValue< T, LockType > &sync, AdoptLockTag) noexcept
 Adopt the given SynchronizedValue's lock, which the calling thread must already hold.
 
 UniqueLockPtr (SynchronizedValue< T, LockType > &sync, DeferLockTag) noexcept
 Reference the given SynchronizedValue without acquiring the lock.
 
 UniqueLockPtr (SynchronizedValue< T, LockType > &sync, TryToLockTag, int timeout_ms=0) noexcept
 Try to lock the given SynchronizedValue, waiting up to timeout_ms; check OwnsLock() for the result. More...
 
T & operator* ()
 Get a mutable reference to the guarded value. More...
 
T * operator-> ()
 Access a member of the guarded value. More...
 
- Public Member Functions inherited from ConstUniqueLockPtr< T, LockType >
 ConstUniqueLockPtr (const SynchronizedValue< T, LockType > &sync)
 Lock the given SynchronizedValue for read-only access, blocking until the lock is acquired. More...
 
 ConstUniqueLockPtr (const SynchronizedValue< T, LockType > &sync, AdoptLockTag) noexcept
 Adopt the given SynchronizedValue's lock, which the calling thread must already hold.
 
 ConstUniqueLockPtr (const SynchronizedValue< T, LockType > &sync, DeferLockTag) noexcept
 Reference the given SynchronizedValue without acquiring the lock.
 
 ConstUniqueLockPtr (const SynchronizedValue< T, LockType > &sync, TryToLockTag, int timeout_ms=0) noexcept
 Try to lock the given SynchronizedValue, waiting up to timeout_ms; check OwnsLock() for the result. More...
 
 ConstUniqueLockPtr (const ConstUniqueLockPtr &)=delete
 
ConstUniqueLockPtroperator= (const ConstUniqueLockPtr &)=delete
 
ConstUniqueLockPtroperator= (ConstUniqueLockPtr &&)=delete
 
 ConstUniqueLockPtr (ConstUniqueLockPtr &&other) noexcept
 Move ownership from another handle, which is left empty.
 
 ~ConstUniqueLockPtr () noexcept
 Release the lock if owned.
 
 operator bool () const noexcept
 Whether this handle currently owns the lock.
 
bool OwnsLock () const noexcept
 Whether this handle currently owns the lock.
 
void Unlock () noexcept
 Release the lock early, if owned. Does nothing otherwise.
 
void Lock ()
 Acquire the lock, blocking until it is available. More...
 
bool TryLock (int timeout_ms=0)
 Try to acquire the lock, waiting up to timeout_ms. More...
 
void Release () noexcept
 Disassociate from the lock without unlocking it; the caller becomes responsible for the lock.
 
const T & operator* () const
 Get a reference to the guarded value. More...
 
const T * operator-> () const
 Access a member of the guarded value. More...
 

Additional Inherited Members

- Protected Attributes inherited from ConstUniqueLockPtr< T, LockType >
const SynchronizedValue< T, LockType > * sync_
 
bool owns_
 

Constructor & Destructor Documentation

◆ UniqueLockPtr() [1/2]

UniqueLockPtr ( SynchronizedValue< T, LockType > &  sync)
inlineexplicit

Lock the given SynchronizedValue for mutable access, blocking until the lock is acquired.

Exceptions
std::runtime_errorif lock could not be acquired.

◆ UniqueLockPtr() [2/2]

UniqueLockPtr ( SynchronizedValue< T, LockType > &  sync,
TryToLockTag  ,
int  timeout_ms = 0 
)
inlinenoexcept

Try to lock the given SynchronizedValue, waiting up to timeout_ms; check OwnsLock() for the result.

timeout_ms has the same semantics as TryLock().

Member Function Documentation

◆ operator*()

T& operator* ( )
inline

Get a mutable reference to the guarded value.

Exceptions
std::runtime_errorif this handle does not own the lock.

◆ operator->()

T* operator-> ( )
inline

Access a member of the guarded value.

Exceptions
std::runtime_errorif this handle does not own the lock.

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