Timer_Wheel_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Timer_Wheel_T.h
00006  *
00007  *  Timer_Wheel_T.h,v 1.47 2006/04/27 11:17:56 jwillemsen Exp
00008  *
00009  *  @author Darrell Brunsch <brunsch@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_TIMER_WHEEL_T_H
00014 #define ACE_TIMER_WHEEL_T_H
00015 #include /**/ "ace/pre.h"
00016 
00017 #include "ace/Timer_Queue_T.h"
00018 
00019 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00020 # pragma once
00021 #endif /* ACE_LACKS_PRAGMA_ONCE */
00022 
00023 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00024 
00025 // Forward declaration
00026 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00027 class ACE_Timer_Wheel_T;
00028 
00029 /**
00030  * @class ACE_Timer_Wheel_Iterator_T
00031  *
00032  * @brief Iterates over an <ACE_Timer_Wheel>.
00033  *
00034  * This is a generic iterator that can be used to visit every
00035  * node of a timer queue.  Be aware that it doesn't traverse
00036  * in the order of timeout values.
00037  */
00038 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00039 class ACE_Timer_Wheel_Iterator_T
00040   : public ACE_Timer_Queue_Iterator_T <TYPE, FUNCTOR, ACE_LOCK>
00041 {
00042 public:
00043   typedef ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK> Wheel;
00044   typedef ACE_Timer_Node_T<TYPE> Node;
00045 
00046   /// Constructor
00047   ACE_Timer_Wheel_Iterator_T (Wheel &);
00048 
00049   /// Destructor
00050   ~ACE_Timer_Wheel_Iterator_T (void);
00051 
00052   /// Positions the iterator at the earliest node in the Timer Queue
00053   virtual void first (void);
00054 
00055   /// Positions the iterator at the next node in the Timer Queue
00056   virtual void next (void);
00057 
00058   /// Returns true when there are no more nodes in the sequence
00059   virtual int isdone (void) const;
00060 
00061   /// Returns the node at the current position in the sequence
00062   virtual ACE_Timer_Node_T<TYPE>* item (void);
00063 
00064 protected:
00065   /// Pointer to the ACE_Timer_List that we are iterating over.
00066   Wheel& timer_wheel_;
00067 
00068   /// Current position in the timing wheel
00069   u_int spoke_;
00070 
00071   /// Pointer to the position in the the <pos_>th list
00072   ACE_Timer_Node_T<TYPE>* current_node_;
00073 private:
00074   void goto_next(u_int start_spoke);
00075 };
00076 
00077 /**
00078  * @class ACE_Timer_Wheel_T
00079  *
00080  * @brief Provides a Timing Wheel version of ACE_Timer_Queue.
00081  *
00082  * This implementation uses a hash table of ordered doubly-
00083  * linked lists of absolute times.  The enhancements over the
00084  * @c ACE_Timer_List include adding a free list and the ability
00085  * to preallocate nodes.  Timer Wheel is based on the timing
00086  * wheel implementation used in Adam M. Costello and
00087  * George Varghese's paper "Redesigning the BSD Callout and
00088  * Timer Facilities"
00089  * (http://dworkin.wustl.edu/~varghese/PAPERS/newbsd.ps.Z)
00090  */
00091 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00092 class ACE_Timer_Wheel_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>
00093 {
00094 public:
00095   /// Type of iterator
00096   typedef ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> Iterator;
00097   /// Iterator is a friend
00098   friend class ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>;
00099   typedef ACE_Timer_Node_T<TYPE> Node;
00100   /// Type inherited from
00101   typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> Base;
00102   typedef ACE_Free_List<Node> FreeList;
00103 
00104   /// Default constructor
00105   ACE_Timer_Wheel_T (FUNCTOR* upcall_functor = 0, FreeList* freelist = 0);
00106 
00107   /// Constructor with opportunities to set the wheelsize and resolution
00108   ACE_Timer_Wheel_T (u_int spoke_count,
00109                      u_int resolution,
00110                      size_t prealloc = 0,
00111                      FUNCTOR* upcall_functor = 0,
00112                      FreeList* freelist = 0);
00113 
00114   /// Destructor
00115   virtual ~ACE_Timer_Wheel_T (void);
00116 
00117   /// True if queue is empty, else false.
00118   virtual int is_empty (void) const;
00119 
00120   /// Returns the time of the earlier node in the <ACE_Timer_Wheel>.
00121   /// Must be called on a non-empty queue.
00122   virtual const ACE_Time_Value& earliest_time (void) const;
00123 
00124   /// Changes the interval of a timer (and can make it periodic or non
00125   /// periodic by setting it to ACE_Time_Value::zero or not).
00126   virtual int reset_interval (long timer_id,
00127                               const ACE_Time_Value& interval);
00128 
00129   /// Cancel all timer associated with <type>.  If <dont_call> is 0
00130   /// then the <functor> will be invoked.  Returns number of timers
00131   /// cancelled.
00132   virtual int cancel (const TYPE& type,
00133                       int dont_call_handle_close = 1);
00134 
00135   // Cancel a timer, storing the magic cookie in act (if nonzero).
00136   // Calls the functor if dont_call_handle_close is 0 and returns 1
00137   // on success
00138   virtual int cancel (long timer_id,
00139                       const void** act = 0,
00140                       int dont_call_handle_close = 1);
00141 
00142   /// Run the <functor> for all timers whose values are <=
00143   /// <ACE_OS::gettimeofday>.  Also accounts for <timer_skew>.  Returns
00144   /// the number of timers canceled.
00145   virtual int expire (void);
00146 
00147   // Run the <functor> for all timers whose values are <= <cur_time>.
00148   // This does not account for <timer_skew>.  Returns the number of
00149   // timers canceled.
00150   int expire (const ACE_Time_Value&);
00151 
00152   /// Returns a pointer to this <ACE_Timer_Queue_T>'s iterator.
00153   virtual ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>& iter (void);
00154 
00155   /// Removes the earliest node from the queue and returns it
00156   virtual ACE_Timer_Node_T<TYPE>* remove_first (void);
00157 
00158   /// Dump the state of an object.
00159   virtual void dump (void) const;
00160 
00161   /// Reads the earliest node from the queue and returns it.
00162   virtual ACE_Timer_Node_T<TYPE>* get_first (void);
00163 
00164 protected:
00165 
00166   /// Schedules a timer.
00167   virtual long schedule_i (const TYPE& type,
00168                            const void* act,
00169                            const ACE_Time_Value& future_time,
00170                            const ACE_Time_Value& interval);
00171 
00172 private:
00173   // The following are documented in the .cpp file.
00174   ACE_Timer_Node_T<TYPE>* get_first_i (void) const;
00175   ACE_Timer_Node_T<TYPE>* remove_first_expired (const ACE_Time_Value& now);
00176   void open_i (size_t prealloc, u_int spokes, u_int res);
00177   virtual void reschedule (ACE_Timer_Node_T<TYPE> *);
00178   ACE_Timer_Node_T<TYPE>* find_spoke_node(u_int spoke, long timer_id) const;
00179   ACE_Timer_Node_T<TYPE>* find_node(long timer_id) const;
00180   u_int calculate_spoke(const ACE_Time_Value& expire) const;
00181   long generate_timer_id(u_int spoke);
00182   void schedule_i (ACE_Timer_Node_T<TYPE>* n, u_int spoke, const ACE_Time_Value& expire);
00183   void cancel_i (ACE_Timer_Node_T<TYPE>* n);
00184   void unlink (ACE_Timer_Node_T<TYPE>* n);
00185   void recalc_earliest(const ACE_Time_Value& last);
00186 
00187 private:
00188   int power2bits (int n, int min_bits, int max_bits);
00189 
00190   /// Timing Wheel.
00191   ACE_Timer_Node_T<TYPE>** spokes_;
00192   /// Size of the timing wheel.
00193   u_int spoke_count_;
00194   /// Number of timer_id bits used for the spoke
00195   int spoke_bits_;
00196   /// Maximum number of timers per spoke
00197   u_int max_per_spoke_;
00198   /// Resolution (in microsoconds) of the timing wheel.
00199   int res_bits_;
00200   /// Index of the list with the earliest time
00201   u_int earliest_spoke_;
00202   /// Iterator used to expire timers.
00203   Iterator* iterator_;
00204   /// The total amount of time in one iteration of the wheel. (resolution * spoke_count)
00205   ACE_Time_Value wheel_time_;
00206   /// The total number of timers currently scheduled.
00207   u_int timer_count_;
00208 
00209   // = Don't allow these operations for now, don't split into multiple lines
00210   // breaks sun compilers
00211   ACE_UNIMPLEMENTED_FUNC (ACE_Timer_Wheel_T (const ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK> &))
00212   ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK> &))
00213 };
00214 
00215 ACE_END_VERSIONED_NAMESPACE_DECL
00216 
00217 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00218 #if !defined (ACE_HAS_BROKEN_HPUX_TEMPLATES)
00219 #include "ace/Timer_Wheel_T.cpp"
00220 #endif /* !ACE_HAS_BROKEN_HPUX_TEMPLATES */
00221 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00222 
00223 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00224 #pragma implementation ("Timer_Wheel_T.cpp")
00225 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00226 
00227 #include /**/ "ace/post.h"
00228 #endif /* ACE_TIMER_WHEEL_T_H */

Generated on Thu Nov 9 09:42:07 2006 for ACE by doxygen 1.3.6