John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/phased_loop.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 3 | #include "glog/logging.h" |
| 4 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | namespace aos { |
| 6 | namespace time { |
| 7 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 8 | PhasedLoop::PhasedLoop(const monotonic_clock::duration interval, |
| 9 | const monotonic_clock::time_point monotonic_now, |
| 10 | const monotonic_clock::duration offset) |
| 11 | : interval_(interval), offset_(offset), last_time_(offset) { |
| 12 | CHECK(offset >= monotonic_clock::duration(0)); |
| 13 | CHECK(interval > monotonic_clock::duration(0)); |
| 14 | CHECK(offset < interval); |
| 15 | Reset(monotonic_now); |
| 16 | } |
| 17 | |
| 18 | void PhasedLoop::set_interval_and_offset( |
| 19 | const monotonic_clock::duration interval, |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame^] | 20 | const monotonic_clock::duration offset, |
| 21 | std::optional<monotonic_clock::time_point> monotonic_now) { |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 22 | // Update last_time_ to the new offset so that we have an even interval |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame^] | 23 | // In doing so, set things so that last_time_ will only ever decrease on calls |
| 24 | // to set_interval_and_offset. |
| 25 | last_time_ += offset - offset_ - |
| 26 | (offset > offset_ ? interval : monotonic_clock::duration(0)); |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 27 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 28 | interval_ = interval; |
| 29 | offset_ = offset; |
| 30 | CHECK(offset_ >= monotonic_clock::duration(0)); |
| 31 | CHECK(interval_ > monotonic_clock::duration(0)); |
| 32 | CHECK(offset_ < interval_); |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame^] | 33 | // Reset effectively clears the skipped iteration count and ensures that the |
| 34 | // last time is in the interval (monotonic_now - interval, monotonic_now], |
| 35 | // which means that a call to Iterate(monotonic_now) will return 1 and set a |
| 36 | // wakeup time after monotonic_now. |
| 37 | if (monotonic_now.has_value()) { |
| 38 | Iterate(monotonic_now.value()); |
| 39 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | monotonic_clock::duration PhasedLoop::OffsetFromIntervalAndTime( |
| 43 | const monotonic_clock::duration interval, |
| 44 | const monotonic_clock::time_point monotonic_trigger) { |
| 45 | CHECK(interval > monotonic_clock::duration(0)); |
| 46 | return monotonic_trigger.time_since_epoch() - |
| 47 | (monotonic_trigger.time_since_epoch() / interval) * interval + |
| 48 | ((monotonic_trigger.time_since_epoch() >= monotonic_clock::zero()) |
| 49 | ? monotonic_clock::zero() |
| 50 | : interval); |
| 51 | } |
| 52 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 53 | int PhasedLoop::Iterate(const monotonic_clock::time_point now) { |
Brian Silverman | 8babd8f | 2020-06-23 16:38:50 -0700 | [diff] [blame] | 54 | auto next_time = monotonic_clock::epoch(); |
| 55 | // Round up to the next whole interval, ignoring offset_. |
| 56 | { |
| 57 | const auto offset_now = (now - offset_).time_since_epoch(); |
| 58 | monotonic_clock::duration prerounding; |
| 59 | if (now.time_since_epoch() >= offset_) { |
| 60 | // We're above 0, so rounding up means away from 0. |
| 61 | prerounding = offset_now + interval_; |
| 62 | } else { |
| 63 | // We're below 0, so rounding up means towards 0. |
| 64 | prerounding = offset_now + monotonic_clock::duration(1); |
| 65 | } |
| 66 | next_time += (prerounding / interval_) * interval_; |
| 67 | } |
| 68 | // Add offset_ back in. |
| 69 | next_time += offset_; |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 70 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 71 | const monotonic_clock::duration difference = next_time - last_time_; |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 72 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 73 | const int result = difference / interval_; |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 74 | CHECK_EQ( |
| 75 | 0, (next_time - offset_).time_since_epoch().count() % interval_.count()); |
Brian Silverman | 8babd8f | 2020-06-23 16:38:50 -0700 | [diff] [blame] | 76 | CHECK(next_time > now); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 77 | CHECK(next_time - now <= interval_); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 78 | last_time_ = next_time; |
| 79 | return result; |
| 80 | } |
| 81 | |
Milind Upadhyay | 42589bb | 2021-05-19 20:05:16 -0700 | [diff] [blame] | 82 | } // namespace time |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 83 | } // namespace aos |