summaryrefslogtreecommitdiff
path: root/ace/Timer_Queue_T.cpp
blob: e0aec3b7e9c726ac67b8d02a3ee498822dec641c (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
// Timer_Queue_T.cpp
// $Id$

#if !defined (ACE_TIMER_QUEUE_T_C)
#define ACE_TIMER_QUEUE_T_C

#define ACE_BUILD_DLL
#include "ace/Synch.h"
#include "ace/Timer_Queue_T.h"

#if !defined (__ACE_INLINE__)
#include "ace/Timer_Queue_T.i"
#endif /* __ACE_INLINE__ */

ACE_ALLOC_HOOK_DEFINE(ACE_Timer_Node_T)

template <class TYPE, class FUNCTOR, class LOCK> void
ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK>::dump (void) const
{
  ACE_TRACE ("ACE_Timer_Node::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  // ACE_DEBUG ((LM_DEBUG, "\type_ = %x", this->type_));
  ACE_DEBUG ((LM_DEBUG, "\nact_ = %x", this->act_));
  this->timer_value_.dump ();
  this->interval_.dump ();
  ACE_DEBUG ((LM_DEBUG, "\nprev_ = %x", this->prev_));
  ACE_DEBUG ((LM_DEBUG, "\nnext_ = %x", this->next_));
  ACE_DEBUG ((LM_DEBUG, "\ntimer_id_ = %d", this->timer_id_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK>::ACE_Timer_Node_T (void)
{
  ACE_TRACE ("ACE_Timer_Node::ACE_Timer_Node");
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK>::ACE_Timer_Node_T (const TYPE &type, 
							 const void *a, 
							 const ACE_Time_Value &t, 
							 const ACE_Time_Value &i, 
							 ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK> *n,
							 long timer_id)
  : type_ (type), 
    act_ (a), 
    timer_value_ (t), 
    interval_ (i), 
    prev_ (0),
    next_ (n),
    timer_id_ (timer_id)
{
  ACE_TRACE ("ACE_Timer_Node::ACE_Timer_Node");
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK>::ACE_Timer_Node_T (const TYPE &type, 
							 const void *a, 
							 const ACE_Time_Value &t, 
							 const ACE_Time_Value &i, 
							 ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK> *p,
							 ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK> *n,
							 long timer_id)
  : type_ (type), 
    act_ (a), 
    timer_value_ (t), 
    interval_ (i), 
    prev_ (p),
    next_ (n),
    timer_id_ (timer_id)
{
  ACE_TRACE ("ACE_Timer_Node::ACE_Timer_Node");
}


template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, LOCK>::ACE_Timer_Queue_Iterator_T (void)
{
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, LOCK>::~ACE_Timer_Queue_Iterator_T (void)
{
}

// Determines the minimum amount of time that the Reactor must wait
// before timing out.  This is computed as the smaller of (1) the
// amount the caller requested when calling handle_events() and (2)
// the earliest time registered in the Timer Queue (if any).  Must be
// called with an external lock held since it returns a pointer to a
// Time_Value type stored in the Timer_Queue type itself.  If some
// external lock isn't held we'll have reentrancy problems!

template <class TYPE, class FUNCTOR, class LOCK> ACE_Time_Value *
ACE_Timer_Queue_T<TYPE, FUNCTOR, LOCK>::calculate_timeout (ACE_Time_Value *max_wait_time)
{
  ACE_TRACE ("ACE_Timer_List::calculate_timeout");
  ACE_MT (ACE_GUARD_RETURN (LOCK, ace_mon, this->mutex_, max_wait_time));
  
  if (this->is_empty ())
    // Nothing on the Timer_Queue, so use whatever the caller gave us.
    return max_wait_time; 
  else
    {
      ACE_Time_Value cur_time = this->gettimeofday ();

      if (this->earliest_time () > cur_time)
        {
	  // The earliest item on the Timer_Queue is still in the
	  // future.  Therefore, use the smaller of (1) caller's wait
	  // time or (2) the delta time between now and the earliest
	  // time on the Timer_Queue.

          this->timeout_ = this->earliest_time () - cur_time;
          if (max_wait_time == 0 || *max_wait_time > timeout_)
            return &this->timeout_;
	  else
	    return max_wait_time;
        }
      else 	  
        {
	  // The earliest item on the Timer_Queue is now in the past.
	  // Therefore, we've got to "poll" the Reactor, i.e., it must
	  // just check the descriptors and then dispatch timers, etc.
          this->timeout_ = ACE_Time_Value::zero;
	  return &this->timeout_;
        }
    }
}

template <class TYPE, class FUNCTOR, class LOCK> void
ACE_Timer_Queue_T<TYPE, FUNCTOR, LOCK>::dump (void) const
{
  ACE_TRACE ("ACE_Timer_Queue::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  this->timeout_.dump ();
  this->timer_skew_.dump ();
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));    
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Queue_T<TYPE, FUNCTOR, LOCK>::ACE_Timer_Queue_T (FUNCTOR *upcall_functor)
  : gettimeofday_ (ACE_OS::gettimeofday),
    upcall_functor_ (upcall_functor == 0 ? *(new FUNCTOR) : *upcall_functor),
    delete_upcall_functor_ (upcall_functor == 0),
    timer_skew_ (0, ACE_TIMER_SKEW)
{
  ACE_TRACE ("ACE_Timer_Queue::ACE_Timer_Queue");
}

template <class TYPE, class FUNCTOR, class LOCK> 
ACE_Timer_Queue_T<TYPE, FUNCTOR, LOCK>::~ACE_Timer_Queue_T (void)
{
  // Cleanup the functor on the way out
  if (delete_upcall_functor_)
    delete &this->upcall_functor_;

  ACE_TRACE ("ACE_Timer_Queue::~ACE_Timer_Queue");
}

// Run the <handle_timeout> method for all Timers whose values are <=
// <cur_time>.

template <class TYPE, class FUNCTOR, class LOCK> int
ACE_Timer_Queue_T<TYPE, FUNCTOR, LOCK>::expire (const ACE_Time_Value &cur_time)
{
  ACE_TRACE ("ACE_Timer_List::expire");
  ACE_MT (ACE_GUARD_RETURN (LOCK, ace_mon, this->mutex_, -1));

  int number_of_timers_expired = 0;

  ACE_Timer_Node_T<TYPE, FUNCTOR, LOCK> *expired;

  // Keep looping while there are timers remaining and the earliest
  // timer is <= the <cur_time> passed in to the method.

  for (ITERATOR &iter = this->iter ();
       iter.next (expired, cur_time) != 0;
       )
    {
      TYPE &type = expired->type_;
      const void *act = expired->act_;
      int reclaim = 1;
      
      // Check if this is an interval timer.
      if (expired->interval_ > ACE_Time_Value::zero)
	{
	  // Make sure that we skip past values that have already
	  // "expired".
	  do
	    expired->timer_value_ += expired->interval_;
	  while (expired->timer_value_ <= cur_time);

	  // Since this is an interval timer, we need to reschedule
	  // it.
	  this->reschedule (expired);
	  reclaim = 0;
	}
      
      // call the functor
      this->upcall (type, act, cur_time);
      
      if (reclaim)
	// Call the factory method to free up the node.
	this->free_node (expired);
      
      number_of_timers_expired++;
    }

  return number_of_timers_expired;
}

#endif /* ACE_TIMER_QUEUE_T_C*/