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