[asycxx-devel] SF.net SVN: asycxx:[49] trunk
Status: Alpha
Brought to you by:
joe_steeve
From: <joe...@us...> - 2009-04-08 07:48:14
|
Revision: 49 http://asycxx.svn.sourceforge.net/asycxx/?rev=49&view=rev Author: joe_steeve Date: 2009-04-08 07:48:12 +0000 (Wed, 08 Apr 2009) Log Message: ----------- fixed class:Timer to not use Deferreds The timer is now fired directly from the reactor From: Joe Steeve <js...@hi...> Modified Paths: -------------- trunk/include/asycxx/Timer.h trunk/src/Timer.cxx Modified: trunk/include/asycxx/Timer.h =================================================================== --- trunk/include/asycxx/Timer.h 2009-04-08 07:47:28 UTC (rev 48) +++ trunk/include/asycxx/Timer.h 2009-04-08 07:48:12 UTC (rev 49) @@ -15,70 +15,131 @@ #define __HIPRO_ASYCXX__TIMER_H__ -#include <vector> +#include <list> #include "Reactor.h" +namespace asycxx +{ + /** + * \brief Timer's callback function type + * + * \param[in] obj The context-object to use for the callback + * \param[in] excess_time The number of milli-seconds elapsed after + * the configured timeout period. Consult the documentation of Timer + * for more information on this. + */ + typedef void (*cbTimerCallback_t)(void *obj, asycxx_msecs_t excess_time); -/** - * \brief Timer's callback function type - * - * \param[in] obj The context-object to use for the callback - * \param[in] excess_time The number of milli-seconds elapsed after the - * configured timeout period. Consult the documentation of Timer for - * more information on this. - */ -typedef void (*cbTimerCallback_t)(void *obj, h_msecs_t excess_time); - #define ASYCXX_TIMER_RESOLUTION 200 -class Timer -{ -public: - /** - * \brief Timer type + * \class Timer + * \brief A timer that fires on a configured timeout. * - * \detail The timer-type changes the behaviour of the - * timer. Consult the documentation of Timer class for more - * information. + * \details The Timer fires when its configured timeout-time + * elapses. This class hooks itself with the reactor to keep track + * of time. On successful creation, when the timeout-time elapses, + * it calls a callback. A timer can be either active or + * in-active. This can be controlled using the Timer::StartTimer and + * Timer::StopTimer methods. + * + * There are two types of timers supported by this class and are + * defined in the enumeration Timer::TimerType. + * + * (1) Timer_OneShot: This fires exactly one-time and then goes to + * inactive mode. This can be brought back to active mode using + * Timer::StartTimer + * + * (2) Timer_Periodic: This fires every time the timeout occurs. + * + * The callback function is of the type cbTimerCallback_t. It takes + * two parameters. The 'excess_time' parameter in the callback + * contains the number of milli-seconds that exceeded the timeout. */ - enum TimerType - { - Timer_OneShot, /**< A one-shot timer. Fires only once */ - Timer_Periodic, /**< A periodic timer. Fires periodically */ - }; - static void Init (Reactor *reactor); - static void UpdateTimers (h_timestamp_t currentTS); - - Timer (h_msecs_t timeoutTime, TimerType type, - cbTimerCallback_t cb_fn, void * cb_obj); - ~Timer (); - void StartTimer (void); - void StopTimer (void); + class Timer + { + public: -private: - static Reactor *m_Reactor; - static std::vector<Timer *> m_Timers; - static h_timestamp_t m_LastUpdatedTS; - static Deferred *m_ReactorDeferred; - static void RegisterTimer (Timer *timer); - static void UnRegisterTimer (Timer *timer); - static void CheckAndFireTimers (void *obj, h_msecs_t excess_time); + /** + * \brief Timer type + * + * \detail The timer-type changes the behaviour of the + * timer. Consult the documentation of Timer class for more + * information. + */ + enum TimerType + { + Timer_OneShot, /**< A one-shot timer. Fires only once */ + Timer_Periodic, /**< A periodic timer. Fires periodically */ + }; + + Timer (asycxx_msecs_t timeoutTime, TimerType type, + cbTimerCallback_t cb_fn, void * cb_obj); + ~Timer (); - enum TimerType m_Type; - bool m_bTimerActive; - h_msecs_t m_TimeoutTime; - h_timestamp_t m_LastTriggerTS; - h_timestamp_t m_NextTriggerTS; + /** + * \brief Puts the timer in active mode + * + * \detail This method puts the timer in active mode. When in + * active mode, the timer fires when a timeout happens. + */ + void StartTimer (void); - cbTimerCallback_t m_cbFunction; - void * m_cbObject; -}; + /** + * \brief Puts the timer in in-active mode + * + * \detail This method puts the timer in in-active mode. All + * timeouts are lost. + */ + void StopTimer (void); + /** + * \brief One time timer initialization method + * + * \param[in] reactor The reactor object to hook to + * + * \details This method is a static method which Initializes the + * 'Timer' class. This should be called before creating any timer + * objects. + */ + static void Init (Reactor *reactor); + /** + * \brief Check for expired timers and fire them + * + * \details This method iterates through the global list of timers + * and fires the ones that have expired + */ + static void CheckAndFireTimers (void); + + private: + /* static data and methods */ + static Reactor *m_Reactor; + static std::list<Timer *> m_Timers; + static asycxx_timestamp_t m_LastUpdatedTS; + + /* convenience methods to register/unregister a timer from the + global list of timers */ + static void RegisterTimer (Timer *timer); + static void UnRegisterTimer (Timer *timer); + + private: + /* non-static data */ + enum TimerType m_Type; + bool m_bTimerActive; + + asycxx_msecs_t m_TimeoutTime; + asycxx_timestamp_t m_LastTriggerTS; + asycxx_timestamp_t m_NextTriggerTS; + + cbTimerCallback_t m_cbFunction; + void * m_cbObject; + }; +} + #endif /* __HIPRO_ASYCXX__TIMER_H__ */ /* Modified: trunk/src/Timer.cxx =================================================================== --- trunk/src/Timer.cxx 2009-04-08 07:47:28 UTC (rev 48) +++ trunk/src/Timer.cxx 2009-04-08 07:48:12 UTC (rev 49) @@ -11,120 +11,72 @@ * *******************************************************************/ -/** - * \author Joe Steeve, jo...@hi... - * \class Timer - * \brief A timer that fires on a configured timeout. - * - * \details The Timer fires when its configured timeout-time - * elapses. This class hooks itself with the reactor to keep track of - * time. On successful creation, when the timeout-time elapses, it - * calls a callback. A timer can be either active or in-active. This - * can be controlled using the Timer::StartTimer and Timer::StopTimer - * methods. - * - * There are two types of timers supported by this class and are - * defined in the enumeration Timer::TimerType. - * - * (1) Timer_OneShot: This fires exactly one-time and then goes to - * inactive mode. This can be brought back to active mode using - * Timer::StartTimer - * - * (2) Timer_Periodic: This fires every time the timeout occurs. - * - * The callback function is of the type cbTimerCallback_t. It takes - * two parameters. The 'excess_time' parameter in the callback - * contains the number of milli-seconds that exceeded the timeout. - */ #ifdef HAVE_CONFIG_H #include <asycxx-config.h> #endif -#include <vector> +#include <list> #include "asycxx-common.h" #include <asycxx/Error.h> #include <asycxx/Timer.h> -/*****************************************************************************/ +using namespace asycxx; + Reactor * Timer::m_Reactor = NULL; -std::vector<Timer *> Timer::m_Timers; -h_timestamp_t Timer::m_LastUpdatedTS = 0LL; -Deferred * Timer::m_ReactorDeferred = NULL; +std::list<Timer *> Timer::m_Timers; +asycxx_timestamp_t Timer::m_LastUpdatedTS = 0LL; -/** - * \brief Initialize the Timer class. - * - * \param[in] reactor The reactor object to hook to - * - * \details This method is a static method which Initializes the - * 'Timer' class. This should be called before creating any timer - * objects. - */ +/* this must be called from the reactor at the time of + * initialization */ void Timer::Init (Reactor *reactor) { - if (reactor == NULL) - { THROW (DevError, "cannot marry a <NULL> reactor. :("); } - + ASSERT ((reactor != NULL), "cannot marry a <NULL> reactor. :("); m_Reactor = reactor; m_LastUpdatedTS = m_Reactor->CurrentTS(); - - /* Register with the reactor */ - m_ReactorDeferred = m_Reactor->OnTimeElapsed (ASYCXX_TIMER_RESOLUTION); - m_ReactorDeferred->OnTimeout (CheckAndFireTimers, NULL); } -/** - * \brief Registers a timer with the global list of timers - * - * \param[in] timer The timer object that should be registered - * - * \details This method adds the given timer (if it does not exist - * already) to the global list of timers. A timer object will recieve - * a 'trigger' only when it is registered. This should be called from - * the ctor of the timer-object. - */ +/* This method adds the given timer (if it does not exist already) to + * the global list of timers. A timer object will recieve a 'trigger' + * only when it is registered. This should be called from the ctor of + * the timer-object. */ void Timer::RegisterTimer (Timer *timer) { - size_t i; - + /* we should tell the user if the timeout-time is is less than our + resolution */ if (timer->m_TimeoutTime < ASYCXX_TIMER_RESOLUTION) { ERR ("given timeout-time(%lld) < ASYCXX_TIMER_RESOLUTION(%lld)", timer->m_TimeoutTime, ASYCXX_TIMER_RESOLUTION); } - for (i=0; i<m_Timers.size(); i++) + /* check if this timer is not already activated */ + std::list<Timer *>::iterator it; + for (it = m_Timers.begin(); it != m_Timers.end(); it++) { - if (m_Timers[i] == timer) { return; } + if (*it == timer) { return; } } + /* add the timer to the list */ m_Timers.push_back (timer); } -/** - * \brief Un-registers a timer from the global list of timers - * - * \param[in] timer The timer object that should be un-registered - * - * \details This method removes the given timer from the global list - * of timers. This should be called from the dtor of the timer-object. - */ +/* This method removes the given timer from the global list of + * timers. This should be called from the dtor of the timer-object. */ void Timer::UnRegisterTimer (Timer *timer) { - size_t i; - - for (i=0; i<m_Timers.size(); i++) + std::list<Timer *>::iterator it; + for (it = m_Timers.begin(); it != m_Timers.end(); it++) { - if (m_Timers[i] == timer) + if (*it == timer) { - m_Timers.erase (m_Timers.begin()+i); + m_Timers.erase (it); return; } } @@ -133,70 +85,57 @@ } -/** - * \brief Callback hooked into the reactor - * - * \detail This method is registered with the reactor to recieve - * notifications on timeout of ASYCXX_TIMER_RESOLUTION milliseconds. - */ +/* This method is registered with the reactor to recieve notifications + * on timeout of ASYCXX_TIMER_RESOLUTION milliseconds. */ void -Timer::CheckAndFireTimers (void *obj, h_msecs_t excess_time) +Timer::CheckAndFireTimers (void) { - h_msecs_t diffTime; - h_timestamp_t currentTS; - size_t i; + asycxx_msecs_t diffTime; + asycxx_timestamp_t currentTS; + std::list<Timer *>::iterator it; currentTS = m_Reactor->CurrentTS(); - if (currentTS < m_LastUpdatedTS) - { - THROW (DevError, "Timer hooked to Reactor<%p>, " - "current-TimeStamp(%lld) < last-updated-TimeStamp(%lld)", - m_Reactor, currentTS, m_LastUpdatedTS); - } + ASSERT ((currentTS >= m_LastUpdatedTS), + "Timer hooked to Reactor<%p>, " + "current-TimeStamp(%lld) < last-updated-TimeStamp(%lld)", + m_Reactor, currentTS, m_LastUpdatedTS); /* Look through the list of timers and fire the ones that are ready to be fired */ Timer *t; - for (i=0; i<m_Timers.size(); i++) + for (it = m_Timers.begin(); it != m_Timers.end(); it++) { - t = m_Timers[i]; + t = *it; /* If trigger-time not yet arrived, go on to the next one */ - if (t->m_NextTriggerTS > currentTS) - { - continue; - } + if (t->m_NextTriggerTS > currentTS) { continue; } /* Fire the timer's callbacks */ diffTime = currentTS - t->m_NextTriggerTS; t->m_cbFunction (t->m_cbObject, diffTime); + /* Update the timer */ t->m_LastTriggerTS = currentTS; t->m_NextTriggerTS = currentTS + t->m_TimeoutTime; + /* stop a one-shot timer after firing once */ - if (t->m_Type == Timer_OneShot) - { - t->StopTimer (); - } + if (t->m_Type == Timer_OneShot) { t->StopTimer (); } } m_LastUpdatedTS = currentTS; } + /*****************************************************************************/ -/** - * \brief Constructor - * - */ -Timer::Timer (h_msecs_t timeoutTime, TimerType type, + +/* ctor */ +Timer::Timer (asycxx_msecs_t timeoutTime, TimerType type, cbTimerCallback_t cb_fn, void * cb_obj) { - if (timeoutTime == 0) - { THROW (DevError, "timeoutTime=0. wait indefinitely??"); } - if ((type < Timer::Timer_OneShot) || (type > Timer::Timer_Periodic)) - { THROW (DevError, "i dont know a timer of type=%d", type); } - if (cb_fn == NULL) - { THROW (DevError, "callback-function is NULL."); } + ASSERT ((timeoutTime != 0), "timeoutTime=0. wait indefinitely??"); + ASSERT (((type >= Timer::Timer_OneShot) && (type <= Timer::Timer_Periodic)), + "i dont know a timer of type=%d", type); + ASSERT ((cb_fn != NULL), "callback-function is NULL."); /* initialize the timer variables */ m_TimeoutTime = timeoutTime; @@ -212,22 +151,14 @@ } -/** - * \brief Destructor - * - */ +/* dtor */ Timer::~Timer () { StopTimer (); } -/** - * \brief Puts the timer in active mode - * - * \detail This method puts the timer in active mode. When in active - * mode, the timer fires when a timeout happens. - */ +/* activate the timer */ void Timer::StartTimer (void) { @@ -248,12 +179,7 @@ } -/** - * \brief Puts the timer in in-active mode - * - * \detail This method puts the timer in in-active mode. All timeouts - * are lost. - */ +/* de-activate the timer */ void Timer::StopTimer (void) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |