1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "ace/Timer_Heap.h"
ACE_Timer_Heap_Iterator::ACE_Timer_Heap_Iterator (ACE_Timer_Heap &heap)
: timer_heap_ (heap)
{
}
int
ACE_Timer_Heap_Iterator::next (ACE_Timer_Node *&node,
const ACE_Time_Value &cur_time)
{
if (this->timer_heap_.cur_size_ == 0
|| this->timer_heap_.heap_[0]->timer_value_ > cur_time)
return 0;
else
{
node = this->timer_heap_.heap_[0];
// Transfer the last element in the heap to the front and
// restore the heap property.
this->timer_heap_.heap_[0] =
this->timer_heap_.heap_[--this->timer_heap_.cur_size_];
this->timer_heap_.reheap_down ();
return 1;
}
}
ACE_Timer_Heap::ACE_Timer_Heap (size_t size)
: max_size_ (size),
cur_size_ (0),
iterator_ (*this)
{
ACE_NEW (this->heap_,
ACE_Timer_Node *[size]);
}
ACE_Timer_Heap::~ACE_Timer_Heap (void)
{
delete [] this->heap_;
}
// Checks if queue is empty.
int
ACE_Timer_Heap::is_empty (void) const
{
ACE_TRACE ("ACE_Timer_Heap::is_empty");
return this->cur_size_ == 0;
}
ACE_Timer_Queue_Iterator &
ACE_Timer_Heap::iterator (void)
{
return this->iterator_;
}
// Returns earliest time in a non-empty queue.
const ACE_Time_Value &
ACE_Timer_Heap::earliest_time (void) const
{
ACE_TRACE ("ACE_Timer_Heap::earliest_time");
return this->heap_[0]->timer_value_;
}
void
ACE_Timer_Heap::dump (void) const
{
for (size_t index = 0; index < this->cur_size_; index++)
this->heap_[index]->dump ();
}
void
ACE_Timer_Heap::reheap_down (void)
{
int parent = 0;
int child = 1;
ACE_Timer_Node *temp = this->heap_[parent];
// Restore the heap property.
while (child < this->cur_size_)
{
if (child + 1 < this->cur_size_
&& this->heap_[child + 1]->timer_value_ > this->heap_[child]->timer_value_)
child++;
if (this->heap_[child]->timer_value_ > temp->timer_value_)
{
this->heap_[parent] = this->heap_[child];
parent = child;
// Multiple child by 2 and add 1.
child += child + 1;
}
else
break;
}
this->heap_[parent] = temp;
}
void
ACE_Timer_Heap::reheap_up (void)
{
int parent;
int child = this->cur_size_ - 1;
ACE_Timer_Node *temp = this->heap_[child];
// Restore the heap property.
while (child > 0)
{
parent = (child - 1) / 2;
if (temp->timer_value_ < this->heap_[parent]->timer_value_)
{
this->heap_[child] = this->heap_[parent];
child = parent;
}
else
break;
}
this->heap_[child] = temp;
}
// Reschedule a periodic timer. This function must be called with the
// mutex lock held.
void
ACE_Timer_Heap::reschedule (ACE_Timer_Node *expired)
{
ACE_TRACE ("ACE_Timer_Heap::reschedule");
// Insert the <expired> node into the end of the heap and restore
// the heap property.
this->heap_[this->cur_size_++] = expired;
this->reheap_up ();
}
// Insert a new handler that expires at time future_time; if interval
// is > 0, the handler will be reinvoked periodically.
int
ACE_Timer_Heap::schedule (ACE_Event_Handler *handler,
const void *arg,
const ACE_Time_Value &future_time,
const ACE_Time_Value &interval)
{
ACE_TRACE ("ACE_Timer_Heap::schedule");
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
// Increment the sequence number (it will wrap around).
int timer_id = this->timer_id ();
if (this->cur_size_ >= this->max_size_)
{
errno = ENOMEM;
return -1;
}
else
{
ACE_Timer_Node *temp;
ACE_NEW_RETURN (temp,
ACE_Timer_Node (handler,
arg,
future_time,
interval,
0,
timer_id),
-1);
this->reheap_up (temp);
}
return timer_id;
}
// Locate and remove the single <ACE_Event_Handler> with a value of
// <timer_id> from the timer queue.
int
ACE_Timer_Heap::cancel (int timer_id, const void **arg)
{
ACE_TRACE ("ACE_Timer_Heap::cancel");
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
return 0;
}
// Locate and remove all values of <handler> from the timer queue.
int
ACE_Timer_Heap::cancel (ACE_Event_Handler *handler)
{
ACE_TRACE ("ACE_Timer_Heap::cancel");
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));
return 0;
}
|