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>
123 : public experimental::Future<
TimeFuture<Clock>,
124 typename Clock::time_point>,
132 *
this = std::move(other);
136 std::lock_guard lock(internal::time_lock());
139 provider_ = other.provider_;
140 expiration_ = other.expiration_;
143 if (!other.unlisted()) {
144 auto previous = provider_->futures_.before_begin();
145 while (&*std::next(previous) != &other) {
151 other.unlist(&*previous);
152 provider_->futures_.insert_after(previous, *
this);
185 experimental::Future<TimeFuture<Clock>,
typename Clock::time_point>;
187 friend class TimeProvider<Clock>;
189 Poll<typename Clock::time_point> DoPend(Context& cx)
191 std::lock_guard lock(internal::time_lock());
192 if (this->unlisted()) {
193 return Ready(expiration_);
202 void DoMarkComplete() {
203 std::lock_guard lock(internal::time_lock());
209 return provider_ ==
nullptr;
213 TimeFuture(TimeProvider<Clock>& provider,
214 typename Clock::time_point expiration)
215 : waker_(), provider_(&provider), expiration_(expiration) {
216 std::lock_guard lock(internal::time_lock());
224 if (provider_->now() >= expiration_) {
228 if (provider_->futures_.empty() ||
229 provider_->futures_.front().expiration_ > expiration_) {
230 provider_->futures_.push_front(*
this);
231 provider_->DoInvokeAt(expiration_);
234 auto current = provider_->futures_.begin();
235 while (std::next(current) != provider_->futures_.end() &&
236 std::next(current)->expiration_ < expiration_) {
239 provider_->futures_.insert_after(current, *
this);
243 std::lock_guard lock(internal::time_lock());
253 if (this->unlisted()) {
256 if (&provider_->futures_.front() ==
this) {
257 provider_->futures_.pop_front();
258 if (provider_->futures_.empty()) {
259 provider_->DoCancel();
261 provider_->DoInvokeAt(provider_->futures_.front().expiration_);
266 provider_->futures_.remove(*
this);
277 TimeProvider<Clock>* provider_
PW_GUARDED_BY(internal::time_lock());
278 typename Clock::time_point expiration_
PW_GUARDED_BY(internal::time_lock());
281template <
typename Clock>
283 std::lock_guard lock(internal::time_lock());
284 while (!futures_.empty()) {
285 if (futures_.front().expiration_ >
now) {
289 std::move(futures_.front().waker_).Wake();
290 futures_.pop_front();
Definition: intrusive_forward_list.h:91
Definition: time_provider.h:125
~TimeFuture()
Definition: time_provider.h:161
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:282
#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