MADARA  3.2.3
EpochEnforcer.h
Go to the documentation of this file.
1 #ifndef _MADARA_UTILITY_EPOCHENFORCER_H_
2 #define _MADARA_UTILITY_EPOCHENFORCER_H_
3 
4 #include <thread>
5 #include <chrono>
6 #include "IntTypes.h"
7 
8 namespace madara
9 {
10  namespace utility
11  {
16  template <typename CLOCK=std::chrono::steady_clock>
18  {
19  public:
25  EpochEnforcer (double period, double max_duration)
26  {
27  start ();
28  set_period (period);
29  set_duration (max_duration);
30  }
31 
35  inline void start (void)
36  {
37  start_ = CLOCK::now ();
38  next_ = start_;
39  }
40 
45  inline void set_duration (double duration)
46  {
47  std::chrono::duration<double> temp_duration (duration);
48  auto duration_ns = std::chrono::duration_cast<
49  std::chrono::nanoseconds> (temp_duration);
50 
52  }
53 
58  inline void set_period (double period)
59  {
60  std::chrono::duration<double> temp_period (period);
61  period_ = std::chrono::duration_cast<
62  std::chrono::nanoseconds> (temp_period);
63 
64  next_ = start_ + period_;
65  }
66 
70  inline void advance_next (void)
71  {
72  auto current_time = CLOCK::now ();
73 
74  while (next_ <= current_time)
75  next_ += period_;
76  }
77 
82  inline bool has_reached_next (void) const
83  {
84  auto current_time = CLOCK::now ();
85 
86  return current_time >= next_;
87  }
88 
92  inline void sleep_until_next (void)
93  {
94  std::this_thread::sleep_until (next_);
95 
96  advance_next ();
97  }
98 
103  inline bool is_done (void) const
104  {
105  return CLOCK::now () >= max_;
106  }
107 
112  inline uint64_t duration_ns (void) const
113  {
114  auto dur = std::chrono::duration_cast<std::chrono::nanoseconds> (
115  CLOCK::now () - start_);
116 
117  return (uint64_t) dur.count ();
118  }
119 
124  inline uint64_t duration_s (void) const
125  {
126  auto dur = std::chrono::duration_cast<std::chrono::seconds> (
127  CLOCK::now () - start_);
128 
129  return (uint64_t) dur.count ();
130  }
131 
136  inline double duration_ds (void) const
137  {
138  auto dur = std::chrono::duration_cast<std::chrono::duration <double>> (
139  CLOCK::now () - start_);
140 
141  return (double) dur.count ();
142  }
143 
148  template <typename PERIOD=std::chrono::nanoseconds>
149  inline uint64_t duration (void) const
150  {
151  auto dur = std::chrono::duration_cast<PERIOD> (
152  CLOCK::now () - start_);
153 
154  return (uint64_t) dur.count ();
155  }
156 
157  private:
159  std::chrono::time_point<CLOCK> start_;
160 
162  std::chrono::time_point<CLOCK> max_;
163 
165  std::chrono::time_point<CLOCK> next_;
166 
168  std::chrono::nanoseconds period_;
169 
170  };
171  }
172 }
173 
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.
Definition: EpochEnforcer.h:35
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.
Definition: EpochEnforcer.h:70
EpochEnforcer(double period, double max_duration)
Constructor.
Definition: EpochEnforcer.h:25
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.
Definition: EpochEnforcer.h:17
void set_period(double period)
Sets the period for the enforcer.
Definition: EpochEnforcer.h:58
double duration_ds(void) const
Returns a duration in seconds (double format)
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
void set_duration(double duration)
Sets the maximum duration for the enforcer.
Definition: EpochEnforcer.h:45
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.
Definition: EpochEnforcer.h:92
bool has_reached_next(void) const
Checks if current time is at or past next.
Definition: EpochEnforcer.h:82