~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to ace/Timer_Heap_T.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Timer_Heap_T.cpp,v 4.70 2003/11/05 23:30:47 shuston Exp
2
 
 
3
 
#ifndef ACE_TIMER_HEAP_T_C
4
 
#define ACE_TIMER_HEAP_T_C
5
 
 
6
 
#include "ace/Timer_Heap_T.h"
7
 
#include "ace/Log_Msg.h"
8
 
#include "ace/Guard_T.h"
9
 
#include "ace/OS_NS_string.h"
10
 
 
11
 
#if !defined (ACE_LACKS_PRAGMA_ONCE)
12
 
# pragma once
13
 
#endif /* ACE_LACKS_PRAGMA_ONCE */
14
 
 
15
 
ACE_RCSID(ace, Timer_Heap_T, "Timer_Heap_T.cpp,v 4.70 2003/11/05 23:30:47 shuston Exp")
16
 
 
17
 
// Define some simple macros to clarify the code.
18
 
#define ACE_HEAP_PARENT(X) (X == 0 ? 0 : (((X) - 1) / 2))
19
 
#define ACE_HEAP_LCHILD(X) (((X)+(X))+1)
20
 
 
21
 
// Constructor that takes in an <ACE_Timer_Heap_T> to iterate over.
22
 
 
23
 
template <class TYPE, class FUNCTOR, class ACE_LOCK>
24
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Heap_Iterator_T (ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK> &heap)
25
 
  : timer_heap_ (heap)
26
 
{
27
 
  ACE_TRACE ("ACE_Timer_Heap_Iterator_T::ACE_Timer_Heap_Iterator");
28
 
  this->first();
29
 
}
30
 
 
31
 
template <class TYPE, class FUNCTOR, class ACE_LOCK>
32
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Heap_Iterator_T (void)
33
 
{
34
 
}
35
 
 
36
 
// Positions the iterator at the first node in the heap array
37
 
 
38
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
39
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::first (void)
40
 
{
41
 
  this->position_ = 0;
42
 
}
43
 
 
44
 
// Positions the iterator at the next node in the heap array
45
 
 
46
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
47
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::next (void)
48
 
{
49
 
  if (this->position_ != this->timer_heap_.cur_size_)
50
 
    this->position_++;
51
 
}
52
 
 
53
 
// Returns true the <position_> is at the end of the heap array
54
 
 
55
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
56
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::isdone (void) const
57
 
{
58
 
  return this->position_ == this->timer_heap_.cur_size_;
59
 
}
60
 
 
61
 
// Returns the node at the current position in the heap or 0 if at the end
62
 
 
63
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> *
64
 
ACE_Timer_Heap_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::item (void)
65
 
{
66
 
  if (this->position_ != this->timer_heap_.cur_size_)
67
 
    return this->timer_heap_.heap_[this->position_];
68
 
  return 0;
69
 
}
70
 
 
71
 
// Constructor
72
 
// Note that timer_ids_curr_ and timer_ids_min_free_ both start at 0.
73
 
// Since timer IDs are assigned by first incrementing the timer_ids_curr_
74
 
// value, the first ID assigned will be 1 (just as in the previous design).
75
 
// When it's time to wrap, the next ID given out will be 0.
76
 
template <class TYPE, class FUNCTOR, class ACE_LOCK>
77
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Heap_T (size_t size,
78
 
                                                             int preallocate,
79
 
                                                             FUNCTOR *upcall_functor,
80
 
                                                             ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist)
81
 
  : ACE_Timer_Queue_T<TYPE,FUNCTOR,ACE_LOCK> (upcall_functor, freelist),
82
 
    max_size_ (size),
83
 
    cur_size_ (0),
84
 
    cur_limbo_ (0),
85
 
    timer_ids_curr_ (0),
86
 
    timer_ids_min_free_ (0),
87
 
    preallocated_nodes_ (0),
88
 
    preallocated_nodes_freelist_ (0)
89
 
