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

#include <borrow.h>

Inheritance diagram for pw::sync::Borrowable< GuardedType, LockType >:
pw::sync::InlineBorrowable< GuardedType, Lock, LockInterface > pw::sync::TimedBorrowable< GuardedType, LockType >

Public Member Functions

constexpr Borrowable (GuardedType &object, LockType &lock) noexcept
 
template<typename U >
constexpr Borrowable (const Borrowable< U, LockType > &other)
 
 Borrowable (const Borrowable &)=default
 
Borrowableoperator= (const Borrowable &)=default
 
 Borrowable (Borrowable &&other)=default
 
Borrowableoperator= (Borrowable &&other)=default
 
BorrowedPointer< GuardedType, LockType > acquire () const
 Blocks indefinitely until the object can be borrowed. Failures are fatal.
 
template<int &... ExplicitArgumentBarrier, typename T = LockType, typename = std::enable_if_t<is_lockable_v<T>>>
std::optional< BorrowedPointer< GuardedType, LockType > > try_acquire () const
 

Friends

template<typename , typename >
class TimedBorrowable
 

Detailed Description

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

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 C++ named requirement. A try_acquire method is conditionally available if the lock also meets the Lockable requirement. This class is further extended by TimedBorrowable for locks that meet the TimedLockable requirement.

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 LockType = pw::sync::VirtualBasicLockable>
template<int &... ExplicitArgumentBarrier, typename T = LockType, typename = std::enable_if_t<is_lockable_v<T>>>
std::optional< BorrowedPointer< GuardedType, LockType > > pw::sync::Borrowable< GuardedType, LockType >::try_acquire ( ) const
inline

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


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