blob: 1f617757c91c64daaf0e12c03837996dcfa3fc96 [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
brians343bc112013-02-10 01:53:46 +00005namespace aos {
6namespace time {
7
Austin Schuhf257f3c2019-10-27 21:00:43 -07008PhasedLoop::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
18void PhasedLoop::set_interval_and_offset(
19 const monotonic_clock::duration interval,
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080020 const monotonic_clock::duration offset,
21 std::optional<monotonic_clock::time_point> monotonic_now) {
Milind Upadhyay42589bb2021-05-19 20:05:16 -070022 // Update last_time_ to the new offset so that we have an even interval
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080023 // 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 Upadhyay42589bb2021-05-19 20:05:16 -070027
Austin Schuhf257f3c2019-10-27 21:00:43 -070028 interval_ = interval;
29 offset_ = offset;
30 CHECK(offset_ >= monotonic_clock::duration(0));
31 CHECK(interval_ > monotonic_clock::duration(0));
32 CHECK(offset_ < interval_);
James Kuszmaul20dcc7c2023-01-20 11:06:31 -080033 // 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 Schuhf257f3c2019-10-27 21:00:43 -070040}
41
42monotonic_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 Schuh8aec1ed2016-05-01 13:29:20 -070053int PhasedLoop::Iterate(const monotonic_clock::time_point now) {
Brian Silverman8babd8f2020-06-23 16:38:50 -070054 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 Silvermandcaa3f72015-11-29 05:32:08 +000070
Austin Schuh8aec1ed2016-05-01 13:29:20 -070071 const monotonic_clock::duration difference = next_time - last_time_;
Milind Upadhyay42589bb2021-05-19 20:05:16 -070072
Austin Schuh8aec1ed2016-05-01 13:29:20 -070073 const int result = difference / interval_;
Austin Schuh8aec1ed2016-05-01 13:29:20 -070074 CHECK_EQ(
75 0, (next_time - offset_).time_since_epoch().count() % interval_.count());
Brian Silverman8babd8f2020-06-23 16:38:50 -070076 CHECK(next_time > now);
Austin Schuhf257f3c2019-10-27 21:00:43 -070077 CHECK(next_time - now <= interval_);
Brian Silvermandcaa3f72015-11-29 05:32:08 +000078 last_time_ = next_time;
79 return result;
80}
81
Milind Upadhyay42589bb2021-05-19 20:05:16 -070082} // namespace time
brians343bc112013-02-10 01:53:46 +000083} // namespace aos