Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends

ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK > Class Template Reference

Provides a simple implementation of timers. More...

#include <Timer_List_T.h>

Inheritance diagram for ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >:
Inheritance graph
[legend]
Collaboration diagram for ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef
ACE_Timer_List_Iterator_T
< TYPE, FUNCTOR, ACE_LOCK > 
Iterator
 Type of iterator.
typedef ACE_Timer_Node_T< TYPE > Node
typedef ACE_Timer_Queue_T
< TYPE, FUNCTOR, ACE_LOCK > 
Base
 Type inherited from.
typedef ACE_Free_List< NodeFreeList

Public Member Functions

 ACE_Timer_List_T (FUNCTOR *upcall_functor=0, FreeList *freelist=0)
virtual ~ACE_Timer_List_T (void)
 Destructor.
virtual bool is_empty (void) const
 True if queue is empty, else false.
virtual const ACE_Time_Valueearliest_time (void) const
virtual int reset_interval (long timer_id, const ACE_Time_Value &interval)
virtual int cancel (const TYPE &type, int dont_call_handle_close=1)
virtual int cancel (long timer_id, const void **act=0, int dont_call_handle_close=1)
virtual
ACE_Timer_Queue_Iterator_T
< TYPE, FUNCTOR, ACE_LOCK > & 
iter (void)
 Returns a pointer to this ACE_Timer_Queue's iterator.
virtual ACE_Timer_Node_T< TYPE > * remove_first (void)
 Removes the earliest node from the queue and returns it.
virtual void dump (void) const
 Dump the state of an object.
virtual void reschedule (ACE_Timer_Node_T< TYPE > *)
virtual ACE_Timer_Node_T< TYPE > * get_first (void)
 Reads the earliest node from the queue and returns it.

Private Member Functions

virtual long schedule_i (const TYPE &type, const void *act, const ACE_Time_Value &future_time, const ACE_Time_Value &interval)
void schedule_i (ACE_Timer_Node_T< TYPE > *n, const ACE_Time_Value &exp)
 The shared scheduling functionality between schedule() and reschedule().
ACE_Timer_Node_T< TYPE > * find_node (long timer_id) const
void cancel_i (ACE_Timer_Node_T< TYPE > *n)
 Shared subset of the two cancel() methods.
void unlink (ACE_Timer_Node_T< TYPE > *n)
ACE_Timer_Node_T< TYPE > * get_first_i (void) const
 ACE_Timer_List_T (const ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK > &)
void operator= (const ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK > &)

Private Attributes

ACE_Timer_Node_T< TYPE > * head_
 Pointer to linked list of <ACE_Timer_Handles>.
Iteratoriterator_
 Iterator used to expire timers.
long id_counter_

Friends

class ACE_Timer_List_Iterator_T< TYPE, FUNCTOR, ACE_LOCK >
 Iterator is a friend.

Detailed Description

template<class TYPE, class FUNCTOR, class ACE_LOCK>
class ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >

Provides a simple implementation of timers.

This implementation uses a linked list of absolute times. Therefore, in the average case, scheduling and canceling timers is O(N) (where N is the total number of timers) and expiring timers is O(K) (where K is the total number of timers that are < the current time of day). More clever implementations could use a delta-list, a heap, or timing wheels, etc. For instance, ACE_Timer_Heap is a subclass of ACE_Timer_List that implements a heap-based callout queue. For most applications, the ACE_Timer_Heap will perform substantially faster than the ACE_Timer_List.

Definition at line 85 of file Timer_List_T.h.


Member Typedef Documentation

template<class TYPE, class FUNCTOR, class ACE_LOCK>
typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK> ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::Base

Type inherited from.

Definition at line 96 of file Timer_List_T.h.

template<class TYPE, class FUNCTOR, class ACE_LOCK>
typedef ACE_Free_List<Node> ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::FreeList

Definition at line 97 of file Timer_List_T.h.

template<class TYPE, class FUNCTOR, class ACE_LOCK>
typedef ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::Iterator

Type of iterator.

