comparison win32-mingw/include/kj/time.h @ 135:38d1c0e7850b

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