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 #pragma once
18
19 #include <stdint.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22
23 #include <utils/Compat.h>
24
25 // ------------------------------------------------------------------
26 // C API
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 typedef int64_t nsecs_t; // nano-seconds
33
seconds_to_nanoseconds(nsecs_t secs)34 static CONSTEXPR inline nsecs_t seconds_to_nanoseconds(nsecs_t secs)
35 {
36 return secs*1000000000;
37 }
38
milliseconds_to_nanoseconds(nsecs_t secs)39 static CONSTEXPR inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs)
40 {
41 return secs*1000000;
42 }
43
microseconds_to_nanoseconds(nsecs_t secs)44 static CONSTEXPR inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs)
45 {
46 return secs*1000;
47 }
48
nanoseconds_to_seconds(nsecs_t secs)49 static CONSTEXPR inline nsecs_t nanoseconds_to_seconds(nsecs_t secs)
50 {
51 return secs/1000000000;
52 }
53
nanoseconds_to_milliseconds(nsecs_t secs)54 static CONSTEXPR inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
55 {
56 return secs/1000000;
57 }
58
nanoseconds_to_microseconds(nsecs_t secs)59 static CONSTEXPR inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs)
60 {
61 return secs/1000;
62 }
63
s2ns(nsecs_t v)64 static CONSTEXPR inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);}
ms2ns(nsecs_t v)65 static CONSTEXPR inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);}
us2ns(nsecs_t v)66 static CONSTEXPR inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);}
ns2s(nsecs_t v)67 static CONSTEXPR inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);}
ns2ms(nsecs_t v)68 static CONSTEXPR inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);}
ns2us(nsecs_t v)69 static CONSTEXPR inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);}
70
seconds(nsecs_t v)71 static CONSTEXPR inline nsecs_t seconds(nsecs_t v) { return s2ns(v); }
milliseconds(nsecs_t v)72 static CONSTEXPR inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); }
microseconds(nsecs_t v)73 static CONSTEXPR inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); }
74
75 enum {
76 SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock
77 SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
78 SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock
79 SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock
80 SYSTEM_TIME_BOOTTIME = 4, // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time
81 };
82
83 // return the system-time according to the specified clock
84 #ifdef __cplusplus
85 nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC);
86 #else
87 nsecs_t systemTime(int clock);
88 #endif // def __cplusplus
89
90 /**
91 * Returns the number of milliseconds to wait between the reference time and the timeout time.
92 * If the timeout is in the past relative to the reference time, returns 0.
93 * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time,
94 * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay.
95 * Otherwise, returns the difference between the reference time and timeout time
96 * rounded up to the next millisecond.
97 */
98 int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime);
99
100 #ifdef __cplusplus
101 } // extern "C"
102 #endif
103