1cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project/* 2cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Copyright (C) 2005 The Android Open Source Project 3cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * 4cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * you may not use this file except in compliance with the License. 6cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * You may obtain a copy of the License at 7cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * 8cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * 10cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * See the License for the specific language governing permissions and 14cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * limitations under the License. 15cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project */ 16cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project 17cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project// 18cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project// Timer functions. 19cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project// 20cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <utils/Timers.h> 21cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <utils/Log.h> 22cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project 23cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <stdlib.h> 24cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <stdio.h> 25cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <unistd.h> 26cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <sys/time.h> 27cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <time.h> 28cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <errno.h> 2943550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown#include <limits.h> 30cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project 31cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#ifdef HAVE_WIN32_THREADS 32cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <windows.h> 33cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#endif 34cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project 35cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Projectnsecs_t systemTime(int clock) 36cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project{ 37cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#if defined(HAVE_POSIX_CLOCKS) 38cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project static const clockid_t clocks[] = { 39cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project CLOCK_REALTIME, 40cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project CLOCK_MONOTONIC, 41cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project CLOCK_PROCESS_CPUTIME_ID, 42af1e7b77217430cc2d423e9f552dcfb4e004539bNick Pelly CLOCK_THREAD_CPUTIME_ID, 43af1e7b77217430cc2d423e9f552dcfb4e004539bNick Pelly CLOCK_BOOTTIME 44cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project }; 45cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project struct timespec t; 46cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project t.tv_sec = t.tv_nsec = 0; 47cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project clock_gettime(clocks[clock], &t); 48cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; 49cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#else 50cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project // we don't support the clocks here. 51cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project struct timeval t; 52cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project t.tv_sec = t.tv_usec = 0; 53cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project gettimeofday(&t, NULL); 54cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; 55cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#endif 56cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project} 57cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project 5843550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brownint toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) 5943550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown{ 6043550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown int timeoutDelayMillis; 6143550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown if (timeoutTime > referenceTime) { 6243550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime); 6343550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) { 6443550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown timeoutDelayMillis = -1; 6543550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown } else { 6643550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL; 6743550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown } 6843550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown } else { 6943550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown timeoutDelayMillis = 0; 7043550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown } 7143550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown return timeoutDelayMillis; 7243550eee5bfeaf7832487a2285ae86be0f7ce561Jeff Brown} 73