ACE_Async_Timer_Queue_Adapter< TQ > Class Template Reference

Adapts an ACE timer queue to be driven asynchronously using signals. More...

#include <Timer_Queue_Adapters.h>

Inheritance diagram for ACE_Async_Timer_Queue_Adapter< TQ >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Async_Timer_Queue_Adapter< TQ >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef TQ TIMER_QUEUE

Public Member Functions

 ACE_Async_Timer_Queue_Adapter (ACE_Sig_Set *mask=0)
 Constructor.
long schedule (ACE_Event_Handler *type, const void *act, const ACE_Time_Value &future_time, const ACE_Time_Value &interval=ACE_Time_Value::zero)
int cancel (long timer_id, const void **act=0)
int expire (void)
TQ & timer_queue (void)
 Return a reference to the underlying timer queue.

Private Member Functions

virtual int schedule_ualarm (void)
 Perform the logic to compute the new ualarm(2) setting.
virtual int handle_signal (int signum, siginfo_t *, ucontext_t *)
 Called back by SIGALRM handler.

Private Attributes

ACE_Sig_Handler sig_handler_
TQ timer_queue_
ACE_Sig_Set mask_
 Mask of signals to be blocked when we're servicing SIGALRM.

Detailed Description

template<class TQ>
class ACE_Async_Timer_Queue_Adapter< TQ >

Adapts an ACE timer queue to be driven asynchronously using signals.

This implementation uses the ACE_OS::ualarm call, to generate the SIGARLM signal that is caught by this class.

Note:
This adapter only works on platforms that support ualarm(). POSIX platforms generally do; Windows and some others do not.
Todo:
This adapter does not automatically reschedule repeating timers.

Definition at line 53 of file Timer_Queue_Adapters.h.


Member Typedef Documentation

template<class TQ>
typedef TQ ACE_Async_Timer_Queue_Adapter< TQ >::TIMER_QUEUE

Definition at line 56 of file Timer_Queue_Adapters.h.


Constructor & Destructor Documentation

template<class TQ>
ACE_Async_Timer_Queue_Adapter< TQ >::ACE_Async_Timer_Queue_Adapter ( ACE_Sig_Set mask = 0  ) 

Constructor.

Register the SIGALRM handler. If mask == 0 then block all signals when SIGALRM is run. Otherwise, just block the signals indicated in mask.

Definition at line 100 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR, ACE_TEXT, LM_ERROR, SA_RESTART, and SIGALRM.

00103   : mask_ (mask)
00104 {
00105   // The following code is necessary to selectively "block" certain
00106   // signals when SIGALRM is running.  Also, we always restart system
00107   // calls that are interrupted by the signals.
00108 
00109   ACE_Sig_Action sa ((ACE_SignalHandler) 0,
00110                      this->mask_,
00111                      SA_RESTART);
00112 
00113   if (this->sig_handler_.register_handler (SIGALRM, this, &sa) == -1)
00114     ACE_ERROR ((LM_ERROR,
00115                 ACE_TEXT ("%p\n"),
00116                 ACE_TEXT ("register_handler")));
00117 }


Member Function Documentation

template<class TQ>
int ACE_Async_Timer_Queue_Adapter< TQ >::cancel ( long  timer_id,
const void **  act = 0 
)

Cancel the timer_id and pass back the act if an address is passed in.

Definition at line 33 of file Timer_Queue_Adapters.cpp.

References ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_.

00035 {
00036   // Block designated signals.
00037   ACE_Sig_Guard sg (&this->mask_);
00038   ACE_UNUSED_ARG (sg);
00039 
00040   return this->timer_queue_.cancel (timer_id, act);
00041 }

template<class TQ>
int ACE_Async_Timer_Queue_Adapter< TQ >::expire ( void   ) 

Dispatch all timers with expiry time at or before the current time. Returns the number of timers expired.

Definition at line 44 of file Timer_Queue_Adapters.cpp.

References ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_.

00045 {
00046   // Block designated signals.
00047   ACE_Sig_Guard sg (&this->mask_);
00048   ACE_UNUSED_ARG (sg);
00049 
00050   return this->timer_queue_.expire ();
00051 }

template<class TQ>
int ACE_Async_Timer_Queue_Adapter< TQ >::handle_signal ( int  signum,
siginfo_t ,
ucontext_t  
) [private, virtual]

Called back by SIGALRM handler.

Reimplemented from ACE_Event_Handler.

Definition at line 124 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR_RETURN, LM_ERROR, SIGALRM, and ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_.

