annotate win32-mingw/include/kj/time.h @ 169:223a55898ab9 tip default

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