{
90
 
  ACE_TRACE ("ACE_Timer_Heap_T::ACE_Timer_Heap_T");
91
 
 
92
 
  // Create the heap array.
93
 
  ACE_NEW (this->heap_,
94
 
           ACE_Timer_Node_T<TYPE> *[size]);
95
 
 
96
 
  // Create the parallel
97
 
  ACE_NEW (this->timer_ids_,
98
 
           ssize_t[size]);
99
 
 
100
 
  // Initialize the "freelist," which uses negative values to
101
 
  // distinguish freelist elements from "pointers" into the <heap_>
102
 
  // array.
103
 
  for (size_t i = 0; i < size; i++)
104
 
    this->timer_ids_[i] = -1;
105
 
 
106
 
  if (preallocate)
107
 
    {
108
 
      ACE_NEW (this->preallocated_nodes_,
109
 
               ACE_Timer_Node_T<TYPE>[size]);
110
 
 
111
 
      // Add allocated array to set of such arrays for deletion on
112
 
      // cleanup.
113
 
      this->preallocated_node_set_.insert (this->preallocated_nodes_);
114
 
 
115
 
      // Form the freelist by linking the next_ pointers together.
116
 
      for (size_t j = 1; j < size; j++)
117
 
        this->preallocated_nodes_[j - 1].set_next (&this->preallocated_nodes_[j]);
118
 
 
119
 
      // NULL-terminate the freelist.
120
 
      this->preallocated_nodes_[size - 1].set_next (0);
121
 
 
122
 
      // Assign the freelist pointer to the front of the list.
123
 
      this->preallocated_nodes_freelist_ =
124
 
        &this->preallocated_nodes_[0];
125
 
    }
126
 
 
127
 
  ACE_NEW (iterator_,
128
 
           HEAP_ITERATOR (*this));
129
 
}
130
 
 
131
 
// Note that timer_ids_curr_ and timer_ids_min_free_ both start at 0.
132
 
// Since timer IDs are assigned by first incrementing the timer_ids_curr_
133
 
// value, the first ID assigned will be 1 (just as in the previous design).
134
 
// When it's time to wrap, the next ID given out will be 0.
135
 
template <class TYPE, class FUNCTOR, class ACE_LOCK>
136
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Heap_T (FUNCTOR *upcall_functor,
137
 
                                                             ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist)
138
 
  : ACE_Timer_Queue_T<TYPE,FUNCTOR,ACE_LOCK> (upcall_functor, freelist),
139
 
    max_size_ (ACE_DEFAULT_TIMERS),
140
 
    cur_size_ (0),
141
 
    cur_limbo_ (0),
142
 
    timer_ids_curr_ (0),
143
 
    timer_ids_min_free_ (0),
144
 
    preallocated_nodes_ (0),
145
 
    preallocated_nodes_freelist_ (0)
146
 
{
147
 
  ACE_TRACE ("ACE_Timer_Heap_T::ACE_Timer_Heap_T");
148
 
 
149
 
  // Create the heap array.
150
 
#if defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS)
151
 
    ACE_NEW (this->heap_,
152
 
             ACE_Timer_Node_T<TYPE> *[ACE_DEFAULT_TIMERS]);
153
 
#else
154
 
    ACE_NEW (this->heap_,
155
 
             ACE_Timer_Node_T<TYPE> *[this->max_size_]);
156
 
#endif /* defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS) */
157
 
 
158
 
  // Create the parallel array.
159
 
  ACE_NEW (this->timer_ids_,
160
 
           ssize_t[this->max_size_]);
161
 
 
162
 
  // Initialize the "freelist," which uses negative values to
163
 
  // distinguish freelist elements from "pointers" into the <heap_>
164
 
  // array.
165
 
  for (size_t i = 0; i < this->max_size_; i++)
166
 
    this->timer_ids_[i] = -1;
167
 
 
168
 
  ACE_NEW (iterator_,
169
 
           HEAP_ITERATOR (*this));
170
 
}
171
 
 
172
 
template <class TYPE, class FUNCTOR, class ACE_LOCK>
173
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Heap_T (void)
174
 
