annotate osx/include/kj/time.h @ 83:ae30d91d2ffe

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