| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2005 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | // | 
|  | 18 | // Timer functions. | 
|  | 19 | // | 
|  | 20 | #ifndef _LIBS_UTILS_TIMERS_H | 
|  | 21 | #define _LIBS_UTILS_TIMERS_H | 
|  | 22 |  | 
|  | 23 | #include <stdint.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 | #include <sys/time.h> | 
|  | 26 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 27 | #include <utils/Compat.h> | 
|  | 28 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | // ------------------------------------------------------------------ | 
|  | 30 | // C API | 
|  | 31 |  | 
|  | 32 | #ifdef __cplusplus | 
|  | 33 | extern "C" { | 
|  | 34 | #endif | 
|  | 35 |  | 
|  | 36 | typedef int64_t nsecs_t;       // nano-seconds | 
|  | 37 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 38 | static CONSTEXPR inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | { | 
|  | 40 | return secs*1000000000; | 
|  | 41 | } | 
|  | 42 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 43 | static CONSTEXPR inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | { | 
|  | 45 | return secs*1000000; | 
|  | 46 | } | 
|  | 47 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 48 | static CONSTEXPR inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | { | 
|  | 50 | return secs*1000; | 
|  | 51 | } | 
|  | 52 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 53 | static CONSTEXPR inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | { | 
|  | 55 | return secs/1000000000; | 
|  | 56 | } | 
|  | 57 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 58 | static CONSTEXPR inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | { | 
|  | 60 | return secs/1000000; | 
|  | 61 | } | 
|  | 62 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 63 | static CONSTEXPR inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | { | 
|  | 65 | return secs/1000; | 
|  | 66 | } | 
|  | 67 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 68 | static CONSTEXPR inline nsecs_t s2ns(nsecs_t v)  {return seconds_to_nanoseconds(v);} | 
|  | 69 | static CONSTEXPR inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} | 
|  | 70 | static CONSTEXPR inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} | 
|  | 71 | static CONSTEXPR inline nsecs_t ns2s(nsecs_t v)  {return nanoseconds_to_seconds(v);} | 
|  | 72 | static CONSTEXPR inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} | 
|  | 73 | static CONSTEXPR inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 |  | 
| Michael Wright | 65e93c3 | 2015-04-20 15:06:01 +0100 | [diff] [blame] | 75 | static CONSTEXPR inline nsecs_t seconds(nsecs_t v)      { return s2ns(v); } | 
|  | 76 | static CONSTEXPR inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } | 
|  | 77 | static CONSTEXPR inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | enum { | 
|  | 80 | SYSTEM_TIME_REALTIME = 0,  // system-wide realtime clock | 
|  | 81 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point | 
|  | 82 | SYSTEM_TIME_PROCESS = 2,   // high-resolution per-process clock | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 83 | SYSTEM_TIME_THREAD = 3,    // high-resolution per-thread clock | 
|  | 84 | SYSTEM_TIME_BOOTTIME = 4   // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | }; | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 86 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | // return the system-time according to the specified clock | 
|  | 88 | #ifdef __cplusplus | 
|  | 89 | nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); | 
|  | 90 | #else | 
|  | 91 | nsecs_t systemTime(int clock); | 
|  | 92 | #endif // def __cplusplus | 
|  | 93 |  | 
| Jeff Brown | 43550ee | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 94 | /** | 
|  | 95 | * Returns the number of milliseconds to wait between the reference time and the timeout time. | 
|  | 96 | * If the timeout is in the past relative to the reference time, returns 0. | 
|  | 97 | * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, | 
|  | 98 | * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. | 
|  | 99 | * Otherwise, returns the difference between the reference time and timeout time | 
|  | 100 | * rounded up to the next millisecond. | 
|  | 101 | */ | 
|  | 102 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); | 
|  | 103 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | #ifdef __cplusplus | 
|  | 105 | } // extern "C" | 
|  | 106 | #endif | 
|  | 107 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | #endif // _LIBS_UTILS_TIMERS_H |