21#include "pw_async2/dispatcher.h"
22#include "pw_async2/future.h"
23#include "pw_chrono/virtual_clock.h"
24#include "pw_containers/intrusive_list.h"
25#include "pw_sync/interrupt_spin_lock.h"
26#include "pw_sync/lock_annotations.h"
27#include "pw_toolchain/no_destructor.h"
40void AssertTimeFutureObjectsAllGone(
bool empty);
46template <
typename Clock>
62template <
typename Clock>
66 internal::AssertTimeFutureObjectsAllGone(futures_.empty());
69 typename Clock::time_point
now()
override = 0;
80 return WaitUntil(
now() + delay +
typename Clock::duration(1));
88 typename Clock::time_point timestamp) {
121template <typename Clock>
131 *
this = std::move(other);
135 std::lock_guard lock(internal::time_lock());
138 provider_ = other.provider_;
139 expiration_ = other.expiration_;
142 if (!other.unlisted()) {
143 auto previous = provider_->futures_.before_begin();
144 while (&*std::next(previous) != &other) {
150 other.unlist(&*previous);
151 provider_->futures_.insert_after(previous, *
this);
183 using Base = Future<TimeFuture<Clock>,
typename Clock::time_point>;
185 friend class TimeProvider<Clock>;
187 Poll<typename Clock::time_point> DoPend(Context& cx)
189 std::lock_guard lock(internal::time_lock());
190 if (this->unlisted()) {
191 return Ready(expiration_);
200 void DoMarkComplete() {
201 std::lock_guard lock(internal::time_lock());
207 return provider_ ==
nullptr;
211 TimeFuture(TimeProvider<Clock>& provider,
212 typename Clock::time_point expiration)
213 : waker_(), provider_(&provider), expiration_(expiration) {
214 std::lock_guard lock(internal::time_lock());
222 if (provider_->now() >= expiration_) {
226 if (provider_->futures_.empty() ||
227 provider_->futures_.front().expiration_ > expiration_) {
228 provider_->futures_.push_front(*
this);
229 provider_->DoInvokeAt(expiration_);
232 auto current = provider_->futures_.begin();
233 while (std::next(current) != provider_->futures_.end() &&
234 std::next(current)->expiration_ < expiration_) {
237 provider_->futures_.insert_after(current, *
this);
241 std::lock_guard lock(internal::time_lock());
251 if (this->unlisted()) {
254 if (&provider_->futures_.front() ==
this) {
255 provider_->futures_.pop_front();
256 if (provider_->futures_.empty()) {
257 provider_->DoCancel();
259 provider_->DoInvokeAt(provider_->futures_.front().expiration_);
264 provider_->futures_.remove(*
this);
275 TimeProvider<Clock>* provider_
PW_GUARDED_BY(internal::time_lock());
276 typename Clock::time_point expiration_
PW_GUARDED_BY(internal::time_lock());
279template <
typename Clock>
281 std::lock_guard lock(internal::time_lock());
282 while (!futures_.empty()) {
283 if (futures_.front().expiration_ >
now) {
287 std::move(futures_.front().waker_).Wake();
288 futures_.pop_front();
Definition: intrusive_forward_list.h:91
Definition: time_provider.h:124
~TimeFuture()
Definition: time_provider.h:160
Definition: time_provider.h:63
virtual void DoCancel()=0
Optimistically cancels all pending DoInvokeAt requests.
Clock::time_point now() override=0
Returns the current time.
TimeFuture< Clock > WaitFor(typename Clock::duration delay)
Definition: time_provider.h:75
virtual void DoInvokeAt(typename Clock::time_point)=0
TimeFuture< Clock > WaitUntil(typename Clock::time_point timestamp)
Definition: time_provider.h:87
Definition: virtual_clock.h:31
Definition: intrusive_list.h:88
Definition: interrupt_spin_lock.h:50
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
#define PW_ASYNC_STORE_WAKER(context, waker_or_queue_out, wait_reason_string)
Definition: waker.h:60
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
void RunExpired(typename Clock::time_point now)
Definition: time_provider.h:280
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_NO_LOCK_SAFETY_ANALYSIS
Definition: lock_annotations.h:292
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176