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>
diff --git a/pw_sync/binary_semaphore_facade_test.cc b/pw_sync/binary_semaphore_facade_test.cc
index 7c2273c..d5ad28c 100644
--- a/pw_sync/binary_semaphore_facade_test.cc
+++ b/pw_sync/binary_semaphore_facade_test.cc
@@ -19,6 +19,7 @@
#include "pw_sync/binary_semaphore.h"
using pw::chrono::SystemClock;
+using namespace std::chrono_literals;
namespace pw::sync {
namespace {
@@ -32,7 +33,7 @@
bool pw_sync_BinarySemaphore_CallTryAcquire(pw_sync_BinarySemaphore* semaphore);
bool pw_sync_BinarySemaphore_CallTryAcquireFor(
pw_sync_BinarySemaphore* semaphore,
- pw_chrono_SystemClock_TickCount for_at_least);
+ pw_chrono_SystemClock_Duration for_at_least);
bool pw_sync_BinarySemaphore_CallTryAcquireUntil(
pw_sync_BinarySemaphore* semaphore,
pw_chrono_SystemClock_TimePoint until_at_least);
@@ -40,14 +41,13 @@
} // extern "C"
-static constexpr auto kArbitraryDuration = std::chrono::milliseconds(42);
// We can't control the SystemClock's period configuration, so just in case
// duration cannot be accurately expressed in integer ticks, round the
-// duration w/ duration_cast.
-static constexpr auto kRoundedArbitraryDuration =
- std::chrono::duration_cast<SystemClock::duration>(kArbitraryDuration);
-static constexpr pw_chrono_SystemClock_TickCount kRoundedArbitraryDurationInC =
- kRoundedArbitraryDuration.count();
+// duration w/ ceil.
+constexpr auto kRoundedArbitraryDuration =
+ std::chrono::ceil<SystemClock::duration>(42ms);
+constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
+ PW_SYSTEM_CLOCK_MS(42);
TEST(BinarySemaphore, EmptyInitialState) {
BinarySemaphore semaphore;
@@ -130,17 +130,17 @@
pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
ASSERT_TRUE(pw_sync_BinarySemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
- pw_chrono_SystemClock_TickCount time_elapsed =
- pw_chrono_SystemClock_Now().ticks_since_epoch - before.ticks_since_epoch;
- EXPECT_LT(time_elapsed, kRoundedArbitraryDurationInC);
+ pw_chrono_SystemClock_Duration time_elapsed =
+ pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
+ EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
// Ensure it blocks and fails when empty.
before = pw_chrono_SystemClock_Now();
EXPECT_FALSE(pw_sync_BinarySemaphore_CallTryAcquireFor(
&semaphore, kRoundedArbitraryDurationInC));
time_elapsed =
- pw_chrono_SystemClock_Now().ticks_since_epoch - before.ticks_since_epoch;
- EXPECT_GE(time_elapsed, kRoundedArbitraryDurationInC);
+ pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
+ EXPECT_GE(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
}
TEST(BinarySemaphore, TryAcquireUntilInC) {
@@ -148,18 +148,20 @@
pw_sync_BinarySemaphore_CallRelease(&semaphore);
pw_chrono_SystemClock_TimePoint deadline;
- deadline.ticks_since_epoch = pw_chrono_SystemClock_Now().ticks_since_epoch +
- kRoundedArbitraryDurationInC;
+ deadline.duration_since_epoch = {
+ .ticks = pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
+ kRoundedArbitraryDurationInC.ticks,
+ };
ASSERT_TRUE(
pw_sync_BinarySemaphore_CallTryAcquireUntil(&semaphore, deadline));
- EXPECT_LT(pw_chrono_SystemClock_Now().ticks_since_epoch,
- deadline.ticks_since_epoch);
+ EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
+ deadline.duration_since_epoch.ticks);
// Ensure it blocks and fails when empty.
EXPECT_FALSE(
pw_sync_BinarySemaphore_CallTryAcquireUntil(&semaphore, deadline));
- EXPECT_GE(pw_chrono_SystemClock_Now().ticks_since_epoch,
- deadline.ticks_since_epoch);
+ EXPECT_GE(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
+ deadline.duration_since_epoch.ticks);
}
TEST(BinarySemaphore, MaxInC) {