00001
00002
00003 #ifndef ACE_TIMER_QUEUE_T_CPP
00004 #define ACE_TIMER_QUEUE_T_CPP
00005
00006 #include "ace/config-all.h"
00007
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif
00011
00012
00013
00014
00015
00016
00017 #include "ace/Timer_Queue_T.h"
00018 #include "ace/Guard_T.h"
00019 #include "ace/Log_Msg.h"
00020 #include "ace/Reactor_Timer_Interface.h"
00021 #include "ace/Null_Mutex.h"
00022 #include "ace/OS_NS_sys_time.h"
00023
00024 #if !defined (__ACE_INLINE__)
00025 #include "ace/Timer_Queue_T.inl"
00026 #endif
00027
00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00029
00030
00031
00032
00033 #if !defined (ACE_TIMER_SKEW)
00034 # define ACE_TIMER_SKEW 0
00035 #endif
00036
00037 template <class TYPE> void
00038 ACE_Timer_Node_T<TYPE>::dump (void) const
00039 {
00040 #if defined (ACE_HAS_DUMP)
00041 ACE_TRACE ("ACE_Timer_Node_T::dump");
00042 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00043 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nact_ = %x"), this->act_));
00044 this->timer_value_.dump ();
00045 this->interval_.dump ();
00046 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nprev_ = %x"), this->prev_));
00047 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nnext_ = %x"), this->next_));
00048 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\ntimer_id_ = %d\n"), this->timer_id_));
00049 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00050 #endif
00051 }
00052
00053 template <class TYPE>
00054 ACE_Timer_Node_T<TYPE>::ACE_Timer_Node_T (void)
00055 {
00056 ACE_TRACE ("ACE_Timer_Node_T::ACE_Timer_Node_T");
00057 }
00058
00059 template <class TYPE>
00060 ACE_Timer_Node_T<TYPE>::~ACE_Timer_Node_T (void)
00061 {
00062 ACE_TRACE ("ACE_Timer_Node_T::~ACE_Timer_Node_T");
00063 }
00064
00065 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00066 ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Queue_Iterator_T (void)
00067 {
00068 }
00069
00070 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00071 ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Queue_Iterator_T (void)
00072 {
00073 }
00074
00075 template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Time_Value *
00076 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::calculate_timeout (ACE_Time_Value *max_wait_time)
00077 {
00078 ACE_TRACE ("ACE_Timer_Queue_T::calculate_timeout");
00079 ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, max_wait_time));
00080
00081 if (this->is_empty ())
00082
00083 return max_wait_time;
00084 else
00085 {
00086 ACE_Time_Value const cur_time = this->gettimeofday ();
00087
00088 if (this->earliest_time () > cur_time)
00089 {
00090
00091
00092
00093
00094
00095 this->timeout_ = this->earliest_time () - cur_time;
00096 if (max_wait_time == 0 || *max_wait_time > timeout_)
00097 return &this->timeout_;
00098 else
00099 return max_wait_time;
00100 }
00101 else
00102 {
00103
00104
00105
00106 this->timeout_ = ACE_Time_Value::zero;
00107 return &this->timeout_;
00108 }
00109 }
00110 }
00111
00112 template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Time_Value *
00113 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::calculate_timeout (ACE_Time_Value *max_wait_time,
00114 ACE_Time_Value *the_timeout)
00115 {
00116 ACE_TRACE ("ACE_Timer_Queue_T::calculate_timeout");
00117
00118 if (the_timeout == 0)
00119 return 0;
00120
00121 if (this->is_empty ())
00122 {
00123
00124 if (max_wait_time)
00125 *the_timeout = *max_wait_time;
00126 else
00127 return 0;
00128 }
00129 else
00130 {
00131 ACE_Time_Value cur_time = this->gettimeofday ();
00132
00133 if (this->earliest_time () > cur_time)
00134 {
00135
00136
00137
00138
00139
00140 *the_timeout = this->earliest_time () - cur_time;
00141 if (!(max_wait_time == 0 || *max_wait_time > *the_timeout))
00142 *the_timeout = *max_wait_time;
00143 }
00144 else
00145 {
00146
00147
00148
00149 *the_timeout = ACE_Time_Value::zero;
00150 }
00151 }
00152 return the_timeout;
00153 }
00154
00155 template <class TYPE, class FUNCTOR, class ACE_LOCK> void
00156 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::dump (void) const
00157 {
00158 #if defined (ACE_HAS_DUMP)
00159 ACE_TRACE ("ACE_Timer_Queue_T::dump");
00160 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00161 this->timeout_.dump ();
00162 this->timer_skew_.dump ();
00163 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00164 #endif
00165 }
00166
00167 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00168 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Queue_T (FUNCTOR *upcall_functor,
00169 ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist)
00170 : gettimeofday_ (ACE_OS::gettimeofday),
00171 delete_upcall_functor_ (upcall_functor == 0),
00172 delete_free_list_ (freelist == 0),
00173 timer_skew_ (0, ACE_TIMER_SKEW)
00174 {
00175 ACE_TRACE ("ACE_Timer_Queue_T::ACE_Timer_Queue_T");
00176
00177 if (!freelist)
00178 ACE_NEW (free_list_,
00179 (ACE_Locked_Free_List<ACE_Timer_Node_T<TYPE>,ACE_Null_Mutex>));
00180 else
00181 free_list_ = freelist;
00182
00183 if (!upcall_functor)
00184 ACE_NEW (upcall_functor_,
00185 FUNCTOR);
00186 else
00187 upcall_functor_ = upcall_functor;
00188 }
00189
00190 template <class TYPE, class FUNCTOR, class ACE_LOCK>
00191 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Queue_T (void)
00192 {
00193 ACE_TRACE ("ACE_Timer_Queue_T::~ACE_Timer_Queue_T");
00194
00195
00196 if (this->delete_upcall_functor_)
00197 delete this->upcall_functor_;
00198
00199 if (this->delete_free_list_)
00200 delete this->free_list_;
00201 }
00202
00203 template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> *
00204 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::alloc_node (void)
00205 {
00206 return this->free_list_->remove ();
00207 }
00208
00209 template <class TYPE, class FUNCTOR, class ACE_LOCK> void
00210 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::free_node (ACE_Timer_Node_T<TYPE> *node)
00211 {
00212 this->free_list_->add (node);
00213 }
00214
00215 template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_LOCK &
00216 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::mutex (void)
00217 {
00218 return this->mutex_;
00219 }
00220
00221 template <class TYPE, class FUNCTOR, class ACE_LOCK> long
00222 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::schedule (const TYPE &type,
00223 const void *act,
00224 const ACE_Time_Value &future_time,
00225 const ACE_Time_Value &interval)
00226 {
00227 ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
00228
00229
00230 long const result =
00231 this->schedule_i (type,
00232 act,
00233 future_time,
00234 interval);
00235
00236
00237 if (result == -1)
00238 return result;
00239
00240
00241 this->upcall_functor ().registration (*this,
00242 type,
00243 act);
00244
00245
00246 return result;
00247 }
00248
00249
00250
00251 template <class TYPE, class FUNCTOR, class ACE_LOCK> int
00252 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::expire (const ACE_Time_Value &cur_time)
00253 {
00254 ACE_TRACE ("ACE_Timer_Queue_T::expire");
00255 ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
00256
00257
00258
00259
00260 if (this->is_empty ())
00261 return 0;
00262
00263 int number_of_timers_expired = 0;
00264 int result = 0;
00265
00266 ACE_Timer_Node_Dispatch_Info_T<TYPE> info;
00267
00268 while ((result = this->dispatch_info_i (cur_time,
00269 info)) != 0)
00270 {
00271 const void *upcall_act = 0;
00272
00273 this->preinvoke (info, cur_time, upcall_act);
00274
00275 this->upcall (info, cur_time);
00276
00277 this->postinvoke (info, cur_time, upcall_act);
00278
00279 ++number_of_timers_expired;
00280
00281 }
00282
00283 ACE_UNUSED_ARG (result);
00284 return number_of_timers_expired;
00285 }
00286
00287 template <class TYPE, class FUNCTOR, class ACE_LOCK> int
00288 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::dispatch_info_i (const ACE_Time_Value &cur_time,
00289 ACE_Timer_Node_Dispatch_Info_T<TYPE> &info)
00290 {
00291 ACE_TRACE ("ACE_Timer_Queue_T::dispatch_info_i");
00292
00293 if (this->is_empty ())
00294 return 0;
00295
00296 ACE_Timer_Node_T<TYPE> *expired = 0;
00297
00298 if (this->earliest_time () <= cur_time)
00299 {
00300 expired = this->remove_first ();
00301
00302
00303 expired->get_dispatch_info (info);
00304
00305
00306 if (expired->get_interval () > ACE_Time_Value::zero)
00307 {
00308
00309
00310 do
00311 expired->set_timer_value (expired->get_timer_value () +
00312 expired->get_interval ());
00313 while (expired->get_timer_value () <= cur_time);
00314
00315
00316
00317 this->reschedule (expired);
00318 }
00319 else
00320 {
00321
00322 this->free_node (expired);
00323 }
00324
00325 return 1;
00326 }
00327
00328 return 0;
00329 }
00330
00331 template <class TYPE, class FUNCTOR, class ACE_LOCK> void
00332 ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK>::return_node (ACE_Timer_Node_T<TYPE> *node)
00333 {
00334 ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));
00335 this->free_node (node);
00336 }
00337
00338
00339 template <class ACE_LOCK>
00340 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::ACE_Event_Handler_Handle_Timeout_Upcall (void)
00341 {
00342 }
00343
00344 template <class ACE_LOCK>
00345 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::~ACE_Event_Handler_Handle_Timeout_Upcall (void)
00346 {
00347 }
00348
00349 template <class ACE_LOCK> int
00350 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::registration (TIMER_QUEUE &,
00351 ACE_Event_Handler *event_handler,
00352 const void *)
00353 {
00354 event_handler->add_reference ();
00355 return 0;
00356 }
00357
00358 template <class ACE_LOCK> int
00359 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::preinvoke (TIMER_QUEUE & ,
00360 ACE_Event_Handler *event_handler,
00361 const void * ,
00362 int ,
00363 const ACE_Time_Value & ,
00364 const void *&upcall_act)
00365 {
00366 bool const requires_reference_counting =
00367 event_handler->reference_counting_policy ().value () ==
00368 ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
00369
00370 if (requires_reference_counting)
00371 {
00372 event_handler->add_reference ();
00373
00374 upcall_act = &this->requires_reference_counting_;
00375 }
00376
00377 return 0;
00378 }
00379
00380 template <class ACE_LOCK> int
00381 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::postinvoke (TIMER_QUEUE & ,
00382 ACE_Event_Handler *event_handler,
00383 const void * ,
00384 int ,
00385 const ACE_Time_Value & ,
00386 const void *upcall_act)
00387 {
00388 if (upcall_act == &this->requires_reference_counting_)
00389 {
00390 event_handler->remove_reference ();
00391 }
00392
00393 return 0;
00394 }
00395
00396 template <class ACE_LOCK> int
00397 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::timeout (TIMER_QUEUE &timer_queue,
00398 ACE_Event_Handler *event_handler,
00399 const void *act,
00400 int recurring_timer,
00401 const ACE_Time_Value &cur_time)
00402 {
00403 int requires_reference_counting = 0;
00404
00405 if (!recurring_timer)
00406 {
00407 requires_reference_counting =
00408 event_handler->reference_counting_policy ().value () ==
00409 ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
00410 }
00411
00412
00413 if (event_handler->handle_timeout (cur_time, act) == -1)
00414 {
00415 if (event_handler->reactor_timer_interface ())
00416 event_handler->reactor_timer_interface ()->cancel_timer (event_handler, 0);
00417 else
00418 timer_queue.cancel (event_handler, 0);
00419 }
00420
00421 if (!recurring_timer &&
00422 requires_reference_counting)
00423 {
00424 event_handler->remove_reference ();
00425 }
00426
00427 return 0;
00428 }
00429
00430 template <class ACE_LOCK> int
00431 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::cancel_type (TIMER_QUEUE &,
00432 ACE_Event_Handler *event_handler,
00433 int dont_call,
00434 int &requires_reference_counting)
00435 {
00436 requires_reference_counting =
00437 event_handler->reference_counting_policy ().value () ==
00438 ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
00439
00440
00441 if (dont_call == 0)
00442 event_handler->handle_close (ACE_INVALID_HANDLE,
00443 ACE_Event_Handler::TIMER_MASK);
00444
00445 return 0;
00446 }
00447
00448 template <class ACE_LOCK> int
00449 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::cancel_timer (TIMER_QUEUE &,
00450 ACE_Event_Handler *event_handler,
00451 int,
00452 int requires_reference_counting)
00453 {
00454 if (requires_reference_counting)
00455 event_handler->remove_reference ();
00456
00457 return 0;
00458 }
00459
00460 template <class ACE_LOCK> int
00461 ACE_Event_Handler_Handle_Timeout_Upcall<ACE_LOCK>::deletion (TIMER_QUEUE &timer_queue,
00462 ACE_Event_Handler *event_handler,
00463 const void *)
00464 {
00465 int requires_reference_counting = 0;
00466
00467 this->cancel_type (timer_queue,
00468 event_handler,
00469 0,
00470 requires_reference_counting);
00471
00472 this->cancel_timer (timer_queue,
00473 event_handler,
00474 0,
00475 requires_reference_counting);
00476
00477 return 0;
00478 }
00479
00480 ACE_END_VERSIONED_NAMESPACE_DECL
00481
00482 #endif