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

Reactor.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Reactor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00003 
00004 #include "ace/Reactor.h"
00005 #include "ace/Reactor_Impl.h"
00006 #include "ace/Handle_Set.h"
00007 #if !defined (ACE_HAS_WINCE)
00008 #  if !defined (ACE_LACKS_ACE_SVCCONF)
00009 #    include "ace/Service_Config.h"
00010 #  endif /* !ACE_LACKS_ACE_SVCCONF */
00011 #  if defined (ACE_WIN32)
00012 #    include "ace/WFMO_Reactor.h"
00013 #    include "ace/Msg_WFMO_Reactor.h"
00014 #  endif /* ACE_WIN32 */
00015 #endif /* ! ACE_HAS_WINCE */
00016 #include "ace/Select_Reactor.h"
00017 #include "ace/TP_Reactor.h"
00018 #include "ace/Object_Manager.h"
00019 #include "ace/Framework_Component.h"
00020 
00021 #if !defined (__ACE_INLINE__)
00022 #include "ace/Reactor.i"
00023 #endif /* __ACE_INLINE__ */
00024 
00025 ACE_RCSID(ace, Reactor, "$Id: Reactor.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00026 
00027 ACE_ALLOC_HOOK_DEFINE(ACE_Reactor)
00028 
00029 ACE_Reactor::ACE_Reactor (ACE_Reactor_Impl *impl,
00030                           int delete_implementation)
00031   : implementation_ (0),
00032     delete_implementation_ (delete_implementation)
00033 {
00034   this->implementation (impl);
00035 
00036   if (this->implementation () == 0)
00037     {
00038 #if !defined (ACE_WIN32) \
00039       || !defined (ACE_HAS_WINSOCK2) || (ACE_HAS_WINSOCK2 == 0) \
00040       || defined (ACE_USE_SELECT_REACTOR_FOR_REACTOR_IMPL) \
00041       || defined (ACE_USE_TP_REACTOR_FOR_REACTOR_IMPL)
00042   #if defined (ACE_USE_TP_REACTOR_FOR_REACTOR_IMPL)
00043       ACE_NEW (impl,
00044                ACE_TP_Reactor);
00045   #else
00046       ACE_NEW (impl,
00047                ACE_Select_Reactor);
00048   #endif /* ACE_USE_TP_REACTOR_FOR_REACTOR_IMPL */
00049 #else /* We are on Win32 and we have winsock and ACE_USE_SELECT_REACTOR_FOR_REACTOR_IMPL is not defined */
00050   #if defined (ACE_USE_MSG_WFMO_REACTOR_FOR_REACTOR_IMPL)
00051       ACE_NEW (impl,
00052                ACE_Msg_WFMO_Reactor);
00053   #else
00054       ACE_NEW (impl,
00055                ACE_WFMO_Reactor);
00056   #endif /* ACE_USE_MSG_WFMO_REACTOR_FOR_REACTOR_IMPL */
00057 #endif /* !defined (ACE_WIN32) || !defined (ACE_HAS_WINSOCK2) || (ACE_HAS_WINSOCK2 == 0) || defined (ACE_USE_SELECT_REACTOR_FOR_REACTOR_IMPL) */
00058       this->implementation (impl);
00059       this->delete_implementation_ = 1;
00060     }
00061 }
00062 
00063 ACE_Reactor::~ACE_Reactor (void)
00064 {
00065   if (this->delete_implementation_)
00066     delete this->implementation ();
00067 }
00068 
00069 // Process-wide ACE_Reactor.
00070 ACE_Reactor *ACE_Reactor::reactor_ = 0;
00071 
00072 // Controls whether the Reactor is deleted when we shut down (we can
00073 // only delete it safely if we created it!)
00074 int ACE_Reactor::delete_reactor_ = 0;
00075 
00076 ACE_Reactor *
00077 ACE_Reactor::instance (void)
00078 {
00079   ACE_TRACE ("ACE_Reactor::instance");
00080 
00081   if (ACE_Reactor::reactor_ == 0)
00082     {
00083       // Perform Double-Checked Locking Optimization.
00084       ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00085                                 *ACE_Static_Object_Lock::instance (), 0));
00086 
00087       if (ACE_Reactor::reactor_ == 0)
00088         {
00089           ACE_NEW_RETURN (ACE_Reactor::reactor_,
00090                           ACE_Reactor,
00091                           0);
00092           ACE_Reactor::delete_reactor_ = 1;
00093           ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Reactor, ACE_Reactor::reactor_)
00094         }
00095     }
00096   return ACE_Reactor::reactor_;
00097 }
00098 
00099 ACE_Reactor *
00100 ACE_Reactor::instance (ACE_Reactor *r,
00101                        int delete_reactor)
00102 {
00103   ACE_TRACE ("ACE_Reactor::instance");
00104 
00105   ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
00106                             *ACE_Static_Object_Lock::instance (), 0));
00107   ACE_Reactor *t = ACE_Reactor::reactor_;
00108   if (delete_reactor != 0)
00109     ACE_Reactor::delete_reactor_ = 1;
00110   else
00111     // We can't safely delete it since we don't know who created it!
00112     ACE_Reactor::delete_reactor_ = 0;
00113 
00114   ACE_Reactor::reactor_ = r;
00115 
00116   // We can't register the Reactor singleton as a framework component twice.
00117   // Therefore we test to see if we had an existing reactor instance, which
00118   // if so means it must have already been registered.
00119   if (t == 0)
00120     ACE_REGISTER_FRAMEWORK_COMPONENT(ACE_Reactor, ACE_Reactor::reactor_);
00121 
00122   return t;
00123 }
00124 
00125 void
00126 ACE_Reactor::close_singleton (void)
00127 {
00128   ACE_TRACE ("ACE_Reactor::close_singleton");
00129 
00130   ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
00131                      *ACE_Static_Object_Lock::instance ()));
00132 
00133   if (ACE_Reactor::delete_reactor_)
00134     {
00135       delete ACE_Reactor::reactor_;
00136       ACE_Reactor::reactor_ = 0;
00137       ACE_Reactor::delete_reactor_ = 0;
00138     }
00139 }
00140 
00141 const ACE_TCHAR *
00142 ACE_Reactor::dll_name (void)
00143 {
00144   return ACE_LIB_TEXT ("ACE");
00145 }
00146 
00147 const ACE_TCHAR *
00148 ACE_Reactor::name (void)
00149 {
00150   return ACE_LIB_TEXT ("ACE_Reactor");
00151 }
00152 
00153 int
00154 ACE_Reactor::check_reconfiguration (ACE_Reactor *)
00155 {
00156 #if !defined (ACE_HAS_WINCE)  &&  !defined (ACE_LACKS_ACE_SVCCONF)
00157   if (ACE_Service_Config::reconfig_occurred ())
00158     {
00159       ACE_Service_Config::reconfigure ();
00160       return 1;
00161     }
00162 #endif /* ! ACE_HAS_WINCE || ! ACE_LACKS_ACE_SVCCONF */
00163   return 0;
00164 }
00165 
00166 int
00167 ACE_Reactor::run_reactor_event_loop (REACTOR_EVENT_HOOK eh)
00168 {
00169   ACE_TRACE ("ACE_Reactor::run_reactor_event_loop");
00170 
00171   if (this->reactor_event_loop_done ())
00172     return 0;
00173 
00174   while (1)
00175     {
00176       int result = this->implementation_->handle_events ();
00177 
00178       if (eh != 0 && (*eh)(this))
00179         continue;
00180       else if (result == -1 && this->implementation_->deactivated ())
00181         return 0;
00182       else if (result == -1)
00183         return -1;
00184     }
00185 
00186   ACE_NOTREACHED (return 0;)
00187 }
00188 
00189 int
00190 ACE_Reactor::run_alertable_reactor_event_loop (REACTOR_EVENT_HOOK eh)
00191 {
00192   ACE_TRACE ("ACE_Reactor::run_alertable_reactor_event_loop");
00193 
00194   if (this->reactor_event_loop_done ())
00195     return 0;
00196 
00197   while (1)
00198     {
00199       int result = this->implementation_->alertable_handle_events ();
00200 
00201       if (eh != 0 && (*eh)(this))
00202         continue;
00203       else if (result == -1 && this->implementation_->deactivated ())
00204         return 0;
00205       else if (result == -1)
00206         return -1;
00207     }
00208 
00209   ACE_NOTREACHED (return 0;)
00210 }
00211 
00212 int
00213 ACE_Reactor::run_reactor_event_loop (ACE_Time_Value &tv,
00214                                      REACTOR_EVENT_HOOK eh)
00215 {
00216   ACE_TRACE ("ACE_Reactor::run_reactor_event_loop");
00217 
00218   if (this->reactor_event_loop_done ())
00219     return 0;
00220 
00221   while (1)
00222     {
00223       int result = this->implementation_->handle_events (tv);
00224 
00225       if (eh != 0 && (*eh) (this))
00226         continue;
00227       else if (result == -1)
00228         {
00229           if (this->implementation_->deactivated ())
00230             result = 0;
00231           return result;
00232         }
00233       else if (result == 0)
00234         {
00235           // The <handle_events> method timed out without dispatching
00236           // anything.  Because of rounding and conversion errors and
00237           // such, it could be that the wait loop (WFMO, select, etc.)
00238           // timed out, but the timer queue said it wasn't quite ready
00239           // to expire a timer. In this case, the ACE_Time_Value we
00240           // passed into handle_events won't have quite been reduced
00241           // to 0, and we need to go around again. If we are all the
00242           // way to 0, just return, as the entire time the caller
00243           // wanted to wait has been used up.
00244           if (tv.usec () > 0)
00245             continue;
00246           return 0;
00247         }
00248       // Else there were some events dispatched; go around again
00249     }
00250 
00251   ACE_NOTREACHED (return 0;)
00252 }
00253 
00254 int
00255 ACE_Reactor::run_alertable_reactor_event_loop (ACE_Time_Value &tv,
00256                                                REACTOR_EVENT_HOOK eh)
00257 {
00258   ACE_TRACE ("ACE_Reactor::run_alertable_reactor_event_loop");
00259 
00260   if (this->reactor_event_loop_done ())
00261     return 0;
00262 
00263   for (;;)
00264     {
00265       int result = this->implementation_->alertable_handle_events (tv);
00266 
00267       if (eh != 0 && (*eh)(this))
00268         continue;
00269       else if (result == -1 && this->implementation_->deactivated ())
00270         return 0;
00271       else if (result <= 0)
00272         return result;
00273     }
00274 
00275   ACE_NOTREACHED (return 0;)
00276 }
00277 
00278 int
00279 ACE_Reactor::end_reactor_event_loop (void)
00280 {
00281   ACE_TRACE ("ACE_Reactor::end_reactor_event_loop");
00282 
00283   this->implementation_->deactivate (1);
00284 
00285   return 0;
00286 }
00287 
00288 void
00289 ACE_Reactor::reset_reactor_event_loop (void)
00290 {
00291   ACE_TRACE ("ACE_Reactor::reset_reactor_event_loop");
00292 
00293   this->implementation_->deactivate (0);
00294 }
00295 
00296 
00297 int
00298 ACE_Reactor::resumable_handler (void)
00299 {
00300   return this->implementation ()->resumable_handler ();
00301 }
00302 
00303 void
00304 ACE_Reactor::dump (void) const
00305 {
00306   ACE_TRACE ("ACE_Reactor::dump");
00307 
00308   implementation_->dump ();
00309 }
00310 
00311 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00312 template class ACE_Framework_Component_T<ACE_Reactor>;
00313 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
00314 #pragma instantiate ACE_Framework_Component_T<ACE_Reactor>
00315 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

Generated on Mon Jun 16 11:21:01 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002