annotate win64-msvc/include/kj/time.h @ 155:54abead6ecce

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