Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Member Functions | Friends | List of all members
pw::async2::TimeProvider< Clock > Class Template Referenceabstract
Inheritance diagram for pw::async2::TimeProvider< Clock >:
pw::chrono::VirtualClock< Clock > pw::async2::SimulatedTimeProvider< Clock >

Public Member Functions

Clock::time_point now () override=0
 Returns the current time.
 
TimeFuture< Clock > WaitFor (typename Clock::duration delay)
 
TimeFuture< Clock > WaitUntil (typename Clock::time_point timestamp)
 
virtual Clock::time_point now ()=0
 Returns the current time.
 

Protected Member Functions

void RunExpired (typename Clock::time_point now)
 

Private Member Functions

virtual void DoInvokeAt (typename Clock::time_point)=0
 
virtual void DoCancel ()=0
 Optimistically cancels all pending DoInvokeAt requests.
 

Friends

class TimeFuture< Clock >
 

Detailed Description

template<typename Clock>
class pw::async2::TimeProvider< Clock >

A factory for time and timers.

This extends the VirtualClock interface with the ability to create async timers.

TimeProvider is designed to be dependency-injection friendly so that code that uses time and timers is not bound to real wall-clock time. This is particularly helpful for testing timing-sensitive code without adding manual delays to tests (which often results in flakiness and long-running tests).

Note that Timer objects must not outlive the TimeProvider from which they were created.

Member Function Documentation

◆ DoCancel()

template<typename Clock >
virtual void pw::async2::TimeProvider< Clock >::DoCancel ( )
privatepure virtual

Optimistically cancels all pending DoInvokeAt requests.

Implemented in pw::async2::SimulatedTimeProvider< Clock >.

◆ DoInvokeAt()

template<typename Clock >
virtual void pw::async2::TimeProvider< Clock >::DoInvokeAt ( typename Clock::time_point  )
privatepure virtual

Schedule RunExpired to be invoked at time_point. Newer calls to DoInvokeAt supersede previous calls.

Implemented in pw::async2::SimulatedTimeProvider< Clock >.

◆ now()

template<typename Clock >
Clock::time_point pw::async2::TimeProvider< Clock >::now ( )
overridepure virtual

Returns the current time.

Implements pw::chrono::VirtualClock< Clock >.

Implemented in pw::async2::SimulatedTimeProvider< Clock >.

◆ RunExpired()

template<typename Clock >
void pw::async2::TimeProvider< Clock >::RunExpired ( typename Clock::time_point  now)
protected

Run all expired timers with the current (provided) time_point.

This method should be invoked by subclasses when DoInvokeAt's timer expires.

◆ WaitFor()

template<typename Clock >
TimeFuture< Clock > pw::async2::TimeProvider< Clock >::WaitFor ( typename Clock::duration  delay)
inline

Queues the callback to be invoked after delay.

This method is thread-safe and can be invoked from callback but may not be interrupt-safe on all platforms.

The time_point is computed based on now() plus the specified duration where a singular clock tick is added to handle partial ticks. This ensures that a duration of at least 1 tick does not result in [0,1] ticks and instead in [1,2] ticks.

◆ WaitUntil()

template<typename Clock >
TimeFuture< Clock > pw::async2::TimeProvider< Clock >::WaitUntil ( typename Clock::time_point  timestamp)
inline

Queues the callback to be invoked after timestamp.

This method is thread-safe and can be invoked from callback but may not be interrupt-safe on all platforms.


The documentation for this class was generated from the following file: