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