summaryrefslogtreecommitdiff
path: root/ace/Timer_List.cpp
blob: 50d6548ef6e5fee1ad72bfa1fa931aa91bf680b4 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#define ACE_BUILD_DLL

#include "ace/Timer_List.h"

ACE_Timer_List_Iterator::ACE_Timer_List_Iterator (ACE_Timer_List &list)
  : timer_list_ (list)
{
}

int
ACE_Timer_List_Iterator::next (ACE_Timer_Node *&node,
			       const ACE_Time_Value &cur_time)
{
  if (this->timer_list_.head_ == 0 
      || this->timer_list_.head_->timer_value_ > cur_time)
    return 0;
  else
    {
      node = this->timer_list_.head_;
      this->timer_list_.head_ = this->timer_list_.head_->next_;
      return 1;
    }
}

ACE_ALLOC_HOOK_DEFINE(ACE_Timer_List)

ACE_Timer_Queue_Iterator &
ACE_Timer_List::iter (void)
{
  return this->iterator_;
}

// Create an empty list.

ACE_Timer_List::ACE_Timer_List (void)
  : head_ (0),
    iterator_ (*this),
    timer_id_ (0)
{
  ACE_TRACE ("ACE_Timer_List::ACE_Timer_List");
}

// Checks if list is empty.

int 
ACE_Timer_List::is_empty (void) const
{
  ACE_TRACE ("ACE_Timer_List::is_empty");
  return this->head_ == 0;
}

ACE_Timer_Node *
ACE_Timer_List::alloc_node (void)
{
  return new ACE_Timer_Node;
}

void
ACE_Timer_List::free_node (ACE_Timer_Node *node)
{
  delete node;
}

// Returns earliest time in a non-empty list.

const ACE_Time_Value &
ACE_Timer_List::earliest_time (void) const
{
  ACE_TRACE ("ACE_Timer_List::earliest_time");
  return this->head_->timer_value_;
}

// Remove all remaining items in the list.

ACE_Timer_List::~ACE_Timer_List (void)
{
  ACE_TRACE ("ACE_Timer_List::~ACE_Timer_List");
  ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_));
  
  ACE_Timer_Node *curr = this->head_;
  
  while (curr != 0)
    {
      ACE_Timer_Node *next = curr->next_;
      this->free_node (curr);
      curr = next;
    }
}

void 
ACE_Timer_List::dump (void) const
{
  ACE_TRACE ("ACE_Timer_List::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG, "\nhead_ = %d", this->head_));
  ACE_DEBUG ((LM_DEBUG, "\ntimer_id_ = %d", this->timer_id_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));    
}

// Reschedule a periodic timer.  This function must be called with the
// mutex lock held.

void 
ACE_Timer_List::reschedule (ACE_Timer_Node *expired)
{
  ACE_TRACE ("ACE_Timer_List::reschedule");
  if (this->is_empty () 
      || expired->timer_value_ < this->earliest_time ())
    {
      expired->next_ = this->head_;
      this->head_ = expired;
    }
  else
    {
      ACE_Timer_Node *prev = this->head_;
      ACE_Timer_Node *after = this->head_->next_;

      // Locate the proper position in the queue. 

      while (after != 0 
	     && expired->timer_value_ > after->timer_value_)
	{
	  prev = after;
	  after = after->next_;
	}

      expired->next_ = after;
      prev->next_ = expired;
    }
}

// Insert a new handler that expires at time future_time; if interval
// is > 0, the handler will be reinvoked periodically.

int
ACE_Timer_List::schedule (ACE_Event_Handler *handler,
			  const void *arg,
			  const ACE_Time_Value	&future_time, 
			  const ACE_Time_Value	&interval)
{
  ACE_TRACE ("ACE_Timer_List::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->is_empty () || future_time < this->earliest_time ())
    {
      // Place at the beginning of the list.
      ACE_Timer_Node *temp = this->alloc_node ();

      // Use operator placement new.
      this->head_ = new (temp) ACE_Timer_Node (handler, 
					       arg, 
					       future_time,
					       interval, 
					       this->head_,
					       timer_id);
      return timer_id;
    }

  // Place in the middle of the list where it belongs (i.e., sorted in
  // ascending order of absolute time to expire).
  else 
    {
      ACE_Timer_Node *prev = this->head_;
      ACE_Timer_Node *after = this->head_->next_;

      while (after != 0 && future_time > after->timer_value_)
	{
	  prev = after;
	  after = after->next_;
	}

      ACE_Timer_Node *temp = this->alloc_node ();

      // Use operator placement new.
      prev->next_ = new (temp) ACE_Timer_Node (handler, 
					       arg, 
					       future_time,
					       interval, 
					       after,
					       timer_id);
      return timer_id;
    }
}

int
ACE_Timer_List::timer_id (void)
{
  this->timer_id_++;

  // Make sure this never == -1
  if (this->timer_id_ == -1)
    this->timer_id_ = 0;

  return this->timer_id_;
}

// Locate and remove the single <ACE_Event_Handler> with a value of
// <timer_id> from the timer queue.

int
ACE_Timer_List::cancel (int timer_id, 
			const void **arg)
{
  ACE_TRACE ("ACE_Timer_List::cancel");
  ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_Timer_Node *prev = 0;
  ACE_Timer_Node *curr = 0;

  // Try to locate the ACE_Timer_Node that matches the timer_id.

  for (curr = this->head_; 
       curr != 0 && curr->timer_id_ != timer_id;
       curr = curr->next_)
    prev = curr;

  if (curr != 0)
    {
      if (prev == 0)
	this->head_ = curr->next_;
      else
	prev->next_ = curr->next_;

      if (arg != 0)
	*arg = curr->arg_;

      this->free_node (curr);
      return 1;
    }
  else
    return 0;
}

// Locate and remove all values of <handler> from the timer queue.

int
ACE_Timer_List::cancel (ACE_Event_Handler *handler)
{
  ACE_TRACE ("ACE_Timer_List::cancel");
  ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon, this->lock_, -1));

  ACE_Timer_Node *prev = 0;
  ACE_Timer_Node *curr = this->head_;

  int number_of_cancellations = 0;

  while (curr != 0)
    {
      if (curr->handler_ == handler)
	{
	  number_of_cancellations++;

	  if (prev == 0)
	    {
	      this->head_ = curr->next_;
	      this->free_node (curr);
	      curr = this->head_;
	    }
	  else
	    {
	      prev->next_ = curr->next_;
	      this->free_node (curr);
	      curr = prev->next_;
	    }
	}
      else
	{
	  prev = curr;
	  curr = curr->next_;
	}
    }

  return number_of_cancellations;
}