diff --git a/base_cpp/include/embb/base/duration.h b/base_cpp/include/embb/base/duration.h index 5c6d2e8..4dbc4e4 100644 --- a/base_cpp/include/embb/base/duration.h +++ b/base_cpp/include/embb/base/duration.h @@ -46,6 +46,9 @@ namespace base { * Represents a relative time duration for a given tick type. * * \notthreadsafe + * \note The typedefs DurationSeconds, DurationMilliseconds, + * DurationMicroseconds, and DurationNanoseconds provide directly usable + * duration types. * \tparam Tick Possible tick types are Seconds, Milliseconds, Microseconds, * Nanoseconds * \ingroup CPP_BASE_TIMEDURATION @@ -271,6 +274,8 @@ Duration operator+( return Duration(lhs.Count() + rhs.Count()); } +namespace internal { + /** * Base class for ticks. */ @@ -517,6 +522,33 @@ class Nanoseconds : public Tick { static unsigned long long Max(); }; +} // namespace internal + +/** + * Duration with seconds tick. + * + * \ingroup CPP_BASE_TIMEDURATION + */ +typedef Duration DurationSeconds; +/** + * Duration with milliseconds tick. + * + * \ingroup CPP_BASE_TIMEDURATION + */ +typedef Duration DurationMilliseconds; +/** + * Duration with microseconds tick. + * + * \ingroup CPP_BASE_TIMEDURATION + */ +typedef Duration DurationMicroseconds; +/** + * Duration with nanoseconds tick. + * + * \ingroup CPP_BASE_TIMEDURATION + */ +typedef Duration DurationNanoseconds; + } // namespace base } // namespace embb diff --git a/base_cpp/src/duration.cc b/base_cpp/src/duration.cc index 38ed5d1..ae710d3 100644 --- a/base_cpp/src/duration.cc +++ b/base_cpp/src/duration.cc @@ -35,7 +35,7 @@ namespace embb { namespace base { -void Tick::CheckExceptions(int status, const char* msg) { +void internal::Tick::CheckExceptions(int status, const char* msg) { switch (status) { case EMBB_SUCCESS: return; case EMBB_OVERFLOW: EMBB_THROW(OverflowException, msg); @@ -44,16 +44,16 @@ void Tick::CheckExceptions(int status, const char* msg) { } } -int Seconds::Set(embb_duration_t& duration, unsigned long long ticks) { +int internal::Seconds::Set(embb_duration_t& duration, unsigned long long ticks) { return embb_duration_set_seconds(&duration, ticks); } -void Seconds::SetAndCheck(embb_duration_t& duration, unsigned long long ticks) { +void internal::Seconds::SetAndCheck(embb_duration_t& duration, unsigned long long ticks) { int status = Set(duration, ticks); CheckExceptions(status, "Setting duration from seconds"); } -unsigned long long Seconds::Get(const embb_duration_t& duration) { +unsigned long long internal::Seconds::Get(const embb_duration_t& duration) { unsigned long long ticks = 0; int status = embb_duration_as_seconds(&duration, &ticks); assert(status == EMBB_SUCCESS); @@ -61,25 +61,25 @@ unsigned long long Seconds::Get(const embb_duration_t& duration) { return ticks; } -unsigned long long Seconds::Min() { +unsigned long long internal::Seconds::Min() { return 1; } -unsigned long long Seconds::Max() { +unsigned long long internal::Seconds::Max() { return EMBB_DURATION_MAX_SECONDS; } -int Milliseconds::Set(embb_duration_t& duration, unsigned long long ticks) { +int internal::Milliseconds::Set(embb_duration_t& duration, unsigned long long ticks) { return embb_duration_set_milliseconds(&duration, ticks); } -void Milliseconds::SetAndCheck( +void internal::Milliseconds::SetAndCheck( embb_duration_t& duration, unsigned long long ticks) { int status = Set(duration, ticks); CheckExceptions(status, "Setting duration from milliseconds"); } -unsigned long long Milliseconds::Get(const embb_duration_t& duration) { +unsigned long long internal::Milliseconds::Get(const embb_duration_t& duration) { unsigned long long ticks = 0; int status = embb_duration_as_milliseconds(&duration, &ticks); assert(status == EMBB_SUCCESS); @@ -87,7 +87,7 @@ unsigned long long Milliseconds::Get(const embb_duration_t& duration) { return ticks; } -unsigned long long Milliseconds::Min() { +unsigned long long internal::Milliseconds::Min() { #if EMBB_DURATION_MIN_NANOSECONDS > 1000000 assert(EMBB_DURATION_MIN_NANOSECONDS % 1000000 == 0); return EMBB_DURATION_MIN_NANOSECONDS / 1000000; @@ -95,7 +95,7 @@ unsigned long long Milliseconds::Min() { return 1; } -unsigned long long Milliseconds::Max() { +unsigned long long internal::Milliseconds::Max() { #if EMBB_DURATION_MAX_SECONDS < ULLONG_MAX / 1000 return ULLONG_MAX; #else @@ -103,17 +103,17 @@ unsigned long long Milliseconds::Max() { #endif } -int Microseconds::Set(embb_duration_t& duration, unsigned long long ticks) { +int internal::Microseconds::Set(embb_duration_t& duration, unsigned long long ticks) { return embb_duration_set_microseconds(&duration, ticks); } -void Microseconds::SetAndCheck( +void internal::Microseconds::SetAndCheck( embb_duration_t& duration, unsigned long long ticks) { int status = Set(duration, ticks); CheckExceptions(status, "Setting duration from microseconds"); } -unsigned long long Microseconds::Get(const embb_duration_t& duration) { +unsigned long long internal::Microseconds::Get(const embb_duration_t& duration) { unsigned long long ticks = 0; int status = embb_duration_as_microseconds(&duration, &ticks); @@ -123,7 +123,7 @@ unsigned long long Microseconds::Get(const embb_duration_t& duration) { return ticks; } -unsigned long long Microseconds::Min() { +unsigned long long internal::Microseconds::Min() { #if EMBB_DURATION_MIN_NANOSECONDS > 1000 assert(EMBB_DURATION_MIN_NANOSECONDS % 1000 == 0); return EMBB_DURATION_MIN_NANOSECONDS / 1000; @@ -131,7 +131,7 @@ unsigned long long Microseconds::Min() { return 1; } -unsigned long long Microseconds::Max() { +unsigned long long internal::Microseconds::Max() { #if EMBB_DURATION_MAX_SECONDS < ULLONG_MAX / 1000000 return ULLONG_MAX; #else @@ -139,17 +139,17 @@ unsigned long long Microseconds::Max() { #endif } -int Nanoseconds::Set(embb_duration_t& duration, unsigned long long ticks) { +int internal::Nanoseconds::Set(embb_duration_t& duration, unsigned long long ticks) { return embb_duration_set_nanoseconds(&duration, ticks); } -void Nanoseconds::SetAndCheck( +void internal::Nanoseconds::SetAndCheck( embb_duration_t& duration, unsigned long long ticks) { int status = Set(duration, ticks); CheckExceptions(status, "Setting duration from microseconds"); } -unsigned long long Nanoseconds::Get(const embb_duration_t& duration) { +unsigned long long internal::Nanoseconds::Get(const embb_duration_t& duration) { unsigned long long ticks = 0; int status = embb_duration_as_nanoseconds(&duration, &ticks); assert(status == EMBB_SUCCESS); @@ -157,11 +157,11 @@ unsigned long long Nanoseconds::Get(const embb_duration_t& duration) { return ticks; } -unsigned long long Nanoseconds::Min() { +unsigned long long internal::Nanoseconds::Min() { return EMBB_DURATION_MIN_NANOSECONDS; } -unsigned long long Nanoseconds::Max() { +unsigned long long internal::Nanoseconds::Max() { #if EMBB_DURATION_MAX_SECONDS < ULLONG_MAX / 1000000000 return ULLONG_MAX; #else diff --git a/base_cpp/test/condition_var_test.cc b/base_cpp/test/condition_var_test.cc index fd7a3e6..a023cf0 100644 --- a/base_cpp/test/condition_var_test.cc +++ b/base_cpp/test/condition_var_test.cc @@ -63,15 +63,15 @@ void ConditionVarTest::TestTimedWaitTimeouts() { PT_EXPECT_EQ(success, false); // Wait for a future timepoint - success = cond.WaitUntil(lock, Time(Duration(1))); + success = cond.WaitUntil(lock, Time(DurationMilliseconds(1))); PT_EXPECT_EQ(success, false); // Wait for a zero duration - success = cond.WaitFor(lock, Duration()); + success = cond.WaitFor(lock, DurationMilliseconds()); PT_EXPECT_EQ(success, false); // Wait for some duration - success = cond.WaitFor(lock, Duration(1)); + success = cond.WaitFor(lock, DurationMilliseconds(1)); PT_EXPECT_EQ(success, false); } @@ -96,14 +96,14 @@ void ConditionVarTest::TestNotify() { cond_notify_.NotifyOne(); - cond_wait_.WaitUntil(lock_wait, Time(Duration(1))); + cond_wait_.WaitUntil(lock_wait, Time(DurationMilliseconds(1))); while (embb_counter_get(&counter_) == 0) {} // If hangs here signal has not succeeded PT_ASSERT_EQ_MSG(embb_counter_get(&counter_), static_cast(1), "Only 1 thread notified"); cond_notify_.NotifyAll(); - cond_wait_.WaitUntil(lock_wait, Time(Duration(2))); + cond_wait_.WaitUntil(lock_wait, Time(DurationMilliseconds(2))); while (embb_counter_get(&counter_) != static_cast(num_threads_-1)) diff --git a/base_cpp/test/duration_test.cc b/base_cpp/test/duration_test.cc index c7da972..71a16bc 100644 --- a/base_cpp/test/duration_test.cc +++ b/base_cpp/test/duration_test.cc @@ -31,10 +31,10 @@ namespace base { namespace test { DurationTest::DurationTest() { - CreateUnit("Seconds").Add(&DurationTest::Test, this); - CreateUnit("Milliseconds").Add(&DurationTest::Test, this); - CreateUnit("Microseconds").Add(&DurationTest::Test, this); - CreateUnit("Nanoseconds").Add(&DurationTest::Test, this); + CreateUnit("Seconds").Add(&DurationTest::Test, this); + CreateUnit("Milliseconds").Add(&DurationTest::Test, this); + CreateUnit("Microseconds").Add(&DurationTest::Test, this); + CreateUnit("Nanoseconds").Add(&DurationTest::Test, this); } } // namespace test