3 //=============================================================================
7 * $Id: Timer_List_T.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
11 //=============================================================================
13 #ifndef ACE_TIMER_LIST_T_H
14 #define ACE_TIMER_LIST_T_H
15 #include /**/ "ace/pre.h"
17 #include "ace/Timer_Queue_T.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 // Forward declaration.
24 template <class TYPE
, class FUNCTOR
, class ACE_LOCK
>
25 class ACE_Timer_List_T
;
28 * @class ACE_Timer_List_Iterator_T
30 * @brief Iterates over an ACE_Timer_List.
32 * This is a generic iterator that can be used to visit every
33 * node of a timer queue.
35 template <class TYPE
, class FUNCTOR
, class ACE_LOCK
>
36 class ACE_Timer_List_Iterator_T
37 : public ACE_Timer_Queue_Iterator_T
<TYPE
, FUNCTOR
, ACE_LOCK
>
40 typedef ACE_Timer_List_T
<TYPE
, FUNCTOR
, ACE_LOCK
> List
;
42 ACE_Timer_List_Iterator_T (List
& lst
);
45 virtual ~ACE_Timer_List_Iterator_T (void);
47 /// Positions the iterator at the earliest node in the Timer Queue
48 virtual void first (void);
50 /// Positions the iterator at the next node in the Timer Queue
51 virtual void next (void);
53 /// Returns true when there are no more nodes in the sequence
54 virtual bool isdone (void) const;
56 /// Returns the node at the current position in the sequence
57 virtual ACE_Timer_Node_T
<TYPE
> *item (void);
60 /// Pointer to the ACE_Timer_List that we are iterating over.
63 /// Current position in the ACE_Timer_List
64 ACE_Timer_Node_T
<TYPE
>* current_node_
;
68 * @class ACE_Timer_List_T
70 * @brief Provides a simple implementation of timers.
72 * This implementation uses a linked list of absolute times.
73 * Therefore, in the average case, scheduling and canceling
74 * timers is O(N) (where N is the total number of timers) and
75 * expiring timers is O(K) (where K is the total number of timers
76 * that are < the current time of day).
77 * More clever implementations could use a delta-list, a heap,
78 * or timing wheels, etc. For instance, ACE_Timer_Heap
79 * is a subclass of ACE_Timer_List that implements a
80 * heap-based callout queue. For most applications, the
81 * ACE_Timer_Heap will perform substantially faster than the
84 template <class TYPE
, class FUNCTOR
, class ACE_LOCK
>
85 class ACE_Timer_List_T
: public ACE_Timer_Queue_T
<TYPE
, FUNCTOR
, ACE_LOCK
>
89 typedef ACE_Timer_List_Iterator_T
<TYPE
, FUNCTOR
, ACE_LOCK
> Iterator
;
91 /// Iterator is a friend
92 friend class ACE_Timer_List_Iterator_T
<TYPE
, FUNCTOR
, ACE_LOCK
>;
94 typedef ACE_Timer_Node_T
<TYPE
> Node
;
95 /// Type inherited from
96 typedef ACE_Timer_Queue_T
<TYPE
, FUNCTOR
, ACE_LOCK
> Base
;
97 typedef ACE_Free_List
<Node
> FreeList
;
99 // = Initialization and termination methods.
101 * Default constructor. @a upcall_functor is the instance of the
102 * FUNCTOR to be used by the list. If @a upcall_functor is 0, a
103 * default FUNCTOR will be created. @a freelist is the freelist of
104 * timer nodes. If 0, then a default freelist will be created.
106 ACE_Timer_List_T (FUNCTOR
* upcall_functor
= 0, FreeList
* freelist
= 0);
109 virtual ~ACE_Timer_List_T (void);
111 /// True if queue is empty, else false.
112 virtual bool is_empty (void) const;
114 /// Returns the time of the earlier node in the ACE_Timer_List.
115 /// Must be called on a non-empty queue.
116 virtual const ACE_Time_Value
& earliest_time (void) const;
119 * Resets the interval of the timer represented by @a timer_id to
120 * @a interval, which is specified in relative time to the current
121 * <gettimeofday>. If @a interval is equal to
122 * ACE_Time_Value::zero, the timer will become a non-rescheduling
123 * timer. Returns 0 if successful, -1 if not.
125 virtual int reset_interval (long timer_id
,
126 const ACE_Time_Value
& interval
);
129 * Cancel all timers associated with @a type. If dont_call_handle_close is 0
130 * then the @a functor will be invoked. Returns the number of timers
133 virtual int cancel (const TYPE
& type
,
134 int dont_call_handle_close
= 1);
137 * Cancel the single timer that matches the @a timer_id value (which
138 * was returned from the <schedule> method). If act is non-NULL
139 * then it will be set to point to the ``magic cookie'' argument
140 * passed in when the timer was registered. This makes it possible
141 * to free up the memory and avoid memory leaks. If <dont_call> is
142 * 0 then the <functor> will be invoked. Returns 1 if cancellation
143 * succeeded and 0 if the @a timer_id wasn't found.
145 virtual int cancel (long timer_id
,
146 const void** act
= 0,
147 int dont_call_handle_close
= 1);
149 /// Returns a pointer to this ACE_Timer_Queue's iterator.
150 virtual ACE_Timer_Queue_Iterator_T
<TYPE
, FUNCTOR
, ACE_LOCK
>& iter (void);
152 /// Removes the earliest node from the queue and returns it
153 virtual ACE_Timer_Node_T
<TYPE
>* remove_first (void);
155 /// Dump the state of an object.
156 virtual void dump (void) const;
158 /// Reschedule an "interval" ACE_Timer_Node_T. This should be private
159 /// but for now it needs to be public for <ACE_Timer_Hash_T>
160 virtual void reschedule (ACE_Timer_Node_T
<TYPE
> *);
162 /// Reads the earliest node from the queue and returns it.
163 virtual ACE_Timer_Node_T
<TYPE
>* get_first (void);
168 * Schedule @a type that will expire at @a future_time, which is
169 * specified in absolute time. If it expires then @a act is passed
170 * in as the value to the <functor>. If @a interval is != to
171 * ACE_Time_Value::zero then it is used to reschedule the @a type
172 * automatically, using relative time to the current <gettimeofday>.
173 * This method returns a <timer_id> that uniquely identifies the the
174 * @a type entry in an internal list. This <timer_id> can be used to
175 * cancel the timer before it expires. The cancellation ensures
176 * that <timer_ids> are unique up to values of greater than 2
177 * billion timers. As long as timers don't stay around longer than
178 * this there should be no problems with accidentally deleting the
179 * wrong timer. Returns -1 on failure (which is guaranteed never to
180 * be a valid <timer_id>).
182 virtual long schedule_i (const TYPE
& type
,
184 const ACE_Time_Value
& future_time
,
185 const ACE_Time_Value
& interval
);
187 void schedule_i(ACE_Timer_Node_T
<TYPE
>* n
, const ACE_Time_Value
& exp
);
189 ACE_Timer_Node_T
<TYPE
>* find_node(long timer_id
) const;
191 void cancel_i (ACE_Timer_Node_T
<TYPE
>* n
);
193 void unlink (ACE_Timer_Node_T
<TYPE
>* n
);
195 ACE_Timer_Node_T
<TYPE
>* get_first_i(void) const;
199 /// Pointer to linked list of <ACE_Timer_Handles>.
200 ACE_Timer_Node_T
<TYPE
>* head_
;
202 /// Iterator used to expire timers.
206 * Keeps track of the timer id that uniquely identifies each timer.
207 * This id can be used to cancel a timer via the <cancel(long)>
212 // = Don't allow these operations for now.
213 ACE_UNIMPLEMENTED_FUNC (ACE_Timer_List_T (const ACE_Timer_List_T
<TYPE
, FUNCTOR
, ACE_LOCK
> &))
214 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Timer_List_T
<TYPE
, FUNCTOR
, ACE_LOCK
> &))
217 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
218 #include "ace/Timer_List_T.cpp"
219 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
221 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
222 #pragma implementation ("Timer_List_T.cpp")
223 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
225 #include /**/ "ace/post.h"
226 #endif /* ACE_TIMER_LIST_T_H */