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