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

Timer_List_T.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Timer_List_T.h
00006  *
00007  *  $Id: Timer_List_T.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_TIMER_LIST_T_H
00014 #define ACE_TIMER_LIST_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_List_T;
00026 
00027 /**
00028  * @class ACE_Timer_List_Iterator_T
00029  *
00030  * @brief Iterates over an <ACE_Timer_List>.
00031  *
00032  * This is a generic iterator that can be used to visit every
00033  * node of a timer queue.
00034  */
00035 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00036 class ACE_Timer_List_Iterator_T 
00037 : public ACE_Timer_Queue_Iterator_T <TYPE, FUNCTOR, ACE_LOCK>
00038 {
00039 public:
00040   typedef ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK> List;
00041   /// Constructor.
00042   ACE_Timer_List_Iterator_T (List& lst);
00043 
00044   /// Destructor.
00045   virtual ~ACE_Timer_List_Iterator_T (void);
00046 
00047   /// Positions the iterator at the earliest node in the Timer Queue
00048   virtual void first (void);
00049 
00050   /// Positions the iterator at the next node in the Timer Queue
00051   virtual void next (void);
00052 
00053   /// Returns true when there are no more nodes in the sequence
00054   virtual int isdone (void) const;
00055 
00056   /// Returns the node at the current position in the sequence
00057   virtual ACE_Timer_Node_T<TYPE> *item (void);
00058 
00059 protected:
00060   /// Pointer to the <ACE_Timer_List> that we are iterating over.
00061   List& list_;
00062 
00063   /// Current position in the <ACE_Timer_List>
00064   ACE_Timer_Node_T<TYPE>* current_node_;
00065 };
00066 
00067 /**
00068  * @class ACE_Timer_List_T
00069  *
00070  * @brief Provides a simple implementation of timers.
00071  *
00072  * This implementation uses a linked list of absolute times.
00073  * Therefore, in the average case, scheduling and canceling
00074  * timers is O(N) (where N is the total number of timers) and
00075  * expiring timers is O(K) (where K is the total number of timers
00076  * that are < the current time of day).
00077  * More clever implementations could use a delta-list, a heap,
00078  * or timing wheels, etc.  For instance, <ACE_Timer_Heap>
00079  * is a subclass of <ACE_Timer_List> that implements a
00080  * heap-based callout queue.  For most applications, the
00081  * <ACE_Timer_Heap> will perform substantially faster than the
00082  * <ACE_Timer_List>.
00083  */
00084 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00085 class ACE_Timer_List_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>
00086 {
00087 public:
00088   /// Type of iterator
00089   typedef ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> Iterator;
00090 
00091   /// Iterator is a friend
00092   friend class ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>;
00093 
00094   typedef ACE_Timer_Node_T<TYPE> Node;
00095   /// Type inherited from
00096   typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> Base;
00097   typedef ACE_Free_List<Node> FreeList;
00098 
00099   // = Initialization and termination methods.
00100   /**
00101    * Default constructor. <upcall_functor> is the instance of the
00102    * FUNCTOR to be used by the list. If <upcall_functor> is 0, a
00103    * default FUNCTOR will be created.  <freelist> the freelist of
00104    * timer nodes.  If 0, then a default freelist will be created.
00105    */
00106   ACE_Timer_List_T (FUNCTOR* upcall_functor = 0, FreeList* freelist = 0);
00107 
00108   /// Destructor
00109   virtual ~ACE_Timer_List_T (void);
00110 
00111   /// True if queue is empty, else false.
00112   virtual int is_empty (void) const;
00113 
00114   /// Returns the time of the earlier node in the <ACE_Timer_List>.
00115   /// Must be called on a non-empty queue.
00116   virtual const ACE_Time_Value& earliest_time (void) const;
00117 
00118   /**
00119    * Schedule <type> that will expire at <future_time>,
00120    * which is specified in absolute time.  If it expires then <act> is
00121    * passed in as the value to the <functor>.  If <interval> is != to
00122    * <ACE_Time_Value::zero> then it is used to reschedule the <type>
00123    * automatically, using relative time to the current <gettimeofday>.
00124    * This method returns a <timer_id> that uniquely identifies the the
00125    * <type> entry in an internal list.  This <timer_id> can be used to
00126    * cancel the timer before it expires.  The cancellation ensures
00127    * that <timer_ids> are unique up to values of greater than 2
00128    * billion timers.  As long as timers don't stay around longer than
00129    * this there should be no problems with accidentally deleting the
00130    * wrong timer.  Returns -1 on failure (which is guaranteed never to
00131    * be a valid <timer_id>).
00132    */
00133   virtual long schedule (const TYPE& type,
00134                          const void* act,
00135                          const ACE_Time_Value& future_time,
00136                          const ACE_Time_Value& interval = ACE_Time_Value::zero);
00137 
00138   /**
00139    * Resets the interval of the timer represented by <timer_id> to
00140    * <interval>, which is specified in relative time to the current
00141    * <gettimeofday>.  If <interval> is equal to
00142    * <ACE_Time_Value::zero>, the timer will become a non-rescheduling
00143    * timer.  Returns 0 if successful, -1 if not.
00144    */
00145   virtual int reset_interval (long timer_id,
00146                               const ACE_Time_Value& interval);
00147 
00148   /**
00149    * Cancel all timers associated with <type>.  If <dont_call> is 0
00150    * then the <functor> will be invoked.  Returns number of timers
00151    * cancelled.
00152    */
00153   virtual int cancel (const TYPE& type,
00154                       int dont_call_handle_close = 1);
00155 
00156   /**
00157    * Cancel the single timer that matches the <timer_id> value (which
00158    * was returned from the <schedule> method).  If act is non-NULL
00159    * then it will be set to point to the ``magic cookie'' argument
00160    * passed in when the timer was registered.  This makes it possible
00161    * to free up the memory and avoid memory leaks.  If <dont_call> is
00162    * 0 then the <functor> will be invoked.  Returns 1 if cancellation
00163    * succeeded and 0 if the <timer_id> wasn't found.
00164    */
00165   virtual int cancel (long timer_id,
00166                       const void** act = 0,
00167                       int dont_call_handle_close = 1);
00168 
00169   /// Returns a pointer to this <ACE_Timer_Queue>'s iterator.
00170   virtual ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>& iter (void);
00171 
00172   /// Removes the earliest node from the queue and returns it
00173   virtual ACE_Timer_Node_T<TYPE>* remove_first (void);
00174 
00175   /// Dump the state of an object.
00176   virtual void dump (void) const;
00177 
00178   /// Reschedule an "interval" <ACE_Timer_Node>.  This should be private
00179   /// but for now it needs to be public for <ACE_Timer_Hash_T>
00180   virtual void reschedule (ACE_Timer_Node_T<TYPE> *);
00181 
00182   /// Reads the earliest node from the queue and returns it.
00183   virtual ACE_Timer_Node_T<TYPE>* get_first (void);
00184 
00185 private:
00186 
00187   void schedule_i(ACE_Timer_Node_T<TYPE>* n, const ACE_Time_Value& exp);
00188   ACE_Timer_Node_T<TYPE>* find_node(long timer_id) const;
00189   void cancel_i (ACE_Timer_Node_T<TYPE>* n, int skip_close);
00190   void unlink (ACE_Timer_Node_T<TYPE>* n);
00191   ACE_Timer_Node_T<TYPE>* get_first_i(void) const;
00192 
00193 private:
00194 
00195   /// Pointer to linked list of <ACE_Timer_Handles>.
00196   ACE_Timer_Node_T<TYPE>* head_;
00197 
00198   /// Iterator used to expire timers.
00199   Iterator* iterator_;
00200 
00201   /**
00202    * Keeps track of the timer id that uniquely identifies each timer.
00203    * This id can be used to cancel a timer via the <cancel(long)>
00204    * method.
00205    */
00206   long id_counter_;
00207 
00208   // = Don't allow these operations for now.
00209   ACE_UNIMPLEMENTED_FUNC (ACE_Timer_List_T (const ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK> &))
00210   ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK> &))
00211 };
00212 
00213 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) && !defined(ACE_HAS_BROKEN_HPUX_TEMPLATES)
00214 #include "ace/Timer_List_T.cpp"
00215 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE && !ACE_HAS_BROKEN_HPUX_TEMPLATES */
00216 
00217 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00218 #pragma implementation ("Timer_List_T.cpp")
00219 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00220 
00221 #include "ace/post.h"
00222 #endif /* ACE_TIMER_LIST_T_H */

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