annotate win32-mingw/include/kj/time.h @ 52:44a948c37b77

Remove compiler/platform combos we don't support
author Chris Cannam
date Wed, 26 Oct 2016 13:25:08 +0100
parents 37d53a7e8262
children eccd51b72864
rev   line source
Chris@50 1 // Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>)
Chris@50 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@50 3 // Licensed under the MIT License:
Chris@50 4 //
Chris@50 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@50 6 // of this software and associated documentation files (the "Software"), to deal
Chris@50 7 // in the Software without restriction, including without limitation the rights
Chris@50 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@50 9 // copies of the Software, and to permit persons to whom the Software is
Chris@50 10 // furnished to do so, subject to the following conditions:
Chris@50 11 //
Chris@50 12 // The above copyright notice and this permission notice shall be included in
Chris@50 13 // all copies or substantial portions of the Software.
Chris@50 14 //
Chris@50 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@50 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@50 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@50 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@50 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@50 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@50 21 // THE SOFTWARE.
Chris@50 22
Chris@50 23 #ifndef KJ_TIME_H_
Chris@50 24 #define KJ_TIME_H_
Chris@50 25
Chris@50 26 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@50 27 #pragma GCC system_header
Chris@50 28 #endif
Chris@50 29
Chris@50 30 #include "async.h"
Chris@50 31 #include "units.h"
Chris@50 32 #include <inttypes.h>
Chris@50 33
Chris@50 34 namespace kj {
Chris@50 35 namespace _ { // private
Chris@50 36
Chris@50 37 class NanosecondLabel;
Chris@50 38 class TimeLabel;
Chris@50 39 class DateLabel;
Chris@50 40
Chris@50 41 } // namespace _ (private)
Chris@50 42
Chris@50 43 using Duration = Quantity<int64_t, _::NanosecondLabel>;
Chris@50 44 // A time value, in microseconds.
Chris@50 45
Chris@50 46 constexpr Duration NANOSECONDS = unit<Duration>();
Chris@50 47 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS;
Chris@50 48 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS;
Chris@50 49 constexpr Duration SECONDS = 1000 * MILLISECONDS;
Chris@50 50 constexpr Duration MINUTES = 60 * SECONDS;
Chris@50 51 constexpr Duration HOURS = 60 * MINUTES;
Chris@50 52 constexpr Duration DAYS = 24 * HOURS;
Chris@50 53
Chris@50 54 using TimePoint = Absolute<Duration, _::TimeLabel>;
Chris@50 55 // An absolute time measured by some particular instance of `Timer`. `Time`s from two different
Chris@50 56 // `Timer`s may be measured from different origins and so are not necessarily compatible.
Chris@50 57
Chris@50 58 using Date = Absolute<Duration, _::DateLabel>;
Chris@50 59 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC).
Chris@50 60
Chris@50 61 constexpr Date UNIX_EPOCH = origin<Date>();
Chris@50 62 // The `Date` representing Jan 1, 1970 00:00:00 UTC.
Chris@50 63
Chris@50 64 class Timer {
Chris@50 65 // Interface to time and timer functionality.
Chris@50 66 //
Chris@50 67 // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different
Chris@50 68 // rate than real time (e.g. a `Timer` could represent CPU time consumed by a thread). However,
Chris@50 69 // all `Timer`s are monotonic: time will never appear to move backwards, even if the calendar
Chris@50 70 // date as tracked by the system is manually modified.
Chris@50 71
Chris@50 72 public:
Chris@50 73 virtual TimePoint now() = 0;
Chris@50 74 // Returns the current value of a clock that moves steadily forward, independent of any
Chris@50 75 // changes in the wall clock. The value is updated every time the event loop waits,
Chris@50 76 // and is constant in-between waits.
Chris@50 77
Chris@50 78 virtual Promise<void> atTime(TimePoint time) = 0;
Chris@50 79 // Returns a promise that returns as soon as now() >= time.
Chris@50 80
Chris@50 81 virtual Promise<void> afterDelay(Duration delay) = 0;
Chris@50 82 // Equivalent to atTime(now() + delay).
Chris@50 83
Chris@50 84 template <typename T>
Chris@50 85 Promise<T> timeoutAt(TimePoint time, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT;
Chris@50 86 // Return a promise equivalent to `promise` but which throws an exception (and cancels the
Chris@50 87 // original promise) if it hasn't completed by `time`. The thrown exception is of type
Chris@50 88 // "OVERLOADED".
Chris@50 89
Chris@50 90 template <typename T>
Chris@50 91 Promise<T> timeoutAfter(Duration delay, Promise<T>&& promise) KJ_WARN_UNUSED_RESULT;
Chris@50 92 // Return a promise equivalent to `promise` but which throws an exception (and cancels the
Chris@50 93 // original promise) if it hasn't completed after `delay` from now. The thrown exception is of
Chris@50 94 // type "OVERLOADED".
Chris@50 95
Chris@50 96 private:
Chris@50 97 static kj::Exception makeTimeoutException();
Chris@50 98 };
Chris@50 99
Chris@50 100 // =======================================================================================
Chris@50 101 // inline implementation details
Chris@50 102
Chris@50 103 template <typename T>
Chris@50 104 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) {
Chris@50 105 return promise.exclusiveJoin(atTime(time).then([]() -> kj::Promise<T> {
Chris@50 106 return makeTimeoutException();
Chris@50 107 }));
Chris@50 108 }
Chris@50 109
Chris@50 110 template <typename T>
Chris@50 111 Promise<T> Timer::timeoutAfter(Duration delay, Promise<T>&& promise) {
Chris@50 112 return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> {
Chris@50 113 return makeTimeoutException();
Chris@50 114 }));
Chris@50 115 }
Chris@50 116
Chris@50 117 } // namespace kj
Chris@50 118
Chris@50 119 #endif // KJ_TIME_H_