summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/Time.cpp
diff options
context:
space:
mode:
authorBenoit Foucher <benoit@zeroc.com>2008-01-23 11:31:53 +0100
committerBenoit Foucher <benoit@zeroc.com>2008-01-23 11:31:53 +0100
commitc7fb26230801e62f3e690a8948d37c33517c4c13 (patch)
treef17689f60e13fbbd20d12473272a6f0652f39a78 /cpp/src/IceUtil/Time.cpp
parentremoving EventHandler in C# (diff)
downloadice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.bz2
ice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.xz
ice-c7fb26230801e62f3e690a8948d37c33517c4c13.zip
- Added IceUtil::SyscallException and cleaned up few IceUtil exceptions
- Added errorToString() and lastErrorToString() functions to IceUtil/StringUtil.h - Replaced multiple implementations of errorToString methods with the IceUtil one. - Fixed bug 2641.
Diffstat (limited to 'cpp/src/IceUtil/Time.cpp')
-rw-r--r--cpp/src/IceUtil/Time.cpp91
1 files changed, 73 insertions, 18 deletions
diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp
index d562d7a72b4..0c747fb7104 100644
--- a/cpp/src/IceUtil/Time.cpp
+++ b/cpp/src/IceUtil/Time.cpp
@@ -8,6 +8,7 @@
// **********************************************************************
#include <IceUtil/DisableWarnings.h>
+#include <IceUtil/Exception.h>
#include <IceUtil/Time.h>
#include <iomanip>
@@ -20,15 +21,48 @@
using namespace IceUtil;
+#ifdef _WIN32
+
+namespace
+{
+
+static double frequency = -1.0;
+
+//
+// Initialize the frequency
+//
+class InitializeFrequency
+{
+public:
+
+ InitializeFrequency()
+ {
+ //
+ // Get the frequency of performance counters. We also make a call to
+ // QueryPerformanceCounter to ensure it works. If it fails or if the
+ // call to QueryPerformanceFrequency fails, the frequency will remain
+ // set to -1.0 and ftime will be used instead.
+ //
+ Int64 v;
+ if(QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&v)))
+ {
+ if(QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&v)))
+ {
+ frequency = static_cast<double>(v);
+ }
+ }
+ }
+};
+static InitializeFrequency frequencyInitializer;
+
+};
+#endif
+
Time::Time() :
_usec(0)
{
}
-#ifdef _WIN32
-Int64 IceUtil::Time::_frequency = -1;
-#endif
-
Time
IceUtil::Time::now(Clock clock)
{
@@ -42,38 +76,59 @@ IceUtil::Time::now(Clock clock)
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);
+ if(gettimeofday(&tv, 0) < 0)
+ {
+ assert(0);
+ throw SyscallException(__FILE__, __LINE__, errno);
+ }
return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec);
#endif
}
else // Monotonic
{
#if defined(_WIN32)
- if(_frequency == -1)
+ if(frequency > 0.0)
+ {
+ Int64 count;
+ if(!QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&count)))
+ {
+ assert(0);
+ throw SyscallException(__FILE__, __LINE__, GetLastError());
+ }
+ return Time(static_cast<Int64>(count / frequency * 1000000.0));
+ }
+ else
{
- //
- // Frequency cannot change while machine is running so it
- // only needs to be retrieved once.
- //
- QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency);
+# if defined(_MSC_VER)
+ struct _timeb tb;
+ _ftime(&tb);
+# elif defined(__BCPLUSPLUS__)
+ struct timeb tb;
+ ftime(&tb);
+# endif
+ return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + tb.millitm * 1000);
}
- 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);
+ if(gettimeofday(&tv, 0) < 0)
+ {
+ assert(0);
+ throw SyscallException(__FILE__, __LINE__, errno);
+ }
return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec);
#else
struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
+ if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
+ {
+ assert(0);
+ throw SyscallException(__FILE__, __LINE__, errno);
+ }
return Time(ts.tv_sec * ICE_INT64(1000000) + ts.tv_nsec / ICE_INT64(1000));
#endif
}