{
175
 
  ACE_TRACE ("ACE_Timer_Heap_T::~ACE_Timer_Heap_T");
176
 
 
177
 
  delete iterator_;
178
 
 
179
 
  size_t current_size =
180
 
    this->cur_size_;
181
 
 
182
 
  // Clean up all the nodes still in the queue
183
 
  for (size_t i = 0; i < current_size; i++)
184
 
    {
185
 
      this->upcall_functor ().deletion (*this,
186
 
                                        this->heap_[i]->get_type (),
187
 
                                        this->heap_[i]->get_act ());
188
 
      this->free_node (this->heap_[i]);
189
 
    }
190
 
 
191
 
  delete [] this->heap_;
192
 
  delete [] this->timer_ids_;
193
 
 
194
 
  // clean up any preallocated timer nodes
195
 
  if (preallocated_nodes_ != 0)
196
 
    {
197
 
      ACE_Unbounded_Set_Iterator<ACE_Timer_Node_T<TYPE> *>
198
 
        set_iterator (this->preallocated_node_set_);
199
 
 
200
 
      for (ACE_Timer_Node_T<TYPE> **entry = 0;
201
 
           set_iterator.next (entry) !=0;
202
 
           set_iterator.advance ())
203
 
        delete [] *entry;
204
 
    }
205
 
}
206
 
 
207
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
208
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::pop_freelist (void)
209
 
{
210
 
  ACE_TRACE ("ACE_Timer_Heap_T::pop_freelist");
211
 
 
212
 
  // Scan for a free timer ID. Note that since this function is called
213
 
  // _after_ the check for a full timer heap, we are guaranteed to find
214
 
  // a free ID, even if we need to wrap around and start reusing freed IDs.
215
 
  // On entry, the curr_ index is at the previous ID given out; start
216
 
  // up where we left off last time.
217
 
  // NOTE - a timer_ids_ slot with -2 is out of the heap, but not freed.
218
 
  // It must be either freed (free_node) or rescheduled (reschedule).
219
 
  ++this->timer_ids_curr_;
220
 
  while (this->timer_ids_curr_ < this->max_size_ &&
221
 
         (this->timer_ids_[this->timer_ids_curr_] >= 0 ||
222
 
          this->timer_ids_[this->timer_ids_curr_] == -2  ))
223
 
    ++this->timer_ids_curr_;
224
 
  if (this->timer_ids_curr_ == this->max_size_)
225
 
    {
226
 
      ACE_ASSERT (this->timer_ids_min_free_ < this->max_size_);
227
 
      this->timer_ids_curr_ = this->timer_ids_min_free_;
228
 
      // We restarted the free search at min. Since min won't be
229
 
      // free anymore, and curr_ will just keep marching up the list
230
 
      // on each successive need for an ID, reset min_free_ to the
231
 
      // size of the list until an ID is freed that curr_ has already
232
 
      // gone past (see push_freelist).
233
 
      this->timer_ids_min_free_ = this->max_size_;
234
 
    }
235
 
 
236
 
  // We need to truncate this to <int> for backwards compatibility.
237
 
  int new_id = ACE_static_cast (int,
238
 
                                this->timer_ids_curr_);
239
 
  return new_id;
240
 
}
241
 
 
242
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
243
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::push_freelist (int old_id)
244
 
{
245
 
  ACE_TRACE ("ACE_Timer_Heap_T::push_freelist");
246
 
 
247
 
  // Since this ID has already been checked by one of the public
248
 
  // functions, it's safe to cast it here.
249
 
  size_t oldid = size_t (old_id);
250
 
 
251
 
  // The freelist values in the <timer_ids_> are negative, so set the
252
 
  // freed entry back to 'free'. If this is the new lowest value free
253
 
  // timer ID that curr_ won't see on it's normal march through the list,
254
 
  // remember it.
255
 
  ACE_ASSERT (this->timer_ids_[oldid] >= 0 || this->timer_ids_[oldid] == -2);
256
 
  if (this->timer_ids_[oldid] == -2)
257
 
    --this->cur_limbo_;
258
 
  else
259
 
    --this->cur_size_;
260
 
  this->timer_ids_[oldid] = -1;
261
 
  if (oldid < this->timer_ids_min_free_ && oldid <= this->timer_ids_curr_)
262
 
    this->timer_ids_min_free_ = oldid;
263
 
  return;
264
 
}
265
 
 
266
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
267
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::timer_id (void)
268
 
{
269
 
  ACE_TRACE ("ACE_Timer_Heap_T::timer_id");
270
 
 
271
 
  // Return the next item off the freelist and use it as the timer id.
272
 
  return this->pop_freelist ();
273
 
}
274
 
 
275
 
