aboutsummaryrefslogtreecommitdiff
path: root/pw_chrono_stl
diff options
context:
space:
mode:
authorEwout van Bekkum <ewout@google.com>2021-01-28 18:58:03 -0800
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2021-01-30 01:48:08 +0000
commit830d5d1ac09c300b7c268f7768e7187a3f4de74e (patch)
tree3b8aaaa1c2eddfdda141d86ad261383c4c54a416 /pw_chrono_stl
parentcddc5cda875b2eea7dc3fbf0a2be0c1a9edd2b3a (diff)
downloadpigweed-830d5d1ac09c300b7c268f7768e7187a3f4de74e.tar.gz
pw_chrono: Improve SystemClock C API
Changes the SystemClock C Api to: 1) Use a pw_chrono_SystemClock_Duration struct instead of aliasing an int64_t under pw_chrono_SystemClock_TickCount which could accidentally permit direct tick usage. 2) Add PW_SYSTEM_CLOCK_{MS,S,MIN,H} and PW_SYSTEM_CLOCK_{MS,S,MIN,H}_CEIL to permit C API users to create durations which round up to the nearest tick for deadlines and timeouts, mirroring std::chrono::ceil. 3) Add PW_SYSTEM_CLOCK_{MS,S,MIN,H}_FLOOR to permit C API users to create durations which round down to the nearest tick for oddball corner cases, mirroring std::chrono::floor. 4) In order to enable said macros, the system_clock_config.h backend config was changed to require the clock period as a preprocessor defines instead of a std::ratio<>. 5) Renames pw_chrono_SystemClock_TimeDelta to pw_chrono_SystemClock_TimeElapsed to make the argument ordering make more sense. 6) Changes existing std::chrono::duration_cast usage to std::chrono::ceil and std::chrono:floor to set a good example to be explicit on rounding. 7) Renames pw_chrono_SystemClock_TickCountsToNsTruncate accordingly to pw_chrono_SystemClock_DurationToNsFloor. Requires: pigweed-internal:9340 Change-Id: Ia628dceac53f964eda7c4aacd3d790b8b0e92207 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/31280 Commit-Queue: Ewout van Bekkum <ewout@google.com> Reviewed-by: Wyatt Hepler <hepler@google.com>
Diffstat (limited to 'pw_chrono_stl')
-rw-r--r--pw_chrono_stl/public/pw_chrono_stl/system_clock_config.h14
1 files changed, 10 insertions, 4 deletions
diff --git a/pw_chrono_stl/public/pw_chrono_stl/system_clock_config.h b/pw_chrono_stl/public/pw_chrono_stl/system_clock_config.h
index 05b1c712b..9766fa244 100644
--- a/pw_chrono_stl/public/pw_chrono_stl/system_clock_config.h
+++ b/pw_chrono_stl/public/pw_chrono_stl/system_clock_config.h
@@ -13,15 +13,19 @@
// the License.
#pragma once
-#include <chrono>
+// Ideally we'd use std::chrono::steady_clock::period, however this is not
+// something we can expose to the C API. Instead we assume it has nanosecond
+// compatibility and we rely on implicit conversion to tell us at compile time
+// whether this is incompatible.
+#define PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR 1
+#define PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR 100'000'0000
+
+#ifdef __cplusplus
#include "pw_chrono/epoch.h"
namespace pw::chrono::backend {
-// Provide the native std::chrono::steady_clock period.
-using SystemClockPeriodSecondsRatio = std::chrono::steady_clock::period;
-
// The std::chrono::steady_clock does not have a defined epoch.
constexpr inline Epoch kSystemClockEpoch = pw::chrono::Epoch::kUnknown;
@@ -32,3 +36,5 @@ constexpr inline bool kSystemClockNmiSafe = true;
constexpr inline bool kSystemClockFreeRunning = true;
} // namespace pw::chrono::backend
+
+#endif // __cplusplus