Definition at line 89 of file Timer_List_T.h.

template<class TYPE, class FUNCTOR, class ACE_LOCK>
typedef ACE_Timer_Node_T<TYPE> ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::Node

Definition at line 94 of file Timer_List_T.h.


Constructor & Destructor Documentation

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::ACE_Timer_List_T ( FUNCTOR *  upcall_functor = 0,
FreeList freelist = 0 
)

Default constructor. upcall_functor is the instance of the FUNCTOR to be used by the list. If upcall_functor is 0, a default FUNCTOR will be created. freelist is the freelist of timer nodes. If 0, then a default freelist will be created.

Definition at line 83 of file Timer_List_T.cpp.

  : Base(uf, fl)
  , head_ (new ACE_Timer_Node_T<TYPE>)
  , id_counter_ (0)
{
  ACE_TRACE ("ACE_Timer_List_T::ACE_Timer_List_T");

  this->head_->set_next (this->head_);
  this->head_->set_prev (this->head_);

  ACE_NEW (iterator_, Iterator(*this));
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::~ACE_Timer_List_T ( void   )  [virtual]

Destructor.

Definition at line 123 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::~ACE_Timer_List_T");
  ACE_MT (ACE_GUARD (ACE_LOCK, ace_mon, this->mutex_));

  delete iterator_;

  if (!this->is_empty())
    {
      for (ACE_Timer_Node_T<TYPE>* n = this->get_first();
           n != this->head_;
           )
        {
          this->upcall_functor ().deletion (*this,
                                            n->get_type(),
                                            n->get_act());

          ACE_Timer_Node_T<TYPE> *next =
            n->get_next ();

          this->free_node (n);

          n = next;
        }
    }

  // delete the dummy node
  delete this->head_;
}

template<class TYPE, class FUNCTOR, class ACE_LOCK>
ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::ACE_Timer_List_T ( const ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK > &   )  [private]

Member Function Documentation

template<class TYPE , class FUNCTOR , class ACE_LOCK >
int ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::cancel ( const TYPE &  type,
int  dont_call_handle_close = 1 
) [virtual]

Cancel all timers associated with type. If dont_call_handle_close is 0 then the functor will be invoked. Returns the number of timers cancelled.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 314 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::cancel");

  int num_canceled = 0; // Note : Technically this can overflow.

  int cookie = 0;

  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));

  if (!this->is_empty ())
    {
      for (ACE_Timer_Node_T<TYPE>* n = this->get_first();
           n != this->head_;
           )
        {
          if (n->get_type() == type) // Note: Typically Type is an ACE_Event_Handler*
            {
              ++num_canceled;

              ACE_Timer_Node_T<TYPE>* tmp = n;
              n = n->get_next();

              this->cancel_i (tmp);
            }
          else
            {
              n = n->get_next();
            }
        }
    }

  // Call the close hooks.

  // cancel_type() called once per <type>.
  this->upcall_functor ().cancel_type (*this,
                                       type,
                                       skip_close,
                                       cookie);

  for (int i = 0;
       i < num_canceled;
       ++i)
    {
      // cancel_timer() called once per <timer>.
      this->upcall_functor ().cancel_timer (*this,
                                            type,
                                            skip_close,
                                            cookie);
    }

  return num_canceled;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
int ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::cancel ( long  timer_id,
const void **  act = 0,
int  dont_call_handle_close = 1 
) [virtual]

Cancel the single timer that matches the timer_id value (which was returned from the <schedule> method). If act is non-NULL then it will be set to point to the ``magic cookie'' argument passed in when the timer was registered. This makes it possible to free up the memory and avoid memory leaks. If <dont_call> is 0 then the <functor> will be invoked. Returns 1 if cancellation succeeded and 0 if the timer_id wasn't found.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 277 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::cancel");
  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
  ACE_Timer_Node_T<TYPE>* n = this->find_node(timer_id);
  if (n != 0)
    {
      if (act != 0)
        *act = n->get_act ();

      // Call the close hooks.
      int cookie = 0;

      // cancel_type() called once per <type>.
      this->upcall_functor ().cancel_type (*this,
                                           n->get_type (),
                                           skip_close,
                                           cookie);

      // cancel_timer() called once per <timer>.
      this->upcall_functor ().cancel_timer (*this,
                                            n->get_type (),
                                            skip_close,
                                            cookie);

      this->cancel_i (n);

      return 1;
    }

  return 0;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::cancel_i ( ACE_Timer_Node_T< TYPE > *  n  )  [private]