// Checks if queue is empty.
276
 
 
277
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
278
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::is_empty (void) const
279
 
{
280
 
  ACE_TRACE ("ACE_Timer_Heap_T::is_empty");
281
 
  return this->cur_size_ == 0;
282
 
}
283
 
 
284
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> &
285
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::iter (void)
286
 
{
287
 
  this->iterator_->first ();
288
 
  return *this->iterator_;
289
 
}
290
 
 
291
 
// Returns earliest time in a non-empty queue.
292
 
 
293
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> const ACE_Time_Value &
294
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::earliest_time (void) const
295
 
{
296
 
  ACE_TRACE ("ACE_Timer_Heap_T::earliest_time");
297
 
  return this->heap_[0]->get_timer_value ();
298
 
}
299
 
 
300
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
301
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::dump (void) const
302
 
{
303
 
#if defined (ACE_HAS_DUMP)
304
 
  ACE_TRACE ("ACE_Timer_Heap_T::dump");
305
 
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
306
 
 
307
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nmax_size_ = %d"), this->max_size_));
308
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncur_size_ = %d"), this->cur_size_));
309
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncur_limbo_= %d"), this->cur_limbo_));
310
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nids_curr_ = %d"),
311
 
              this->timer_ids_curr_));
312
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nmin_free_ = %d"),
313
 
              this->timer_ids_min_free_));
314
 
 
315
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nheap_ = \n")));
316
 
 
317
 
  for (size_t i = 0; i < this->cur_size_; i++)
318
 
    {
319
 
      ACE_DEBUG ((LM_DEBUG,
320
 
                  ACE_LIB_TEXT ("%d\n"),
321
 
                  i));
322
 
      this->heap_[i]->dump ();
323
 
    }
324
 
 
325
 
  ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntimer_ids_ = \n")));
326
 
 
327
 
  for (size_t j = 0; j < this->max_size_; j++)
328
 
    ACE_DEBUG ((LM_DEBUG,
329
 
                ACE_LIB_TEXT ("%d\t%d\n"),
330
 
                j,
331
 
                this->timer_ids_[j]));
332
 
 
333
 
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
334
 
#endif /* ACE_HAS_DUMP */
335
 
}
336
 
 
337
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
338
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::copy (size_t slot,
339
 
                                                 ACE_Timer_Node_T<TYPE> *moved_node)
340
 
{
341
 
  // Insert <moved_node> into its new location in the heap.
342
 
  this->heap_[slot] = moved_node;
343
 
 
344
 
  ACE_ASSERT (moved_node->get_timer_id () >= 0
345
 
              && moved_node->get_timer_id () < (int) this->max_size_);
346
 
 
347
 
  // Update the corresponding slot in the parallel <timer_ids_> array.
348
 
  this->timer_ids_[moved_node->get_timer_id ()] = slot;
349
 
}
350
 
 
351
 
// Remove the slot'th timer node from the heap, but do not reclaim its
352
 
// timer ID or change the size of this timer heap object. The caller of
353
 
// this function must call either free_node (to reclaim the timer ID
354
 
// and the timer node memory, as well as decrement the size of the queue)
355
 
// or reschedule (to reinsert the node in the heap at a new time).
356
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> *
357
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::remove (size_t slot)
358
 
