annotate win64-msvc/include/kj/time.h @ 147:45360b968bf4

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