Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2005 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | // |
michael@0 | 18 | // Timer functions. |
michael@0 | 19 | // |
michael@0 | 20 | #ifndef _LIBS_UTILS_TIMERS_H |
michael@0 | 21 | #define _LIBS_UTILS_TIMERS_H |
michael@0 | 22 | |
michael@0 | 23 | #include <stdint.h> |
michael@0 | 24 | #include <sys/types.h> |
michael@0 | 25 | #include <sys/time.h> |
michael@0 | 26 | |
michael@0 | 27 | // ------------------------------------------------------------------ |
michael@0 | 28 | // C API |
michael@0 | 29 | |
michael@0 | 30 | #ifdef __cplusplus |
michael@0 | 31 | extern "C" { |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | typedef int64_t nsecs_t; // nano-seconds |
michael@0 | 35 | |
michael@0 | 36 | static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) |
michael@0 | 37 | { |
michael@0 | 38 | return secs*1000000000; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) |
michael@0 | 42 | { |
michael@0 | 43 | return secs*1000000; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) |
michael@0 | 47 | { |
michael@0 | 48 | return secs*1000; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) |
michael@0 | 52 | { |
michael@0 | 53 | return secs/1000000000; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) |
michael@0 | 57 | { |
michael@0 | 58 | return secs/1000000; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) |
michael@0 | 62 | { |
michael@0 | 63 | return secs/1000; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | static inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} |
michael@0 | 67 | static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} |
michael@0 | 68 | static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} |
michael@0 | 69 | static inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} |
michael@0 | 70 | static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} |
michael@0 | 71 | static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} |
michael@0 | 72 | |
michael@0 | 73 | static inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } |
michael@0 | 74 | static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } |
michael@0 | 75 | static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } |
michael@0 | 76 | |
michael@0 | 77 | enum { |
michael@0 | 78 | SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock |
michael@0 | 79 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point |
michael@0 | 80 | SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock |
michael@0 | 81 | SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | // return the system-time according to the specified clock |
michael@0 | 85 | #ifdef __cplusplus |
michael@0 | 86 | nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); |
michael@0 | 87 | #else |
michael@0 | 88 | nsecs_t systemTime(int clock); |
michael@0 | 89 | #endif // def __cplusplus |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * Returns the number of milliseconds to wait between the reference time and the timeout time. |
michael@0 | 93 | * If the timeout is in the past relative to the reference time, returns 0. |
michael@0 | 94 | * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, |
michael@0 | 95 | * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. |
michael@0 | 96 | * Otherwise, returns the difference between the reference time and timeout time |
michael@0 | 97 | * rounded up to the next millisecond. |
michael@0 | 98 | */ |
michael@0 | 99 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); |
michael@0 | 100 | |
michael@0 | 101 | #ifdef __cplusplus |
michael@0 | 102 | } // extern "C" |
michael@0 | 103 | #endif |
michael@0 | 104 | |
michael@0 | 105 | // ------------------------------------------------------------------ |
michael@0 | 106 | // C++ API |
michael@0 | 107 | |
michael@0 | 108 | #ifdef __cplusplus |
michael@0 | 109 | |
michael@0 | 110 | namespace android { |
michael@0 | 111 | /* |
michael@0 | 112 | * Time the duration of something. |
michael@0 | 113 | * |
michael@0 | 114 | * Includes some timeval manipulation functions. |
michael@0 | 115 | */ |
michael@0 | 116 | class DurationTimer { |
michael@0 | 117 | public: |
michael@0 | 118 | DurationTimer() {} |
michael@0 | 119 | ~DurationTimer() {} |
michael@0 | 120 | |
michael@0 | 121 | // Start the timer. |
michael@0 | 122 | void start(); |
michael@0 | 123 | // Stop the timer. |
michael@0 | 124 | void stop(); |
michael@0 | 125 | // Get the duration in microseconds. |
michael@0 | 126 | long long durationUsecs() const; |
michael@0 | 127 | |
michael@0 | 128 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in |
michael@0 | 129 | // microseconds. |
michael@0 | 130 | static long long subtractTimevals(const struct timeval* ptv1, |
michael@0 | 131 | const struct timeval* ptv2); |
michael@0 | 132 | |
michael@0 | 133 | // Add the specified amount of time to the timeval. |
michael@0 | 134 | static void addToTimeval(struct timeval* ptv, long usec); |
michael@0 | 135 | |
michael@0 | 136 | private: |
michael@0 | 137 | struct timeval mStartWhen; |
michael@0 | 138 | struct timeval mStopWhen; |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | }; // android |
michael@0 | 142 | #endif // def __cplusplus |
michael@0 | 143 | |
michael@0 | 144 | #endif // _LIBS_UTILS_TIMERS_H |