Shared subset of the two cancel() methods.

Definition at line 379 of file Timer_List_T.cpp.

{
  this->unlink (n);
  this->free_node (n);
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::dump ( void   )  const [virtual]

Dump the state of an object.

Reimplemented from ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 154 of file Timer_List_T.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Timer_List_T::dump");
  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  int count = 0;

  ACE_Timer_Node_T<TYPE>* n = this->get_first_i();
  if (n != 0) {
    for (; n != this->head_; n = n->get_next()) {
      ++count;
    }
  }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nsize_ = %d"), count));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
const ACE_Time_Value & ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::earliest_time ( void   )  const [virtual]

Returns the time of the earlier node in the ACE_Timer_List. Must be called on a non-empty queue.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 110 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::earliest_time");
  ACE_Timer_Node_T<TYPE>* first = this->get_first_i();
  if (first != 0)
    return first->get_timer_value ();
  return ACE_Time_Value::zero;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_Node_T< TYPE > * ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::find_node ( long  timer_id  )  const [private]

Definition at line 245 of file Timer_List_T.cpp.

{
  ACE_Timer_Node_T<TYPE>* n = this->get_first_i();
  if (n == 0)
    return 0;

  for (; n != this->head_; n = n->get_next()) {
    if (n->get_timer_id() == timer_id) {
      return n;
    }
  }
  return 0;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_Node_T< TYPE > * ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::get_first ( void   )  [virtual]

Reads the earliest node from the queue and returns it.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 387 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::get_first");
  return this->get_first_i();
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_Node_T< TYPE > * ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::get_first_i ( void   )  const [private]

Definition at line 394 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::get_first_i");
  ACE_Timer_Node_T<TYPE>* first = this->head_->get_next();
  if (first != this->head_) // Note : is_empty() uses get_first()
    return first;
  return 0;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
bool ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::is_empty ( void   )  const [virtual]

True if queue is empty, else false.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 100 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::is_empty");
  return this->get_first_i() == 0;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_Queue_Iterator_T< TYPE, FUNCTOR, ACE_LOCK > & ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::iter ( void   )  [virtual]

Returns a pointer to this ACE_Timer_Queue's iterator.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 74 of file Timer_List_T.cpp.

{
  this->iterator_->first ();
  return *this->iterator_;
}

template<class TYPE, class FUNCTOR, class ACE_LOCK>
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::operator= ( const ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK > &   )  [private]
template<class TYPE , class FUNCTOR , class ACE_LOCK >
ACE_Timer_Node_T< TYPE > * ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::remove_first ( void   )  [virtual]

Removes the earliest node from the queue and returns it.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 407 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::remove_first");
  ACE_Timer_Node_T<TYPE>* first = this->get_first();
  if (first != 0) {
    this->unlink(first);
    return first;
  }
  return 0;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::reschedule ( ACE_Timer_Node_T< TYPE > *  n  )  [virtual]

Reschedule an "interval" ACE_Timer_Node_T. This should be private but for now it needs to be public for <ACE_Timer_Hash_T>

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 179 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::reschedule");
  this->schedule_i(n, n->get_timer_value());
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
int ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::reset_interval ( long  timer_id,
const ACE_Time_Value interval 
) [virtual]

Resets the interval of the timer represented by timer_id to interval, which is specified in relative time to the current <gettimeofday>. If interval is equal to ACE_Time_Value::zero, the timer will become a non-rescheduling timer. Returns 0 if successful, -1 if not.

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 261 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::reset_interval");
  ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1));
  ACE_Timer_Node_T<TYPE>* n = this->find_node(timer_id);
  if (n != 0) {
    n->set_interval(interval); // The interval will take effect the next time this node is expired.
    return 0;
  }
  return -1;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
long ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::schedule_i ( const TYPE &  type,
const void *  act,
const ACE_Time_Value future_time,
const ACE_Time_Value interval 
) [private, virtual]

Schedule type that will expire at future_time, which is specified in absolute time. If it expires then act is passed in as the value to the <functor>. If interval is != to ACE_Time_Value::zero then it is used to reschedule the type automatically, using relative time to the current <gettimeofday>. This method returns a <timer_id> that uniquely identifies the the type entry in an internal list. This <timer_id> can be used to cancel the timer before it expires. The cancellation ensures that <timer_ids> are unique up to values of greater than 2 billion timers. As long as timers don't stay around longer than this there should be no problems with accidentally deleting the wrong timer. Returns -1 on failure (which is guaranteed never to be a valid <timer_id>).

Implements ACE_Timer_Queue_T< TYPE, FUNCTOR, ACE_LOCK >.

Definition at line 190 of file Timer_List_T.cpp.

{
  ACE_TRACE ("ACE_Timer_List_T::schedule_i");

  ACE_Timer_Node_T<TYPE>* n = this->alloc_node();

  if (n != 0)
  {
    long id = this->id_counter_++;

    if (id != -1) {
      n->set (type, act, future_time, interval, 0, 0, id);
      this->schedule_i (n, future_time);
    }
    return id;
  }

  // Failure return
  errno = ENOMEM;
  return -1;
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::schedule_i ( ACE_Timer_Node_T< TYPE > *  n,
const ACE_Time_Value exp 
) [private]

The shared scheduling functionality between schedule() and reschedule().

Definition at line 217 of file Timer_List_T.cpp.

{
  if (this->is_empty()) {
    n->set_prev(this->head_);
    n->set_next(this->head_);
    this->head_->set_prev(n);
    this->head_->set_next(n);
    return;
  }

  // We always want to search backwards from the tail of the list, because
  // this minimizes the search in the extreme case when lots of timers are
  // scheduled for exactly the same time, and it also assumes that most of
  // the timers will be scheduled later than existing timers.
  ACE_Timer_Node_T<TYPE>* p = this->head_->get_prev();
  while (p != this->head_ && p->get_timer_value() > expire)
    p = p->get_prev();

  // insert after
  n->set_prev(p);
  n->set_next(p->get_next());
  p->get_next()->set_prev(n);
  p->set_next(n);
}

template<class TYPE , class FUNCTOR , class ACE_LOCK >
void ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::unlink ( ACE_Timer_Node_T< TYPE > *  n  )  [private]

Definition at line 369 of file Timer_List_T.cpp.

{
  n->get_prev()->set_next(n->get_next());
  n->get_next()->set_prev(n->get_prev());
  n->set_prev(0);
  n->set_next(0);
}


Friends And Related Function Documentation

template<class TYPE, class FUNCTOR, class ACE_LOCK>
friend class ACE_Timer_List_Iterator_T< TYPE, FUNCTOR, ACE_LOCK > [friend]

Iterator is a friend.

Definition at line 92 of file Timer_List_T.h.


Member Data Documentation

template<class TYPE, class FUNCTOR, class ACE_LOCK>
ACE_Timer_Node_T<TYPE>* ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::head_ [private]

Pointer to linked list of <ACE_Timer_Handles>.

Definition at line 200 of file Timer_List_T.h.

template<class TYPE, class FUNCTOR, class ACE_LOCK>
long ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::id_counter_ [private]

Keeps track of the timer id that uniquely identifies each timer. This id can be used to cancel a timer via the <cancel(long)> method.

Definition at line 210 of file Timer_List_T.h.

template<class TYPE, class FUNCTOR, class ACE_LOCK>
Iterator* ACE_Timer_List_T< TYPE, FUNCTOR, ACE_LOCK >::iterator_ [private]

Iterator used to expire timers.

Definition at line 203 of file Timer_List_T.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines