| Chris@63 | 1 // Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>) | 
| Chris@63 | 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors | 
| Chris@63 | 3 // Licensed under the MIT License: | 
| Chris@63 | 4 // | 
| Chris@63 | 5 // Permission is hereby granted, free of charge, to any person obtaining a copy | 
| Chris@63 | 6 // of this software and associated documentation files (the "Software"), to deal | 
| Chris@63 | 7 // in the Software without restriction, including without limitation the rights | 
| Chris@63 | 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
| Chris@63 | 9 // copies of the Software, and to permit persons to whom the Software is | 
| Chris@63 | 10 // furnished to do so, subject to the following conditions: | 
| Chris@63 | 11 // | 
| Chris@63 | 12 // The above copyright notice and this permission notice shall be included in | 
| Chris@63 | 13 // all copies or substantial portions of the Software. | 
| Chris@63 | 14 // | 
| Chris@63 | 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| Chris@63 | 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| Chris@63 | 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
| Chris@63 | 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
| Chris@63 | 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
| Chris@63 | 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
| Chris@63 | 21 // THE SOFTWARE. | 
| Chris@63 | 22 | 
| Chris@63 | 23 #ifndef KJ_TIME_H_ | 
| Chris@63 | 24 #define KJ_TIME_H_ | 
| Chris@63 | 25 | 
| Chris@63 | 26 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS | 
| Chris@63 | 27 #pragma GCC system_header | 
| Chris@63 | 28 #endif | 
| Chris@63 | 29 | 
| Chris@63 | 30 #include "async.h" | 
| Chris@63 | 31 #include "units.h" | 
| Chris@63 | 32 #include <inttypes.h> | 
| Chris@63 | 33 | 
| Chris@63 | 34 namespace kj { | 
| Chris@63 | 35 namespace _ {  // private | 
| Chris@63 | 36 | 
| Chris@63 | 37 class NanosecondLabel; | 
| Chris@63 | 38 class TimeLabel; | 
| Chris@63 | 39 class DateLabel; | 
| Chris@63 | 40 | 
| Chris@63 | 41 }  // namespace _ (private) | 
| Chris@63 | 42 | 
| Chris@63 | 43 using Duration = Quantity<int64_t, _::NanosecondLabel>; | 
| Chris@63 | 44 // A time value, in nanoseconds. | 
| Chris@63 | 45 | 
| Chris@63 | 46 constexpr Duration NANOSECONDS = unit<Duration>(); | 
| Chris@63 | 47 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; | 
| Chris@63 | 48 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; | 
| Chris@63 | 49 constexpr Duration SECONDS = 1000 * MILLISECONDS; | 
| Chris@63 | 50 constexpr Duration MINUTES = 60 * SECONDS; | 
| Chris@63 | 51 constexpr Duration HOURS = 60 * MINUTES; | 
| Chris@63 | 52 constexpr Duration DAYS = 24 * HOURS; | 
| Chris@63 | 53 | 
| Chris@63 | 54 using TimePoint = Absolute<Duration, _::TimeLabel>; | 
| Chris@63 | 55 // An absolute time measured by some particular instance of `Timer`.  `Time`s from two different | 
| Chris@63 | 56 // `Timer`s may be measured from different origins and so are not necessarily compatible. | 
| Chris@63 | 57 | 
| Chris@63 | 58 using Date = Absolute<Duration, _::DateLabel>; | 
| Chris@63 | 59 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). | 
| Chris@63 | 60 | 
| Chris@63 | 61 constexpr Date UNIX_EPOCH = origin<Date>(); | 
| Chris@63 | 62 // The `Date` representing Jan 1, 1970 00:00:00 UTC. | 
| Chris@63 | 63 | 
| Chris@63 | 64 class Clock { | 
| Chris@63 | 65   // Interface to read the current date and time. | 
| Chris@63 | 66 public: | 
| Chris@63 | 67   virtual Date now() = 0; | 
| Chris@63 | 68 }; | 
| Chris@63 | 69 | 
| Chris@63 | 70 Clock& nullClock(); | 
| Chris@63 | 71 // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about | 
| Chris@63 | 72 // time. | 
| Chris@63 | 73 | 
| Chris@63 | 74 class Timer { | 
| Chris@63 | 75   // Interface to time and timer functionality. | 
| Chris@63 | 76   // | 
| Chris@63 | 77   // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different | 
| Chris@63 | 78   // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread).  However, | 
| Chris@63 | 79   // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar | 
| Chris@63 | 80   // date as tracked by the system is manually modified. | 
| Chris@63 | 81 | 
| Chris@63 | 82 public: | 
| Chris@63 | 83   virtual TimePoint now() = 0; | 
| Chris@63 | 84   // Returns the current value of a clock that moves steadily forward, independent of any | 
| Chris@63 | 85   // changes in the wall clock. The value is updated every time the event loop waits, | 
| Chris@63 | 86   // and is constant in-between waits. | 
| Chris@63 | 87 | 
| Chris@63 | 88   virtual Promise<void> atTime(TimePoint time) = 0; | 
| Chris@63 | 89   // Returns a promise that returns as soon as now() >= time. | 
| Chris@63 | 90 | 
| Chris@63 | 91   virtual Promise<void> afterDelay(Duration delay) = 0; | 
| Chris@63 | 92   // Equivalent to atTime(now() + delay). | 
| Chris@63 | 93 | 
| Chris@63 | 94   template <typename T> | 
| Chris@63 | 95   Promise<T> timeoutAt(TimePoint time, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT; | 
| Chris@63 | 96   // Return a promise equivalent to `promise` but which throws an exception (and cancels the | 
| Chris@63 | 97   // original promise) if it hasn't completed by `time`. The thrown exception is of type | 
| Chris@63 | 98   // "OVERLOADED". | 
| Chris@63 | 99 | 
| Chris@63 | 100   template <typename T> | 
| Chris@63 | 101   Promise<T> timeoutAfter(Duration delay, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT; | 
| Chris@63 | 102   // Return a promise equivalent to `promise` but which throws an exception (and cancels the | 
| Chris@63 | 103   // original promise) if it hasn't completed after `delay` from now. The thrown exception is of | 
| Chris@63 | 104   // type "OVERLOADED". | 
| Chris@63 | 105 | 
| Chris@63 | 106 private: | 
| Chris@63 | 107   static kj::Exception makeTimeoutException(); | 
| Chris@63 | 108 }; | 
| Chris@63 | 109 | 
| Chris@63 | 110 class TimerImpl final: public Timer { | 
| Chris@63 | 111   // Implementation of Timer that expects an external caller -- usually, the EventPort | 
| Chris@63 | 112   // implementation -- to tell it when time has advanced. | 
| Chris@63 | 113 | 
| Chris@63 | 114 public: | 
| Chris@63 | 115   TimerImpl(TimePoint startTime); | 
| Chris@63 | 116   ~TimerImpl() noexcept(false); | 
| Chris@63 | 117 | 
| Chris@63 | 118   Maybe<TimePoint> nextEvent(); | 
| Chris@63 | 119   // Returns the time at which the next scheduled timer event will occur, or null if no timer | 
| Chris@63 | 120   // events are scheduled. | 
| Chris@63 | 121 | 
| Chris@63 | 122   Maybe<uint64_t> timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max); | 
| Chris@63 | 123   // Convenience method which computes a timeout value to pass to an event-waiting system call to | 
| Chris@63 | 124   // cause it to time out when the next timer event occurs. | 
| Chris@63 | 125   // | 
| Chris@63 | 126   // `start` is the time at which the timeout starts counting. This is typically not the same as | 
| Chris@63 | 127   // now() since some time may have passed since the last time advanceTo() was called. | 
| Chris@63 | 128   // | 
| Chris@63 | 129   // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note | 
| Chris@63 | 130   // that this method will fractional values *up*, to guarantee that the returned timeout waits | 
| Chris@63 | 131   // until just *after* the time the event is scheduled. | 
| Chris@63 | 132   // | 
| Chris@63 | 133   // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a | 
| Chris@63 | 134   // 32-bit value or a signed value. | 
| Chris@63 | 135   // | 
| Chris@63 | 136   // Returns nullptr if there are no future events. | 
| Chris@63 | 137 | 
| Chris@63 | 138   void advanceTo(TimePoint newTime); | 
| Chris@63 | 139   // Set the time to `time` and fire any at() events that have been passed. | 
| Chris@63 | 140 | 
| Chris@63 | 141   // implements Timer ---------------------------------------------------------- | 
| Chris@63 | 142   TimePoint now() override; | 
| Chris@63 | 143   Promise<void> atTime(TimePoint time) override; | 
| Chris@63 | 144   Promise<void> afterDelay(Duration delay) override; | 
| Chris@63 | 145 | 
| Chris@63 | 146 private: | 
| Chris@63 | 147   struct Impl; | 
| Chris@63 | 148   class TimerPromiseAdapter; | 
| Chris@63 | 149   TimePoint time; | 
| Chris@63 | 150   Own<Impl> impl; | 
| Chris@63 | 151 }; | 
| Chris@63 | 152 | 
| Chris@63 | 153 // ======================================================================================= | 
| Chris@63 | 154 // inline implementation details | 
| Chris@63 | 155 | 
| Chris@63 | 156 template <typename T> | 
| Chris@63 | 157 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) { | 
| Chris@63 | 158   return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise<T> { | 
| Chris@63 | 159     return makeTimeoutException(); | 
| Chris@63 | 160   })); | 
| Chris@63 | 161 } | 
| Chris@63 | 162 | 
| Chris@63 | 163 template <typename T> | 
| Chris@63 | 164 Promise<T> Timer::timeoutAfter(Duration delay, Promise<T>&& promise) { | 
| Chris@63 | 165   return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> { | 
| Chris@63 | 166     return makeTimeoutException(); | 
| Chris@63 | 167   })); | 
| Chris@63 | 168 } | 
| Chris@63 | 169 | 
| Chris@63 | 170 inline TimePoint TimerImpl::now() { return time; } | 
| Chris@63 | 171 | 
| Chris@63 | 172 }  // namespace kj | 
| Chris@63 | 173 | 
| Chris@63 | 174 #endif  // KJ_TIME_H_ |