21#include "pw_async2/dispatcher.h"
22#include "pw_chrono/virtual_clock.h"
23#include "pw_containers/intrusive_list.h"
24#include "pw_sync/interrupt_spin_lock.h"
25#include "pw_sync/lock_annotations.h"
26#include "pw_toolchain/no_destructor.h"
39void AssertTimeFutureObjectsAllGone(
bool empty);
45template <
typename Clock>
61template <
typename Clock>
65 internal::AssertTimeFutureObjectsAllGone(futures_.empty());
68 typename Clock::time_point
now()
override = 0;
79 return WaitUntil(
now() + delay +
typename Clock::duration(1));
87 typename Clock::time_point timestamp) {
120template <typename Clock>
129 *
this = std::move(other);
133 std::lock_guard lock(internal::time_lock());
136 provider_ = other.provider_;
137 expiration_ = other.expiration_;
140 if (!other.unlisted()) {
141 auto previous = provider_->futures_.before_begin();
142 while (&*std::next(previous) != &other) {
148 other.unlist(&*previous);
149 provider_->futures_.insert_after(previous, *
this);
162 std::lock_guard lock(internal::time_lock());
163 if (this->unlisted()) {
164 return Ready(expiration_);
174 void Reset(
typename Clock::time_point expiration)
176 std::lock_guard lock(internal::time_lock());
178 expiration_ = expiration;
203 friend class TimeProvider<Clock>;
206 TimeFuture(TimeProvider<Clock>& provider,
207 typename Clock::time_point expiration)
208 : waker_(), provider_(&provider), expiration_(expiration) {
209 std::lock_guard lock(internal::time_lock());
217 if (provider_->now() >= expiration_) {
221 if (provider_->futures_.empty() ||
222 provider_->futures_.front().expiration_ > expiration_) {
223 provider_->futures_.push_front(*
this);
224 provider_->DoInvokeAt(expiration_);
227 auto current = provider_->futures_.begin();
228 while (std::next(current) != provider_->futures_.end() &&
229 std::next(current)->expiration_ < expiration_) {
232 provider_->futures_.insert_after(current, *
this);
236 std::lock_guard lock(internal::time_lock());
246 if (this->unlisted()) {
249 if (&provider_->futures_.front() ==
this) {
250 provider_->futures_.pop_front();
251 if (provider_->futures_.empty()) {
252 provider_->DoCancel();
254 provider_->DoInvokeAt(provider_->futures_.front().expiration_);
259 provider_->futures_.remove(*
this);
270 TimeProvider<Clock>* provider_
PW_GUARDED_BY(internal::time_lock());
271 typename Clock::time_point expiration_
PW_GUARDED_BY(internal::time_lock());
274template <
typename Clock>
276 std::lock_guard lock(internal::time_lock());
277 while (!futures_.empty()) {
278 if (futures_.front().expiration_ >
now) {
282 std::move(futures_.front().waker_).Wake();
283 futures_.pop_front();
Definition: intrusive_forward_list.h:91
Definition: time_provider.h:122
~TimeFuture()
Definition: time_provider.h:158
void Reset(typename Clock::time_point expiration)
Resets TimeFuture to expire at expiration.
Definition: time_provider.h:174
Definition: time_provider.h:62
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:74
virtual void DoInvokeAt(typename Clock::time_point)=0
TimeFuture< Clock > WaitUntil(typename Clock::time_point timestamp)
Definition: time_provider.h:86
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:275
#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