Pigweed
Loading...
Searching...
No Matches
system_clock.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <stddef.h>
17#include <stdint.h>
18
19#include "pw_preprocessor/util.h"
20
21// The backend implements this header to provide the following SystemClock
22// parameters, for more detail on the parameters see the SystemClock usage of
23// them below:
24// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR
25// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR
26// constexpr pw::chrono::Epoch pw::chrono::backend::kSystemClockEpoch;
27// constexpr bool pw::chrono::backend::kSystemClockFreeRunning;
28// constexpr bool pw::chrono::backend::kSystemClockNmiSafe;
29#include "pw_chrono_backend/system_clock_config.h"
30
31#ifdef __cplusplus
32
33#include <chrono>
34#include <ratio>
35
36namespace pw::chrono {
37namespace backend {
38
44int64_t GetSystemClockTickCount();
45
46} // namespace backend
47
79 using rep = int64_t;
81 using period = std::ratio<PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR,
82 PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR>;
84 using duration = std::chrono::duration<rep, period>;
85 using time_point = std::chrono::time_point<SystemClock>;
87 static constexpr Epoch epoch = backend::kSystemClockEpoch;
88
92 static constexpr bool is_monotonic = true;
93 static constexpr bool is_steady = false;
94
97 static constexpr bool is_free_running = backend::kSystemClockFreeRunning;
98
100 static constexpr bool is_stopped_in_halting_debug_mode = true;
101
103 static constexpr bool is_always_enabled = true;
104
108 static constexpr bool is_nmi_safe = backend::kSystemClockNmiSafe;
109
111 static time_point now() noexcept {
112 return time_point(duration(backend::GetSystemClockTickCount()));
113 }
114
118 template <class Rep, class Period>
119 static constexpr duration for_at_least(std::chrono::duration<Rep, Period> d) {
120 return std::chrono::ceil<duration>(d);
121 }
122
131 static time_point TimePointAfterAtLeast(duration after_at_least) {
132 return now() + after_at_least + duration(1);
133 }
134};
135
166 public:
169
170 virtual ~VirtualSystemClock() = default;
171
173 virtual SystemClock::time_point now() = 0;
174};
175
176} // namespace pw::chrono
177
178// The backend can opt to include an inlined implementation of the following:
179// int64_t GetSystemClockTickCount();
180#if __has_include("pw_chrono_backend/system_clock_inline.h")
181#include "pw_chrono_backend/system_clock_inline.h"
182#endif // __has_include("pw_chrono_backend/system_clock_inline.h")
183
184#endif // __cplusplus
185
186PW_EXTERN_C_START
187
188// C API Users should not create pw_chrono_SystemClock_Duration's directly,
189// instead it is strongly recommended to use macros which express the duration
190// in time units, instead of non-portable ticks.
191//
192// The following macros round up just like std::chrono::ceil, this is the
193// recommended rounding to maintain the "at least" contract of timeouts and
194// deadlines (note the *_CEIL macros are the same only more explicit):
195// PW_SYSTEM_CLOCK_MS(milliseconds)
196// PW_SYSTEM_CLOCK_S(seconds)
197// PW_SYSTEM_CLOCK_MIN(minutes)
198// PW_SYSTEM_CLOCK_H(hours)
199// PW_SYSTEM_CLOCK_MS_CEIL(milliseconds)
200// PW_SYSTEM_CLOCK_S_CEIL(seconds)
201// PW_SYSTEM_CLOCK_MIN_CEIL(minutes)
202// PW_SYSTEM_CLOCK_H_CEIL(hours)
203//
204// The following macros round down like std::chrono::{floor,duration_cast},
205// these are discouraged but sometimes necessary:
206// PW_SYSTEM_CLOCK_MS_FLOOR(milliseconds)
207// PW_SYSTEM_CLOCK_S_FLOOR(seconds)
208// PW_SYSTEM_CLOCK_MIN_FLOOR(minutes)
209// PW_SYSTEM_CLOCK_H_FLOOR(hours)
210#include "pw_chrono/internal/system_clock_macros.h"
211
212typedef struct {
213 int64_t ticks;
215
216typedef struct {
217 pw_chrono_SystemClock_Duration duration_since_epoch;
219typedef int64_t pw_chrono_SystemClock_Nanoseconds;
220
221// Returns the current time, see SystemClock::now() for more detail.
222pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now(void);
223
224// Returns the change in time between the current_time - last_time.
225pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed(
228
229// For lossless time unit conversion, the seconds per tick ratio that is
230// numerator/denominator should be used:
231// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR
232// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR
233
234// Warning, this may be lossy due to the use of std::chrono::floor,
235// rounding towards zero.
236pw_chrono_SystemClock_Nanoseconds pw_chrono_SystemClock_DurationToNsFloor(
238
239PW_EXTERN_C_END
Definition: system_clock.h:165
virtual SystemClock::time_point now()=0
Returns the current time.
static VirtualSystemClock & RealClock()
Returns a reference to the real system clock to aid instantiation.
Definition: system_clock.h:78
static constexpr Epoch epoch
The epoch must be provided by the backend.
Definition: system_clock.h:87
static constexpr bool is_nmi_safe
Definition: system_clock.h:108
static constexpr bool is_monotonic
Definition: system_clock.h:92
static constexpr duration for_at_least(std::chrono::duration< Rep, Period > d)
Definition: system_clock.h:119
static constexpr bool is_stopped_in_halting_debug_mode
The clock must stop while in halting debug mode.
Definition: system_clock.h:100
static time_point now() noexcept
This is thread and IRQ safe. This must be provided by the backend.
Definition: system_clock.h:111
static time_point TimePointAfterAtLeast(duration after_at_least)
Definition: system_clock.h:131
static constexpr bool is_free_running
Definition: system_clock.h:97
std::ratio< PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR, PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR > period
The period must be provided by the backend.
Definition: system_clock.h:82
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:84
static constexpr bool is_always_enabled
The now() function can be invoked at any time.
Definition: system_clock.h:103
Definition: system_clock.h:212
Definition: system_clock.h:216