{
359
 
  ACE_Timer_Node_T<TYPE> *removed_node =
360
 
    this->heap_[slot];
361
 
 
362
 
  // NOTE - the cur_size_ is being decremented since the queue has one
363
 
  // less active timer in it. However, this ACE_Timer_Node is not being
364
 
  // freed, and there is still a place for it in timer_ids_ (the timer ID
365
 
  // is not being relinquished). The node can still be rescheduled, or
366
 
  // it can be freed via free_node.
367
 
  --this->cur_size_;
368
 
 
369
 
  // Only try to reheapify if we're not deleting the last entry.
370
 
 
371
 
  if (slot < this->cur_size_)
372
 
    {
373
 
      ACE_Timer_Node_T<TYPE> *moved_node =
374
 
        this->heap_[this->cur_size_];
375
 
 
376
 
      // Move the end node to the location being removed and update
377
 
      // the corresponding slot in the parallel <timer_ids> array.
378
 
      this->copy (slot, moved_node);
379
 
 
380
 
      // If the <moved_node->time_value_> is great than or equal its
381
 
      // parent it needs be moved down the heap.
382
 
      size_t parent = ACE_HEAP_PARENT (slot);
383
 
 
384
 
      if (moved_node->get_timer_value ()
385
 
          >= this->heap_[parent]->get_timer_value ())
386
 
        this->reheap_down (moved_node,
387
 
                           slot,
388
 
                           ACE_HEAP_LCHILD (slot));
389
 
      else
390
 
        this->reheap_up (moved_node,
391
 
                         slot,
392
 
                         parent);
393
 
    }
394
 
 
395
 
  this->timer_ids_[removed_node->get_timer_id ()] = -2;
396
 
  ++this->cur_limbo_;
397
 
  return removed_node;
398
 
}
399
 
 
400
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
401
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::reheap_down (ACE_Timer_Node_T<TYPE> *moved_node,
402
 
                                                    size_t slot,
403
 
                                                    size_t child)
404
 
{
405
 
  // Restore the heap property after a deletion.
406
 
 
407
 
  while (child < this->cur_size_)
408
 
    {
409
 
      // Choose the smaller of the two children.
410
 
      if (child + 1 < this->cur_size_
411
 
          && this->heap_[child + 1]->get_timer_value ()
412
 
          < this->heap_[child]->get_timer_value ())
413
 
        child++;
414
 
 
415
 
      // Perform a <copy> if the child has a larger timeout value than
416
 
      // the <moved_node>.
417
 
      if (this->heap_[child]->get_timer_value ()
418
 
          < moved_node->get_timer_value ())
419
 
        {
420
 
          this->copy (slot,
421
 
                      this->heap_[child]);
422
 
          slot = child;
423
 
          child = ACE_HEAP_LCHILD (child);
424
 
        }
425
 
      else
426
 
        // We've found our location in the heap.
427
 
        break;
428
 
    }
429
 
 
430
 
  this->copy (slot, moved_node);
431
 
}
432
 
 
433
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
434
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::reheap_up (ACE_Timer_Node_T<TYPE> *moved_node,
435
 
                                                      size_t slot,
436
 
                                                      size_t parent)
437
 
{
438
 
  // Restore the heap property after an insertion.
439
 
 
440
 
  while (slot > 0)
441
 
    {
442
 
      // If the parent node is greater than the <moved_node> we need
443
 
      // to copy it down.
444
 
      if (moved_node->get_timer_value ()
445
 
          < this->heap_[parent]->get_timer_value ())
446
 
        {
447
 
          this->copy (slot, this->heap_[parent]);
448
 
          slot = parent;
449
 
          parent = ACE_HEAP_PARENT (slot);
450
 
        }
451
 
      else
452
 
        break;
453
 
    }
454
 
 
455
 
  // Insert the new node into its proper resting place in the heap and
456
 
  // update the corresponding slot in the parallel <timer_ids> array.
457
 
  this->copy (slot,
458
 
              moved_node);
459
 
}
460
 
 
461
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
462
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::insert (ACE_Timer_Node_T<TYPE> *new_node)
463
 
{
464
 
  if (this->cur_size_ + this->cur_limbo_ + 2 >= this->max_size_)
465
 
    this->grow_heap ();
466
 
 
467
 
  this->reheap_up (new_node,
468
 
                   this->cur_size_,
469
 
                   ACE_HEAP_PARENT (this->cur_size_));
470
 
  this->cur_size_++;
471
 
}
472
 
 
473
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
474
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::grow_heap (void)
475
 
