| 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 | #include <utils/Timers.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 |  | 
| Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 22 | #include <limits.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <sys/time.h> | 
|  | 24 | #include <time.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 |  | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 26 | #if defined(__ANDROID__) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | nsecs_t systemTime(int clock) | 
|  | 28 | { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | static const clockid_t clocks[] = { | 
|  | 30 | CLOCK_REALTIME, | 
|  | 31 | CLOCK_MONOTONIC, | 
|  | 32 | CLOCK_PROCESS_CPUTIME_ID, | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 33 | CLOCK_THREAD_CPUTIME_ID, | 
|  | 34 | CLOCK_BOOTTIME | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | }; | 
|  | 36 | struct timespec t; | 
|  | 37 | t.tv_sec = t.tv_nsec = 0; | 
|  | 38 | clock_gettime(clocks[clock], &t); | 
|  | 39 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 40 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | #else | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 42 | nsecs_t systemTime(int /*clock*/) | 
|  | 43 | { | 
| Narayan Kamath | ea65964 | 2014-04-10 18:40:55 +0100 | [diff] [blame] | 44 | // Clock support varies widely across hosts. Mac OS doesn't support | 
|  | 45 | // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows | 
|  | 46 | // is windows. | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | struct timeval t; | 
|  | 48 | t.tv_sec = t.tv_usec = 0; | 
|  | 49 | gettimeofday(&t, NULL); | 
|  | 50 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 52 | #endif | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 |  | 
| Jeff Brown | 43550ee | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 54 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) | 
|  | 55 | { | 
|  | 56 | int timeoutDelayMillis; | 
|  | 57 | if (timeoutTime > referenceTime) { | 
|  | 58 | uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime); | 
|  | 59 | if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) { | 
|  | 60 | timeoutDelayMillis = -1; | 
|  | 61 | } else { | 
|  | 62 | timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL; | 
|  | 63 | } | 
|  | 64 | } else { | 
|  | 65 | timeoutDelayMillis = 0; | 
|  | 66 | } | 
|  | 67 | return timeoutDelayMillis; | 
|  | 68 | } |