ACE_Thread_Timer_Queue_Adapter< TQ > Class Template Reference

Adapts an ACE timer queue using a separate thread for dispatching. More...

#include <Timer_Queue_Adapters.h>

Inheritance diagram for ACE_Thread_Timer_Queue_Adapter< TQ >:

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

Collaboration graph
[legend]
List of all members.

Public Types

typedef TQ TIMER_QUEUE
 Trait for the underlying queue type.


Public Member Functions

 ACE_Thread_Timer_Queue_Adapter (ACE_Thread_Manager *=ACE_Thread_Manager::instance(), TQ *timer_queue=0)
virtual ~ACE_Thread_Timer_Queue_Adapter (void)
 Destructor.

long schedule (ACE_Event_Handler *handler, 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)
virtual int svc (void)
 Runs the dispatching thread.

virtual void deactivate (void)
 Inform the dispatching thread that it should terminate.

ACE_SYNCH_RECURSIVE_MUTEX & mutex (void)
 Access the locking mechanism, useful for iteration.

int timer_queue (TQ *tq)
 Set a user-specified timer queue.

TQ * timer_queue (void) const
 Return the current .

ACE_thread_t thr_id (void) const
 Return the thread id of our active object.

virtual int activate (long flags=THR_NEW_LWP|THR_JOINABLE, int n_threads=1, int force_active=0, long priority=ACE_DEFAULT_THREAD_PRIORITY, int grp_id=-1, ACE_Task_Base *task=0, ACE_hthread_t thread_handles[]=0, void *stack[]=0, size_t stack_size[]=0, ACE_thread_t thread_names[]=0)

Private Attributes

TQ * timer_queue_
 The underlying Timer_Queue.

int delete_timer_queue_
ACE_SYNCH_RECURSIVE_MUTEX mutex_
ACE_SYNCH_RECURSIVE_CONDITION condition_
int active_
ACE_thread_t thr_id_
 Thread id of our active object task.


Detailed Description

template<class TQ>
class ACE_Thread_Timer_Queue_Adapter< TQ >

Adapts an ACE timer queue using a separate thread for dispatching.

This implementation uses a separate thread to dispatch the timers. The base queue need not be thread safe; this class takes all the necessary locks.

Note:
This is a case where template parameters will be useful, but (IMHO) the effort and portability problems discourage their use.

Definition at line 120 of file Timer_Queue_Adapters.h.


Member Typedef Documentation

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

Trait for the underlying queue type.

Definition at line 124 of file Timer_Queue_Adapters.h.


Constructor & Destructor Documentation

template<class TQ>
ACE_Thread_Timer_Queue_Adapter< TQ >::ACE_Thread_Timer_Queue_Adapter ACE_Thread_Manager = ACE_Thread_Manager::instance(),
TQ *  timer_queue = 0
 

Creates the timer queue. Activation of the task is the user's responsibility. Optionally a pointer to a timer queue can be passed, when no pointer is passed, a TQ is dynamically created

Definition at line 153 of file Timer_Queue_Adapters.cpp.

References ACE_NEW.

00155   : ACE_Task_Base (tm),
00156     timer_queue_(timer_queue),
00157     delete_timer_queue_(0),
00158     condition_ (mutex_),
00159     active_ (1), // Assume that we start in active mode.
00160     thr_id_ (ACE_OS::NULL_thread)
00161 {
00162   if (timer_queue_ == 0)
00163     {
00164       ACE_NEW (this->timer_queue_,
00165                TQ);
00166       this->delete_timer_queue_ = 1;
00167     }
00168 }

template<class TQ>
ACE_Thread_Timer_Queue_Adapter< TQ >::~ACE_Thread_Timer_Queue_Adapter void   )  [virtual]
 

Destructor.

Definition at line 171 of file Timer_Queue_Adapters.cpp.

00172 {
00173   if (this->delete_timer_queue_)
00174     {
00175       delete this->timer_queue_;
00176       this->timer_queue_ = 0;
00177       this->delete_timer_queue_ = 0;
00178     }
00179 }


Member Function Documentation

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter< TQ >::activate long  flags = THR_NEW_LWP|THR_JOINABLE,
int  n_threads = 1,
int  force_active = 0,
long  priority = ACE_DEFAULT_THREAD_PRIORITY,
int  grp_id = -1,
ACE_Task_Base task = 0,
ACE_hthread_t  thread_handles[] = 0,
void *  stack[] = 0,
size_t  stack_size[] = 0,
ACE_thread_t  thread_names[] = 0
[virtual]
 

