annotate win32-mingw/include/kj/time.h @ 142:75bf92aa2d1f

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