annotate win32-mingw/include/kj/time.h @ 79:91c729825bca pa_catalina

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