summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/ReapThread.cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2006-09-07 01:26:41 +0000
committerMatthew Newhook <matthew@zeroc.com>2006-09-07 01:26:41 +0000
commit37d9f5a4d8ba8376b223b05c29ff2669d9734515 (patch)
tree960d682a2f21818b47a441853546b56e0def3ec1 /cpp/src/IceGrid/ReapThread.cpp
parentFix (diff)
downloadice-37d9f5a4d8ba8376b223b05c29ff2669d9734515.tar.bz2
ice-37d9f5a4d8ba8376b223b05c29ff2669d9734515.tar.xz
ice-37d9f5a4d8ba8376b223b05c29ff2669d9734515.zip
reaper thread does not use a fixed timeout.
Diffstat (limited to 'cpp/src/IceGrid/ReapThread.cpp')
-rw-r--r--cpp/src/IceGrid/ReapThread.cpp63
1 files changed, 60 insertions, 3 deletions
diff --git a/cpp/src/IceGrid/ReapThread.cpp b/cpp/src/IceGrid/ReapThread.cpp
index 382d8e928fc..f9f89e65765 100644
--- a/cpp/src/IceGrid/ReapThread.cpp
+++ b/cpp/src/IceGrid/ReapThread.cpp
@@ -13,8 +13,7 @@
using namespace std;
using namespace IceGrid;
-ReapThread::ReapThread(int wakeInterval) :
- _wakeInterval(IceUtil::Time::seconds(wakeInterval)),
+ReapThread::ReapThread() :
_terminated(false)
{
}
@@ -27,7 +26,20 @@ ReapThread::run()
{
{
Lock sync(*this);
- timedWait(_wakeInterval);
+ calcWakeInterval();
+ //
+ // If the wake interval is zero then we wait forever.
+ //
+ if(_wakeInterval == IceUtil::Time())
+ {
+ assert(_sessions.empty());
+ wait();
+ }
+ else
+ {
+ assert(!_sessions.empty());
+ timedWait(_wakeInterval);
+ }
if(_terminated)
{
@@ -94,9 +106,54 @@ ReapThread::add(const ReapablePtr& reapable, int timeout)
return;
}
+ //
+ // 10 seconds is the minimum permissable timeout.
+ //
+ if(timeout < 10)
+ {
+ timeout = 10;
+ }
+
ReapableItem item;
item.item = reapable;
item.timeout = IceUtil::Time::seconds(timeout);
_sessions.push_back(item);
+
+ //
+ // If there is a new minimum wake interval then wake the reaping
+ // thread.
+ //
+ if(calcWakeInterval())
+ {
+ notify();
+ }
+
+ //
+ // Since we just added a new session there must be a non-zero
+ // wakeInterval.
+ //
+ assert(_wakeInterval != IceUtil::Time());
}
+// Returns true iff the calculated wake interval is less than the
+// current wake interval (or if the original wake interval was
+// "forever").
+bool
+ReapThread::calcWakeInterval()
+{
+ // Re-calculate minimum timeout
+ IceUtil::Time oldWakeInterval = _wakeInterval;
+ IceUtil::Time minimum;
+ bool first = true;
+ for(list<ReapableItem>::const_iterator p = _sessions.begin(); p != _sessions.end(); ++p)
+ {
+ if(first || p->timeout < minimum)
+ {
+ minimum = p->timeout;
+ first = false;
+ }
+ }
+
+ _wakeInterval = minimum;
+ return oldWakeInterval == IceUtil::Time() || minimum < oldWakeInterval;
+}