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