blob: 476cc3f9d36fa111ccdefc44942a5dfad440eb00 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/phased_loop.h"
brians343bc112013-02-10 01:53:46 +00002
Austin Schuhf257f3c2019-10-27 21:00:43 -07003#include "glog/logging.h"
4
Stephan Pleinesf63bde82024-01-13 15:59:33 -08005namespace aos::time {
brians343bc112013-02-10 01:53:46 +00006
Austin Schuhf257f3c2019-10-27 21:00:43 -07007PhasedLoop::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
17void PhasedLoop::set_interval_and_offset(
18 const monotonic_clock::duration interval,
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080019 const monotonic_clock::duration offset,
20 std::optional<monotonic_clock::time_point> monotonic_now) {
Milind Upadhyay42589bb2021-05-19 20:05:16 -070021 // Update last_time_ to the new offset so that we have an even interval
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080022 // 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 Upadhyay42589bb2021-05-19 20:05:16 -070026
Austin Schuhf257f3c2019-10-27 21:00:43 -070027 interval_ = interval;
28 offset_ = offset;
29 CHECK(offset_ >= monotonic_clock::duration(0));
30 CHECK(interval_ > monotonic_clock::duration(0));
31 CHECK(offset_ < interval_);
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080032 // 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 Schuhf257f3c2019-10-27 21:00:43 -070039}
40
41monotonic_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 Schuh8aec1ed2016-05-01 13:29:20 -070052int PhasedLoop::Iterate(const monotonic_clock::time_point now) {
Brian Silverman8babd8f2020-06-23 16:38:50 -070053 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 Silvermandcaa3f72015-11-29 05:32:08 +000069
Austin Schuh8aec1ed2016-05-01 13:29:20 -070070 const monotonic_clock::duration difference = next_time - last_time_;
Milind Upadhyay42589bb2021-05-19 20:05:16 -070071
Austin Schuh8aec1ed2016-05-01 13:29:20 -070072 const int result = difference / interval_;
Austin Schuh8aec1ed2016-05-01 13:29:20 -070073 CHECK_EQ(
74 0, (next_time - offset_).time_since_epoch().count() % interval_.count());
Brian Silverman8babd8f2020-06-23 16:38:50 -070075 CHECK(next_time > now);
Austin Schuhf257f3c2019-10-27 21:00:43 -070076 CHECK(next_time - now <= interval_);
Brian Silvermandcaa3f72015-11-29 05:32:08 +000077 last_time_ = next_time;
78 return result;
79}
80
Stephan Pleinesf63bde82024-01-13 15:59:33 -080081} // namespace aos::time