annotate osx/include/kj/time.h @ 140:59a8758c56b1

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