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

Timer_Hash_T.h

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

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