Pigweed
Loading...
Searching...
No Matches
pw::sync::Borrowable< GuardedType, Lock > Class Template Reference

#include <borrow.h>

Public Member Functions

constexpr Borrowable (GuardedType &object, Lock &lock) noexcept
 
template<typename U >
constexpr Borrowable (const Borrowable< U, Lock > &other)
 
 Borrowable (const Borrowable &)=default
 
Borrowableoperator= (const Borrowable &)=default
 
 Borrowable (Borrowable &&other)=default
 
Borrowableoperator= (Borrowable &&other)=default
 
BorrowedPointer< GuardedType, Lock > acquire () const
 Blocks indefinitely until the object can be borrowed. Failures are fatal.
 
template<int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t<is_lockable_v<T>>>
std::optional< BorrowedPointer< GuardedType, Lock > > try_acquire () const
 
template<class Rep , class Period , int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t< is_lockable_for_v<T, std::chrono::duration<Rep, Period>>>>
std::optional< BorrowedPointer< GuardedType, Lock > > try_acquire_for (std::chrono::duration< Rep, Period > timeout) const
 
template<class Clock , class Duration , int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t< is_lockable_until_v<T, std::chrono::time_point<Clock, Duration>>>>
std::optional< BorrowedPointer< GuardedType, Lock > > try_acquire_until (std::chrono::time_point< Clock, Duration > deadline) const
 

Detailed Description

template<typename GuardedType, typename Lock = pw::sync::VirtualBasicLockable>
class pw::sync::Borrowable< GuardedType, Lock >

The Borrowable is a helper construct that enables callers to borrow an object which is guarded by a lock.

Users who need access to the guarded object can ask to acquire a BorrowedPointer which permits access while the lock is held.

Thread-safety analysis is not supported for this class, as the BorrowedPointers it creates conditionally releases the lock. See also https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-conditionally-held-locks

This class is compatible with locks which comply with BasicLockable, Lockable, and TimedLockable C++ named requirements.

Borrowable<T> is covariant with respect to T, so that Borrowable<U> can be converted to Borrowable<T>, if U is a subclass of T.

Borrowable has pointer-like semantics and should be passed by value.

Member Function Documentation

◆ try_acquire()

template<typename GuardedType , typename Lock = pw::sync::VirtualBasicLockable>
template<int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t<is_lockable_v<T>>>
std::optional< BorrowedPointer< GuardedType, Lock > > pw::sync::Borrowable< GuardedType, Lock >::try_acquire ( ) const
inline

Tries to borrow the object in a non-blocking manner. Returns a BorrowedPointer on success, otherwise std::nullopt (nothing).

◆ try_acquire_for()

template<typename GuardedType , typename Lock = pw::sync::VirtualBasicLockable>
template<class Rep , class Period , int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t< is_lockable_for_v<T, std::chrono::duration<Rep, Period>>>>
std::optional< BorrowedPointer< GuardedType, Lock > > pw::sync::Borrowable< GuardedType, Lock >::try_acquire_for ( std::chrono::duration< Rep, Period >  timeout) const
inline

Tries to borrow the object. Blocks until the specified timeout has elapsed or the object has been borrowed, whichever comes first. Returns a BorrowedPointer on success, otherwise std::nullopt (nothing).

◆ try_acquire_until()

template<typename GuardedType , typename Lock = pw::sync::VirtualBasicLockable>
template<class Clock , class Duration , int &... ExplicitArgumentBarrier, typename T = Lock, typename = std::enable_if_t< is_lockable_until_v<T, std::chrono::time_point<Clock, Duration>>>>
std::optional< BorrowedPointer< GuardedType, Lock > > pw::sync::Borrowable< GuardedType, Lock >::try_acquire_until ( std::chrono::time_point< Clock, Duration >  deadline) const
inline

Tries to borrow the object. Blocks until the specified deadline has passed or the object has been borrowed, whichever comes first. Returns a BorrowedPointer on success, otherwise std::nullopt (nothing).


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