comparison win32-mingw/include/kj/time.h @ 149:279b18cc7785

Update Win32 capnp builds to v0.6
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 23 May 2017 09:16:54 +0100
parents 38d1c0e7850b
children
comparison
equal deleted inserted replaced
148:b4bfdf10c4b3 149:279b18cc7785
39 class DateLabel; 39 class DateLabel;
40 40
41 } // namespace _ (private) 41 } // namespace _ (private)
42 42
43 using Duration = Quantity<int64_t, _::NanosecondLabel>; 43 using Duration = Quantity<int64_t, _::NanosecondLabel>;
44 // A time value, in microseconds. 44 // A time value, in nanoseconds.
45 45
46 constexpr Duration NANOSECONDS = unit<Duration>(); 46 constexpr Duration NANOSECONDS = unit<Duration>();
47 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; 47 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS;
48 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; 48 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS;
49 constexpr Duration SECONDS = 1000 * MILLISECONDS; 49 constexpr Duration SECONDS = 1000 * MILLISECONDS;
58 using Date = Absolute<Duration, _::DateLabel>; 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). 59 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC).
60 60
61 constexpr Date UNIX_EPOCH = origin<Date>(); 61 constexpr Date UNIX_EPOCH = origin<Date>();
62 // The `Date` representing Jan 1, 1970 00:00:00 UTC. 62 // The `Date` representing Jan 1, 1970 00:00:00 UTC.
63
64 class Clock {
65 // Interface to read the current date and time.
66 public:
67 virtual Date now() = 0;
68 };
69
70 Clock& nullClock();
71 // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about
72 // time.
63 73
64 class Timer { 74 class Timer {
65 // Interface to time and timer functionality. 75 // Interface to time and timer functionality.
66 // 76 //
67 // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different 77 // Each `Timer` may have a different origin, and some `Timer`s may in fact tick at a different
95 105
96 private: 106 private:
97 static kj::Exception makeTimeoutException(); 107 static kj::Exception makeTimeoutException();
98 }; 108 };
99 109
110 class TimerImpl final: public Timer {
111 // Implementation of Timer that expects an external caller -- usually, the EventPort
112 // implementation -- to tell it when time has advanced.
113
114 public:
115 TimerImpl(TimePoint startTime);
116 ~TimerImpl() noexcept(false);
117
118 Maybe<TimePoint> nextEvent();
119 // Returns the time at which the next scheduled timer event will occur, or null if no timer
120 // events are scheduled.
121
122 Maybe<uint64_t> timeoutToNextEvent(TimePoint start, Duration unit, uint64_t max);
123 // Convenience method which computes a timeout value to pass to an event-waiting system call to
124 // cause it to time out when the next timer event occurs.
125 //
126 // `start` is the time at which the timeout starts counting. This is typically not the same as
127 // now() since some time may have passed since the last time advanceTo() was called.
128 //
129 // `unit` is the time unit in which the timeout is measured. This is often MILLISECONDS. Note
130 // that this method will fractional values *up*, to guarantee that the returned timeout waits
131 // until just *after* the time the event is scheduled.
132 //
133 // The timeout will be clamped to `max`. Use this to avoid an overflow if e.g. the OS wants a
134 // 32-bit value or a signed value.
135 //
136 // Returns nullptr if there are no future events.
137
138 void advanceTo(TimePoint newTime);
139 // Set the time to `time` and fire any at() events that have been passed.
140
141 // implements Timer ----------------------------------------------------------
142 TimePoint now() override;
143 Promise<void> atTime(TimePoint time) override;
144 Promise<void> afterDelay(Duration delay) override;
145
146 private:
147 struct Impl;
148 class TimerPromiseAdapter;
149 TimePoint time;
150 Own<Impl> impl;
151 };
152
100 // ======================================================================================= 153 // =======================================================================================
101 // inline implementation details 154 // inline implementation details
102 155
103 template <typename T> 156 template <typename T>
104 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) { 157 Promise<T> Timer::timeoutAt(TimePoint time, Promise<T>&& promise) {
112 return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> { 165 return promise.exclusiveJoin(afterDelay(delay).then([]() -> kj::Promise<T> {
113 return makeTimeoutException(); 166 return makeTimeoutException();
114 })); 167 }));
115 } 168 }
116 169
170 inline TimePoint TimerImpl::now() { return time; }
171
117 } // namespace kj 172 } // namespace kj
118 173
119 #endif // KJ_TIME_H_ 174 #endif // KJ_TIME_H_