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