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