UTTimer.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_TIMER_H_
00026 #define _UT_TIMER_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== Project headers
00031 //==================================================================================================
00032 #include "UTTask.h"
00033 #include "UTMessage.h"
00034 
00035 
00036 //==================================================================================================
00037 //=== Forward name declarations
00038 //==================================================================================================
00039 class TimerExpiredMessage_t;
00040 class TimerManager_t;
00041 class TimersPlatformInfo_t;
00042 
00043 
00044 //==================================================================================================
00045 class UT_EXPORT Timer_t
00046 //==================================================================================================
00047 :   private BTreeNode_t< Timer_t, ustime_t >
00072 {
00073     //----------------------------------------------------------------------------------------------
00074     public:
00075     //----------------------------------------------------------------------------------------------
00076     inline  Timer_t(bool accept_jitter = false);
00078 
00079     inline  ~Timer_t();
00081 
00082     void    StartOneShot(   ustime_t expires_at,
00083                             const MessageDestination_t<TimerExpiredMessage_t>& dest,
00084                             TimerManager_t* manager );
00095 
00096     void    StartPeriodic(  uint32 period_ms,
00097                             const MessageDestination_t<TimerExpiredMessage_t>& dest,
00098                             TimerManager_t* manager );
00103 
00104     void    Cancel();
00107 
00108     //----------------------------------------------------------------------------------------------
00109     private:
00110     //----------------------------------------------------------------------------------------------
00111     inline int                                  Compare(ustime_t other) const;
00112     inline                                      operator ustime_t() const;
00113 
00114     //----------------------------------------------------------------------------------------------
00115     private:
00116     //----------------------------------------------------------------------------------------------
00117     TimerManager_t*                             m_owner;
00118     bool                                        m_periodic;
00119     bool                                        m_accept_jitter;
00120     ustime_t                                    m_next_expire_at;
00121     uint32                                      m_period_ms;
00122     MessageDestination_t<TimerExpiredMessage_t> m_dest;
00123 
00124     friend class BTree_t< Timer_t, ustime_t >;
00125     friend class BTreeNode_t< Timer_t, ustime_t >;
00126     friend class TimerManager_t;
00127 };
00128 
00129 
00130 //==================================================================================================
00131 class UT_EXPORT TimerManager_t
00132 //==================================================================================================
00133 : public Task_t
00138 {
00139     //----------------------------------------------------------------------------------------------
00140     public:
00141     //----------------------------------------------------------------------------------------------
00142                                     TimerManager_t();
00144 
00145                                     ~TimerManager_t();
00147 
00148     //----------------------------------------------------------------------------------------------
00149     private:
00150     //----------------------------------------------------------------------------------------------
00151     void                            Add(Timer_t* timer);
00152     void                            Remove(Timer_t* timer);
00153     Timer_t*                        DispatchExpiredTimers();
00154                                         // Returns a pointer to the next timer to expire
00155 
00156     //----------------------------------------------------------------------------------------------
00157     private:
00158     //----------------------------------------------------------------------------------------------
00159     BTree_t< Timer_t, ustime_t >    m_timers;
00160     TimersPlatformInfo_t*           m_platform_info;
00161     bool                            m_reset_timer_msg_pending;
00162     bool                            m_quitting;
00163 
00164     //----------------------------------------------------------------------------------------------
00165     private:
00166     //----------------------------------------------------------------------------------------------
00167     void TimerExpiredReceived(const TimerExpiredMessage_t* message);
00168     MessageReceiver_t< TimerManager_t, TimerExpiredMessage_t >  m_TimerExpiredReceiver;
00169 
00170     friend class Timer_t;
00171     friend class TimersPlatformInfo_t;
00172 };
00173 
00174 
00175 //==================================================================================================
00176 class UT_EXPORT TimerExpiredMessage_t
00177 //==================================================================================================
00178 : public Message_t
00180 {
00181     //----------------------------------------------------------------------------------------------
00182     public:
00183     //----------------------------------------------------------------------------------------------
00184     inline Timer_t* Timer() const;
00187 
00188     inline ustime_t DispatchTime() const;
00196 
00197     //----------------------------------------------------------------------------------------------
00198     private:
00199     //----------------------------------------------------------------------------------------------
00200     inline          TimerExpiredMessage_t();
00201     inline          TimerExpiredMessage_t( Timer_t* timer, ustime_t dispatch_time );
00202 
00203     //----------------------------------------------------------------------------------------------
00204     private:
00205     //----------------------------------------------------------------------------------------------
00206     Timer_t*        m_timer;
00207     ustime_t        m_dispatch_time;
00208 
00209     friend class TimerManager_t;
00210     friend class TimersPlatformInfo_t;
00211 };
00212 
00213 
00214 
00215 
00216 //==================================================================================================
00217 //==================================================================================================
00218 //===
00219 //=== Inline function implementations: class Timer_t
00220 //===
00221 //==================================================================================================
00222 //==================================================================================================
00223 inline
00224 Timer_t::Timer_t(bool accept_jitter)
00225 {
00226     m_owner = NULL;
00227     m_accept_jitter = accept_jitter;
00228 }
00229 
00230 
00231 inline
00232 Timer_t::~Timer_t()
00233 {
00234     Cancel();
00235     assert(m_owner == NULL);
00236 }
00237 
00238 
00239 inline int
00240 Timer_t::Compare(ustime_t other) const
00241 {
00242     if(m_next_expire_at - other < 0)
00243         return -1;
00244     if(m_next_expire_at - other > 0)
00245         return 1;
00246     return 0;
00247 }
00248 
00249 
00250 inline
00251 Timer_t::operator ustime_t() const
00252 {
00253     return m_next_expire_at;
00254 }
00255 
00256 
00257 
00258 
00259 //==================================================================================================
00260 //==================================================================================================
00261 //===
00262 //=== Inline function implementations: class TimerExpiredMessage_t
00263 //===
00264 //==================================================================================================
00265 //==================================================================================================
00266 inline
00267 TimerExpiredMessage_t::TimerExpiredMessage_t()
00268 {
00269     // Nothing to do
00270 }
00271 
00272 
00273 inline Timer_t*
00274 TimerExpiredMessage_t::Timer() const
00275 {
00276     return m_timer;
00277 }
00278 
00279 
00280 inline
00281 TimerExpiredMessage_t::TimerExpiredMessage_t( Timer_t* timer, ustime_t dispatch_time )
00282 {
00283     m_timer = timer;
00284     m_dispatch_time = dispatch_time;
00285 }
00286 
00287 
00288 inline ustime_t
00289 TimerExpiredMessage_t::DispatchTime() const
00290 {
00291     return m_dispatch_time;
00292 }
00293 
00294 
00295 #endif // _UT_TIMER_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1