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