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 50 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 53 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 95 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_SignalHandler, LM_ERROR, ACE_Sig_Handler::register_handler(), SA_RESTART, and SIGALRM.

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


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 28 of file Timer_Queue_Adapters.cpp.

00030 {
00031   // Block designated signals.
00032   ACE_Sig_Guard sg (&this->mask_);
00033   ACE_UNUSED_ARG (sg);
00034 
00035   return this->timer_queue_.cancel (timer_id, act);
00036 }

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 39 of file Timer_Queue_Adapters.cpp.

00040 {
00041   // Block designated signals.
00042   ACE_Sig_Guard sg (&this->mask_);
00043   ACE_UNUSED_ARG (sg);
00044 
00045   return this->timer_queue_.expire ();
00046 }

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 119 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR_RETURN, LM_ERROR, ACE_Async_Timer_Queue_Adapter< TQ >::schedule_ualarm(), SIGALRM, and ucontext_t.

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

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 67 of file Timer_Queue_Adapters.cpp.

References ACE_ERROR_RETURN, ACE_LIB_TEXT, LM_ERROR, and ACE_Async_Timer_Queue_Adapter< TQ >::schedule_ualarm().

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

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 49 of file Timer_Queue_Adapters.cpp.

References ACE_Time_Value, ACE_OS::gettimeofday(), and ACE_OS::ualarm().

Referenced by ACE_Async_Timer_Queue_Adapter< TQ >::handle_signal(), and ACE_Async_Timer_Queue_Adapter< TQ >::schedule().

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

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 22 of file Timer_Queue_Adapters.cpp.

00023 {
00024   return this->timer_queue_;
00025 }


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 102 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 95 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 99 of file Timer_Queue_Adapters.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:19:16 2006 for ACE by doxygen 1.3.6