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"
31template <
typename GuardedType,
37 if (lock_ !=
nullptr) {
52 template <
typename G,
typename L>
54 : lock_(other.lock_), object_(other.object_) {
56 std::is_assignable_v<GuardedType*&, G*>,
57 "Attempted to construct a BorrowedPointer from another whose "
58 "GuardedType* is not assignable to this object's GuardedType*.");
59 static_assert(std::is_assignable_v<LockType*&, L*>,
60 "Attempted to construct a BorrowedPointer from another whose "
61 "LockType* is not assignable to this object's Lock*.");
62 other.lock_ =
nullptr;
63 other.object_ =
nullptr;
76 template <
typename G,
typename L>
79 std::is_assignable_v<GuardedType*&, G*>,
80 "Attempted to construct a BorrowedPointer from another whose "
81 "GuardedType* is not assignable to this object's GuardedType*.");
82 static_assert(std::is_assignable_v<LockType*&, L*>,
83 "Attempted to construct a BorrowedPointer from another whose "
84 "LockType* is not assignable to this object's Lock*.");
86 object_ = other.object_;
87 other.lock_ =
nullptr;
88 other.object_ =
nullptr;
96 PW_ASSERT(object_ !=
nullptr);
102 PW_ASSERT(object_ !=
nullptr);
114 PW_ASSERT(object_ !=
nullptr);
120 PW_ASSERT(object_ !=
nullptr);
126 template <
typename,
typename>
130 : lock_(&lock), object_(&object) {}
133 GuardedType* object_;
137 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_) {}
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
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:53
GuardedType * operator->()
Provides access to the borrowed object's members.
Definition: borrow.h:95
BorrowedPointer & operator=(BorrowedPointer< G, L > &&other)
Definition: borrow.h:77
friend class BorrowedPointer
Definition: borrow.h:138
GuardedType & operator*()
Definition: borrow.h:113
const GuardedType & operator*() const
Const overload.
Definition: borrow.h:119
const GuardedType * operator->() const
Const overload.
Definition: borrow.h:101
~BorrowedPointer()
Release the lock on destruction.
Definition: borrow.h:36
Definition: timed_borrow.h:34
Definition: virtual_basic_lockable.h:31
#define PW_NO_LOCK_SAFETY_ANALYSIS
Definition: lock_annotations.h:292
Definition: binary_semaphore.h:26