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