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
36#include "pw_chrono/virtual_clock.h"
37
38namespace pw::chrono {
39namespace backend {
40
46int64_t GetSystemClockTickCount();
47
48} // namespace backend
49
81 using rep = int64_t;
83 using period = std::ratio<PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR,
84 PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR>;
86 using duration = std::chrono::duration<rep, period>;
87 using time_point = std::chrono::time_point<SystemClock>;
89 static constexpr Epoch epoch = backend::kSystemClockEpoch;
90
94 static constexpr bool is_monotonic = true;
95 static constexpr bool is_steady = false;
96
99 static constexpr bool is_free_running = backend::kSystemClockFreeRunning;
100
102 static constexpr bool is_stopped_in_halting_debug_mode = true;
103
105 static constexpr bool is_always_enabled = true;
106
110 static constexpr bool is_nmi_safe = backend::kSystemClockNmiSafe;
111
113 static time_point now() noexcept {
114 return time_point(duration(backend::GetSystemClockTickCount()));
115 }
116
120 template <class Rep, class Period>
121 static constexpr duration for_at_least(std::chrono::duration<Rep, Period> d) {
122 return std::chrono::ceil<duration>(d);
123 }
124
133 static time_point TimePointAfterAtLeast(duration after_at_least) {
134 return now() + after_at_least + duration(1);
135 }
136};
137
138// NOTE: VirtualClock here is specialized on SystemClock in order to provide
139// the `RealClock` function.
140//
141// SystemClock is defined in this file, so there's no risk of an ODR violation
142// as other libraries are unable to spell VirtualClock<SystemClock> unless
143// they have first included this file.
144
174template <>
176 public:
179
180 virtual ~VirtualClock() = default;
181
183 virtual SystemClock::time_point now() = 0;
184};
185
187
188} // namespace pw::chrono
189
190// The backend can opt to include an inlined implementation of the following:
191// int64_t GetSystemClockTickCount();
192#if __has_include("pw_chrono_backend/system_clock_inline.h")
193#include "pw_chrono_backend/system_clock_inline.h"
194#endif // __has_include("pw_chrono_backend/system_clock_inline.h")
195
196#endif // __cplusplus
197
198PW_EXTERN_C_START
199
200// C API Users should not create pw_chrono_SystemClock_Duration's directly,
201// instead it is strongly recommended to use macros which express the duration
202// in time units, instead of non-portable ticks.
203//
204// The following macros round up just like std::chrono::ceil, this is the
205// recommended rounding to maintain the "at least" contract of timeouts and
206// deadlines (note the *_CEIL macros are the same only more explicit):
207// PW_SYSTEM_CLOCK_MS(milliseconds)
208// PW_SYSTEM_CLOCK_S(seconds)
209// PW_SYSTEM_CLOCK_MIN(minutes)
210// PW_SYSTEM_CLOCK_H(hours)
211// PW_SYSTEM_CLOCK_MS_CEIL(milliseconds)
212// PW_SYSTEM_CLOCK_S_CEIL(seconds)
213// PW_SYSTEM_CLOCK_MIN_CEIL(minutes)
214// PW_SYSTEM_CLOCK_H_CEIL(hours)
215//
216// The following macros round down like std::chrono::{floor,duration_cast},
217// these are discouraged but sometimes necessary:
218// PW_SYSTEM_CLOCK_MS_FLOOR(milliseconds)
219// PW_SYSTEM_CLOCK_S_FLOOR(seconds)
220// PW_SYSTEM_CLOCK_MIN_FLOOR(minutes)
221// PW_SYSTEM_CLOCK_H_FLOOR(hours)
222#include "pw_chrono/internal/system_clock_macros.h"
223
224typedef struct {
225 int64_t ticks;
227
228typedef struct {
229 pw_chrono_SystemClock_Duration duration_since_epoch;
231typedef int64_t pw_chrono_SystemClock_Nanoseconds;
232
233// Returns the current time, see SystemClock::now() for more detail.
234pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now(void);
235
236// Returns the change in time between the current_time - last_time.
237pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed(
240
241// For lossless time unit conversion, the seconds per tick ratio that is
242// numerator/denominator should be used:
243// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR
244// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR
245
246// Warning, this may be lossy due to the use of std::chrono::floor,
247// rounding towards zero.
248pw_chrono_SystemClock_Nanoseconds pw_chrono_SystemClock_DurationToNsFloor(
250
251PW_EXTERN_C_END
Definition: system_clock.h:175
virtual SystemClock::time_point now()=0
Returns the current time.
static VirtualClock< SystemClock > & RealClock()
Returns a reference to the real system clock to aid instantiation.
Definition: virtual_clock.h:29
Definition: system_clock.h:80
static constexpr Epoch epoch
The epoch must be provided by the backend.
Definition: system_clock.h:89
static constexpr bool is_nmi_safe
Definition: system_clock.h:110
static constexpr bool is_monotonic
Definition: system_clock.h:94
static constexpr duration for_at_least(std::chrono::duration< Rep, Period > d)
Definition: system_clock.h:121
static constexpr bool is_stopped_in_halting_debug_mode
The clock must stop while in halting debug mode.
Definition: system_clock.h:102
static time_point now() noexcept
This is thread and IRQ safe. This must be provided by the backend.
Definition: system_clock.h:113
static time_point TimePointAfterAtLeast(duration after_at_least)
Definition: system_clock.h:133
static constexpr bool is_free_running
Definition: system_clock.h:99
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:84
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:86
static constexpr bool is_always_enabled
The now() function can be invoked at any time.
Definition: system_clock.h:105
Definition: system_clock.h:224
Definition: system_clock.h:228