17#include "pw_async2/time_provider.h"
18#include "pw_sync/interrupt_spin_lock.h"
25template <
typename Clock>
29 typename Clock::time_point timestamp =
30 typename Clock::time_point(
typename Clock::duration(0)))
36 SetTimeUnlockAndRun(now_ + duration);
45 if (next_wake_time_ == std::nullopt) {
49 SetTimeUnlockAndRun(*next_wake_time_);
58 void SetTime(
typename Clock::time_point new_now) {
60 SetTimeUnlockAndRun(new_now);
72 typename Clock::time_point
now() final {
73 std::lock_guard lock(lock_);
77 std::optional<typename Clock::time_point> NextExpiration() {
78 std::lock_guard lock(lock_);
79 return next_wake_time_;
82 std::optional<typename Clock::duration> TimeUntilNextExpiration() {
83 std::lock_guard lock(lock_);
84 if (next_wake_time_ == std::nullopt) {
87 return *next_wake_time_ - now_;
91 void SetTimeUnlockAndRun(
typename Clock::time_point new_now)
94 if (new_now >= next_wake_time_) {
95 next_wake_time_ = std::nullopt;
103 void DoInvokeAt(
typename Clock::time_point wake_time)
final {
104 std::lock_guard lock(lock_);
105 next_wake_time_ = wake_time;
118 std::lock_guard lock(lock_);
119 next_wake_time_ = std::nullopt;
127 std::optional<typename Clock::time_point> next_wake_time_
A simulated TimeProvider suitable for testing APIs which use Timer.
Definition: simulated_time_provider.h:26
bool AdvanceUntilNextExpiration()
Definition: simulated_time_provider.h:43
void RunExpiredTimers()
Definition: simulated_time_provider.h:70
void AdvanceTime(typename Clock::duration duration)
Advances the simulated time and runs any newly-expired timers.
Definition: simulated_time_provider.h:34
void DoInvokeAt(typename Clock::time_point wake_time) final
Definition: simulated_time_provider.h:103
void DoCancel() final
Optimistically cancels all pending DoInvokeAt requests.
Definition: simulated_time_provider.h:117
void SetTime(typename Clock::time_point new_now)
Definition: simulated_time_provider.h:58
Clock::time_point now() final
Returns the current time.
Definition: simulated_time_provider.h:72
Definition: time_provider.h:62
Definition: interrupt_spin_lock.h:50
void RunExpired(typename Clock::time_point now)
Definition: time_provider.h:275
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_UNLOCK_FUNCTION(...)
Definition: lock_annotations.h:247