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