{
476
 
  // All the containers will double in size from max_size_
477
 
  size_t new_size = this->max_size_ * 2;
478
 
 
479
 
   // First grow the heap itself.
480
 
 
481
 
  ACE_Timer_Node_T<TYPE> **new_heap = 0;
482
 
 
483
 
#if defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS)
484
 
  ACE_NEW (new_heap,
485
 
           ACE_Timer_Node_T<TYPE> *[1024]);
486
 
#else
487
 
  ACE_NEW (new_heap,
488
 
           ACE_Timer_Node_T<TYPE> *[new_size]);
489
 
#endif /* defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS) */
490
 
  ACE_OS::memcpy (new_heap,
491
 
                  this->heap_,
492
 
                  this->max_size_ * sizeof *new_heap);
493
 
  delete [] this->heap_;
494
 
  this->heap_ = new_heap;
495
 
 
496
 
  // Grow the array of timer ids.
497
 
 
498
 
  ssize_t *new_timer_ids = 0;
499
 
 
500
 
  ACE_NEW (new_timer_ids,
501
 
           ssize_t[new_size]);
502
 
 
503
 
  ACE_OS::memcpy (new_timer_ids,
504
 
                  this->timer_ids_,
505
 
                  this->max_size_ * sizeof (ssize_t));
506
 
 
507
 
  delete [] timer_ids_;
508
 
  this->timer_ids_ = new_timer_ids;
509
 
 
510
 
  // And add the new elements to the end of the "freelist".
511
 
  for (size_t i = this->max_size_; i < new_size; i++)
512
 
    this->timer_ids_[i] = -(ACE_static_cast (ssize_t, i) + 1);
513
 
 
514
 
   // Grow the preallocation array (if using preallocation)
515
 
  if (this->preallocated_nodes_ != 0)
516
 
    {
517
 
      // Create a new array with max_size elements to link in to
518
 
      // existing list.
519
 
#if defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS)
520
 
      ACE_NEW (this->preallocated_nodes_,
521
 
               ACE_Timer_Node_T<TYPE>[88]);
522
 
#else
523
 
      ACE_NEW (this->preallocated_nodes_,
524
 
               ACE_Timer_Node_T<TYPE>[this->max_size_]);
525
 
#endif /* defined (__IBMCPP__) && (__IBMCPP__ >= 400) && defined (_WINDOWS) */
526
 
 
527
 
      // Add it to the set for later deletion
528
 
      this->preallocated_node_set_.insert (this->preallocated_nodes_);
529
 
 
530
 
      // Link new nodes together (as for original list).
531
 
      for (size_t k = 1; k < this->max_size_; k++)
532
 
        this->preallocated_nodes_[k - 1].set_next (&this->preallocated_nodes_[k]);
533
 
 
534
 
      // NULL-terminate the new list.
535
 
      this->preallocated_nodes_[this->max_size_ - 1].set_next (0);
536
 
 
537
 
      // Link new array to the end of the existling list.
538
 
      if (this->preallocated_nodes_freelist_ == 0)
539
 
        this->preallocated_nodes_freelist_ =
540
 
          &preallocated_nodes_[0];
541
 
      else
542
 
        {
543
 
          ACE_Timer_Node_T<TYPE> *previous =
544
 
            this->preallocated_nodes_freelist_;
545
 
 
546
 
          for (ACE_Timer_Node_T<TYPE> *current = this->preallocated_nodes_freelist_->get_next ();
547
 
               current != 0;
548
 
               current = current->get_next ())
549
 
            previous = current;
550
 
 
551
 
          previous->set_next (&this->preallocated_nodes_[0]);
552
 
        }
553
 
    }
554
 
 
555
 
  this->max_size_ = new_size;
556
 
}
557
 
 
558
 
// Reschedule a periodic timer.  This function must be called with the
559
 
// mutex lock held.
560
 
 
561
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
562
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::reschedule (ACE_Timer_Node_T<TYPE> *expired)
563
 
{
564
 
  ACE_TRACE ("ACE_Timer_Heap_T::reschedule");
565
 
 
566
 
  // If we are rescheduling, then the most recent call was to
567
 
  // remove_first (). That called remove () to remove the node from the
568
 
  // heap, but did not free the timer ID. The ACE_Timer_Node still has
569
 
  // its assigned ID - just needs to be inserted at the new proper
570
 
  // place, and the heap restored properly.
571
 
  if (this->timer_ids_[expired->get_timer_id ()] == -2)
572
 
    --this->cur_limbo_;
573
 
  this->insert (expired);
574
 
}
575
 
 
576
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> *
577
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::alloc_node (void)
578
 
