17#include "pw_async2/time_provider.h"
18#include "pw_sync/interrupt_spin_lock.h"
23template <
typename Clock>
27 typename Clock::time_point timestamp =
28 typename Clock::time_point(
typename Clock::duration(0)))
34 SetTimeUnlockAndRun(now_ + duration);
43 if (next_wake_time_ == std::nullopt) {
47 SetTimeUnlockAndRun(*next_wake_time_);
56 void SetTime(
typename Clock::time_point new_now) {
58 SetTimeUnlockAndRun(new_now);
70 typename Clock::time_point
now() final {
71 std::lock_guard lock(lock_);
75 std::optional<typename Clock::time_point> NextExpiration() {
76 std::lock_guard lock(lock_);
77 return next_wake_time_;
80 std::optional<typename Clock::duration> TimeUntilNextExpiration() {
81 std::lock_guard lock(lock_);
82 if (next_wake_time_ == std::nullopt) {
85 return *next_wake_time_ - now_;
89 void SetTimeUnlockAndRun(
typename Clock::time_point new_now)
90 PW_UNLOCK_FUNCTION(lock_) {
92 if (new_now >= next_wake_time_) {
93 next_wake_time_ = std::nullopt;
101 void DoInvokeAt(
typename Clock::time_point wake_time)
final {
102 std::lock_guard lock(lock_);
103 next_wake_time_ = wake_time;
116 std::lock_guard lock(lock_);
117 next_wake_time_ = std::nullopt;
124 typename Clock::time_point now_ PW_GUARDED_BY(lock_);
125 std::optional<typename Clock::time_point> next_wake_time_
126 PW_GUARDED_BY(lock_);
A simulated TimeProvider suitable for testing APIs which use Timer.
Definition: simulated_time_provider.h:24
bool AdvanceUntilNextExpiration()
Definition: simulated_time_provider.h:41
void RunExpiredTimers()
Definition: simulated_time_provider.h:68
void AdvanceTime(typename Clock::duration duration)
Advances the simulated time and runs any newly-expired timers.
Definition: simulated_time_provider.h:32
void DoInvokeAt(typename Clock::time_point wake_time) final
Definition: simulated_time_provider.h:101
void DoCancel() final
Optimistically cancels all pending DoInvokeAt requests.
Definition: simulated_time_provider.h:115
void SetTime(typename Clock::time_point new_now)
Definition: simulated_time_provider.h:56
Clock::time_point now() final
Returns the current time.
Definition: simulated_time_provider.h:70
Definition: time_provider.h:60
void RunExpired(typename Clock::time_point now)
Definition: time_provider.h:273
Definition: interrupt_spin_lock.h:48