00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Timer_List_T.h 00006 * 00007 * Timer_List_T.h,v 4.40 2006/04/27 11:17:56 jwillemsen 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. @a upcall_functor is the instance of the 00102 * FUNCTOR to be used by the list. If @a upcall_functor is 0, a 00103 * default FUNCTOR will be created. @a freelist is 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 * Resets the interval of the timer represented by @a timer_id to 00120 * @a interval, which is specified in relative time to the current 00121 * <gettimeofday>. If @a interval is equal to 00122 * ACE_Time_Value::zero, the timer will become a non-rescheduling 00123 * timer. Returns 0 if successful, -1 if not. 00124 */ 00125 virtual int reset_interval (long timer_id, 00126 const ACE_Time_Value& interval); 00127 00128 /** 00129 * Cancel all timers associated with @a type. If dont_call_handle_close is 0 00130 * then the @a functor will be invoked. Returns the number of timers 00131 * cancelled. 00132 */ 00133 virtual int cancel (const TYPE& type, 00134 int dont_call_handle_close = 1); 00135 00136 /** 00137 * Cancel the single timer that matches the @a timer_id value (which 00138 * was returned from the <schedule> method). If act is non-NULL 00139 * then it will be set to point to the ``magic cookie'' argument 00140 * passed in when the timer was registered. This makes it possible 00141 * to free up the memory and avoid memory leaks. If <dont_call> is 00142 * 0 then the <functor> will be invoked. Returns 1 if cancellation 00143 * succeeded and 0 if the <timer_id> wasn't found. 00144 */ 00145 virtual int cancel (long timer_id, 00146 const void** act = 0, 00147 int dont_call_handle_close = 1); 00148 00149 /// Returns a pointer to this ACE_Timer_Queue's iterator. 00150 virtual ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>& iter (void); 00151 00152 /// Removes the earliest node from the queue and returns it 00153 virtual ACE_Timer_Node_T<TYPE>* remove_first (void); 00154 00155 /// Dump the state of an object. 00156 virtual void dump (void) const; 00157 00158 /// Reschedule an "interval" ACE_Timer_Node_T. This should be private 00159 /// but for now it needs to be public for <ACE_Timer_Hash_T> 00160 virtual void reschedule (ACE_Timer_Node_T<TYPE> *); 00161 00162 /// Reads the earliest node from the queue and returns it. 00163 virtual ACE_Timer_Node_T<TYPE>* get_first (void); 00164 00165 private: 00166 00167 /** 00168 * Schedule <type> that will expire at <future_time>, which is 00169 * specified in absolute time. If it expires then <act> is passed 00170 * in as the value to the <functor>. If <interval> is != to 00171 * <ACE_Time_Value::zero> then it is used to reschedule the <type> 00172 * automatically, using relative time to the current <gettimeofday>. 00173 * This method returns a <timer_id> that uniquely identifies the the 00174 * <type> entry in an internal list. This <timer_id> can be used to 00175 * cancel the timer before it expires. The cancellation ensures 00176 * that <timer_ids> are unique up to values of greater than 2 00177 * billion timers. As long as timers don't stay around longer than 00178 * this there should be no problems with accidentally deleting the 00179 * wrong timer. Returns -1 on failure (which is guaranteed never to 00180 * be a valid <timer_id>). 00181 */ 00182 virtual long schedule_i (const TYPE& type, 00183 const void* act, 00184 const ACE_Time_Value& future_time, 00185 const ACE_Time_Value& interval); 00186 00187 void schedule_i(ACE_Timer_Node_T<TYPE>* n, const ACE_Time_Value& exp); 00188 00189 ACE_Timer_Node_T<TYPE>* find_node(long timer_id) const; 00190 00191 void cancel_i (ACE_Timer_Node_T<TYPE>* n); 00192 00193 void unlink (ACE_Timer_Node_T<TYPE>* n); 00194 00195 ACE_Timer_Node_T<TYPE>* get_first_i(void) const; 00196 00197 private: 00198 00199 /// Pointer to linked list of <ACE_Timer_Handles>. 00200 ACE_Timer_Node_T<TYPE>* head_; 00201 00202 /// Iterator used to expire timers. 00203 Iterator* iterator_; 00204 00205 /** 00206 * Keeps track of the timer id that uniquely identifies each timer. 00207 * This id can be used to cancel a timer via the <cancel(long)> 00208 * method. 00209 */ 00210 long id_counter_; 00211 00212 // = Don't allow these operations for now. 00213 ACE_UNIMPLEMENTED_FUNC (ACE_Timer_List_T (const ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK> &)) 00214 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_List_T<TYPE, FUNCTOR, ACE_LOCK> &)) 00215 }; 00216 00217 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) && !defined(ACE_HAS_BROKEN_HPUX_TEMPLATES) 00218 #include "ace/Timer_List_T.cpp" 00219 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE && !ACE_HAS_BROKEN_HPUX_TEMPLATES */ 00220 00221 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00222 #pragma implementation ("Timer_List_T.cpp") 00223 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00224 00225 #include /**/ "ace/post.h" 00226 #endif /* ACE_TIMER_LIST_T_H */