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