diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2007-09-05 14:44:03 -0230 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2007-09-05 14:44:03 -0230 |
commit | bdcf4f222f50b915d2dfc0ea3960758adf6937db (patch) | |
tree | e263a048b6297395c97e860c87a0ca27b569cb08 /cpp/src/IceUtil/Time.cpp | |
parent | Added missing file (diff) | |
download | ice-bdcf4f222f50b915d2dfc0ea3960758adf6937db.tar.bz2 ice-bdcf4f222f50b915d2dfc0ea3960758adf6937db.tar.xz ice-bdcf4f222f50b915d2dfc0ea3960758adf6937db.zip |
http://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=1351 - use monotonic timers where possible
Diffstat (limited to 'cpp/src/IceUtil/Time.cpp')
-rw-r--r-- | cpp/src/IceUtil/Time.cpp | 54 |
1 files changed, 44 insertions, 10 deletions
diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index c39c2bcf53e..d562d7a72b4 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -25,24 +25,58 @@ Time::Time() : { } +#ifdef _WIN32 +Int64 IceUtil::Time::_frequency = -1; +#endif + Time -IceUtil::Time::now() +IceUtil::Time::now(Clock clock) { + if(clock == Realtime) + { #ifdef _WIN32 # if defined(_MSC_VER) - struct _timeb tb; - _ftime(&tb); + struct _timeb tb; + _ftime(&tb); # elif defined(__BCPLUSPLUS__) - struct timeb tb; - ftime(&tb); + struct timeb tb; + ftime(&tb); # endif - return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + - tb.millitm * 1000); + return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + + tb.millitm * 1000); #else - struct timeval tv; - gettimeofday(&tv, 0); - return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); + struct timeval tv; + gettimeofday(&tv, 0); + return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); #endif + } + else // Monotonic + { +#if defined(_WIN32) + if(_frequency == -1) + { + // + // Frequency cannot change while machine is running so it + // only needs to be retrieved once. + // + QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency); + } + Int64 count; + QueryPerformanceCounter((LARGE_INTEGER*)&count); + return Time((Int64)(1000000.0 / _frequency * count)); +#elif defined(__hpux) || defined(__APPLE__) + // + // HP/MacOS does not support CLOCK_MONOTONIC + // + struct timeval tv; + gettimeofday(&tv, 0); + return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); +#else + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return Time(ts.tv_sec * ICE_INT64(1000000) + ts.tv_nsec / ICE_INT64(1000)); +#endif + } } Time |