Priority_Reactor.cpp

Go to the documentation of this file.
00001 // Priority_Reactor.cpp,v 4.17 2006/04/19 19:13:09 jwillemsen Exp
00002 
00003 #include "ace/Priority_Reactor.h"
00004 #include "ace/Malloc_T.h"
00005 
00006 ACE_RCSID(ace, Priority_Reactor, "Priority_Reactor.cpp,v 4.17 2006/04/19 19:13:09 jwillemsen Exp")
00007 
00008 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00009 
00010 typedef ACE_Unbounded_Queue_Iterator<ACE_Event_Tuple> QUEUE_ITERATOR;
00011 // Its iterator.
00012 
00013 typedef ACE_Cached_Allocator<ACE_Node<ACE_Event_Tuple>, ACE_SYNCH_NULL_MUTEX> TUPLE_ALLOCATOR;
00014 // Defines the memory allocator used, no need for locking because it
00015 // is only used in one thread of control.
00016 
00017 ACE_ALLOC_HOOK_DEFINE(ACE_Priority_Reactor)
00018 
00019 // Initialize ACE_Select_Reactor.
00020 
00021 #define npriorities \
00022         ACE_Event_Handler::HI_PRIORITY-ACE_Event_Handler::LO_PRIORITY+1
00023 
00024 void
00025 ACE_Priority_Reactor::init_bucket (void)
00026 {
00027   // Allocate enough space for all the handles.
00028   // TODO: This can be wrong, maybe we should use other kind of
00029   // allocator here?
00030   ACE_NEW (this->tuple_allocator_,
00031            TUPLE_ALLOCATOR (ACE_Select_Reactor::DEFAULT_SIZE));
00032 
00033   // The event handlers are assigned to a new As the Event
00034   ACE_NEW (this->bucket_,
00035            QUEUE *[npriorities]);
00036 
00037   // This loops "ensures" exception safety.
00038   for (int i = 0; i < npriorities; ++i)
00039     ACE_NEW (this->bucket_[i],
00040              QUEUE (this->tuple_allocator_));
00041 }
00042 
00043 ACE_Priority_Reactor::ACE_Priority_Reactor (ACE_Sig_Handler *sh,
00044                                             ACE_Timer_Queue *tq)
00045   : ACE_Select_Reactor(sh, tq),
00046     bucket_ (0),
00047     tuple_allocator_ (0)
00048 {
00049   ACE_TRACE ("ACE_Priority_Reactor::ACE_Priority_Reactor");
00050   this->init_bucket ();
00051 }
00052 
00053 ACE_Priority_Reactor::ACE_Priority_Reactor (size_t size,
00054                                             int rs,
00055                                             ACE_Sig_Handler *sh,
00056                                             ACE_Timer_Queue *tq)
00057   : ACE_Select_Reactor (size, rs, sh, tq),
00058     bucket_ (0),
00059     tuple_allocator_ (0)
00060 {
00061   ACE_TRACE ("ACE_Priority_Reactor::ACE_Priority_Reactor");
00062   this->init_bucket ();
00063 }
00064 
00065 ACE_Priority_Reactor::~ACE_Priority_Reactor (void)
00066 {
00067   ACE_TRACE ("ACE_Priority_Reactor::~ACE_Priority_Reactor");
00068 
00069   for (int i = 0; i < npriorities; ++i)
00070     delete this->bucket_[i];
00071 
00072   delete[] this->bucket_;
00073   delete tuple_allocator_;
00074 }
00075 
00076 void
00077 ACE_Priority_Reactor::build_bucket (ACE_Handle_Set &dispatch_mask,
00078                                     int &min_priority,
00079                                     int &max_priority)
00080 {
00081   ACE_Handle_Set_Iterator handle_iter (dispatch_mask);
00082 
00083   for (ACE_HANDLE handle;
00084        (handle = handle_iter ()) != ACE_INVALID_HANDLE;
00085        )
00086     {
00087       ACE_Event_Tuple et (this->handler_rep_.find (handle),
00088                           handle);
00089       int prio = et.event_handler_->priority ();
00090 
00091       // If the priority is out of range assign the minimum priority.
00092       if (prio < ACE_Event_Handler::LO_PRIORITY
00093           || prio > ACE_Event_Handler::HI_PRIORITY)
00094         prio = ACE_Event_Handler::LO_PRIORITY;
00095 
00096       bucket_[prio]->enqueue_tail (et);
00097 
00098       // Update the priority ranges....
00099       if (min_priority > prio)
00100         min_priority = prio;
00101       if (max_priority < prio)
00102         max_priority = prio;
00103     }
00104 
00105 }
00106 
00107 int
00108 ACE_Priority_Reactor::dispatch_io_set (int number_of_active_handles,
00109                                        int& number_dispatched,
00110                                        int mask,
00111                                        ACE_Handle_Set& dispatch_mask,
00112                                        ACE_Handle_Set& ready_mask,
00113                                        ACE_EH_PTMF callback)
00114 {
00115   ACE_TRACE ("ACE_Priority_Reactor::dispatch_io_set");
00116 
00117   if (number_of_active_handles == 0)
00118     return 0;
00119 
00120   // The range for which there exists any Event_Tuple is computed on
00121   // the ordering loop, minimizing iterations on the dispatching loop.
00122   int min_priority =
00123     ACE_Event_Handler::HI_PRIORITY;
00124   int max_priority =
00125     ACE_Event_Handler::LO_PRIORITY;
00126 
00127   (void) this->build_bucket (dispatch_mask,
00128                              min_priority,
00129                              max_priority);
00130 
00131   for (int i = max_priority; i >= min_priority; --i)
00132     {
00133       while (!bucket_[i]->is_empty ()
00134              && number_dispatched < number_of_active_handles)
00135         {
00136 
00137           ACE_Event_Tuple et;
00138 
00139           bucket_[i]->dequeue_head (et);
00140 
00141           this->notify_handle (et.handle_,
00142                                mask,
00143                                ready_mask,
00144                                et.event_handler_,
00145                                callback);
00146           number_dispatched++;
00147 
00148           // clear the bit from that dispatch mask,
00149           // so when we need to restart the iteration (rebuilding the iterator...)
00150           // we will not dispatch the already dipatched handlers
00151           this->clear_dispatch_mask (et.handle_,
00152                                      mask);
00153 
00154           if (this->state_changed_)
00155             {
00156               this->state_changed_ = false; // so it will not rebuild it ...
00157             }
00158         }
00159 
00160       // Even if we are aborting the loop due to this->state_changed
00161       // or another error we still want to cleanup the buckets.
00162       bucket_[i]->reset ();
00163     }
00164 
00165   return 0;
00166 }
00167 
00168 void
00169 ACE_Priority_Reactor::dump (void) const
00170 {
00171 #if defined (ACE_HAS_DUMP)
00172   ACE_TRACE ("ACE_Priority_Reactor::dump");
00173 
00174   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00175 
00176   ACE_Select_Reactor::dump ();
00177 
00178   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00179 #endif /* ACE_HAS_DUMP */
00180 }
00181 
00182 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:59 2006 for ACE by doxygen 1.3.6