diff options
author | Jose <jose@zeroc.com> | 2012-02-21 17:04:50 +0100 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2012-02-21 17:04:50 +0100 |
commit | 31f1254d83018687a36d40d07d878ec07bfbe7c4 (patch) | |
tree | 31da873dd8c19f3f0ce5b10b71ea0f815d08a0c8 | |
parent | ICE-4588 - DynamicLibrary & LoadLibraryW (diff) | |
download | ice-31f1254d83018687a36d40d07d878ec07bfbe7c4.tar.bz2 ice-31f1254d83018687a36d40d07d878ec07bfbe7c4.tar.xz ice-31f1254d83018687a36d40d07d878ec07bfbe7c4.zip |
ICE-4740 - Timer for mac doesn't implement monotonic time
-rw-r--r-- | cpp/include/IceUtil/Cond.h | 4 | ||||
-rw-r--r-- | cpp/src/IceUtil/Time.cpp | 31 |
2 files changed, 33 insertions, 2 deletions
diff --git a/cpp/include/IceUtil/Cond.h b/cpp/include/IceUtil/Cond.h index 4c7f4b34bde..fe6c676b7cc 100644 --- a/cpp/include/IceUtil/Cond.h +++ b/cpp/include/IceUtil/Cond.h @@ -234,7 +234,11 @@ Cond::timedWaitImpl(const M& mutex, const Time& timeout) const LockState state; mutex.unlock(state); +#ifdef __APPLE__ + timeval tv = Time::now(Time::Realtime) + timeout; +#else timeval tv = Time::now(Time::Monotonic) + timeout; +#endif timespec ts; ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index 121208c4bdf..b42b49c2927 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -19,6 +19,12 @@ # include <sys/time.h> #endif +#ifdef __APPLE__ +# include <CoreServices/CoreServices.h> +# include <mach/mach.h> +# include <mach/mach_time.h> +#endif + using namespace IceUtil; #ifdef _WIN32 @@ -58,6 +64,25 @@ InitializeFrequency frequencyInitializer; } #endif +#ifdef __APPLE__ +namespace +{ + +mach_timebase_info_data_t initTimeBase = {0, 0}; +class InitializeTime +{ +public: + + InitializeTime() + { + mach_timebase_info(&initTimeBase); + } +}; +InitializeTime initializeTime; + +} +#endif + Time::Time() : _usec(0) { @@ -111,9 +136,9 @@ IceUtil::Time::now(Clock clock) # endif return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + tb.millitm * 1000); } -#elif defined(__hpux) || defined(__APPLE__) +#elif defined(__hpux) // - // HP/MacOS does not support CLOCK_MONOTONIC + // HP does not support CLOCK_MONOTONIC // struct timeval tv; if(gettimeofday(&tv, 0) < 0) @@ -122,6 +147,8 @@ IceUtil::Time::now(Clock clock) throw SyscallException(__FILE__, __LINE__, errno); } return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); +#elif defined(__APPLE__) + return Time((mach_absolute_time() * initTimeBase.numer / initTimeBase.denom) / ICE_INT64(1000)); #else struct timespec ts; if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0) |