annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/timer.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>)
jpayne@69 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
jpayne@69 3 // Licensed under the MIT License:
jpayne@69 4 //
jpayne@69 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
jpayne@69 6 // of this software and associated documentation files (the "Software"), to deal
jpayne@69 7 // in the Software without restriction, including without limitation the rights
jpayne@69 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jpayne@69 9 // copies of the Software, and to permit persons to whom the Software is
jpayne@69 10 // furnished to do so, subject to the following conditions:
jpayne@69 11 //
jpayne@69 12 // The above copyright notice and this permission notice shall be included in
jpayne@69 13 // all copies or substantial portions of the Software.
jpayne@69 14 //
jpayne@69 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jpayne@69 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jpayne@69 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jpayne@69 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jpayne@69 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jpayne@69 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jpayne@69 21 // THE SOFTWARE.
jpayne@69 22
jpayne@69 23 #pragma once
jpayne@69 24
jpayne@69 25 #include <kj/time.h>
jpayne@69 26 #include "async.h"
jpayne@69 27
jpayne@69 28 KJ_BEGIN_HEADER
jpayne@69 29
jpayne@69 30 namespace kj {
jpayne@69 31
jpayne@69 32 class Timer: public MonotonicClock {
jpayne@69 33 // Interface to time and timer functionality.
jpayne@69 34 //
jpayne@69 35 // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different
jpayne@69 36 // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However,
jpayne@69 37 // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar
jpayne@69 38 // date as tracked by the system is manually modified.
jpayne@69 39 //
jpayne@69 40 // That said, the `Timer` returned by `kj::setupAsyncIo().provider->getTimer()` in particular is
jpayne@69 41 // guaranteed to be synchronized with the `MonotonicClock` returned by
jpayne@69 42 // `systemPreciseMonotonicClock()` (or, more precisely, is updated to match that clock whenever
jpayne@69 43 // the loop waits).
jpayne@69 44 //
jpayne@69 45 // Note that the value returned by `Timer::now()` only changes each time the
jpayne@69 46 // event loop waits for I/O from the system. While the event loop is actively
jpayne@69 47 // running, the time stays constant. This is intended to make behavior more
jpayne@69 48 // deterministic and reproducible. However, if you need up-to-the-cycle
jpayne@69 49 // accurate time, then `Timer::now()` is not appropriate. Instead, use
jpayne@69 50 // `systemPreciseMonotonicClock()` directly in this case.
jpayne@69 51
jpayne@69 52 public:
jpayne@69 53 virtual TimePoint now() const = 0;
jpayne@69 54 // Returns the current value of a clock that moves steadily forward, independent of any
jpayne@69 55 // changes in the wall clock. The value is updated every time the event loop waits,
jpayne@69 56 // and is constant in-between waits.
jpayne@69 57
jpayne@69 58 virtual Promise<void> atTime(TimePoint time) = 0;
jpayne@69 59 // Returns a promise that returns as soon as now() >= time.
jpayne@69 60
jpayne@69 61 virtual Promise<void> afterDelay(Duration delay) = 0;
jpayne@69 62 // Equivalent to atTime(now() + delay).
jpayne@69 63
jpayne@69 64 template <typename T>
jpayne@69 65 Promise<T> timeoutAt(TimePoint time, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT;
jpayne@69 66 // Return a promise equivalent to `promise` but which throws an exception (and cancels the
jpayne@69 67 // original promise) if it hasn't completed by `time`. The thrown exception is of type
jpayne@69 68 // "OVERLOADED".
jpayne@69 69
jpayne@69 70 template <typename T>
jpayne@69 71 Promise<T> timeoutAfter(Duration delay, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT;
jpayne@69 72 // Return a promise equivalent to `promise` but which throws an exception (and cancels the
jpayne@69 73 // original promise) if it hasn't completed after `delay` from now. The thrown exception is of
jpayne@69 74 // type "OVERLOADED".
jpayne@69 75
jpayne@69 76 private:
jpayne@69 77 static kj::Exception makeTimeoutException();
jpayne@69 78 };
jpayne@69 79
jpayne@69 80 class TimerImpl final: public Timer {
jpayne@69 81 // Implementation of Timer that expects an external caller -- usually, the EventPort
jpayne@69 82 // implementation -- to tell it when time has advanced.
jpayne@69 83
jpayne@69 84 public:
jpayne@69 85 TimerImpl(TimePoint startTime);
jpayne@69 86 ~TimerImpl() noexcept(false);
jpayne@69 87
jpayne@69 88 Maybe<TimePoint> nextEvent();
jpayne@69 89 // Returns the time at which the next scheduled timer event will occur, or null if no timer
jpayne@69 90 // events are scheduled.
jpayne@69 91
jpayne@69 92 Maybe<uint64_t> timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max);
jpayne@69 93 // Convenience method which computes a timeout value to pass to an event-waiting system call to
jpayne@69 94 // cause it to time out when the next timer event occurs.
jpayne@69 95 //
jpayne@69 96 // `start` is the time at which the timeout starts counting. This is typically not the same as
jpayne@69 97 // now() since some time may have passed since the last time advanceTo() was called.
jpayne@69 98 //
jpayne@69 99 // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note
jpayne@69 100 // that this method will fractional values *up*, to guarantee that the returned timeout waits
jpayne@69 101 // until just *after* the time the event is scheduled.
jpayne@69 102 //
jpayne@69 103 // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a
jpayne@69 104 // 32-bit value or a signed value.
jpayne@69 105 //
jpayne@69 106 // Returns nullptr if there are no future events.
jpayne@69 107
jpayne@69 108 void advanceTo(TimePoint newTime);
jpayne@69 109 // Set the time to `time` and fire any at() events that have been passed.
jpayne@69 110
jpayne@69 111 // implements Timer ----------------------------------------------------------
jpayne@69 112 TimePoint now() const override;
jpayne@69 113 Promise<void> atTime(TimePoint time) override;
jpayne@69 114 Promise<void> afterDelay(Duration delay) override;
jpayne@69 115
jpayne@69 116 private:
jpayne@69 117 struct Impl;
jpayne@69 118 class TimerPromiseAdapter;
jpayne@69 119 TimePoint time;
jpayne@69 120 Own<Impl> impl;
jpayne@69 121 };
jpayne@69 122
jpayne@69 123 // =======================================================================================
jpayne@69 124 // inline implementation details
jpayne@69 125
jpayne@69 126 template <typename T>
jpayne@69 127 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) {
jpayne@69 128 return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise<T> {
jpayne@69 129 return makeTimeoutException();
jpayne@69 130 }));
jpayne@69 131 }
jpayne@69 132
jpayne@69 133 template <typename T>
jpayne@69 134 Promise<T> Timer::timeoutAfter(Duration delay, Promise<T>&& promise) {
jpayne@69 135 return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> {
jpayne@69 136 return makeTimeoutException();
jpayne@69 137 }));
jpayne@69 138 }
jpayne@69 139
jpayne@69 140 inline TimePoint TimerImpl::now() const { return time; }
jpayne@69 141
jpayne@69 142 } // namespace kj
jpayne@69 143
jpayne@69 144 KJ_END_HEADER