From bfbe78e6952e99e7406af3bfede277d8292eb819 Mon Sep 17 00:00:00 2001 From: weidai Date: Thu, 8 Apr 2004 01:23:05 +0000 Subject: add ThreadUserTimer git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@153 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- hrtimer.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 19 deletions(-) (limited to 'hrtimer.cpp') diff --git a/hrtimer.cpp b/hrtimer.cpp index 24fb6f9..7da494f 100644 --- a/hrtimer.cpp +++ b/hrtimer.cpp @@ -11,8 +11,7 @@ #include #elif defined(CRYPTOPP_UNIX_AVAILABLE) #include -#elif defined(macintosh) -#include +#include #endif #include @@ -30,10 +29,6 @@ word64 Timer::GetCurrentTimerValue() timeval now; gettimeofday(&now, NULL); return (word64)now.tv_sec * 1000000 + now.tv_usec; -#elif defined(macintosh) - UnsignedWide now; - Microseconds(&now); - return now.lo + ((word64)now.hi << 32); #endif } @@ -47,32 +42,64 @@ word64 Timer::TicksPerSecond() throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError())); } return freq.QuadPart; -#elif defined(CRYPTOPP_UNIX_AVAILABLE) || defined(macintosh) +#elif defined(CRYPTOPP_UNIX_AVAILABLE) return 1000000; #endif } -word64 Timer::ConvertTo(word64 t, Unit unit) +word64 ThreadUserTimer::GetCurrentTimerValue() +{ +#if defined(CRYPTOPP_WIN32_AVAILABLE) + static bool getCurrentThreadImplemented = true; + if (getCurrentThreadImplemented) + { + FILETIME now, ignored; + if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now)) + { + DWORD lastError = GetLastError(); + if (lastError == ERROR_CALL_NOT_IMPLEMENTED) + { + getCurrentThreadImplemented = false; + goto GetCurrentThreadNotImplemented; + } + throw Exception(Exception::OTHER_ERROR, "ThreadUserTimer: GetThreadTimes failed with error " + IntToString(lastError)); + } + return now.dwLowDateTime + ((word64)now.dwHighDateTime << 32); + } +GetCurrentThreadNotImplemented: + return (word64)clock() * (10*1000*1000 / CLOCKS_PER_SEC); +#elif defined(CRYPTOPP_UNIX_AVAILABLE) + tms now; + times(&now); + return now.tms_utime; +#endif +} + +word64 ThreadUserTimer::TicksPerSecond() +{ +#if defined(CRYPTOPP_WIN32_AVAILABLE) + return 10*1000*1000; +#elif defined(CRYPTOPP_UNIX_AVAILABLE) + static const long ticksPerSecond = sysconf(_SC_CLK_TCK); + return ticksPerSecond; +#endif +} + +double TimerBase::ConvertTo(word64 t, Unit unit) { static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000}; assert(unit < sizeof(unitsPerSecondTable) / sizeof(unitsPerSecondTable[0])); - unsigned long unitsPerSecond = unitsPerSecondTable[unit]; - const word64 freq = TicksPerSecond(); - - if (freq % unitsPerSecond == 0) - return t / (freq / unitsPerSecond); - else - return word64((double)t * unitsPerSecond / freq); + return (double)t * unitsPerSecondTable[unit] / TicksPerSecond(); } -void Timer::StartTimer() +void TimerBase::StartTimer() { m_start = GetCurrentTimerValue(); m_started = true; } -word64 Timer::ElapsedTimeInWord64() +double TimerBase::ElapsedTimeAsDouble() { if (m_stuckAtZero) return 0; @@ -85,9 +112,9 @@ word64 Timer::ElapsedTimeInWord64() } } -unsigned long Timer::ElapsedTime() +unsigned long TimerBase::ElapsedTime() { - word64 elapsed = ElapsedTimeInWord64(); + double elapsed = ElapsedTimeAsDouble(); assert(elapsed <= ULONG_MAX); return (unsigned long)elapsed; } -- cgit v1.2.1