Timer_Hash_T.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Timer_Hash_T.h
00006  *
00007  *  Timer_Hash_T.h,v 1.40 2006/02/10 10:30:43 jwillemsen Exp
00008  *
00009  *  @author Darrell Brunsch <brunsch@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_TIMER_HASH_T_H
00014 #define ACE_TIMER_HASH_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 #include "ace/Free_List.h"
00024 
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026 
00027 // Forward declaration.
00028 template <class TYPE, class FUNCTOR, class ACE_LOCK, class BUCKET>
00029 class ACE_Timer_Hash_T;
00030 
00031 /**
00032  * @class ACE_Timer_Hash_Upcall
00033  *
00034  * @brief Functor for Timer_Hash
00035  *
00036  * This class calls up to the Timer Hash's functor from the
00037  * timer queues in the hash table
00038  */
00039 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00040 class ACE_Timer_Hash_Upcall
00041 {
00042 public:
00043   typedef ACE_Timer_Queue_T<ACE_Event_Handler *,
00044                             ACE_Timer_Hash_Upcall<TYPE, FUNCTOR, ACE_LOCK>,
00045                             ACE_Null_Mutex>
00046           TIMER_QUEUE;
00047 
00048   /// Default constructor (creates an invalid object, but needs to be here
00049   /// so timer queues using this functor can be constructed)
00050   ACE_Timer_Hash_Upcall (void);
00051 
00052   /// Constructor that specifies a Timer_Hash to call up to
00053   ACE_Timer_Hash_Upcall (ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> *timer_hash);
00054 
00055   /// This method is called when a timer is registered.
00056   int registration (TIMER_QUEUE &timer_queue,
00057                     ACE_Event_Handler *handler,
00058                     const void *arg);
00059 
00060   /// This method is called before the timer expires.
00061   int preinvoke (TIMER_QUEUE &timer_queue,
00062                  ACE_Event_Handler *handler,
00063                  const void *arg,
00064                  int recurring_timer,
00065                  const ACE_Time_Value &cur_time,
00066                  const void *&upcall_act);
00067 
00068   /// This method is called when the timer expires.
00069   int timeout (TIMER_QUEUE &timer_queue,
00070                ACE_Event_Handler *handler,
00071                const void *arg,
00072                int recurring_timer,
00073                const ACE_Time_Value &cur_time);
00074 
00075   /// This method is called after the timer expires.
00076   int postinvoke (TIMER_QUEUE &timer_queue,
00077                   ACE_Event_Handler *handler,
00078                   const void *arg,
00079                   int recurring_timer,
00080                   const ACE_Time_Value &cur_time,
00081                   const void *upcall_act);
00082 
00083   /// This method is called when a handler is cancelled
00084   int cancel_type (TIMER_QUEUE &timer_queue,
00085                    ACE_Event_Handler *handler,
00086                    int dont_call,
00087                    int &requires_reference_counting);
00088 
00089   /// This method is called when a timer is cancelled
00090   int cancel_timer (TIMER_QUEUE &timer_queue,
00091                     ACE_Event_Handler *handler,
00092                     int dont_call,
00093                     int requires_reference_counting);
00094 
00095   /// This method is called when the timer queue is destroyed and
00096   /// the timer is still contained in it
00097   int deletion (TIMER_QUEUE &timer_queue,
00098                 ACE_Event_Handler *handler,
00099                 const void *arg);
00100 
00101 private:
00102   /// Timer Queue to do the calling up to
00103   ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> *timer_hash_;
00104 
00105   // = Don't allow these operations for now.
00106   ACE_UNIMPLEMENTED_FUNC (ACE_Timer_Hash_Upcall (const ACE_Timer_Hash_Upcall<TYPE, FUNCTOR, ACE_LOCK> &))
00107   ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_Hash_Upcall<TYPE, FUNCTOR, ACE_LOCK> &))
00108 };
00109 
00110 /**
00111  * @class ACE_Timer_Hash_Iterator_T
00112  *
00113  * @brief Iterates over an <ACE_Timer_Hash_T>.
00114  *
00115  * This is a generic iterator that can be used to visit every
00116  * node of a timer queue.  Be aware that it doesn't transverse
00117  * in the order of timeout values.
00118  */
00119 template <class TYPE, class FUNCTOR, class ACE_LOCK, class BUCKET>
00120 class ACE_Timer_Hash_Iterator_T : public ACE_Timer_Queue_Iterator_T <TYPE, FUNCTOR, ACE_LOCK>
00121 {
00122 public:
00123   /// Constructor.
00124   ACE_Timer_Hash_Iterator_T (ACE_Timer_Hash_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET> &);
00125 
00126   /// Positions the iterator at the earliest node in the Timer Queue
00127   virtual void first (void);
00128 
00129   /// Positions the iterator at the next node in the Timer Queue
00130   virtual void next (void);
00131 
00132   /// Returns true when there are no more nodes in the sequence
00133   virtual int isdone (void) const;
00134 
00135   /// Returns the node at the current position in the sequence
00136   virtual ACE_Timer_Node_T<TYPE> *item (void);
00137 
00138 protected:
00139   /// Pointer to the ACE_Timer_Hash that we are iterating over.
00140   ACE_Timer_Hash_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET> &timer_hash_;
00141 
00142   /// Current position in <timer_hash_>'s table
00143   size_t position_;
00144 
00145   /// Current iterator used on <position>'s bucket
00146   ACE_Timer_Queue_Iterator_T<TYPE, ACE_Timer_Hash_Upcall<TYPE, FUNCTOR, ACE_LOCK>, ACE_Null_Mutex> *iter_;
00147 };
00148 
00149 /**
00150  * @class ACE_Timer_Hash_T
00151  *
00152  * @brief Provides a hash table of <BUCKET>s as an implementation for
00153  * a timer queue.
00154  *
00155  * This implementation uses a hash table of BUCKETs.  The hash
00156  * is based on the time_value of the event.  Unlike other Timer
00157  * Queues, ACE_Timer_Hash does not expire events in order.
00158  */
00159 template <class TYPE, class FUNCTOR, class ACE_LOCK, class BUCKET>
00160 class ACE_Timer_Hash_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>
00161 {
00162 public:
00163   /// Type of iterator
00164   typedef ACE_Timer_Hash_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET>
00165           HASH_ITERATOR;
00166 
00167   /// Iterator is a friend
00168   friend class ACE_Timer_Hash_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET>;
00169 
00170   /// Type inherited from
00171   typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> INHERITED;
00172 
00173   // = Initialization and termination methods.
00174   /**
00175    * Default constructor. <table_size> determines the size of the
00176    * hash table.  <upcall_functor> is the instance of the FUNCTOR
00177    * to be used by the buckets. If <upcall_functor> is 0, a default
00178    * FUNCTOR will be created.
00179    */
00180   ACE_Timer_Hash_T (size_t table_size,
00181                     FUNCTOR *upcall_functor = 0,
00182                     ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist = 0);
00183 
00184   /**
00185    * Default constructor. <upcall_functor> is the instance of the
00186    * FUNCTOR to be used by the queue. If <upcall_functor> is 0, Timer
00187    * Hash will create a default FUNCTOR.  <freelist> the freelist of
00188    * timer nodes.  If 0, then a default freelist will be created.  The default
00189    * size will be ACE_DEFAULT_TIMERS and there will be no preallocation.
00190    */
00191   ACE_Timer_Hash_T (FUNCTOR *upcall_functor = 0, ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist = 0);
00192 
00193   /// Destructor
00194   virtual ~ACE_Timer_Hash_T (void);
00195 
00196   /// True if queue is empty, else false.
00197   virtual int is_empty (void) const;
00198 
00199   /// Returns the time of the earlier node in the <ACE_Timer_Hash>.
00200   /// Must be called on a non-empty queue.
00201   virtual const ACE_Time_Value &earliest_time (void) const;
00202 
00203   /**
00204    * Resets the interval of the timer represented by <timer_id> to
00205    * <interval>, which is specified in relative time to the current
00206    * <gettimeofday>.  If <interval> is equal to
00207    * <ACE_Time_Value::zero>, the timer will become a non-rescheduling
00208    * timer.  Returns 0 if successful, -1 if not.
00209    */
00210   virtual int reset_interval (long timer_id,
00211                               const ACE_Time_Value &interval);
00212 
00213   /**
00214    * Cancel all timer associated with <type>.  If <dont_call> is 0
00215    * then the <functor> will be invoked.  Returns number of timers
00216    * cancelled.
00217    */
00218   virtual int cancel (const TYPE &type,
00219                       int dont_call_handle_close = 1);
00220 
00221   /**
00222    * Cancel the single timer that matches the <timer_id> value (which
00223    * was returned from the <schedule> method).  If act is non-NULL
00224    * then it will be set to point to the ``magic cookie'' argument
00225    * passed in when the timer was registered.  This makes it possible
00226    * to free up the memory and avoid memory leaks.  If <dont_call> is
00227    * 0 then the <functor> will be invoked.  Returns 1 if cancellation
00228    * succeeded and 0 if the <timer_id> wasn't found.
00229    */
00230   virtual int cancel (long timer_id,
00231                       const void **act = 0,
00232                       int dont_call_handle_close = 1);
00233 
00234   /**
00235    * Run the <functor> for all timers whose values are <=
00236    * <ACE_OS::gettimeofday>.  Also accounts for <timer_skew>.  Returns
00237    * the number of timers canceled.
00238    */
00239   virtual int expire (void);
00240 
00241   /**
00242    * Run the <functor> for all timers whose values are <= <cur_time>.
00243    * This does not account for <timer_skew>.  Returns the number of
00244    * timers canceled.
00245    */
00246   virtual int expire (const ACE_Time_Value &current_time);
00247 
00248   /// Returns a pointer to this ACE_Timer_Queue's iterator.
00249   virtual ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> &iter (void);
00250 
00251   /// Removes the earliest node from the queue and returns it
00252   virtual ACE_Timer_Node_T<TYPE> *remove_first (void);
00253 
00254   /// Dump the state of an object.
00255   virtual void dump (void) const;
00256 
00257   /// Reads the earliest node from the queue and returns it.
00258   virtual ACE_Timer_Node_T<TYPE> *get_first (void);
00259 
00260 private:
00261 
00262   /**
00263    * Schedule <type> that will expire at <future_time>,
00264    * which is specified in absolute time.  If it expires then <act> is
00265    * passed in as the value to the <functor>.  If <interval> is != to
00266    * <ACE_Time_Value::zero> then it is used to reschedule the <type>
00267    * automatically, using relative time to the current <gettimeofday>.
00268    * This method returns a <timer_id> that is a pointer to a token
00269    * which stores information about the event. This <timer_id> can be
00270    * used to cancel the timer before it expires.  Returns -1 on
00271    * failure.
00272    */
00273   virtual long schedule_i (const TYPE &type,
00274                            const void *act,
00275                            const ACE_Time_Value &future_time,
00276                            const ACE_Time_Value &interval);
00277 
00278   /// Non-locking version of dispatch_info ()
00279   virtual int dispatch_info_i (const ACE_Time_Value &current_time,
00280                                ACE_Timer_Node_Dispatch_Info_T<TYPE> &info);
00281 
00282   /// Reschedule an "interval" <ACE_Timer_Node>.
00283   virtual void reschedule (ACE_Timer_Node_T<TYPE> *);
00284 
00285   /// Finds the earliest node
00286   void find_new_earliest (void);
00287 
00288   /// Keeps track of the size of the queue
00289   size_t size_;
00290 
00291   /// Table of BUCKETS
00292   BUCKET **table_;
00293 
00294   /// Keeps track of the size of table_
00295   size_t table_size_;
00296 
00297   /// Functor used for the table's timer queues
00298   ACE_Timer_Hash_Upcall<TYPE, FUNCTOR, ACE_LOCK> table_functor_;
00299 
00300   /// Index to the position with the earliest entry
00301   size_t earliest_position_;
00302 
00303   /// Iterator used to expire timers.
00304   HASH_ITERATOR *iterator_;
00305 
00306 #if defined (ACE_WIN64)
00307   // Part of a hack... see comments in schedule().
00308   // This is, essentially, the upper 32 bits of a 64-bit pointer on Win64.
00309   ptrdiff_t pointer_base_;
00310 #endif
00311 
00312   // = Don't allow these operations for now.
00313   ACE_UNIMPLEMENTED_FUNC (ACE_Timer_Hash_T (const ACE_Timer_Hash_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET> &))
00314   ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_Hash_T<TYPE, FUNCTOR, ACE_LOCK, BUCKET> &))
00315 };
00316 
00317 ACE_END_VERSIONED_NAMESPACE_DECL
00318 
00319 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) && !defined(ACE_HAS_BROKEN_HPUX_TEMPLATES)
00320 #include "ace/Timer_Hash_T.cpp"
00321 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE && !ACE_HAS_BROKEN_HPUX_TEMPLATES */
00322 
00323 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00324 #pragma implementation ("Timer_Hash_T.cpp")
00325 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00326 
00327 #include /**/ "ace/post.h"
00328 #endif /* ACE_TIMER_HASH_T_H */

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