Pigweed
|
#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 | |
Borrowable & | operator= (const Borrowable &)=default |
Borrowable (Borrowable &&other)=default | |
Borrowable & | operator= (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 |
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 BorrowedPointer
s 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.
|
inline |
Tries to borrow the object in a non-blocking manner. Returns a BorrowedPointer on success, otherwise std::nullopt
(nothing).
|
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).
|
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).