00127 {
00128   switch (signum)
00129     {
00130     case SIGALRM:
00131       {
00132         // Expire the pending timers.
00133 
00134         // @@ We need to figure out how to implement interval
00135         // timers...
00136         this->timer_queue_.expire ();
00137 
00138         // Only schedule a new timer if there is one in the list.
00139 
00140         // @@ This code should also become smarter to avoid
00141         // unnecessary calls to ualarm().
00142         if (this->timer_queue_.is_empty () == 0)
00143           return this->schedule_ualarm ();
00144         else
00145           return 0;
00146         /* NOTREACHED */
00147       }
00148     default:
00149       ACE_ERROR_RETURN ((LM_ERROR,
00150                          "unexpected signal %S\n",
00151                          signum),
00152                         -1);
00153       /* NOTREACHED */
00154     }
00155 }

template<class TQ>
long ACE_Async_Timer_Queue_Adapter< TQ >::schedule ( ACE_Event_Handler type,
const void *  act,
const ACE_Time_Value future_time,
const ACE_Time_Value interval = ACE_Time_Value::zero 
)

This timer gets dispatched via a signal, rather than by a user calling expire(). Note that interval timers are not implemented yet.

Definition at line 72 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR_RETURN, ACE_TEXT, LM_ERROR, and ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_.

00076 {
00077   ACE_UNUSED_ARG (act);
00078   ACE_UNUSED_ARG (interval);
00079 
00080   // Block designated signals.
00081   ACE_Sig_Guard sg (&this->mask_);
00082   ACE_UNUSED_ARG (sg);
00083 
00084   // @@ We still need to implement interval timers...
00085   long tid = this->timer_queue_.schedule (eh, act, future_time);
00086 
00087   if (tid == -1)
00088     ACE_ERROR_RETURN ((LM_ERROR,
00089                        ACE_TEXT ("%p\n"),
00090                        ACE_TEXT ("schedule_timer")),
00091                       -1);
00092 
00093   if (this->schedule_ualarm () == -1)
00094     return 0;
00095 
00096   return tid;
00097 }

template<class TQ>
int ACE_Async_Timer_Queue_Adapter< TQ >::schedule_ualarm ( void   )  [private, virtual]

Perform the logic to compute the new ualarm(2) setting.

Definition at line 54 of file Timer_Queue_Adapters.cpp.

References ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_, ACE_OS::ualarm(), and ACE_Time_Value::zero.

00055 {
00056   ACE_Time_Value tv = this->timer_queue_.earliest_time ()
00057     - this->timer_queue_.gettimeofday ();
00058 
00059   // Beware of negative times and zero times (which cause problems for
00060   // <ualarm>).
00061   if (tv < ACE_Time_Value::zero)
00062     tv = ACE_Time_Value (0, 1);
00063 
00064   // @@ This code should be clever enough to avoid updating the
00065   // <ualarm> if we haven't actually changed the earliest time.
00066   // Schedule a new timer.
00067   ACE_OS::ualarm (tv);
00068   return 0;
00069 }

template<class TQ>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL TQ & ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue ( void   ) 

Return a reference to the underlying timer queue.

Definition at line 27 of file Timer_Queue_Adapters.cpp.

References ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_.

00028 {
00029   return this->timer_queue_;
00030 }


Member Data Documentation

template<class TQ>
ACE_Sig_Set ACE_Async_Timer_Queue_Adapter< TQ >::mask_ [private]

Mask of signals to be blocked when we're servicing SIGALRM.

Definition at line 105 of file Timer_Queue_Adapters.h.

template<class TQ>
ACE_Sig_Handler ACE_Async_Timer_Queue_Adapter< TQ >::sig_handler_ [private]

Handler for the SIGALRM signal, so that we can access our state without requiring any global variables.

Definition at line 98 of file Timer_Queue_Adapters.h.

template<class TQ>
TQ ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue_ [private]

Implementation of the timer queue (e.g., ACE_Timer_List, ACE_Timer_Heap, etc.).

Definition at line 102 of file Timer_Queue_Adapters.h.

Referenced by ACE_Async_Timer_Queue_Adapter< TQ >::cancel(), ACE_Async_Timer_Queue_Adapter< TQ >::expire(), ACE_Async_Timer_Queue_Adapter< TQ >::handle_signal(), ACE_Async_Timer_Queue_Adapter< TQ >::schedule(), ACE_Async_Timer_Queue_Adapter< TQ >::schedule_ualarm(), and ACE_Async_Timer_Queue_Adapter< TQ >::timer_queue().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:34:55 2010 for ACE by  doxygen 1.4.7