1 #ifndef _MADARA_UTILITY_EPOCHENFORCER_H_ 2 #define _MADARA_UTILITY_EPOCHENFORCER_H_ 16 template <
typename CLOCK=std::chrono::steady_clock>
47 std::chrono::duration<double> temp_duration (duration);
49 std::chrono::nanoseconds> (temp_duration);
60 std::chrono::duration<double> temp_period (period);
61 period_ = std::chrono::duration_cast<
62 std::chrono::nanoseconds> (temp_period);
72 auto current_time = CLOCK::now ();
74 while (
next_ <= current_time)
84 auto current_time = CLOCK::now ();
86 return current_time >=
next_;
94 std::this_thread::sleep_until (
next_);
105 return CLOCK::now () >=
max_;
114 auto dur = std::chrono::duration_cast<std::chrono::nanoseconds> (
117 return (uint64_t) dur.count ();
126 auto dur = std::chrono::duration_cast<std::chrono::seconds> (
129 return (uint64_t) dur.count ();
138 auto dur = std::chrono::duration_cast<std::chrono::duration <double>> (
141 return (
double) dur.count ();
148 template <
typename PERIOD=std::chrono::nanoseconds>
151 auto dur = std::chrono::duration_cast<PERIOD> (
154 return (uint64_t) dur.count ();
162 std::chrono::time_point<CLOCK>
max_;
165 std::chrono::time_point<CLOCK>
next_;
174 #endif // _MADARA_UTILITY_EPOCHENFORCER_H_ std::chrono::time_point< CLOCK > start_
the start of the timer duration
uint64_t duration(void) const
Returns a duration in user-specified period.
void start(void)
Starts the timer.
std::chrono::time_point< CLOCK > max_
the end of the timer duration
std::chrono::time_point< CLOCK > next_
the next epoch to sleep to
void advance_next(void)
Advance next.
EpochEnforcer(double period, double max_duration)
Constructor.
bool is_done(void) const
Checks to see if max duration is finished.
uint64_t duration_s(void) const
Returns a duration in seconds.
Enforces a periodic epoch.
void set_period(double period)
Sets the period for the enforcer.
double duration_ds(void) const
Returns a duration in seconds (double format)
Provides utility functions and classes for common tasks and needs.
void set_duration(double duration)
Sets the maximum duration for the enforcer.
uint64_t duration_ns(void) const
Returns a duration in nanoseconds.
Copyright (c) 2015 Carnegie Mellon University.
std::chrono::nanoseconds period_
the period of the epochs
void sleep_until_next(void)
Sleeps until the next epoch.
bool has_reached_next(void) const
Checks if current time is at or past next.