Chris@63: // Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@63: // Licensed under the MIT License: Chris@63: // Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@63: // of this software and associated documentation files (the "Software"), to deal Chris@63: // in the Software without restriction, including without limitation the rights Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@63: // copies of the Software, and to permit persons to whom the Software is Chris@63: // furnished to do so, subject to the following conditions: Chris@63: // Chris@63: // The above copyright notice and this permission notice shall be included in Chris@63: // all copies or substantial portions of the Software. Chris@63: // Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@63: // THE SOFTWARE. Chris@63: Chris@63: #ifndef KJ_TIME_H_ Chris@63: #define KJ_TIME_H_ Chris@63: Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@63: #pragma GCC system_header Chris@63: #endif Chris@63: Chris@63: #include "async.h" Chris@63: #include "units.h" Chris@63: #include Chris@63: Chris@63: namespace kj { Chris@63: namespace _ { // private Chris@63: Chris@63: class NanosecondLabel; Chris@63: class TimeLabel; Chris@63: class DateLabel; Chris@63: Chris@63: } // namespace _ (private) Chris@63: Chris@63: using Duration = Quantity; Chris@63: // A time value, in nanoseconds. Chris@63: Chris@63: constexpr Duration NANOSECONDS = unit(); Chris@63: constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; Chris@63: constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; Chris@63: constexpr Duration SECONDS = 1000 * MILLISECONDS; Chris@63: constexpr Duration MINUTES = 60 * SECONDS; Chris@63: constexpr Duration HOURS = 60 * MINUTES; Chris@63: constexpr Duration DAYS = 24 * HOURS; Chris@63: Chris@63: using TimePoint = Absolute; Chris@63: // An absolute time measured by some particular instance of `Timer`. `Time`s from two different Chris@63: // `Timer`s may be measured from different origins and so are not necessarily compatible. Chris@63: Chris@63: using Date = Absolute; Chris@63: // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). Chris@63: Chris@63: constexpr Date UNIX_EPOCH = origin(); Chris@63: // The `Date` representing Jan 1, 1970 00:00:00 UTC. Chris@63: Chris@63: class Clock { Chris@63: // Interface to read the current date and time. Chris@63: public: Chris@63: virtual Date now() = 0; Chris@63: }; Chris@63: Chris@63: Clock& nullClock(); Chris@63: // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about Chris@63: // time. Chris@63: Chris@63: class Timer { Chris@63: // Interface to time and timer functionality. Chris@63: // Chris@63: // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different Chris@63: // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However, Chris@63: // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar Chris@63: // date as tracked by the system is manually modified. Chris@63: Chris@63: public: Chris@63: virtual TimePoint now() = 0; Chris@63: // Returns the current value of a clock that moves steadily forward, independent of any Chris@63: // changes in the wall clock. The value is updated every time the event loop waits, Chris@63: // and is constant in-between waits. Chris@63: Chris@63: virtual Promise atTime(TimePoint time) = 0; Chris@63: // Returns a promise that returns as soon as now() >= time. Chris@63: Chris@63: virtual Promise afterDelay(Duration delay) = 0; Chris@63: // Equivalent to atTime(now() + delay). Chris@63: Chris@63: template Chris@63: Promise timeoutAt(TimePoint time, Promise&& promise) KJ_WARN_UNUSED_RESULT; Chris@63: // Return a promise equivalent to `promise` but which throws an exception (and cancels the Chris@63: // original promise) if it hasn't completed by `time`. The thrown exception is of type Chris@63: // "OVERLOADED". Chris@63: Chris@63: template Chris@63: Promise timeoutAfter(Duration delay, Promise&& promise) KJ_WARN_UNUSED_RESULT; Chris@63: // Return a promise equivalent to `promise` but which throws an exception (and cancels the Chris@63: // original promise) if it hasn't completed after `delay` from now. The thrown exception is of Chris@63: // type "OVERLOADED". Chris@63: Chris@63: private: Chris@63: static kj::Exception makeTimeoutException(); Chris@63: }; Chris@63: Chris@63: class TimerImpl final: public Timer { Chris@63: // Implementation of Timer that expects an external caller -- usually, the EventPort Chris@63: // implementation -- to tell it when time has advanced. Chris@63: Chris@63: public: Chris@63: TimerImpl(TimePoint startTime); Chris@63: ~TimerImpl() noexcept(false); Chris@63: Chris@63: Maybe nextEvent(); Chris@63: // Returns the time at which the next scheduled timer event will occur, or null if no timer Chris@63: // events are scheduled. Chris@63: Chris@63: Maybe timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max); Chris@63: // Convenience method which computes a timeout value to pass to an event-waiting system call to Chris@63: // cause it to time out when the next timer event occurs. Chris@63: // Chris@63: // `start` is the time at which the timeout starts counting. This is typically not the same as Chris@63: // now() since some time may have passed since the last time advanceTo() was called. Chris@63: // Chris@63: // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note Chris@63: // that this method will fractional values *up*, to guarantee that the returned timeout waits Chris@63: // until just *after* the time the event is scheduled. Chris@63: // Chris@63: // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a Chris@63: // 32-bit value or a signed value. Chris@63: // Chris@63: // Returns nullptr if there are no future events. Chris@63: Chris@63: void advanceTo(TimePoint newTime); Chris@63: // Set the time to `time` and fire any at() events that have been passed. Chris@63: Chris@63: // implements Timer ---------------------------------------------------------- Chris@63: TimePoint now() override; Chris@63: Promise atTime(TimePoint time) override; Chris@63: Promise afterDelay(Duration delay) override; Chris@63: Chris@63: private: Chris@63: struct Impl; Chris@63: class TimerPromiseAdapter; Chris@63: TimePoint time; Chris@63: Own impl; Chris@63: }; Chris@63: Chris@63: // ======================================================================================= Chris@63: // inline implementation details Chris@63: Chris@63: template Chris@63: Promise Timer::timeoutAt(TimePoint time, Promise&& promise) { Chris@63: return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise { Chris@63: return makeTimeoutException(); Chris@63: })); Chris@63: } Chris@63: Chris@63: template Chris@63: Promise Timer::timeoutAfter(Duration delay, Promise&& promise) { Chris@63: return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise { Chris@63: return makeTimeoutException(); Chris@63: })); Chris@63: } Chris@63: Chris@63: inline TimePoint TimerImpl::now() { return time; } Chris@63: Chris@63: } // namespace kj Chris@63: Chris@63: #endif // KJ_TIME_H_