From 065e0b531fe827c3a153a76493d11e3e2a73b9ea Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 26 Nov 2006 19:54:00 +0000 Subject: * Fix timer expiry calculation when timers expire in the past * Fix handling of timers which never expire (zero deadline) (refs #9) --- lib/common/Timer.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'lib/common') diff --git a/lib/common/Timer.cpp b/lib/common/Timer.cpp index cfeca3c5..5ebfc133 100644 --- a/lib/common/Timer.cpp +++ b/lib/common/Timer.cpp @@ -143,7 +143,7 @@ void Timers::Reschedule() ASSERT(spTimers); box_time_t timeNow = GetCurrentBoxTime(); - box_time_t timeToNextEvent = 0; + int64_t timeToNextEvent = 0; for (std::vector::iterator i = spTimers->begin(); i != spTimers->end(); i++) @@ -151,7 +151,8 @@ void Timers::Reschedule() Timer& rTimer = **i; ASSERT(!rTimer.HasExpired()); - box_time_t timeToExpiry = rTimer.GetExpiryTime() - timeNow; + int64_t timeToExpiry = rTimer.GetExpiryTime() - timeNow; + if (timeToExpiry <= 0) timeToExpiry = 1; if (timeToNextEvent == 0 || timeToNextEvent > timeToExpiry) { @@ -203,7 +204,7 @@ void Timers::Signal() Timer& rTimer = **i; ASSERT(!rTimer.HasExpired()); - box_time_t timeToExpiry = rTimer.GetExpiryTime() - timeNow; + int64_t timeToExpiry = rTimer.GetExpiryTime() - timeNow; if (timeToExpiry <= 0) { @@ -218,7 +219,14 @@ Timer::Timer(size_t timeoutSecs) : mExpires(GetCurrentBoxTime() + SecondsToBoxTime(timeoutSecs)), mExpired(false) { - Timers::Add(*this); + if (timeoutSecs == 0) + { + mExpires = 0; + } + else + { + Timers::Add(*this); + } } Timer::~Timer() @@ -230,7 +238,10 @@ Timer::Timer(const Timer& rToCopy) : mExpires(rToCopy.mExpires), mExpired(rToCopy.mExpired) { - Timers::Add(*this); + if (mExpires != 0) + { + Timers::Add(*this); + } } Timer& Timer::operator=(const Timer& rToCopy) @@ -238,10 +249,11 @@ Timer& Timer::operator=(const Timer& rToCopy) Timers::Remove(*this); mExpires = rToCopy.mExpires; mExpired = rToCopy.mExpired; - if (!mExpired) + if (!mExpired && mExpires != 0) { Timers::Add(*this); } + return *this; } void Timer::OnExpire() -- cgit v1.2.3