blob: 197fc26b8b6c14c8348625d4e8410d24fcb3c24f [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
Elliott Hughes842e1cc2020-05-27 12:24:30 -070017#pragma once
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080018
19#include <stdint.h>
20#include <sys/types.h>
21#include <sys/time.h>
22
Michael Wright65e93c32015-04-20 15:06:01 +010023#include <utils/Compat.h>
24
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025// ------------------------------------------------------------------
26// C API
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32typedef int64_t nsecs_t; // nano-seconds
33
Michael Wright65e93c32015-04-20 15:06:01 +010034static CONSTEXPR inline nsecs_t seconds_to_nanoseconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035{
36 return secs*1000000000;
37}
38
Michael Wright65e93c32015-04-20 15:06:01 +010039static CONSTEXPR inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040{
41 return secs*1000000;
42}
43
Michael Wright65e93c32015-04-20 15:06:01 +010044static CONSTEXPR inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080045{
46 return secs*1000;
47}
48
Michael Wright65e93c32015-04-20 15:06:01 +010049static CONSTEXPR inline nsecs_t nanoseconds_to_seconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050{
51 return secs/1000000000;
52}
53
Michael Wright65e93c32015-04-20 15:06:01 +010054static CONSTEXPR inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055{
56 return secs/1000000;
57}
58
Michael Wright65e93c32015-04-20 15:06:01 +010059static CONSTEXPR inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080060{
61 return secs/1000;
62}
63
Michael Wright65e93c32015-04-20 15:06:01 +010064static CONSTEXPR inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);}
65static CONSTEXPR inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);}
66static CONSTEXPR inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);}
67static CONSTEXPR inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);}
68static CONSTEXPR inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);}
69static CONSTEXPR inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070
Michael Wright65e93c32015-04-20 15:06:01 +010071static CONSTEXPR inline nsecs_t seconds(nsecs_t v) { return s2ns(v); }
72static CONSTEXPR inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); }
73static CONSTEXPR inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074
75enum {
Elliott Hughes842e1cc2020-05-27 12:24:30 -070076 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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080081};
Nick Pellyaf1e7b72012-07-19 09:17:24 -070082
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080083// return the system-time according to the specified clock
84#ifdef __cplusplus
85nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC);
86#else
87nsecs_t systemTime(int clock);
88#endif // def __cplusplus
89
Jeff Brown43550ee2011-03-17 01:34:19 -070090/**
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 */
98int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime);
99
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800100#ifdef __cplusplus
101} // extern "C"
102#endif