summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2002-04-22 19:33:40 +0000
committerMatthew Newhook <matthew@zeroc.com>2002-04-22 19:33:40 +0000
commit8c1949ea4e31a0df137711f57f68eea014032e10 (patch)
tree87a8229d1f23f0829e5f2f00f73a95a43d6dd1e8 /cpp/src
parentupdate config. Call router->shutdown on termination (diff)
downloadice-8c1949ea4e31a0df137711f57f68eea014032e10.tar.bz2
ice-8c1949ea4e31a0df137711f57f68eea014032e10.tar.xz
ice-8c1949ea4e31a0df137711f57f68eea014032e10.zip
Added IceUtil/Time.h
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/IcePatch/Client.cpp2
-rw-r--r--cpp/src/IceUtil/Makefile1
-rw-r--r--cpp/src/IceUtil/RWRecMutex.cpp82
-rw-r--r--cpp/src/IceUtil/Time.cpp158
4 files changed, 203 insertions, 40 deletions
diff --git a/cpp/src/IcePatch/Client.cpp b/cpp/src/IcePatch/Client.cpp
index f7d37caa762..43570c9be58 100644
--- a/cpp/src/IcePatch/Client.cpp
+++ b/cpp/src/IcePatch/Client.cpp
@@ -54,7 +54,7 @@ IcePatch::Client::usage()
int
IcePatch::Client::run(int argc, char* argv[])
{
- RouterPrx router;
+ Glacier::RouterPrx router;
try
{
diff --git a/cpp/src/IceUtil/Makefile b/cpp/src/IceUtil/Makefile
index 007b1a4ebcf..d42f0e0a66b 100644
--- a/cpp/src/IceUtil/Makefile
+++ b/cpp/src/IceUtil/Makefile
@@ -25,6 +25,7 @@ OBJS = Exception.o \
RWRecMutex.o \
Cond.o \
Thread.o \
+ Time.o \
OutputUtil.o \
Base64.o
diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp
index 44942c2cd9e..9fd75fd8c27 100644
--- a/cpp/src/IceUtil/RWRecMutex.cpp
+++ b/cpp/src/IceUtil/RWRecMutex.cpp
@@ -10,6 +10,7 @@
#include <IceUtil/RWRecMutex.h>
#include <IceUtil/Exception.h>
+#include <IceUtil/Time.h>
#include <assert.h>
@@ -64,11 +65,15 @@ IceUtil::RWRecMutex::timedTryReadlock(int timeout) const
// Wait while a writer holds the lock or while writers are waiting
// to get the lock.
//
- // TODO: This needs to check the time after each notify...
- //
+ Time end = Time::now() + Time::milliSeconds(timeout);
while (_count < 0 || _waitingWriters != 0)
{
- if (!_readers.timedWait(lock, timeout))
+ long t = (end - Time::now()).milliSeconds();
+ if (t > 0)
+ {
+ _readers.timedWait(lock, t);
+ }
+ else
{
throw LockedException(__FILE__, __LINE__);
}
@@ -165,27 +170,25 @@ IceUtil::RWRecMutex::timedTryWritelock(int timeout) const
// Wait for the lock to become available and increment the number
// of waiting writers.
//
- // TODO: This needs to check the time after each notify...
- //
- if (_count != 0)
+ Time end = Time::now() + Time::milliSeconds(timeout);
+ while (_count != 0)
{
- _waitingWriters++;
- bool timedOut;
- try
+ long t = (end - Time::now()).milliSeconds();
+ if (t > 0)
{
- timedOut = !_writers.timedWait(lock, timeout);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
+ _waitingWriters++;
+ try
+ {
+ _writers.timedWait(lock, t);
+ }
+ catch(...)
+ {
+ --_waitingWriters;
+ throw;
+ }
+ _waitingWriters--;
}
- _waitingWriters--;
-
- //
- // If a timeout occurred then the lock wasn't acquired.
- //
- if (timedOut)
+ else
{
throw LockedException(__FILE__, __LINE__);
}
@@ -316,29 +319,30 @@ IceUtil::RWRecMutex::timedUpgrade(int timeout) const
//
// Wait to acquire the write lock.
//
- // TODO: This needs to check the time after each notify...
- //
+ Time end = Time::now() + Time::milliSeconds(timeout);
while (_count != 0)
{
- _waitingWriters++;
- bool timedOut;
- try
+ long t = (end - Time::now()).milliSeconds();
+ if (t > 0)
{
- timedOut = !_writers.timedWait(lock, timeout);
- }
- catch(...)
- {
- --_waitingWriters;
- throw;
+ _waitingWriters++;
+ try
+ {
+ _writers.timedWait(lock, timeout);
+ }
+ catch(...)
+ {
+ --_waitingWriters;
+ throw;
+ }
+ _waitingWriters--;
}
- _waitingWriters--;
-
- //
- // If a timeout occurred then the lock wasn't acquired. Ensure
- // that the _count is increased again before returning.
- //
- if (timedOut)
+ else
{
+ //
+ // If a timeout occurred then the lock wasn't acquired. Ensure
+ // that the _count is increased again before returning.
+ //
++_count;
throw LockedException(__FILE__, __LINE__);
}
diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp
new file mode 100644
index 00000000000..f83758d0a05
--- /dev/null
+++ b/cpp/src/IceUtil/Time.cpp
@@ -0,0 +1,158 @@
+// **********************************************************************
+//
+// Copyright (c) 2001
+// MutableRealms, Inc.
+// Huntsville, AL, USA
+//
+// All Rights Reserved
+//
+// **********************************************************************
+
+#include <IceUtil/Time.h>
+
+#ifdef _WIN32
+# include <sys/timeb.h>
+#else
+# include <sys/time.h>
+#endif
+
+using namespace IceUtil;
+
+Time::Time(TimeInternal::LongLong usec) :
+ _usec(usec)
+{
+}
+
+IceUtil::Time::Time(const timeval& tv) :
+ _usec((tv.tv_sec * (TimeInternal::LongLong)1000000) + tv.tv_usec)
+{
+}
+
+Time
+IceUtil::Time::now()
+{
+#ifdef WIN32
+ struct _timeb timebuffer;
+ _ftime(&timebuffer);
+
+ return Time(timebuffer.time * (TimeInternal::LongLong)1000000) +
+ (timebuffer.millitm * (TimeInternal::LongLong)1000);
+#else
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+
+ return Time(tv);
+#endif
+}
+
+Time
+IceUtil::Time::seconds(TimeInternal::LongLong t)
+{
+ return Time(t * (TimeInternal::LongLong)1000000);
+}
+
+Time
+IceUtil::Time::milliSeconds(TimeInternal::LongLong t)
+{
+ return Time(t * (TimeInternal::LongLong)1000);
+}
+
+Time
+IceUtil::Time::microSeconds(TimeInternal::LongLong t)
+{
+ return Time(t);
+}
+
+Time
+IceUtil::Time::operator-() const
+{
+ return Time(-_usec);
+}
+
+Time
+IceUtil::Time::operator-(const Time& other) const
+{
+ return Time(_usec - other._usec);
+}
+
+Time
+IceUtil::Time::operator+(const Time& other) const
+{
+ return Time(_usec + other._usec);
+}
+
+Time&
+IceUtil::Time::operator+=(const Time& other)
+{
+ _usec += other._usec;
+ return *this;
+}
+
+Time&
+IceUtil::Time::operator-=(const Time& other)
+{
+ _usec -= other._usec;
+ return *this;
+}
+
+bool
+IceUtil::Time::operator<(const Time& other) const
+{
+ return _usec < other._usec;
+}
+
+bool
+IceUtil::Time::operator<=(const Time& other) const
+{
+ return _usec <= other._usec;
+}
+
+bool
+IceUtil::Time::operator>(const Time& other) const
+{
+ return _usec > other._usec;
+}
+
+bool
+IceUtil::Time::operator>=(const Time& other) const
+{
+ return _usec >= other._usec;
+}
+
+bool
+IceUtil::Time::operator==(const Time& other) const
+{
+ return _usec == other._usec;
+}
+
+bool
+IceUtil::Time::operator!=(const Time& other) const
+{
+ return _usec != other._usec;
+}
+
+IceUtil::Time::operator timeval() const
+{
+ timeval tv;
+ tv.tv_sec = _usec / 1000000;
+ tv.tv_usec = _usec % 1000000;
+ return tv;
+}
+
+TimeInternal::LongLong
+IceUtil::Time::seconds() const
+{
+ return _usec / 1000000;
+}
+
+TimeInternal::LongLong
+IceUtil::Time::milliSeconds() const
+{
+ return _usec / 1000;
+}
+
+TimeInternal::LongLong
+IceUtil::Time::microSeconds() const
+{
+ return _usec;
+}