{
579
 
  ACE_Timer_Node_T<TYPE> *temp = 0;
580
 
 
581
 
  // Only allocate a node if we are *not* using the preallocated heap.
582
 
  if (this->preallocated_nodes_ == 0)
583
 
    ACE_NEW_RETURN (temp,
584
 
                    ACE_Timer_Node_T<TYPE>,
585
 
                    0);
586
 
  else
587
 
    {
588
 
      // check to see if the heap needs to grow
589
 
      if (this->preallocated_nodes_freelist_ == 0)
590
 
        this->grow_heap ();
591
 
 
592
 
      temp = this->preallocated_nodes_freelist_;
593
 
 
594
 
      // Remove the first element from the freelist.
595
 
      this->preallocated_nodes_freelist_ =
596
 
        this->preallocated_nodes_freelist_->get_next ();
597
 
    }
598
 
  return temp;
599
 
}
600
 
 
601
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> void
602
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::free_node (ACE_Timer_Node_T<TYPE> *node)
603
 
{
604
 
  // Return this timer id to the freelist.
605
 
  this->push_freelist (node->get_timer_id ());
606
 
 
607
 
  // Only free up a node if we are *not* using the preallocated heap.
608
 
  if (this->preallocated_nodes_ == 0)
609
 
    delete node;
610
 
  else
611
 
    {
612
 
      node->set_next (this->preallocated_nodes_freelist_);
613
 
      this->preallocated_nodes_freelist_ = node;
614
 
    }
615
 
}
616
 
 
617
 
// Insert a new timer that expires at time future_time; if interval is
618
 
// > 0, the handler will be reinvoked periodically.
619
 
 
620
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> long
621
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::schedule_i (const TYPE &type,
622
 
                                                       const void *act,
623
 
                                                       const ACE_Time_Value &future_time,
624
 
                                                       const ACE_Time_Value &interval)
625
 
{
626
 
  ACE_TRACE ("ACE_Timer_Heap_T::schedule_i");
627
 
 
628
 
  if ((this->cur_size_ + this->cur_limbo_) < this->max_size_)
629
 
    {
630
 
      // Obtain the next unique sequence number.
631
 
      int timer_id = this->timer_id ();
632
 
 
633
 
      // Obtain the memory to the new node.
634
 
      ACE_Timer_Node_T<TYPE> *temp = 0;
635
 
 
636
 
      ACE_ALLOCATOR_RETURN (temp,
637
 
                            this->alloc_node (),
638
 
                            -1);
639
 
      temp->set (type,
640
 
                 act,
641
 
                 future_time,
642
 
                 interval,
643
 
                 0,
644
 
                 timer_id);
645
 
 
646
 
      this->insert (temp);
647
 
      return timer_id;
648
 
    }
649
 
  else
650
 
    return -1;
651
 
}
652
 
 
653
 
// Locate and remove the single timer with a value of <timer_id> from
654
 
// the timer queue.
655
 
 
656
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
657
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (long timer_id,
658
 
                                                   const void **act,
659
 
                                                   int dont_call)
660
 
{
661
 
  ACE_TRACE ("ACE_Timer_Heap_T::cancel");
662
 
  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
663
 
 
664
 
  // Locate the ACE_Timer_Node that corresponds to the timer_id.
665
 
 
666
 
  // Check to see if the timer_id is out of range
667
 
  if (timer_id < 0
668
 
      || (size_t) timer_id > this->max_size_)
669
 
    return 0;
670
 
 
671
 
  ssize_t timer_node_slot = this->timer_ids_[timer_id];
672
 
 
673
 
  // Check to see if timer_id is still valid.
674
 
  if (timer_node_slot < 0)
675
 
    return 0;
676
 
 
677
 
  if (timer_id != this->heap_[timer_node_slot]->get_timer_id ())
678
 
    {
679
 
      ACE_ASSERT (timer_id == this->heap_[timer_node_slot]->get_timer_id ());
680
 
      return 0;
681
 
    }
682
 
  else
683
 
    {
684
 
      ACE_Timer_Node_T<TYPE> *temp =
685
 
        this->remove (timer_node_slot);
686
 
 
687
 
      // Call the close hooks.
688
 
      int cookie = 0;
689
 
 
690
 
      // cancel_type() called once per <type>.
691
 
      this->upcall_functor ().cancel_type (*this,
692
 
                                           temp->get_type (),
693
 
                                           dont_call,
694
 
                                           cookie);
695
 
 
696
 
      // cancel_timer() called once per <timer>.
697
 
      this->upcall_functor ().cancel_timer (*this,
698
 
                                            temp->get_type (),
699
 
                                            dont_call,
700
 
                                            cookie);
701
 
 
702
 
      if (act != 0)
703
 
        *act = temp->get_act ();
704
 
 
705
 
      this->free_node (temp);
706
 
      return 1;
707
 
    }
708
 
}
709
 
 
710
 
