=== modified file 'dcpp/TimerManager.cpp' --- dcpp/TimerManager.cpp 2013-01-18 21:28:38 +0000 +++ dcpp/TimerManager.cpp 2013-02-23 19:26:48 +0000 @@ -19,8 +19,6 @@ #include "stdinc.h" #include "TimerManager.h" -#include - namespace dcpp { using namespace boost::posix_time; @@ -40,23 +38,22 @@ } int TimerManager::run() { - int nextMin = 0; + getTick(); //Ensure start is initialized ptime now = microsec_clock::universal_time(); ptime nextSecond = now + seconds(1); + ptime nextMin = now + seconds(60); while(!mtx.timed_lock(nextSecond)) { - uint64_t t = getTick(); now = microsec_clock::universal_time(); - nextSecond += seconds(1); - if(nextSecond < now) { - nextSecond = now; - } - - fire(TimerManagerListener::Second(), t); - if(nextMin++ >= 60) { - fire(TimerManagerListener::Minute(), t); - nextMin = 0; + uint64_t t = getTick(now); + if (now >= nextSecond) { + nextSecond = now + seconds(1); // Wait one second from the time we started triggering stuff + fire(TimerManagerListener::Second(), t); + if (now >= nextMin) { + nextMin = now + seconds(60); // Wait one minute from the time we started triggering stuff + fire(TimerManagerListener::Minute(), t); + } } } @@ -66,9 +63,13 @@ return 0; } +uint64_t TimerManager::getTick(const ptime &t) { + static ptime start = microsec_clock::universal_time(); + return (t - start).total_milliseconds(); +} + uint64_t TimerManager::getTick() { - static ptime start = microsec_clock::universal_time(); - return (microsec_clock::universal_time() - start).total_milliseconds(); + return getTick(microsec_clock::universal_time()).total_milliseconds(); } } // namespace dcpp === modified file 'dcpp/TimerManager.h' --- dcpp/TimerManager.h 2013-01-18 21:28:38 +0000 +++ dcpp/TimerManager.h 2013-02-23 02:11:23 +0000 @@ -28,6 +28,7 @@ #ifndef _WIN32 #include #endif +#include namespace dcpp { @@ -39,7 +40,9 @@ typedef X<0> Second; typedef X<1> Minute; + //This method will be called once each second at most (may be called after more than one second under heavy load so use the ticks to know if you have to run more than one iteration virtual void on(Second, uint64_t) noexcept { } + //This method will be called once each minute at most (may be called after more than one second under heavy load so use the ticks to know if you have to run more than one iteration virtual void on(Minute, uint64_t) noexcept { } }; @@ -51,6 +54,8 @@ static time_t getTime() { return (time_t)time(NULL); } static uint64_t getTick(); private: + static uint64_t getTick(const boost::posix_time::ptime &t); + friend class Singleton; boost::timed_mutex mtx;