cannam@134: // Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) cannam@134: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@134: // Licensed under the MIT License: cannam@134: // cannam@134: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@134: // of this software and associated documentation files (the "Software"), to deal cannam@134: // in the Software without restriction, including without limitation the rights cannam@134: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@134: // copies of the Software, and to permit persons to whom the Software is cannam@134: // furnished to do so, subject to the following conditions: cannam@134: // cannam@134: // The above copyright notice and this permission notice shall be included in cannam@134: // all copies or substantial portions of the Software. cannam@134: // cannam@134: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@134: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@134: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@134: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@134: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@134: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@134: // THE SOFTWARE. cannam@134: cannam@134: #ifndef KJ_TIME_H_ cannam@134: #define KJ_TIME_H_ cannam@134: cannam@134: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@134: #pragma GCC system_header cannam@134: #endif cannam@134: cannam@134: #include "async.h" cannam@134: #include "units.h" cannam@134: #include cannam@134: cannam@134: namespace kj { cannam@134: namespace _ { // private cannam@134: cannam@134: class NanosecondLabel; cannam@134: class TimeLabel; cannam@134: class DateLabel; cannam@134: cannam@134: } // namespace _ (private) cannam@134: cannam@134: using Duration = Quantity; cannam@134: // A time value, in microseconds. cannam@134: cannam@134: constexpr Duration NANOSECONDS = unit(); cannam@134: constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; cannam@134: constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; cannam@134: constexpr Duration SECONDS = 1000 * MILLISECONDS; cannam@134: constexpr Duration MINUTES = 60 * SECONDS; cannam@134: constexpr Duration HOURS = 60 * MINUTES; cannam@134: constexpr Duration DAYS = 24 * HOURS; cannam@134: cannam@134: using TimePoint = Absolute; cannam@134: // An absolute time measured by some particular instance of `Timer`. `Time`s from two different cannam@134: // `Timer`s may be measured from different origins and so are not necessarily compatible. cannam@134: cannam@134: using Date = Absolute; cannam@134: // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). cannam@134: cannam@134: constexpr Date UNIX_EPOCH = origin(); cannam@134: // The `Date` representing Jan 1, 1970 00:00:00 UTC. cannam@134: cannam@134: class Timer { cannam@134: // Interface to time and timer functionality. cannam@134: // cannam@134: // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different cannam@134: // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However, cannam@134: // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar cannam@134: // date as tracked by the system is manually modified. cannam@134: cannam@134: public: cannam@134: virtual TimePoint now() = 0; cannam@134: // Returns the current value of a clock that moves steadily forward, independent of any cannam@134: // changes in the wall clock. The value is updated every time the event loop waits, cannam@134: // and is constant in-between waits. cannam@134: cannam@134: virtual Promise atTime(TimePoint time) = 0; cannam@134: // Returns a promise that returns as soon as now() >= time. cannam@134: cannam@134: virtual Promise afterDelay(Duration delay) = 0; cannam@134: // Equivalent to atTime(now() + delay). cannam@134: cannam@134: template cannam@134: Promise timeoutAt(TimePoint time, Promise&& promise) KJ_WARN_UNUSED_RESULT; cannam@134: // Return a promise equivalent to `promise` but which throws an exception (and cancels the cannam@134: // original promise) if it hasn't completed by `time`. The thrown exception is of type cannam@134: // "OVERLOADED". cannam@134: cannam@134: template cannam@134: Promise timeoutAfter(Duration delay, Promise&& promise) KJ_WARN_UNUSED_RESULT; cannam@134: // Return a promise equivalent to `promise` but which throws an exception (and cancels the cannam@134: // original promise) if it hasn't completed after `delay` from now. The thrown exception is of cannam@134: // type "OVERLOADED". cannam@134: cannam@134: private: cannam@134: static kj::Exception makeTimeoutException(); cannam@134: }; cannam@134: cannam@134: // ======================================================================================= cannam@134: // inline implementation details cannam@134: cannam@134: template cannam@134: Promise Timer::timeoutAt(TimePoint time, Promise&& promise) { cannam@134: return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise { cannam@134: return makeTimeoutException(); cannam@134: })); cannam@134: } cannam@134: cannam@134: template cannam@134: Promise Timer::timeoutAfter(Duration delay, Promise&& promise) { cannam@134: return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise { cannam@134: return makeTimeoutException(); cannam@134: })); cannam@134: } cannam@134: cannam@134: } // namespace kj cannam@134: cannam@134: #endif // KJ_TIME_H_