Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Timer_Heap_T.h
blobedde952efcd46dc05f50472b683be2013093e91d
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Timer_Heap_T.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
9 //=============================================================================
11 #ifndef ACE_TIMER_HEAP_T_H
12 #define ACE_TIMER_HEAP_T_H
13 #include /**/ "ace/pre.h"
15 #include "ace/Timer_Queue_T.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
18 # pragma once
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/Free_List.h"
22 #include "ace/Unbounded_Set.h"
24 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
26 // Forward declaration
27 template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY>
28 class ACE_Timer_Heap_T;
30 /**
31 * @class ACE_Timer_Heap_Iterator_T
33 * @brief Iterates over an ACE_Timer_Heap_T.
35 * This is a generic iterator that can be used to visit every
36 * node of a timer queue. Be aware that it doesn't transverse
37 * in the order of timeout values.
39 template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY = ACE_Default_Time_Policy>
40 class ACE_Timer_Heap_Iterator_T : public ACE_Timer_Queue_Iterator_T<TYPE>
42 public:
43 typedef ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Heap;
44 /// Constructor.
45 ACE_Timer_Heap_Iterator_T (Heap &);
47 /// Destructor.
48 ~ACE_Timer_Heap_Iterator_T () override = default;
50 /// Positions the iterator at the earliest node in the Timer Queue
51 void first () override;
53 /// Positions the iterator at the next node in the Timer Queue
54 void next () override;
56 /// Returns true when there are no more nodes in the sequence
57 bool isdone () const override;
59 /// Returns the node at the current position in the sequence
60 ACE_Timer_Node_T<TYPE> *item () override;
62 /// Declare the dynamic allocation hooks.
63 ACE_ALLOC_HOOK_DECLARE;
65 protected:
66 /// Pointer to the ACE_Timer_Heap that we are iterating over.
67 Heap & timer_heap_;
69 /// Position in the array where the iterator is at
70 size_t position_;
73 /**
74 * @class ACE_Timer_Heap_T
76 * @brief Provides a very fast and predictable timer implementation.
78 * This implementation uses a heap-based callout queue of
79 * absolute times. Therefore, in the average and worst case,
80 * scheduling, canceling, and expiring timers is O(log N) (where
81 * N is the total number of timers). In addition, we can also
82 * preallocate as many @c ACE_Timer_Node objects as there are slots
83 * in the heap. This allows us to completely remove the need for
84 * dynamic memory allocation, which is important for real-time
85 * systems.
87 template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY = ACE_Default_Time_Policy>
88 class ACE_Timer_Heap_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
90 public:
91 typedef ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> HEAP_ITERATOR;
92 friend class ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>;
94 typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Base_Time_Policy;
96 /**
97 * The Constructor creates a heap with specified number of elements.
98 * This can also take in a upcall functor and freelist (if 0, then
99 * defaults will be created).
101 * @param size The maximum number of timers that can be
102 * inserted into the new object.
103 * @param preallocated Default false, true then all the memory
104 * for the @c ACE_Timer_Node objects will be pre-allocated. This saves
105 * time and is more predictable (though it requires more space).
106 * Otherwise, timer nodes are allocated as needed.
107 * @param freelist is the freelist of timer nodes.
108 * @param upcall_functor If 0 Timer Heap will create a default FUNCTOR.
110 ACE_Timer_Heap_T (size_t size,
111 bool preallocated = false,
112 FUNCTOR *upcall_functor = 0,
113 ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist = 0,
114 TIME_POLICY const & time_policy = TIME_POLICY());
117 * Default constructor. @c upcall_functor is the instance of the
118 * FUNCTOR to be used by the queue. If @c upcall_functor is 0, Timer
119 * Heap will create a default FUNCTOR. @c freelist is the freelist of
120 * timer nodes. If 0, then a default freelist will be created. The default
121 * size will be ACE_DEFAULT_TIMERS and there will be no preallocation.
123 ACE_Timer_Heap_T (FUNCTOR *upcall_functor = 0,
124 ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist = 0,
125 TIME_POLICY const & time_policy = TIME_POLICY());
127 /// Destructor.
128 virtual ~ACE_Timer_Heap_T ();
130 /// True if heap is empty, else false.
131 virtual bool is_empty () const;
133 /// Returns the time of the earliest node in the Timer_Queue.
134 /// Must be called on a non-empty queue.
135 virtual const ACE_Time_Value &earliest_time () const;
138 * Resets the interval of the timer represented by @a timer_id to
139 * @a interval, which is specified in relative time to the current
140 * <gettimeofday>. If @a interval is equal to
141 * ACE_Time_Value::zero, the timer will become a non-rescheduling
142 * timer. Returns 0 if successful, -1 if not.
144 virtual int reset_interval (long timer_id,
145 const ACE_Time_Value &interval);
148 * Cancel all timers associated with @a type. If @a dont_call_handle_close
149 * is 0 then the <functor> will be invoked. Returns number of timers
150 * cancelled.
152 virtual int cancel (const TYPE &type,
153 int dont_call_handle_close = 1);
156 * Cancel the single timer that matches the @a timer_id value (which
157 * was returned from the <schedule> method). If act is non-NULL
158 * then it will be set to point to the ``magic cookie'' argument
159 * passed in when the timer was registered. This makes it possible
160 * to free up the memory and avoid memory leaks. If @a dont_call_handle_close
161 * is 0 then the <functor> will be invoked. Returns 1 if cancellation
162 * succeeded and 0 if the @a timer_id wasn't found.
164 virtual int cancel (long timer_id,
165 const void **act = 0,
166 int dont_call_handle_close = 1);
169 * Destroy timer queue. Cancels all timers.
171 virtual int close ();
173 /// Returns a pointer to this ACE_Timer_Queue's iterator.
174 virtual ACE_Timer_Queue_Iterator_T<TYPE> &iter ();
177 * Removes the earliest node from the queue and returns it. Note that
178 * the timer is removed from the heap, but is not freed, and its ID
179 * is not reclaimed. The caller is responsible for calling either
180 * @c reschedule() or @c free_node() after this function returns. Thus,
181 * this function is for support of @c ACE_Timer_Queue::expire and
182 * should not be used unadvisedly in other conditions.
184 ACE_Timer_Node_T <TYPE> *remove_first ();
186 /// Dump the state of an object.
187 virtual void dump () const;
189 /// Reads the earliest node from the queue and returns it.
190 virtual ACE_Timer_Node_T<TYPE> *get_first ();
192 /// Declare the dynamic allocation hooks.
193 ACE_ALLOC_HOOK_DECLARE;
195 protected:
197 * Schedule a timer that may optionally auto-reset.
198 * Schedule @a type that will expire at @a future_time,
199 * which is specified in absolute time. If it expires then @a act is
200 * passed in as the value to the <functor>. If @a interval is != to
201 * ACE_Time_Value::zero then it is used to reschedule the @a type
202 * automatically, using relative time to the current <gettimeofday>.
203 * This method returns a <timer_id> that uniquely identifies the the
204 * @a type entry in an internal list. This <timer_id> can be used to
205 * cancel the timer before it expires. The cancellation ensures
206 * that <timer_ids> are unique up to values of greater than 2
207 * billion timers. As long as timers don't stay around longer than
208 * this there should be no problems with accidentally deleting the
209 * wrong timer. Returns -1 on failure (which is guaranteed never to
210 * be a valid <timer_id>).
212 virtual long schedule_i (const TYPE &type,
213 const void *act,
214 const ACE_Time_Value &future_time,
215 const ACE_Time_Value &interval);
217 /// Reschedule an "interval" ACE_Timer_Node.
218 virtual void reschedule (ACE_Timer_Node_T<TYPE> *);
220 /// Factory method that allocates a new node (uses operator new if
221 /// we're *not* preallocating, otherwise uses an internal freelist).
222 virtual ACE_Timer_Node_T<TYPE> *alloc_node ();
225 * Factory method that frees a previously allocated node (uses
226 * operator delete if we're *not* preallocating, otherwise uses an
227 * internal freelist).
229 virtual void free_node (ACE_Timer_Node_T<TYPE> *);
231 private:
232 /// Remove and return the @a sloth ACE_Timer_Node and restore the
233 /// heap property.
234 ACE_Timer_Node_T<TYPE> *remove (size_t slot);
236 /// Insert @a new_node into the heap and restore the heap property.
237 void insert (ACE_Timer_Node_T<TYPE> *new_node);
240 * Doubles the size of the heap and the corresponding timer_ids array.
241 * If preallocation is used, will also double the size of the
242 * preallocated array of ACE_Timer_Nodes.
244 void grow_heap ();
246 /// Restore the heap property, starting at @a slot.
247 void reheap_up (ACE_Timer_Node_T<TYPE> *new_node,
248 size_t slot,
249 size_t parent);
251 /// Restore the heap property, starting at @a slot.
252 void reheap_down (ACE_Timer_Node_T<TYPE> *moved_node,
253 size_t slot,
254 size_t child);
256 /// Copy @a moved_node into the @a slot slot of <heap_> and move
257 /// @a slot into the corresponding slot in the <timer_id_> array.
258 void copy (size_t slot, ACE_Timer_Node_T<TYPE> *moved_node);
261 * Returns a timer id that uniquely identifies this timer. This id
262 * can be used to cancel a timer via the <cancel (int)> method. The
263 * timer id returned from this method will never == -1 to avoid
264 * conflicts with other failure return values.
266 long timer_id ();
268 /// Pops and returns a new timer id from the freelist.
269 long pop_freelist ();
271 /// Pushes @a old_id onto the freelist.
272 void push_freelist (long old_id);
274 /// Maximum size of the heap.
275 size_t max_size_;
277 /// Current size of the heap.
278 size_t cur_size_;
280 /// Number of heap entries in transition (removed from the queue, but
281 /// not freed) and may be rescheduled or freed.
282 size_t cur_limbo_;
284 /// Iterator used to expire timers.
285 HEAP_ITERATOR *iterator_;
288 * Current contents of the Heap, which is organized as a "heap" of
289 * ACE_Timer_Node *'s. In this context, a heap is a "partially
290 * ordered, almost complete" binary tree, which is stored in an
291 * array.
293 ACE_Timer_Node_T<TYPE> **heap_;
296 * An array of "pointers" that allows each ACE_Timer_Node in the
297 * <heap_> to be located in O(1) time. Basically, <timer_id_[i]>
298 * contains the slot in the <heap_> array where an ACE_Timer_Node
299 * * with timer id \<i\> resides. Thus, the timer id passed back from
300 * <schedule> is really a slot into the <timer_ids> array. The
301 * <timer_ids_> array serves two purposes: negative values are
302 * indications of free timer IDs, whereas positive values are
303 * "pointers" into the <heap_> array for assigned timer IDs.
305 ssize_t *timer_ids_;
307 /// "Pointer" to the element in the <timer_ids_> array that was
308 /// last given out as a timer ID.
309 size_t timer_ids_curr_;
311 /// Index representing the lowest timer ID that has been freed. When
312 /// the timer_ids_next_ value wraps around, it starts back at this
313 /// point.
314 size_t timer_ids_min_free_;
317 * If this is non-0, then we preallocate <max_size_> number of
318 * ACE_Timer_Node objects in order to reduce dynamic allocation
319 * costs. In auto-growing implementation, this points to the
320 * last array of nodes allocated.
322 ACE_Timer_Node_T<TYPE> *preallocated_nodes_;
324 /// This points to the head of the <preallocated_nodes_> freelist,
325 /// which is organized as a stack.
326 ACE_Timer_Node_T<TYPE> *preallocated_nodes_freelist_;
328 /// Set of pointers to the arrays of preallocated timer nodes.
329 /// Used to delete the allocated memory when required.
330 ACE_Unbounded_Set<ACE_Timer_Node_T<TYPE> *> preallocated_node_set_;
333 ACE_END_VERSIONED_NAMESPACE_DECL
335 #include "ace/Timer_Heap_T.cpp"
337 #include /**/ "ace/post.h"
338 #endif /* ACE_TIMER_HEAP_T_H */