use librt and CLOCK_MONOTONIC

This commit is contained in:
Glenn Maynard
2005-12-08 22:10:51 +00:00
parent afbd2f3bbc
commit 8c8c5c2dc7
2 changed files with 46 additions and 11 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ AM_CXXFLAGS += -finline-limit=300
AM_CXXFLAGS += $(XCFLAGS)
LIBS += $(LUA_LIBS)
LIBS += -lpthread
LIBS += -lpthread -lrt
$(srcdir)/libresample/libresample.a:
cd $(srcdir)/libresample && autoconf && sh ./configure && make
+45 -10
View File
@@ -96,21 +96,56 @@ static void TestTLS()
}
#endif
static int64_t GetMicrosecondsSinceEpoch()
#if 1
/* If librt is available, use CLOCK_MONOTONIC to implement GetMicrosecondsSinceStart,
* if supported, so changes to the system clock don't cause problems. */
namespace
{
clockid_t g_Clock = CLOCK_REALTIME;
void OpenGetTime()
{
static bool bInitialized = false;
if( bInitialized )
return;
bInitialized = true;
/* Check whether the clock is actually supported. */
timespec ts;
if( clock_getres(CLOCK_MONOTONIC, &ts) == -1 )
return;
/* If the resolution is worse than a millisecond, fall back on CLOCK_REALTIME. */
if( ts.tv_sec > 0 || ts.tv_nsec > 1000000 )
return;
g_Clock = CLOCK_MONOTONIC;
}
};
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
{
OpenGetTime();
timespec ts;
clock_gettime( g_Clock, &ts );
int64_t iRet = int64_t(ts.tv_sec) * 1000000 + int64_t(ts.tv_nsec)/1000;
if( g_Clock != CLOCK_MONOTONIC )
iRet = ArchHooks::FixupTimeIfBackwards( iRet );
return iRet;
}
#else
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
{
struct timeval tv;
gettimeofday( &tv, NULL );
return int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec);
}
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
{
int64_t ret = GetMicrosecondsSinceEpoch();
if( bAccurate )
ret = FixupTimeIfBackwards( ret );
return ret;
int64_t iRet = int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec);
ret = FixupTimeIfBackwards( ret );
return iRet;
}
#endif
ArchHooks_Unix::ArchHooks_Unix()
{