#include <Timer_Queue_Adapters.h>
Inheritance diagram for ACE_Thread_Timer_Queue_Adapter< TQ >:


| 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. | |
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.
Definition at line 123 of file Timer_Queue_Adapters.h.
| 
 | |||||
| Trait for the underlying queue type. 
 Definition at line 127 of file Timer_Queue_Adapters.h. | 
| 
 | ||||||||||||||||
| 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 158 of file Timer_Queue_Adapters.cpp. References ACE_NEW. 
 00160 : ACE_Task_Base (tm), 00161 timer_queue_(timer_queue), 00162 delete_timer_queue_(0), 00163 condition_ (mutex_), 00164 active_ (1), // Assume that we start in active mode. 00165 thr_id_ (ACE_OS::NULL_thread) 00166 { 00167 if (timer_queue_ == 0) 00168 { 00169 ACE_NEW (this->timer_queue_, 00170 TQ); 00171 this->delete_timer_queue_ = 1; 00172 } 00173 } | 
| 
 | ||||||||||
| Destructor. 
 Definition at line 176 of file Timer_Queue_Adapters.cpp. 
 00177 {
00178   if (this->delete_timer_queue_)
00179     {
00180       delete this->timer_queue_;
00181       this->timer_queue_ = 0;
00182       this->delete_timer_queue_ = 0;
00183     }
00184 }
 | 
| 
 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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 294 of file Timer_Queue_Adapters.cpp. References ACE_hthread_t, and ACE_Task_Base::activate(). 
 00304 {
00305   // Macros to avoid "warning: unused parameter" type warning.
00306   ACE_UNUSED_ARG (thread_handles);
00307 
00308   // Make sure to set this flag in case we were deactivated earlier.
00309   this->active_ = 1;
00310 
00311   // Make sure that we only allow a single thread to be spawned for
00312   // our adapter.  Otherwise, too many weird things can happen.
00313   return ACE_Task_Base::activate (flags, 1, 0, priority, grp_id, task, 0,
00314                                   stack, stack_size, thread_names);
00315 }
 | 
| 
 | ||||||||||||||||
| Cancel the timer_id and return the act parameter if an address is passed in. Also wakes up the dispatching thread. Definition at line 207 of file Timer_Queue_Adapters.cpp. References ACE_GUARD_RETURN, and ACE_SYNCH_RECURSIVE_MUTEX. 
 00209 {
00210   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00211 
00212   int result = this->timer_queue_->cancel (timer_id, act);
00213   condition_.signal ();
00214   return result;
00215 }
 | 
| 
 | ||||||||||
| Inform the dispatching thread that it should terminate. 
 Definition at line 218 of file Timer_Queue_Adapters.cpp. References ACE_GUARD, and ACE_SYNCH_RECURSIVE_MUTEX. 
 00219 {
00220   ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_);
00221 
00222   this->active_ = 0;
00223   this->condition_.signal ();
00224 }
 | 
| 
 | ||||||||||
| Access the locking mechanism, useful for iteration. 
 Definition at line 187 of file Timer_Queue_Adapters.cpp. 
 00188 {
00189   return this->mutex_;
00190 }
 | 
| 
 | ||||||||||||||||||||||||
| Schedule the timer according to the semantics of the ; wakes up the dispatching thread. Definition at line 194 of file Timer_Queue_Adapters.cpp. References ACE_GUARD_RETURN, and ACE_SYNCH_RECURSIVE_MUTEX. 
 00198 {
00199   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00200 
00201   long result = this->timer_queue_->schedule (handler, act, future_time, interval);
00202   this->condition_.signal ();
00203   return result;
00204 }
 | 
| 
 | ||||||||||