We override the default activate() method so that we can ensure that only a single thread is ever spawned. Otherwise, too many weird things can happen...

Reimplemented from ACE_Task_Base.

Definition at line 289 of file Timer_Queue_Adapters.cpp.

References ACE_hthread_t, and ACE_Task_Base::activate().

00299 {
00300   // Macros to avoid "warning: unused parameter" type warning.
00301   ACE_UNUSED_ARG (thread_handles);
00302 
00303   // Make sure to set this flag in case we were deactivated earlier.
00304   this->active_ = 1;
00305 
00306   // Make sure that we only allow a single thread to be spawned for
00307   // our adapter.  Otherwise, too many weird things can happen.
00308   return ACE_Task_Base::activate (flags, 1, 0, priority, grp_id, task, 0,
00309                                   stack, stack_size, thread_names);
00310 }

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

Cancel the timer_id and return the act parameter if an address is passed in. Also wakes up the dispatching thread.

Definition at line 202 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD_RETURN, and ACE_SYNCH_RECURSIVE_MUTEX.

00204 {
00205   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00206 
00207   int result = this->timer_queue_->cancel (timer_id, act);
00208   condition_.signal ();
00209   return result;
00210 }

template<class TQ>
void ACE_Thread_Timer_Queue_Adapter< TQ >::deactivate void   )  [virtual]
 

Inform the dispatching thread that it should terminate.

Definition at line 213 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD, and ACE_SYNCH_RECURSIVE_MUTEX.

00214 {
00215   ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_);
00216 
00217   this->active_ = 0;
00218   this->condition_.signal ();
00219 }

template<class TQ>
ACE_SYNCH_RECURSIVE_MUTEX & ACE_Thread_Timer_Queue_Adapter< TQ >::mutex void   ) 
 

Access the locking mechanism, useful for iteration.

Definition at line 182 of file Timer_Queue_Adapters.cpp.

00183 {
00184   return this->mutex_;
00185 }

template<class TQ>
long ACE_Thread_Timer_Queue_Adapter< TQ >::schedule ACE_Event_Handler handler,
const void *  act,
const ACE_Time_Value future_time,
const ACE_Time_Value interval = ACE_Time_Value::zero
 

Schedule the timer according to the semantics of the ; wakes up the dispatching thread.

Definition at line 189 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD_RETURN, and ACE_SYNCH_RECURSIVE_MUTEX.

00193 {
00194   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00195 
00196   long result = this->timer_queue_->schedule (handler, act, future_time, interval);
00197   this->condition_.signal ();
00198   return result;
00199 }

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter< TQ >::svc void   )  [virtual]
 

Runs the dispatching thread.

Reimplemented from ACE_Task_Base.

Definition at line 222 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD_RETURN, ACE_PTHREAD_CLEANUP_POP, ACE_PTHREAD_CLEANUP_PUSH, ACE_SYNCH_RECURSIVE_MUTEX, ACE_OS::gettimeofday(), and ACE_Thread::self().

00223 {
00224   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00225 
00226   this->thr_id_ = ACE_Thread::self ();
00227 
00228   // Thread cancellation point, if ACE supports it.
00229   //
00230   // Note: This call generates a warning under Solaris because the header
00231   //       file /usr/include/pthread.h redefines the routine argument. This
00232   //       is a bug in the Solaris header files and has nothing to do with
00233   //       ACE.
00234 # if !defined (ACE_LACKS_PTHREAD_CANCEL)
00235   ACE_PTHREAD_CLEANUP_PUSH (&this->condition_.mutex ().get_nesting_mutex ());
00236 # endif /* ACE_LACKS_PTHREAD_CANCEL */
00237 
00238   while (this->active_)
00239     {
00240 # if defined (ACE_HAS_DEFERRED_TIMER_COMMANDS)
00241       // Temporarily suspend ownership of the timer queue mutex in
00242       // order to dispatch deferred execution commands.  These
00243       // commands are to be treated as executing in a context
00244       // "external" to the timer queue adapter, and thus must compete
00245       // separately for this lock.
00246       mutex_.release ();
00247       this->dispatch_commands ();
00248 
00249       // Re-acquire ownership of the timer queue mutex in order to
00250       // restore the "internal" timer queue adapter context
00251       mutex_.acquire ();
00252 # endif /* ACE_HAS_DEFERRED_TIMER_COMMANDS */
00253 
00254       // If the queue is empty, sleep until there is a change on it.
00255       if (this->timer_queue_->is_empty ())
00256         this->condition_.wait ();
00257       else
00258         {
00259           // Compute the remaining time, being careful not to sleep
00260           // for "negative" amounts of time.
00261           const ACE_Time_Value tv_curr = this->timer_queue_->gettimeofday ();
00262           const ACE_Time_Value tv_earl = this->timer_queue_->earliest_time ();
00263  
00264           if (tv_earl > tv_curr)
00265             {
00266               // The earliest time on the Timer_Queue is in future, so
00267               // use ACE_OS::gettimeofday() to convert the tv to the
00268               // absolute time.
00269               const ACE_Time_Value tv = ACE_OS::gettimeofday () + (tv_earl - tv_curr);  
00270               // ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("waiting until %u.%3.3u secs\n"),
00271               // tv.sec(), tv.msec()));
00272               this->condition_.wait (&tv);
00273             }
00274         }
00275 
00276       // Expire timers anyway, at worst this is a no-op.
00277       this->timer_queue_->expire ();
00278     }
00279 
00280    // Thread cancellation point, if ACE supports it.
00281 # if !defined (ACE_LACKS_PTHREAD_CANCEL)
00282   ACE_PTHREAD_CLEANUP_POP (0);
00283 # endif /* ACE_LACKS_PTHREAD_CANCEL */
00284 
00285   return 0;
00286 }

template<class TQ>
ACE_INLINE ACE_thread_t ACE_Thread_Timer_Queue_Adapter< TQ >::thr_id void   )  const
 

Return the thread id of our active object.

Definition at line 24 of file Timer_Queue_Adapters.inl.

00025 {
00026   return this->thr_id_;
00027 }

template<class TQ>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE TQ * ACE_Thread_Timer_Queue_Adapter< TQ >::timer_queue void   )  const
 

Return the current .

Definition at line 8 of file Timer_Queue_Adapters.inl.

00009 {
00010   return this->timer_queue_;
00011 }

template<class TQ>
ACE_INLINE int ACE_Thread_Timer_Queue_Adapter< TQ >::timer_queue TQ *  tq  ) 
 

Set a user-specified timer queue.

Definition at line 14 of file Timer_Queue_Adapters.inl.

00015 {
00016   if (this->delete_timer_queue_ != 0)
00017     delete this->timer_queue_;
00018   this->timer_queue_ = tq;
00019   this->delete_timer_queue_ = 0;
00020   return 0;
00021 }


Member Data Documentation

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter< TQ >::active_ [private]
 

When deactivate is called this variable turns to false and the dispatching thread is signalled, to terminate its main loop.

Definition at line 236 of file Timer_Queue_Adapters.h.

template<class TQ>
ACE_SYNCH_RECURSIVE_CONDITION ACE_Thread_Timer_Queue_Adapter< TQ >::condition_ [private]
 

The dispatching thread sleeps on this condition while waiting to dispatch the next timer; it is used to wake it up if there is a change on the timer queue.

Definition at line 232 of file Timer_Queue_Adapters.h.

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter< TQ >::delete_timer_queue_ [private]
 

Keeps track of whether we should delete the timer queue (if we didn't create it, then we don't delete it).

Definition at line 221 of file Timer_Queue_Adapters.h.

template<class TQ>
ACE_SYNCH_RECURSIVE_MUTEX ACE_Thread_Timer_Queue_Adapter< TQ >::mutex_ [private]
 

The mutual exclusion mechanism that is required to use the .

Definition at line 225 of file Timer_Queue_Adapters.h.

template<class TQ>
ACE_thread_t ACE_Thread_Timer_Queue_Adapter< TQ >::thr_id_ [private]
 

Thread id of our active object task.

Definition at line 239 of file Timer_Queue_Adapters.h.

template<class TQ>
TQ* ACE_Thread_Timer_Queue_Adapter< TQ >::timer_queue_ [private]
 

The underlying Timer_Queue.

Definition at line 217 of file Timer_Queue_Adapters.h.


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