// Locate and update the inteval on the timer_id
711
 
 
712
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
713
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::reset_interval (long timer_id,
714
 
                                                           const ACE_Time_Value &interval)
715
 
{
716
 
  ACE_TRACE ("ACE_Timer_Heap_T::reset_interval");
717
 
  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
718
 
 
719
 
  // Locate the ACE_Timer_Node that corresponds to the timer_id.
720
 
 
721
 
  // Check to see if the timer_id is out of range
722
 
  if (timer_id < 0
723
 
      || (size_t) timer_id > this->max_size_)
724
 
    return -1;
725
 
 
726
 
  ssize_t timer_node_slot = this->timer_ids_[timer_id];
727
 
 
728
 
  // Check to see if timer_id is still valid.
729
 
  if (timer_node_slot < 0)
730
 
    return -1;
731
 
 
732
 
  if (timer_id != this->heap_[timer_node_slot]->get_timer_id ())
733
 
    {
734
 
      ACE_ASSERT (timer_id == this->heap_[timer_node_slot]->get_timer_id ());
735
 
      return -1;
736
 
    }
737
 
  else
738
 
    {
739
 
      // Reset the timer interval
740
 
      this->heap_[timer_node_slot]->set_interval (interval);
741
 
      return 0;
742
 
    }
743
 
}
744
 
 
745
 
// Locate and remove all values of <type> from the timer queue.
746
 
 
747
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> int
748
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (const TYPE &type,
749
 
                                                   int dont_call)
750
 
{
751
 
  ACE_TRACE ("ACE_Timer_Heap_T::cancel");
752
 
  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
753
 
 
754
 
  int number_of_cancellations = 0;
755
 
 
756
 
  // Try to locate the ACE_Timer_Node that matches the timer_id.
757
 
 
758
 
  for (size_t i = 0; i < this->cur_size_; )
759
 
    {
760
 
      if (this->heap_[i]->get_type () == type)
761
 
        {
762
 
          ACE_Timer_Node_T<TYPE> *temp = this->remove (i);
763
 
 
764
 
          number_of_cancellations++;
765
 
 
766
 
          this->free_node (temp);
767
 
        }
768
 
      else
769
 
        i++;
770
 
    }
771
 
 
772
 
  // Call the close hooks.
773
 
  int cookie = 0;
774
 
 
775
 
  // cancel_type() called once per <type>.
776
 
  this->upcall_functor ().cancel_type (*this,
777
 
                                       type,
778
 
                                       dont_call,
779
 
                                       cookie);
780
 
 
781
 
  for (int j = 0;
782
 
       j < number_of_cancellations;
783
 
       ++j)
784
 
    {
785
 
      // cancel_timer() called once per <timer>.
786
 
      this->upcall_functor ().cancel_timer (*this,
787
 
                                            type,
788
 
                                            dont_call,
789
 
                                            cookie);
790
 
    }
791
 
 
792
 
  return number_of_cancellations;
793
 
}
794
 
 
795
 
// Returns the earliest node or returns 0 if the heap is empty.
796
 
 
797
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T <TYPE> *
798
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::remove_first (void)
799
 
{
800
 
  ACE_TRACE ("ACE_Timer_Heap_T::remove_first");
801
 
 
802
 
  if (this->cur_size_ == 0)
803
 
    return 0;
804
 
 
805
 
  return this->remove (0);
806
 
}
807
 
 
808
 
template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T <TYPE> *
809
 
ACE_Timer_Heap_T<TYPE, FUNCTOR, ACE_LOCK>::get_first (void)
810
 
{
811
 
  ACE_TRACE ("ACE_Timer_Heap_T::get_first");
812
 
 
813
 
  return this->cur_size_ == 0 ? 0 : this->heap_[0];
814
 
}
815
 
 
816
 
#endif /* ACE_TIMER_HEAP_T_C */