diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1999-07-03 18:26:32 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1999-07-03 18:26:32 +0000 |
commit | bfb0431fbddd07c6a3f7a98643966b7c662bb53e (patch) | |
tree | 4cf3ea03c84abac2a37ee8bfc4ec6cf47226b5bd /ace/Timer_Wheel_T.cpp | |
parent | 47ae10a45901542f1c9e7f37fa51626663605f98 (diff) | |
download | ATCD-bfb0431fbddd07c6a3f7a98643966b7c662bb53e.tar.gz |
ChangeLogTag:Sat Jul 3 12:05:46 1999 Douglas C. Schmidt <schmidt@tango.cs.wustl.edu>
Diffstat (limited to 'ace/Timer_Wheel_T.cpp')
-rw-r--r-- | ace/Timer_Wheel_T.cpp | 153 |
1 files changed, 81 insertions, 72 deletions
diff --git a/ace/Timer_Wheel_T.cpp b/ace/Timer_Wheel_T.cpp index c654cd7d616..ff1557b2660 100644 --- a/ace/Timer_Wheel_T.cpp +++ b/ace/Timer_Wheel_T.cpp @@ -40,10 +40,11 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::first (void) this->pos_++) { // Skip over empty entries - if (this->timer_wheel_.wheel_[this->pos_]->get_next () != - this->timer_wheel_.wheel_[this->pos_]) + if (this->timer_wheel_.wheel_[this->pos_]->get_next () + != this->timer_wheel_.wheel_[this->pos_]) { - this->list_item_ = this->timer_wheel_.wheel_[this->pos_]->get_next (); + this->list_item_ = + this->timer_wheel_.wheel_[this->pos_]->get_next (); return; } } @@ -52,7 +53,6 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::first (void) this->list_item_ = 0; } - // Positions the iterator at the next node in list or goes to the next // list @@ -62,18 +62,22 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::next (void) if (this->isdone ()) return; - this->list_item_ = this->list_item_->get_next (); + this->list_item_ = + this->list_item_->get_next (); // If there is no more in the current list, go to the next if (this->list_item_ == this->timer_wheel_.wheel_[this->pos_]) { - for (this->pos_++; this->pos_ < this->timer_wheel_.wheel_size_; this->pos_++) + for (this->pos_++; + this->pos_ < this->timer_wheel_.wheel_size_; + this->pos_++) { // Check for an empty entry - if (this->timer_wheel_.wheel_[this->pos_]->get_next () != - this->timer_wheel_.wheel_[this->pos_]) + if (this->timer_wheel_.wheel_[this->pos_]->get_next () + != this->timer_wheel_.wheel_[this->pos_]) { - this->list_item_ = this->timer_wheel_.wheel_[this->pos_]->get_next (); + this->list_item_ = + this->timer_wheel_.wheel_[this->pos_]->get_next (); return; } } @@ -82,7 +86,6 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::next (void) } } - // Returns true when we are at the end (when list_item_ == 0) template <class TYPE, class FUNCTOR, class ACE_LOCK> int @@ -91,7 +94,6 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::isdone (void) return this->list_item_ == 0; } - // Returns the node at the current position in the sequence template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> * @@ -103,9 +105,8 @@ ACE_Timer_Wheel_Iterator_T<TYPE, FUNCTOR, ACE_LOCK>::item (void) return this->list_item_; } - -// Constructor that sets up the timing wheel and also may preallocate some -// nodes on the free list +// Constructor that sets up the timing wheel and also may preallocate +// some nodes on the free list template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (size_t wheelsize, @@ -124,13 +125,14 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (size_t wheelsize, this->gettimeofday (ACE_OS::gettimeofday); // Create the timing wheel - ACE_NEW (this->wheel_, (ACE_Timer_Node_T<TYPE> *[wheelsize])); - + ACE_NEW (this->wheel_, + ACE_Timer_Node_T<TYPE> *[wheelsize]); // Create the dummy nodes for (i = 0; i < wheelsize; i++) { - ACE_Timer_Node_T<TYPE> *tempnode = this->alloc_node (); + ACE_Timer_Node_T<TYPE> *tempnode = + this->alloc_node (); tempnode->set_next (tempnode); tempnode->set_prev (tempnode); this->wheel_[i] = tempnode; @@ -139,12 +141,13 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (size_t wheelsize, // Do the preallocation this->free_list_->resize (prealloc); - iterator_ = new WHEEL_ITERATOR(*this); + ACE_NEW (iterator_, + WHEEL_ITERATOR (*this)); } template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (FUNCTOR *upcall_functor, - ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist) + ACE_Free_List<ACE_Timer_Node_T <TYPE> > *freelist) : ACE_Timer_Queue_T<TYPE,FUNCTOR,ACE_LOCK> (upcall_functor, freelist), wheel_size_ (ACE_DEFAULT_TIMER_WHEEL_SIZE), resolution_ (ACE_DEFAULT_TIMER_WHEEL_RESOLUTION), @@ -156,7 +159,8 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (FUNCTOR *upcall_f this->gettimeofday (ACE_OS::gettimeofday); // Create the timing wheel - ACE_NEW (this->wheel_, (ACE_Timer_Node_T<TYPE> *[this->wheel_size_])); + ACE_NEW (this->wheel_, + ACE_Timer_Node_T<TYPE> *[this->wheel_size_]); // Create the dummy nodes for (i = 0; i < this->wheel_size_; i++) @@ -167,7 +171,8 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::ACE_Timer_Wheel_T (FUNCTOR *upcall_f this->wheel_[i] = tempnode; } - iterator_ = new WHEEL_ITERATOR(*this); + ACE_NEW (iterator_, + WHEEL_ITERATOR (*this)); } // Destructor just cleans up its memory @@ -179,15 +184,20 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Wheel_T (void) delete iterator_; - for (size_t i = 0; i < this->wheel_size_; i++) + for (size_t i = 0; + i < this->wheel_size_; + i++) { // delete nodes until only the dummy node is left while (this->wheel_[i]->get_next () != this->wheel_[i]) { - ACE_Timer_Node_T<TYPE> *next = this->wheel_[i]->get_next (); + ACE_Timer_Node_T<TYPE> *next = + this->wheel_[i]->get_next (); this->wheel_[i]->set_next (next->get_next ()); next->get_next ()->set_prev (this->wheel_[i]); - this->upcall_functor ().deletion (*this, next->get_type (), next->get_act ()); + this->upcall_functor ().deletion (*this, + next->get_type (), + next->get_act ()); this->free_node (next); } @@ -199,7 +209,6 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::~ACE_Timer_Wheel_T (void) delete [] this->wheel_; } - // Checks to see if <earliest_pos> points to a empty list (then it is empty) template <class TYPE, class FUNCTOR, class ACE_LOCK> int @@ -210,7 +219,6 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::is_empty (void) const return this->wheel_[this->earliest_pos_]->get_next () == this->wheel_[this->earliest_pos_]; } - // Returns the first (earliest) node in the <wheel_>'s <earliest_pos_> list template <class TYPE, class FUNCTOR, class ACE_LOCK> const ACE_Time_Value & @@ -224,8 +232,8 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::earliest_time (void) const return this->wheel_[this->earliest_pos_]->get_next ()->get_timer_value (); } -// Create the node and pass it to reschedule. Also check to see if the -// <earliest_pos> should be changed. +// Create the node and pass it to reschedule. Also check to see if +// the <earliest_pos> should be changed. template <class TYPE, class FUNCTOR, class ACE_LOCK> long ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::schedule (const TYPE &type, @@ -262,13 +270,12 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::schedule (const TYPE &type, return -1; } - // Goes through every list in the wheel and if it finds a node with <type> // then it removes the node and continues on looking for other nodes template <class TYPE, class FUNCTOR, class ACE_LOCK> int ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (const TYPE &type, - int dont_call_handle_close) + int dont_call_handle_close) { ACE_TRACE ("ACE_Timer_Wheel_T::cancel"); ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1)); @@ -299,15 +306,14 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (const TYPE &type, this->free_node (tempnode); } else - { - curr = curr->get_next (); - } + curr = curr->get_next (); } } // Look for a new earliest time - ACE_Time_Value earliest_time; // defaults to zero + // Defaults to zero. + ACE_Time_Value earliest_time; // Check every entry in the table for (i = 0; i < this->wheel_size_; i++) @@ -319,36 +325,37 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (const TYPE &type, if (earliest_time == ACE_Time_Value::zero || this->wheel_[i]->get_timer_value () < earliest_time) { - earliest_time = this->wheel_[i]->get_next ()->get_timer_value (); + earliest_time = + this->wheel_[i]->get_next ()->get_timer_value (); this->earliest_pos_ = i; } } } if (dont_call_handle_close == 0) - this->upcall_functor ().cancellation (*this, type); - + this->upcall_functor ().cancellation (*this, + type); return number_of_cancellations; } - // Takes the <timer_id> and casts it to a pointer. Then it removes it // from its neighbors template <class TYPE, class FUNCTOR, class ACE_LOCK> int ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (long timer_id, - const void **act, - int dont_call_handle_close) + const void **act, + int dont_call_handle_close) { ACE_TRACE ("ACE_Timer_Wheel_T::cancel"); ACE_MT (ACE_GUARD_RETURN (ACE_LOCK, ace_mon, this->mutex_, -1)); // Make sure we are getting a valid <timer_id>, not an error - // returned by schedule () + // returned by <schedule>. if (timer_id == -1) return 0; - ACE_Timer_Node_T<TYPE> *node = (ACE_Timer_Node_T<TYPE> *) timer_id; + ACE_Timer_Node_T<TYPE> *node = + (ACE_Timer_Node_T<TYPE> *) timer_id; // Check to see if the node looks like a true ACE_Timer_Node_T<TYPE> if (timer_id == node->get_timer_id ()) @@ -360,9 +367,10 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (long timer_id, *act = node->get_act (); if (dont_call_handle_close == 0) - this->upcall_functor ().cancellation (*this, node->get_type ()); + this->upcall_functor ().cancellation (*this, + node->get_type ()); - // Find out what position it is in + // Find out what position it is in. size_t pos = (node->get_timer_value ().usec () / this->resolution_) % this->wheel_size_; this->free_node (node); @@ -383,7 +391,8 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (long timer_id, if (earliest_time == ACE_Time_Value::zero || this->wheel_[i]->get_timer_value () < earliest_time) { - earliest_time = this->wheel_[i]->get_next ()->get_timer_value (); + earliest_time = + this->wheel_[i]->get_next ()->get_timer_value (); this->earliest_pos_ = i; } } @@ -397,7 +406,6 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::cancel (long timer_id, return 0; } - // Dumps out some properties of this object template <class TYPE, class FUNCTOR, class ACE_LOCK> void @@ -424,7 +432,6 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::dump (void) const ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP)); } - // Removes the earliest node and then find the new <earliest_pos_> template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> * @@ -433,7 +440,8 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::remove_first (void) ACE_TRACE ("ACE_Timer_Wheel_T::remove_first"); // Remove the item - ACE_Timer_Node_T<TYPE> *temp = this->wheel_[this->earliest_pos_]->get_next (); + ACE_Timer_Node_T<TYPE> *temp = + this->wheel_[this->earliest_pos_]->get_next (); temp->get_prev ()->set_next (temp->get_next ()); temp->get_next ()->set_prev (temp->get_prev ()); @@ -449,17 +457,16 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::remove_first (void) if (earliest_time == ACE_Time_Value::zero || this->wheel_[i]->get_timer_value () < earliest_time) { - earliest_time = this->wheel_[i]->get_next ()->get_timer_value (); + earliest_time = + this->wheel_[i]->get_next ()->get_timer_value (); this->earliest_pos_ = i; } } } - return temp; } - // Returns the earliest node without removing it template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Node_T<TYPE> * @@ -470,25 +477,26 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::get_first (void) return this->wheel_[this->earliest_pos_]->get_next (); } - -// Takes an ACE_Timer_Node and inserts it into the correct position in the correct -// list +// Takes an ACE_Timer_Node and inserts it into the correct position in +// the correct list. template <class TYPE, class FUNCTOR, class ACE_LOCK> void ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::reschedule (ACE_Timer_Node_T<TYPE> *expired) { ACE_TRACE ("ACE_Timer_Wheel_T::reschedule"); - size_t pos = (expired->get_timer_value ().usec () / this->resolution_) % this->wheel_size_; + size_t pos = + (expired->get_timer_value ().usec () / this->resolution_) % this->wheel_size_; // See if we need to update the earliest time if (this->earliest_time () == ACE_Time_Value::zero || expired->get_timer_value () < this->earliest_time ()) this->earliest_pos_ = pos; - // Insert time into dummy node + // Insert time into dummy node. this->wheel_[pos]->set_timer_value (expired->get_timer_value ()); - ACE_Timer_Node_T<TYPE> *cursor = this->wheel_[pos]->get_next (); + ACE_Timer_Node_T<TYPE> *cursor = + this->wheel_[pos]->get_next (); // Find position to insert while (cursor->get_timer_value () < expired->get_timer_value ()) @@ -501,7 +509,6 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::reschedule (ACE_Timer_Node_T<TYPE> * expired->get_prev ()->set_next (expired); } - // Just return the iterator template <class TYPE, class FUNCTOR, class ACE_LOCK> ACE_Timer_Queue_Iterator_T<TYPE, FUNCTOR, ACE_LOCK> & @@ -519,11 +526,11 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::expire () return ACE_Timer_Queue_T<TYPE,FUNCTOR,ACE_LOCK>::expire (); } - -// Specialized expire which expires in total order. It is optimized by keeping -// track of the list with the earliest element and the next earliest list. It -// then goes through the earliest list until it can switch to the second list. -// it keeps going until it finishes with everything before the <cur_time> +// Specialized expire which expires in total order. It is optimized +// by keeping track of the list with the earliest element and the next +// earliest list. It then goes through the earliest list until it can +// switch to the second list. it keeps going until it finishes with +// everything before the <cur_time> template <class TYPE, class FUNCTOR, class ACE_LOCK> int ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::expire (const ACE_Time_Value &cur_time) @@ -570,11 +577,14 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::expire (const ACE_Time_Value &cur_ti } } - while (this->wheel_[earliest]->get_next () != this->wheel_[earliest] - && this->wheel_[earliest]->get_next ()->get_timer_value () <= next_earliest_time) + while (this->wheel_[earliest]->get_next () + != this->wheel_[earliest] + && this->wheel_[earliest]->get_next ()->get_timer_value () + <= next_earliest_time) { // Remove the first node in the earliest position - ACE_Timer_Node_T<TYPE> *expired = this->wheel_[earliest]->get_next (); + ACE_Timer_Node_T<TYPE> *expired = + this->wheel_[earliest]->get_next (); this->wheel_[earliest]->set_next (expired->get_next ()); expired->get_next ()->set_prev (this->wheel_[earliest]); @@ -591,22 +601,22 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::expire (const ACE_Time_Value &cur_ti expired->set_timer_value (expired->get_timer_value () + expired->get_interval ()); while (expired->get_timer_value () <= cur_time); - // Since this is an interval timer, we need to reschedule - // it. + // Since this is an interval timer, we need to + // reschedule it. this->reschedule (expired); reclaim = 0; } - // call the functor + // Call the functor. this->upcall (type, act, cur_time); if (reclaim) - // Free up the node and the token + // Free up the node and the token. this->free_node (expired); ++number_of_timers_expired; - // Check to see if we are empty + // Check to see if we are empty. if (this->wheel_[earliest]->get_next () == this->wheel_[earliest]) break; } @@ -620,5 +630,4 @@ ACE_Timer_Wheel_T<TYPE, FUNCTOR, ACE_LOCK>::expire (const ACE_Time_Value &cur_ti return number_of_timers_expired; } - #endif /* ACE_TIMER_WHEEL_T_C */ |