1/* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 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 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 19#ifndef OSCLCONFIG_H_INCLUDED 20#include "osclconfig.h" 21#endif 22 23#ifndef OSCL_SINGLETON_H_INCLUDED 24#include "oscl_singleton.h" 25#endif 26 27 28//See if the gettimeofday implementation is available. 29//It relies on timeval and global variables... 30 31#ifndef OSCLCONFIG_TIME_H_INCLUDED 32#include "osclconfig_time.h" 33#endif 34 35#define PV_USE_GETTIMEOFDAY 1 36 37 38/************************ 39* in each the following routines, there must be an valid clause 40* in the #if - #elif chain for each platform. If not, a #error 41* will be encountered, at compile time. 42* 43* If, in doing a new port, you do not yet know the appropriate value 44* for one of these routines, an "OSCL_ASSERT(false)" could provide 45* a temporary expedient. Better, though, not to depend on the runtime 46* check .. which may occur at an inopportune time. 47*/ 48 49OSCL_COND_EXPORT_REF OSCL_INLINE uint32 OsclTickCount::TickCount() 50{ 51#if PV_USE_GETTIMEOFDAY 52#define ROLLBACK_THRESHOLD 0x80000000 53 // lock this function against other threads changing the static variables 54 // ignore return value and error code 55 int32 errorCode = 0; 56 OsclSingletonRegistry::lockAndGetInstance(OSCL_SINGLETON_ID_TICKCOUNT, errorCode); 57 58 struct timeval tv; 59 60 static struct timeval stv = {0, 0}; 61 static uint32 prev_val = 0; 62 63 if ((0 == stv.tv_sec) && (0 == stv.tv_usec)) 64 gettimeofday(&stv, NULL); 65 66 gettimeofday(&tv, NULL); 67 uint32 clk_val = (tv.tv_sec - stv.tv_sec) * 1000 + (tv.tv_usec - stv.tv_usec) / 1000; 68 69 if ((clk_val - prev_val) > ROLLBACK_THRESHOLD) 70 { 71 // assume clock rolled backwards so this update will be 72 // ignored since we require that the tick counter be monotonic 73 // non-decreasing. 74 clk_val = prev_val; 75 } 76 prev_val = clk_val; 77 OsclSingletonRegistry::registerInstanceAndUnlock(0, OSCL_SINGLETON_ID_TICKCOUNT, errorCode); 78 79 return clk_val; 80 81#else 82#error No definition for OsclTickCount::TickCount 83#endif 84} 85 86// how many ticks per second 87OSCL_COND_EXPORT_REF OSCL_INLINE uint32 OsclTickCount::TickCountFrequency() 88{ 89#if PV_USE_GETTIMEOFDAY 90 return 1000; 91#else 92#error No definition for OsclTickCount::TickCountFrequency 93#endif 94} 95 96// how many microseconds per tick 97OSCL_COND_EXPORT_REF OSCL_INLINE uint32 OsclTickCount::TickCountPeriod() 98{ 99#if PV_USE_GETTIMEOFDAY 100 return 1000; 101#else 102#error No definition for OsclTickCount::TickCountPeriod 103#endif 104} 105 106OSCL_COND_EXPORT_REF OSCL_INLINE uint32 OsclTickCount::TicksToMsec(uint32 ticks) 107{ 108#if PV_USE_GETTIMEOFDAY 109 return ticks; 110#else 111#error No definition for OsclTickCount::TicksToMsec 112#endif 113} 114 115OSCL_COND_EXPORT_REF OSCL_INLINE uint32 OsclTickCount::MsecToTicks(uint32 msec) 116{ 117#if PV_USE_GETTIMEOFDAY 118 return msec; 119#else 120#error No definition for OsclTickCount::msectoticks 121#endif 122} 123 124 125