20#include "pw_assert/assert.h"
21#include "pw_async2/context.h"
22#include "pw_async2/poll.h"
23#include "pw_containers/intrusive_list.h"
24#include "pw_sync/interrupt_spin_lock.h"
26namespace pw::async2::experimental {
59template <
typename Derived,
typename T>
75 derived().DoMarkComplete();
86 constexpr Future() =
default;
89 Derived& derived() {
return static_cast<Derived&
>(*this); }
90 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
95template <
typename D,
typename V>
96std::true_type IsFuture(
const volatile Future<D, V>*);
98std::false_type IsFuture(...);
103struct is_future : decltype(internal::IsFuture(std::declval<T*>())){};
106constexpr bool is_future_v =
132template <
typename FutureType,
typename Lock = sync::InterruptSpinLock>
141 void Push(FutureType& future) {
142 std::lock_guard
lock(lock_);
150 std::lock_guard
lock(lock_);
151 PW_ASSERT(!futures_.
empty());
152 FutureType& future = futures_.
front();
159 std::lock_guard
lock(lock_);
160 return futures_.
empty();
169 template <
typename,
typename>
172 using LockType = Lock;
189template <
typename FutureType>
198 void Set(FutureType& future) {
214 [[nodiscard]] FutureType&
Take() {
return inner_.Pop(); }
220 template <
typename,
typename>
277template <
typename Derived,
typename T>
279 :
public Future<ListableFutureWithWaker<Derived, T>, T>,
295 : provider_(nullptr), complete_(true) {}
298 provider.Push(derived());
300 explicit ListableFutureWithWaker(SingleFutureProvider<Derived>& single)
301 : ListableFutureWithWaker(single.inner_) {}
303 ~ListableFutureWithWaker() {
304 if (provider_ ==
nullptr) {
307 std::lock_guard guard(lock());
308 if (!this->unlisted()) {
313 void MoveFrom(ListableFutureWithWaker& other) {
314 complete_ = std::exchange(other.complete_,
true);
315 provider_ = std::exchange(other.provider_,
nullptr);
316 waker_ = std::move(other.waker_);
319 std::lock_guard guard(lock());
320 if (!other.unlisted()) {
321 this->replace(other);
326 Provider& provider() {
327 PW_ASSERT(provider_ !=
nullptr);
331 typename Provider::LockType& lock() {
return provider().
lock(); }
334 void Wake() { std::move(waker_).Wake(); }
344 std::is_same_v<std::remove_extent_t<
decltype(Derived::kWaitReason)>,
346 "kWaitReason must be a character array");
348 Poll<T> poll = derived().DoPend(cx);
355 void DoMarkComplete() { complete_ =
true; }
356 bool DoIsComplete()
const {
return complete_; }
358 Derived& derived() {
return static_cast<Derived&
>(*this); }
362 bool complete_ =
false;
Poll< value_type > Pend(Context &cx)
Definition: future.h:71
bool is_complete() const
Definition: future.h:83
FutureType & Pop()
Definition: future.h:149
void Push(FutureType &future)
Adds a future to the end of the list.
Definition: future.h:141
Lock & lock()
Provides access to the list's internal lock.
Definition: future.h:164
bool empty()
Returns true if there are no futures listed.
Definition: future.h:158
void Wake()
Wakes the task waiting on the future.
Definition: future.h:334
ListableFutureWithWaker(MovedFromState)
Definition: future.h:294
MovedFromState
Tag to prevent accidental default construction.
Definition: future.h:289
void Set(FutureType &future)
Sets the provider's future. Crashes if a future is already set.
Definition: future.h:198
bool TrySet(FutureType &future)
Attempts to set the provider's future, returning true if successful.
Definition: future.h:204
FutureType & Take()
Definition: future.h:214
bool has_future()
Returns true if the provider has a future.
Definition: future.h:217
Definition: intrusive_list.h:88
constexpr bool IsReady() const noexcept
Returns whether or not this value is Ready.
Definition: poll.h:133
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:136
#define PW_ASYNC_STORE_WAKER(context, waker_or_queue_out, wait_reason_string)
Definition: waker.h:60
void push_back(T &item)
Inserts an element at the end of the list.
Definition: intrusive_list.h:255
bool empty() const noexcept
Definition: intrusive_list.h:207
void pop_front()
Removes the first item in the list. The list must not be empty.
Definition: intrusive_list.h:264
T & front()
Reference to the first element in the list. Undefined behavior if empty().
Definition: intrusive_list.h:167