19#include "pw_assert/assert.h"
20#include "pw_sync/lock_annotations.h"
21#include "pw_sync/lock_traits.h"
22#include "pw_sync/virtual_basic_lockable.h"
29template <
typename GuardedType,
35 if (lock_ !=
nullptr) {
50 template <
typename G,
typename L>
52 : lock_(other.lock_), object_(other.object_) {
54 std::is_assignable_v<GuardedType*&, G*>,
55 "Attempted to construct a BorrowedPointer from another whose "
56 "GuardedType* is not assignable to this object's GuardedType*.");
57 static_assert(std::is_assignable_v<LockType*&, L*>,
58 "Attempted to construct a BorrowedPointer from another whose "
59 "LockType* is not assignable to this object's Lock*.");
60 other.lock_ =
nullptr;
61 other.object_ =
nullptr;
74 template <
typename G,
typename L>
77 std::is_assignable_v<GuardedType*&, G*>,
78 "Attempted to construct a BorrowedPointer from another whose "
79 "GuardedType* is not assignable to this object's GuardedType*.");
80 static_assert(std::is_assignable_v<LockType*&, L*>,
81 "Attempted to construct a BorrowedPointer from another whose "
82 "LockType* is not assignable to this object's Lock*.");
84 object_ = other.object_;
85 other.lock_ =
nullptr;
86 other.object_ =
nullptr;
94 PW_ASSERT(object_ !=
nullptr);
100 PW_ASSERT(object_ !=
nullptr);
116 PW_ASSERT(object_ !=
nullptr);
122 PW_ASSERT(object_ !=
nullptr);
128 template <
typename,
typename>
132 : lock_(&lock), object_(&object) {}
135 GuardedType* object_;
139 template <
typename,
typename>
162template <
typename GuardedType,
166 static_assert(is_basic_lockable_v<LockType>,
167 "lock type must satisfy BasicLockable");
169 constexpr Borrowable(GuardedType&
object, LockType& lock) noexcept
170 : lock_(&lock), object_(&
object) {}
172 template <
typename U>
174 : lock_(other.lock_), object_(other.object_) {}
183 PW_NO_LOCK_SAFETY_ANALYSIS {
190 template <
int&... ExplicitArgumentBarrier,
191 typename T = LockType,
192 typename = std::enable_if_t<is_lockable_v<T>>>
193 std::optional<BorrowedPointer<GuardedType, LockType>>
try_acquire() const
194 PW_NO_LOCK_SAFETY_ANALYSIS {
195 if (!lock_->try_lock()) {
203 template <
typename,
typename>
206 template <
typename,
typename>
214 GuardedType* object_;
std::optional< BorrowedPointer< GuardedType, LockType > > try_acquire() const
Definition: borrow.h:193
BorrowedPointer< GuardedType, LockType > acquire() const
Blocks indefinitely until the object can be borrowed. Failures are fatal.
Definition: borrow.h:182
BorrowedPointer(BorrowedPointer< G, L > &&other)
Definition: borrow.h:51
GuardedType * operator->()
Provides access to the borrowed object's members.
Definition: borrow.h:93
BorrowedPointer & operator=(BorrowedPointer< G, L > &&other)
Definition: borrow.h:75
friend class BorrowedPointer
Definition: borrow.h:140
GuardedType & operator*()
Definition: borrow.h:115
const GuardedType & operator*() const
Const overload.
Definition: borrow.h:121
const GuardedType * operator->() const
Const overload.
Definition: borrow.h:99
~BorrowedPointer()
Release the lock on destruction.
Definition: borrow.h:34
Definition: timed_borrow.h:32
Definition: virtual_basic_lockable.h:29