Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Thread_Timer_Queue_Adapter Class Template Reference

Adapts a Timer_Queue using a separate thread for dispatching. More...

#include <Timer_Queue_Adapters.h>

Inheritance diagram for ACE_Thread_Timer_Queue_Adapter:

Inheritance graph
[legend]
Collaboration diagram for ACE_Thread_Timer_Queue_Adapter:

Collaboration graph
[legend]
List of all members.

Public Types

typedef TQ TIMER_QUEUE
 Trait for the underlying queue type. More...


Public Methods

 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. More...

virtual ~ACE_Thread_Timer_Queue_Adapter (void)
 Destructor. More...

long 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 <TQ>; wakes up the dispatching thread. More...

int 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. More...

virtual int svc (void)
 Runs the dispatching thread. More...

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

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

TQ & timer_queue (void)
 
Deprecated:
Access the implementation queue, useful for iteration. Use the method that returns a pointer instead.
More...


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

TQ * timer_queue (void) const
 Return the current <TQ>. More...

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

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. More...

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

ACE_SYNCH_RECURSIVE_MUTEX mutex_
 The mutual exclusion mechanism that is required to use the <condition_>. More...

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

ACE_thread_t thr_id_
 Thread id of our active object task. More...


Detailed Description

template<class TQ>
class ACE_Thread_Timer_Queue_Adapter< TQ >

Adapts a Timer_Queue using a separate thread for dispatching.

This implementation of a Timer_Queue 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 105 of file Timer_Queue_Adapters.h.


Member Typedef Documentation

template<class TQ>
typedef TQ ACE_Thread_Timer_Queue_Adapter::TIMER_QUEUE
 

Trait for the underlying queue type.

Definition at line 109 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 150 of file Timer_Queue_Adapters.cpp.

References ACE_NEW, delete_timer_queue_, and timer_queue_.

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

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

Destructor.

Definition at line 168 of file Timer_Queue_Adapters.cpp.

References delete_timer_queue_, and timer_queue_.

00169 {
00170   if (this->delete_timer_queue_)
00171     {
00172       delete this->timer_queue_;
00173       this->timer_queue_ = 0;
00174       this->delete_timer_queue_ = 0;
00175     }
00176 }


Member Function Documentation

template<class TQ>
ACE_INLINE 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 33 of file Timer_Queue_Adapters.i.

References ACE_hthread_t, ACE_thread_t, ACE_Task_Base::activate, ACE_Task_Base::grp_id, and ACE_Event_Handler::priority.

00043 {
00044   // Macros to avoid "warning: unused parameter" type warning.
00045   ACE_UNUSED_ARG (n_threads);
00046   ACE_UNUSED_ARG (force_active);
00047   ACE_UNUSED_ARG (thread_handles);
00048 
00049   // Make sure that we only allow a single thread to be spawned for
00050   // our adapter.  Otherwise, too many weird things can happen.
00051   return ACE_Task_Base::activate (flags, 1, 0, priority, grp_id, task, 0,
00052                                   stack, stack_size, thread_names);
00053 }

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

References ACE_GUARD_RETURN, ACE_SYNCH_RECURSIVE_MUTEX, condition_, and timer_queue_.

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

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

Inform the dispatching thread that it should terminate.

Definition at line 210 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD, ACE_SYNCH_RECURSIVE_MUTEX, active_, and condition_.

00211 {
00212   ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_);
00213 
00214   this->active_ = 0;
00215   this->condition_.signal ();
00216 }

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

References mutex_.

00180 {
00181   return this->mutex_;
00182 }

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 <TQ>; wakes up the dispatching thread.

Definition at line 186 of file Timer_Queue_Adapters.cpp.

References ACE_GUARD_RETURN, and ACE_SYNCH_RECURSIVE_MUTEX.

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

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

References ACE_GUARD_RETURN, ACE_PTHREAD_CLEANUP_POP, ACE_PTHREAD_CLEANUP_PUSH, ACE_SYNCH_RECURSIVE_MUTEX, active_, condition_, mutex_, ACE_Thread::self, thr_id_, and timer_queue_.

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

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 27 of file Timer_Queue_Adapters.i.

References thr_id_.

00028 {
00029   return this->thr_id_;
00030 }

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

Return the current <TQ>.

Definition at line 11 of file Timer_Queue_Adapters.i.

References timer_queue_.

00012 {
00013   return this->timer_queue_;
00014 }

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 17 of file Timer_Queue_Adapters.i.

References delete_timer_queue_, and timer_queue_.

00018 {
00019   if (this->delete_timer_queue_ != 0)
00020     delete this->timer_queue_;
00021   this->timer_queue_ = tq;
00022   this->delete_timer_queue_ = 0;
00023   return 0;
00024 }

template<class TQ>
ACE_INLINE TQ & ACE_Thread_Timer_Queue_Adapter< TQ >::timer_queue void   
 

Deprecated:
Access the implementation queue, useful for iteration. Use the method that returns a pointer instead.

Definition at line 5 of file Timer_Queue_Adapters.i.

References timer_queue_.

00006 {
00007   return *(this->timer_queue_);
00008 }


Member Data Documentation

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter::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 225 of file Timer_Queue_Adapters.h.

Referenced by deactivate, and svc.

template<class TQ>
ACE_SYNCH_RECURSIVE_CONDITION ACE_Thread_Timer_Queue_Adapter::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 221 of file Timer_Queue_Adapters.h.

Referenced by cancel, deactivate, and svc.

template<class TQ>
int ACE_Thread_Timer_Queue_Adapter::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 210 of file Timer_Queue_Adapters.h.

Referenced by ACE_Thread_Timer_Queue_Adapter, timer_queue, and ~ACE_Thread_Timer_Queue_Adapter.

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

The mutual exclusion mechanism that is required to use the <condition_>.

Definition at line 214 of file Timer_Queue_Adapters.h.

Referenced by mutex, and svc.

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

Thread id of our active object task.

Definition at line 228 of file Timer_Queue_Adapters.h.

Referenced by svc, and thr_id.

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

The underlying Timer_Queue.

Definition at line 206 of file Timer_Queue_Adapters.h.

Referenced by ACE_Thread_Timer_Queue_Adapter, cancel, svc, timer_queue, and ~ACE_Thread_Timer_Queue_Adapter.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:57:46 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002