| Runs the dispatching thread. 
 Reimplemented from ACE_Task_Base. Definition at line 227 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(). 
 00228 {
00229   ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mutex_, -1);
00230 
00231   this->thr_id_ = ACE_Thread::self ();
00232 
00233   // Thread cancellation point, if ACE supports it.
00234   //
00235   // Note: This call generates a warning under Solaris because the header
00236   //       file /usr/include/pthread.h redefines the routine argument. This
00237   //       is a bug in the Solaris header files and has nothing to do with
00238   //       ACE.
00239 # if !defined (ACE_LACKS_PTHREAD_CANCEL)
00240   ACE_PTHREAD_CLEANUP_PUSH (&this->condition_.mutex ().get_nesting_mutex ());
00241 # endif /* ACE_LACKS_PTHREAD_CANCEL */
00242 
00243   while (this->active_)
00244     {
00245 # if defined (ACE_HAS_DEFERRED_TIMER_COMMANDS)
00246       // Temporarily suspend ownership of the timer queue mutex in
00247       // order to dispatch deferred execution commands.  These
00248       // commands are to be treated as executing in a context
00249       // "external" to the timer queue adapter, and thus must compete
00250       // separately for this lock.
00251       mutex_.release ();
00252       this->dispatch_commands ();
00253 
00254       // Re-acquire ownership of the timer queue mutex in order to
00255       // restore the "internal" timer queue adapter context
00256       mutex_.acquire ();
00257 # endif /* ACE_HAS_DEFERRED_TIMER_COMMANDS */
00258 
00259       // If the queue is empty, sleep until there is a change on it.
00260       if (this->timer_queue_->is_empty ())
00261         this->condition_.wait ();
00262       else
00263         {
00264           // Compute the remaining time, being careful not to sleep
00265           // for "negative" amounts of time.
00266           ACE_Time_Value const tv_curr = this->timer_queue_->gettimeofday ();
00267           ACE_Time_Value const tv_earl = this->timer_queue_->earliest_time ();
00268 
00269           if (tv_earl > tv_curr)
00270             {
00271               // The earliest time on the Timer_Queue is in future, so
00272               // use ACE_OS::gettimeofday() to convert the tv to the
00273               // absolute time.
00274               ACE_Time_Value const tv = ACE_OS::gettimeofday () + (tv_earl - tv_curr);
00275               // ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("waiting until %u.%3.3u secs\n"),
00276               // tv.sec(), tv.msec()));
00277               this->condition_.wait (&tv);
00278             }
00279         }
00280 
00281       // Expire timers anyway, at worst this is a no-op.
00282       this->timer_queue_->expire ();
00283     }
00284 
00285    // Thread cancellation point, if ACE supports it.
00286 # if !defined (ACE_LACKS_PTHREAD_CANCEL)
00287   ACE_PTHREAD_CLEANUP_POP (0);
00288 # endif /* ACE_LACKS_PTHREAD_CANCEL */
00289 
00290   return 0;
00291 }
 | 
| 
 | ||||||||||
| 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 }
 | 
| 
 | ||||||||||
| Return the current . 
 Definition at line 8 of file Timer_Queue_Adapters.inl. 
 00009 {
00010   return this->timer_queue_;
00011 }
 | 
| 
 | ||||||||||
| 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 }
 | 
| 
 | |||||
| When deactivate is called this variable turns to false and the dispatching thread is signalled, to terminate its main loop. Definition at line 239 of file Timer_Queue_Adapters.h. | 
| 
 | |||||
| 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 235 of file Timer_Queue_Adapters.h. | 
| 
 | |||||
| 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 224 of file Timer_Queue_Adapters.h. | 
| 
 | |||||
| The mutual exclusion mechanism that is required to use the . Definition at line 228 of file Timer_Queue_Adapters.h. | 
| 
 | |||||
| Thread id of our active object task. 
 Definition at line 242 of file Timer_Queue_Adapters.h. | 
| 
 | |||||
| The underlying Timer_Queue. 
 Definition at line 220 of file Timer_Queue_Adapters.h. | 
 1.3.6
 
1.3.6