Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_UTIL_PHASED_LOOP_H_ |
| 2 | #define AOS_COMMON_UTIL_PHASED_LOOP_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 4 | #include "aos/common/time.h" |
| 5 | |
| 6 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | namespace time { |
| 10 | |
| 11 | // Will not be accurate if ms isn't a factor of 1000. |
| 12 | // offset is in us. |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 13 | // DEPRECATED(Brian): Use PhasedLoop instead. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | void PhasedLoopXMS(int ms, int offset); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 16 | // Handles sleeping until a fixed offset from some time interval. |
| 17 | class PhasedLoop { |
| 18 | public: |
| 19 | // For example, with interval = 1s and offset = 0.1s this will fire at: |
| 20 | // 0.1s |
| 21 | // 1.1s |
| 22 | // ... |
| 23 | // 10000.1s |
| 24 | // offset must be >= Time::kZero and < interval. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 25 | PhasedLoop( |
| 26 | const monotonic_clock::duration interval, |
| 27 | const monotonic_clock::duration offset = monotonic_clock::duration(0)) |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 28 | : interval_(interval), offset_(offset), last_time_(offset) { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 29 | CHECK_GE(offset, monotonic_clock::duration(0)); |
| 30 | CHECK_GT(interval, monotonic_clock::duration(0)); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 31 | CHECK_LT(offset, interval); |
| 32 | Reset(); |
| 33 | } |
| 34 | |
| 35 | // Resets the count of skipped iterations. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 36 | // Iterate(monotonic_now) will return 1 and set sleep_time() to something |
| 37 | // within interval of monotonic_now. |
| 38 | void Reset(const monotonic_clock::time_point monotonic_now = |
| 39 | monotonic_clock::now()) { |
| 40 | Iterate(monotonic_now - interval_); |
| 41 | } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 42 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 43 | // Calculates the next time to run after monotonic_now. |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 44 | // The result can be retrieved with sleep_time(). |
| 45 | // Returns the number of iterations which have passed (1 if this is called |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 46 | // often enough). This can be < 1 iff monotonic_now goes backwards between |
| 47 | // calls. |
| 48 | int Iterate(const monotonic_clock::time_point monotonic_now = |
| 49 | monotonic_clock::now()); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 50 | |
| 51 | // Sleeps until the next time and returns the number of iterations which have |
| 52 | // passed. |
| 53 | int SleepUntilNext() { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 54 | const int r = Iterate(monotonic_clock::now()); |
| 55 | ::std::this_thread::sleep_until(sleep_time()); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 56 | return r; |
| 57 | } |
| 58 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 59 | monotonic_clock::time_point sleep_time() const { return last_time_; } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 62 | const monotonic_clock::duration interval_, offset_; |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 63 | |
| 64 | // The time we most recently slept until. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame^] | 65 | monotonic_clock::time_point last_time_ = monotonic_clock::epoch(); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 68 | } // namespace time |
| 69 | } // namespace aos |
| 70 | |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 71 | #endif // AOS_COMMON_UTIL_PHASED_LOOP_H_ |