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