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