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