blob: b2df9a58fc2fe2c257b6fbb4d80833b89c59453d [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
17//
18// Timer functions.
19//
20#include <utils/Timers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021
Yabin Cui4a6e5a32015-01-26 19:48:54 -080022#include <limits.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <time.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Elliott Hughes9b828ad2015-07-30 08:47:35 -070025#if defined(__ANDROID__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026nsecs_t systemTime(int clock)
27{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028 static const clockid_t clocks[] = {
29 CLOCK_REALTIME,
30 CLOCK_MONOTONIC,
31 CLOCK_PROCESS_CPUTIME_ID,
Nick Pellyaf1e7b72012-07-19 09:17:24 -070032 CLOCK_THREAD_CPUTIME_ID,
33 CLOCK_BOOTTIME
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034 };
35 struct timespec t;
36 t.tv_sec = t.tv_nsec = 0;
37 clock_gettime(clocks[clock], &t);
38 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
Mark Salyzyn5bed8032014-04-30 11:10:46 -070039}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040#else
Mark Salyzyn5bed8032014-04-30 11:10:46 -070041nsecs_t systemTime(int /*clock*/)
42{
Narayan Kamathea659642014-04-10 18:40:55 +010043 // Clock support varies widely across hosts. Mac OS doesn't support
44 // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows
45 // is windows.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080046 struct timeval t;
47 t.tv_sec = t.tv_usec = 0;
48 gettimeofday(&t, NULL);
49 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050}
Mark Salyzyn5bed8032014-04-30 11:10:46 -070051#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052
Jeff Brown43550ee2011-03-17 01:34:19 -070053int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
54{
Mathias Agopian22dbf392017-02-28 15:06:51 -080055 nsecs_t timeoutDelayMillis;
Jeff Brown43550ee2011-03-17 01:34:19 -070056 if (timeoutTime > referenceTime) {
57 uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
58 if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) {
59 timeoutDelayMillis = -1;
60 } else {
61 timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL;
62 }
63 } else {
64 timeoutDelayMillis = 0;
65 }
Mathias Agopian22dbf392017-02-28 15:06:51 -080066 return (int)timeoutDelayMillis;
Jeff Brown43550ee2011-03-17 01:34:19 -070067}