annotate osx/include/kj/time.h @ 54:5f67a29f0fc7

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