jpayne@69: // Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) jpayne@69: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors jpayne@69: // Licensed under the MIT License: jpayne@69: // jpayne@69: // Permission is hereby granted, free of charge, to any person obtaining a copy jpayne@69: // of this software and associated documentation files (the "Software"), to deal jpayne@69: // in the Software without restriction, including without limitation the rights jpayne@69: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jpayne@69: // copies of the Software, and to permit persons to whom the Software is jpayne@69: // furnished to do so, subject to the following conditions: jpayne@69: // jpayne@69: // The above copyright notice and this permission notice shall be included in jpayne@69: // all copies or substantial portions of the Software. jpayne@69: // jpayne@69: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jpayne@69: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jpayne@69: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jpayne@69: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jpayne@69: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jpayne@69: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jpayne@69: // THE SOFTWARE. jpayne@69: jpayne@69: #pragma once jpayne@69: jpayne@69: #include jpayne@69: #include "async.h" jpayne@69: jpayne@69: KJ_BEGIN_HEADER jpayne@69: jpayne@69: namespace kj { jpayne@69: jpayne@69: class Timer: public MonotonicClock { jpayne@69: // Interface to time and timer functionality. jpayne@69: // jpayne@69: // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different jpayne@69: // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However, jpayne@69: // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar jpayne@69: // date as tracked by the system is manually modified. jpayne@69: // jpayne@69: // That said, the `Timer` returned by `kj::setupAsyncIo().provider->getTimer()` in particular is jpayne@69: // guaranteed to be synchronized with the `MonotonicClock` returned by jpayne@69: // `systemPreciseMonotonicClock()` (or, more precisely, is updated to match that clock whenever jpayne@69: // the loop waits). jpayne@69: // jpayne@69: // Note that the value returned by `Timer::now()` only changes each time the jpayne@69: // event loop waits for I/O from the system. While the event loop is actively jpayne@69: // running, the time stays constant. This is intended to make behavior more jpayne@69: // deterministic and reproducible. However, if you need up-to-the-cycle jpayne@69: // accurate time, then `Timer::now()` is not appropriate. Instead, use jpayne@69: // `systemPreciseMonotonicClock()` directly in this case. jpayne@69: jpayne@69: public: jpayne@69: virtual TimePoint now() const = 0; jpayne@69: // Returns the current value of a clock that moves steadily forward, independent of any jpayne@69: // changes in the wall clock. The value is updated every time the event loop waits, jpayne@69: // and is constant in-between waits. jpayne@69: jpayne@69: virtual Promise atTime(TimePoint time) = 0; jpayne@69: // Returns a promise that returns as soon as now() >= time. jpayne@69: jpayne@69: virtual Promise afterDelay(Duration delay) = 0; jpayne@69: // Equivalent to atTime(now() + delay). jpayne@69: jpayne@69: template jpayne@69: Promise timeoutAt(TimePoint time, Promise&& promise) KJ_WARN_UNUSED_RESULT; jpayne@69: // Return a promise equivalent to `promise` but which throws an exception (and cancels the jpayne@69: // original promise) if it hasn't completed by `time`. The thrown exception is of type jpayne@69: // "OVERLOADED". jpayne@69: jpayne@69: template jpayne@69: Promise timeoutAfter(Duration delay, Promise&& promise) KJ_WARN_UNUSED_RESULT; jpayne@69: // Return a promise equivalent to `promise` but which throws an exception (and cancels the jpayne@69: // original promise) if it hasn't completed after `delay` from now. The thrown exception is of jpayne@69: // type "OVERLOADED". jpayne@69: jpayne@69: private: jpayne@69: static kj::Exception makeTimeoutException(); jpayne@69: }; jpayne@69: jpayne@69: class TimerImpl final: public Timer { jpayne@69: // Implementation of Timer that expects an external caller -- usually, the EventPort jpayne@69: // implementation -- to tell it when time has advanced. jpayne@69: jpayne@69: public: jpayne@69: TimerImpl(TimePoint startTime); jpayne@69: ~TimerImpl() noexcept(false); jpayne@69: jpayne@69: Maybe nextEvent(); jpayne@69: // Returns the time at which the next scheduled timer event will occur, or null if no timer jpayne@69: // events are scheduled. jpayne@69: jpayne@69: Maybe timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max); jpayne@69: // Convenience method which computes a timeout value to pass to an event-waiting system call to jpayne@69: // cause it to time out when the next timer event occurs. jpayne@69: // jpayne@69: // `start` is the time at which the timeout starts counting. This is typically not the same as jpayne@69: // now() since some time may have passed since the last time advanceTo() was called. jpayne@69: // jpayne@69: // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note jpayne@69: // that this method will fractional values *up*, to guarantee that the returned timeout waits jpayne@69: // until just *after* the time the event is scheduled. jpayne@69: // jpayne@69: // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a jpayne@69: // 32-bit value or a signed value. jpayne@69: // jpayne@69: // Returns nullptr if there are no future events. jpayne@69: jpayne@69: void advanceTo(TimePoint newTime); jpayne@69: // Set the time to `time` and fire any at() events that have been passed. jpayne@69: jpayne@69: // implements Timer ---------------------------------------------------------- jpayne@69: TimePoint now() const override; jpayne@69: Promise atTime(TimePoint time) override; jpayne@69: Promise afterDelay(Duration delay) override; jpayne@69: jpayne@69: private: jpayne@69: struct Impl; jpayne@69: class TimerPromiseAdapter; jpayne@69: TimePoint time; jpayne@69: Own impl; jpayne@69: }; jpayne@69: jpayne@69: // ======================================================================================= jpayne@69: // inline implementation details jpayne@69: jpayne@69: template jpayne@69: Promise Timer::timeoutAt(TimePoint time, Promise&& promise) { jpayne@69: return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise { jpayne@69: return makeTimeoutException(); jpayne@69: })); jpayne@69: } jpayne@69: jpayne@69: template jpayne@69: Promise Timer::timeoutAfter(Duration delay, Promise&& promise) { jpayne@69: return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise { jpayne@69: return makeTimeoutException(); jpayne@69: })); jpayne@69: } jpayne@69: jpayne@69: inline TimePoint TimerImpl::now() const { return time; } jpayne@69: jpayne@69: } // namespace kj jpayne@69: jpayne@69: KJ_END_HEADER