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

Generated on Tue Feb 2 17:18:43 2010 for ACE by  doxygen 1.4.7