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 | |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 11 | // Handles sleeping until a fixed offset from some time interval. |
| 12 | class PhasedLoop { |
| 13 | public: |
| 14 | // For example, with interval = 1s and offset = 0.1s this will fire at: |
| 15 | // 0.1s |
| 16 | // 1.1s |
| 17 | // ... |
| 18 | // 10000.1s |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 19 | // offset must be >= chrono::seconds(0) and < interval. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 20 | PhasedLoop( |
| 21 | const monotonic_clock::duration interval, |
| 22 | const monotonic_clock::duration offset = monotonic_clock::duration(0)) |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 23 | : interval_(interval), offset_(offset), last_time_(offset) { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 24 | CHECK_GE(offset, monotonic_clock::duration(0)); |
| 25 | CHECK_GT(interval, monotonic_clock::duration(0)); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 26 | CHECK_LT(offset, interval); |
| 27 | Reset(); |
| 28 | } |
| 29 | |
Austin Schuh | 5d4b098 | 2017-04-08 14:36:08 -0700 | [diff] [blame^] | 30 | // Updates the offset and interval. |
| 31 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 32 | const monotonic_clock::duration offset) { |
| 33 | interval_ = interval; |
| 34 | offset_ = offset; |
| 35 | CHECK_GE(offset_, monotonic_clock::duration(0)); |
| 36 | CHECK_GT(interval_, monotonic_clock::duration(0)); |
| 37 | CHECK_LT(offset_, interval_); |
| 38 | } |
| 39 | |
| 40 | // Computes the offset given an interval and a time that we should trigger. |
| 41 | static monotonic_clock::duration OffsetFromIntervalAndTime( |
| 42 | const monotonic_clock::duration interval, |
| 43 | const monotonic_clock::time_point monotonic_trigger) { |
| 44 | CHECK_GT(interval, monotonic_clock::duration(0)); |
| 45 | return monotonic_trigger.time_since_epoch() - |
| 46 | (monotonic_trigger.time_since_epoch() / interval) * interval + |
| 47 | ((monotonic_trigger.time_since_epoch() >= monotonic_clock::zero()) |
| 48 | ? monotonic_clock::zero() |
| 49 | : interval); |
| 50 | } |
| 51 | |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 52 | // Resets the count of skipped iterations. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 53 | // Iterate(monotonic_now) will return 1 and set sleep_time() to something |
| 54 | // within interval of monotonic_now. |
| 55 | void Reset(const monotonic_clock::time_point monotonic_now = |
| 56 | monotonic_clock::now()) { |
| 57 | Iterate(monotonic_now - interval_); |
| 58 | } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 59 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 60 | // Calculates the next time to run after monotonic_now. |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 61 | // The result can be retrieved with sleep_time(). |
| 62 | // 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] | 63 | // often enough). This can be < 1 iff monotonic_now goes backwards between |
| 64 | // calls. |
| 65 | int Iterate(const monotonic_clock::time_point monotonic_now = |
| 66 | monotonic_clock::now()); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 67 | |
| 68 | // Sleeps until the next time and returns the number of iterations which have |
| 69 | // passed. |
| 70 | int SleepUntilNext() { |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 71 | const int r = Iterate(monotonic_clock::now()); |
| 72 | ::std::this_thread::sleep_until(sleep_time()); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 73 | return r; |
| 74 | } |
| 75 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 76 | monotonic_clock::time_point sleep_time() const { return last_time_; } |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
Austin Schuh | 5d4b098 | 2017-04-08 14:36:08 -0700 | [diff] [blame^] | 79 | monotonic_clock::duration interval_, offset_; |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 80 | |
| 81 | // The time we most recently slept until. |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 82 | monotonic_clock::time_point last_time_ = monotonic_clock::epoch(); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | } // namespace time |
| 86 | } // namespace aos |
| 87 | |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 88 | #endif // AOS_COMMON_UTIL_PHASED_LOOP_H_ |