Pigweed
Loading...
Searching...
No Matches
pw::chrono::SystemClock Struct Reference

#include <system_clock.h>

Public Types

using rep = int64_t
 
using period = std::ratio< PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR, PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR >
 The period must be provided by the backend.
 
using duration = std::chrono::duration< rep, period >
 Alias for durations representable with this clock.
 
using time_point = std::chrono::time_point< SystemClock >
 

Static Public Member Functions

static time_point now () noexcept
 This is thread and IRQ safe. This must be provided by the backend.
 
template<class Rep , class Period >
static constexpr duration for_at_least (std::chrono::duration< Rep, Period > d)
 
static time_point TimePointAfterAtLeast (duration after_at_least)
 

Static Public Attributes

static constexpr Epoch epoch = backend::kSystemClockEpoch
 The epoch must be provided by the backend.
 
static constexpr bool is_monotonic = true
 
static constexpr bool is_steady = false
 
static constexpr bool is_free_running = backend::kSystemClockFreeRunning
 
static constexpr bool is_stopped_in_halting_debug_mode = true
 The clock must stop while in halting debug mode.
 
static constexpr bool is_always_enabled = true
 The now() function can be invoked at any time.
 
static constexpr bool is_nmi_safe = backend::kSystemClockNmiSafe
 

Detailed Description

The SystemClock represents an unsteady, monotonic clock.

The epoch of this clock is unspecified and may not be related to wall time (for example, it can be time since boot). The time between ticks of this clock may vary due to sleep modes and potential interrupt handling. SystemClock meets the requirements of C++'s TrivialClock and Pigweed's PigweedClock.

SystemClock is compatible with C++'s Clock & TrivialClock including:

Example:

SystemClock::time_point before = SystemClock::now();
TakesALongTime();
SystemClock::duration time_taken = SystemClock::now() - before;
bool took_way_too_long = false;
if (time_taken > std::chrono::seconds(42)) {
took_way_too_long = true;
}
static time_point now() noexcept
This is thread and IRQ safe. This must be provided by the backend.
Definition: system_clock.h:111
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:84

This code is thread & IRQ safe, it may be NMI safe depending on is_nmi_safe.

Member Function Documentation

◆ for_at_least()

template<class Rep , class Period >
static constexpr duration pw::chrono::SystemClock::for_at_least ( std::chrono::duration< Rep, Period >  d)
inlinestaticconstexpr

This is purely a helper, identical to directly using std::chrono::ceil, to convert a duration type which cannot be implicitly converted where the result is rounded up.

◆ TimePointAfterAtLeast()

static time_point pw::chrono::SystemClock::TimePointAfterAtLeast ( duration  after_at_least)
inlinestatic

Computes the nearest time_point after the specified duration has elapsed.

This is useful for translating delay or timeout durations into deadlines.

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.

Member Data Documentation

◆ is_free_running

constexpr bool pw::chrono::SystemClock::is_free_running = backend::kSystemClockFreeRunning
staticconstexpr

The now() function may not move forward while in a critical section or interrupt. This must be provided by the backend.

◆ is_monotonic

constexpr bool pw::chrono::SystemClock::is_monotonic = true
staticconstexpr

The time points of this clock cannot decrease, however the time between ticks of this clock may slightly vary due to sleep modes. The duration during sleep may be ignored or backfilled with another clock.

◆ is_nmi_safe

constexpr bool pw::chrono::SystemClock::is_nmi_safe = backend::kSystemClockNmiSafe
staticconstexpr

The now() function may work in non-maskable interrupt contexts (e.g. exception/fault handlers), depending on the backend. This must be provided by the backend.


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