| Chris@47 | 1 // Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>) | 
| Chris@47 | 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | 
| Chris@47 | 3 // Licensed under the MIT License: | 
| Chris@47 | 4 // | 
| Chris@47 | 5 // Permission is hereby granted, free of charge, to any person obtaining a copy | 
| Chris@47 | 6 // of this software and associated documentation files (the "Software"), to deal | 
| Chris@47 | 7 // in the Software without restriction, including without limitation the rights | 
| Chris@47 | 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
| Chris@47 | 9 // copies of the Software, and to permit persons to whom the Software is | 
| Chris@47 | 10 // furnished to do so, subject to the following conditions: | 
| Chris@47 | 11 // | 
| Chris@47 | 12 // The above copyright notice and this permission notice shall be included in | 
| Chris@47 | 13 // all copies or substantial portions of the Software. | 
| Chris@47 | 14 // | 
| Chris@47 | 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| Chris@47 | 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| Chris@47 | 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
| Chris@47 | 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| Chris@47 | 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| Chris@47 | 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
| Chris@47 | 21 // THE SOFTWARE. | 
| Chris@47 | 22 | 
| Chris@47 | 23 #ifndef KJ_TIME_H_ | 
| Chris@47 | 24 #define KJ_TIME_H_ | 
| Chris@47 | 25 | 
| Chris@47 | 26 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS | 
| Chris@47 | 27 #pragma GCC system_header | 
| Chris@47 | 28 #endif | 
| Chris@47 | 29 | 
| Chris@47 | 30 #include "async.h" | 
| Chris@47 | 31 #include "units.h" | 
| Chris@47 | 32 #include <inttypes.h> | 
| Chris@47 | 33 | 
| Chris@47 | 34 namespace kj { | 
| Chris@47 | 35 namespace _ {  // private | 
| Chris@47 | 36 | 
| Chris@47 | 37 class NanosecondLabel; | 
| Chris@47 | 38 class TimeLabel; | 
| Chris@47 | 39 class DateLabel; | 
| Chris@47 | 40 | 
| Chris@47 | 41 }  // namespace _ (private) | 
| Chris@47 | 42 | 
| Chris@47 | 43 using Duration = Quantity<int64_t, _::NanosecondLabel>; | 
| Chris@47 | 44 // A time value, in microseconds. | 
| Chris@47 | 45 | 
| Chris@47 | 46 constexpr Duration NANOSECONDS = unit<Duration>(); | 
| Chris@47 | 47 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; | 
| Chris@47 | 48 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; | 
| Chris@47 | 49 constexpr Duration SECONDS = 1000 * MILLISECONDS; | 
| Chris@47 | 50 constexpr Duration MINUTES = 60 * SECONDS; | 
| Chris@47 | 51 constexpr Duration HOURS = 60 * MINUTES; | 
| Chris@47 | 52 constexpr Duration DAYS = 24 * HOURS; | 
| Chris@47 | 53 | 
| Chris@47 | 54 using TimePoint = Absolute<Duration, _::TimeLabel>; | 
| Chris@47 | 55 // An absolute time measured by some particular instance of `Timer`.  `Time`s from two different | 
| Chris@47 | 56 // `Timer`s may be measured from different origins and so are not necessarily compatible. | 
| Chris@47 | 57 | 
| Chris@47 | 58 using Date = Absolute<Duration, _::DateLabel>; | 
| Chris@47 | 59 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). | 
| Chris@47 | 60 | 
| Chris@47 | 61 constexpr Date UNIX_EPOCH = origin<Date>(); | 
| Chris@47 | 62 // The `Date` representing Jan 1, 1970 00:00:00 UTC. | 
| Chris@47 | 63 | 
| Chris@47 | 64 class Timer { | 
| Chris@47 | 65   // Interface to time and timer functionality. | 
| Chris@47 | 66   // | 
| Chris@47 | 67   // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different | 
| Chris@47 | 68   // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread).  However, | 
| Chris@47 | 69   // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar | 
| Chris@47 | 70   // date as tracked by the system is manually modified. | 
| Chris@47 | 71 | 
| Chris@47 | 72 public: | 
| Chris@47 | 73   virtual TimePoint now() = 0; | 
| Chris@47 | 74   // Returns the current value of a clock that moves steadily forward, independent of any | 
| Chris@47 | 75   // changes in the wall clock. The value is updated every time the event loop waits, | 
| Chris@47 | 76   // and is constant in-between waits. | 
| Chris@47 | 77 | 
| Chris@47 | 78   virtual Promise<void> atTime(TimePoint time) = 0; | 
| Chris@47 | 79   // Returns a promise that returns as soon as now() >= time. | 
| Chris@47 | 80 | 
| Chris@47 | 81   virtual Promise<void> afterDelay(Duration delay) = 0; | 
| Chris@47 | 82   // Equivalent to atTime(now() + delay). | 
| Chris@47 | 83 | 
| Chris@47 | 84   template <typename T> | 
| Chris@47 | 85   Promise<T> timeoutAt(TimePoint time, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT; | 
| Chris@47 | 86   // Return a promise equivalent to `promise` but which throws an exception (and cancels the | 
| Chris@47 | 87   // original promise) if it hasn't completed by `time`. The thrown exception is of type | 
| Chris@47 | 88   // "OVERLOADED". | 
| Chris@47 | 89 | 
| Chris@47 | 90   template <typename T> | 
| Chris@47 | 91   Promise<T> timeoutAfter(Duration delay, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT; | 
| Chris@47 | 92   // Return a promise equivalent to `promise` but which throws an exception (and cancels the | 
| Chris@47 | 93   // original promise) if it hasn't completed after `delay` from now. The thrown exception is of | 
| Chris@47 | 94   // type "OVERLOADED". | 
| Chris@47 | 95 | 
| Chris@47 | 96 private: | 
| Chris@47 | 97   static kj::Exception makeTimeoutException(); | 
| Chris@47 | 98 }; | 
| Chris@47 | 99 | 
| Chris@47 | 100 // ======================================================================================= | 
| Chris@47 | 101 // inline implementation details | 
| Chris@47 | 102 | 
| Chris@47 | 103 template <typename T> | 
| Chris@47 | 104 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) { | 
| Chris@47 | 105   return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise<T> { | 
| Chris@47 | 106     return makeTimeoutException(); | 
| Chris@47 | 107   })); | 
| Chris@47 | 108 } | 
| Chris@47 | 109 | 
| Chris@47 | 110 template <typename T> | 
| Chris@47 | 111 Promise<T> Timer::timeoutAfter(Duration delay, Promise<T>&& promise) { | 
| Chris@47 | 112   return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> { | 
| Chris@47 | 113     return makeTimeoutException(); | 
| Chris@47 | 114   })); | 
| Chris@47 | 115 } | 
| Chris@47 | 116 | 
| Chris@47 | 117 }  // namespace kj | 
| Chris@47 | 118 | 
| Chris@47 | 119 #endif  // KJ_TIME_H_ |