Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Timer_Wheel_T.h

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

Generated on Mon Jun 16 11:21:53 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002