Thread_Lane_Resources.cpp

Go to the documentation of this file.
00001 // Thread_Lane_Resources.cpp,v 1.24 2005/11/02 11:03:27 ossama Exp
00002 
00003 #include "tao/Thread_Lane_Resources.h"
00004 
00005 ACE_RCSID (tao,
00006            Thread_Lane_Resources,
00007            "Thread_Lane_Resources.cpp,v 1.24 2005/11/02 11:03:27 ossama Exp")
00008 
00009 #include "tao/Acceptor_Registry.h"
00010 #include "tao/LF_Follower.h"
00011 #include "tao/Leader_Follower.h"
00012 #include "tao/Connection_Handler.h"
00013 #include "tao/Transport.h"
00014 #include "tao/Connector_Registry.h"
00015 #include "tao/SystemException.h"
00016 #include "tao/ORB_Core.h"
00017 
00018 #include "ace/Reactor.h"
00019 
00020 
00021 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00022 
00023 TAO_Thread_Lane_Resources::TAO_Thread_Lane_Resources (
00024     TAO_ORB_Core &orb_core,
00025     TAO_New_Leader_Generator *new_leader_generator
00026   )
00027   : orb_core_ (orb_core),
00028     acceptor_registry_ (0),
00029     connector_registry_ (0),
00030     transport_cache_ (0),
00031     leader_follower_ (0),
00032     new_leader_generator_ (new_leader_generator),
00033     input_cdr_dblock_allocator_ (0),
00034     input_cdr_buffer_allocator_ (0),
00035     input_cdr_msgblock_allocator_ (0),
00036     transport_message_buffer_allocator_ (0),
00037     output_cdr_dblock_allocator_ (0),
00038     output_cdr_buffer_allocator_ (0),
00039     output_cdr_msgblock_allocator_ (0),
00040     amh_response_handler_allocator_ (0),
00041     ami_response_handler_allocator_ (0)
00042 {
00043   // Create the transport cache.
00044   ACE_NEW (this->transport_cache_,
00045            TAO::Transport_Cache_Manager (orb_core));
00046 
00047 }
00048 
00049 TAO_Thread_Lane_Resources::~TAO_Thread_Lane_Resources (void)
00050 {
00051 
00052 }
00053 
00054 TAO::Transport_Cache_Manager &
00055 TAO_Thread_Lane_Resources::transport_cache (void)
00056 {
00057   return *this->transport_cache_;
00058 }
00059 
00060 int
00061 TAO_Thread_Lane_Resources::has_acceptor_registry_been_created (void) const
00062 {
00063   return this->acceptor_registry_ != 0;
00064 }
00065 
00066 int
00067 TAO_Thread_Lane_Resources::is_collocated (const TAO_MProfile& mprofile)
00068 {
00069   if (!this->has_acceptor_registry_been_created ())
00070     {
00071       return 0;
00072     }
00073 
00074   return this->acceptor_registry ().is_collocated (mprofile);
00075 }
00076 
00077 TAO_Acceptor_Registry &
00078 TAO_Thread_Lane_Resources::acceptor_registry (void)
00079 {
00080   // Double check.
00081   if (this->acceptor_registry_ == 0)
00082     {
00083       // @@todo: Wouldnt this crash big time if you happen to
00084       // dereference a null-pointer? Needs fixing.
00085       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00086                         ace_mon,
00087                         this->lock_,
00088                         *this->acceptor_registry_);
00089 
00090       if (this->acceptor_registry_ == 0)
00091         {
00092           // @@ Not exception safe code
00093           // Get the resource factory.
00094           TAO_Resource_Factory &resource_factory =
00095             *this->orb_core_.resource_factory ();
00096 
00097           // Ask it to create a new acceptor registry.
00098           this->acceptor_registry_ =
00099             resource_factory.get_acceptor_registry ();
00100         }
00101     }
00102 
00103   return *this->acceptor_registry_;
00104 }
00105 
00106 TAO_Connector_Registry *
00107 TAO_Thread_Lane_Resources::connector_registry (ACE_ENV_SINGLE_ARG_DECL)
00108 {
00109   // Double check.
00110   if (this->connector_registry_ == 0)
00111     {
00112       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00113                         ace_mon,
00114                         this->lock_,
00115                         0);
00116 
00117       if (this->connector_registry_ == 0)
00118         {
00119           // Ask it to create a new acceptor registry.
00120           TAO_Connector_Registry *connector_registry =
00121             this->orb_core_.resource_factory ()->get_connector_registry ();
00122 
00123           if (connector_registry == 0)
00124             {
00125               ACE_THROW_RETURN (
00126                   CORBA::INITIALIZE (
00127                       CORBA::SystemException::_tao_minor_code (
00128                           TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
00129                           0
00130                         ),
00131                       CORBA::COMPLETED_NO
00132                     ),
00133                   0
00134                 );
00135             }
00136 
00137           if (connector_registry->open (&this->orb_core_) != 0)
00138             {
00139               ACE_THROW_RETURN (
00140                   CORBA::INITIALIZE (
00141                       CORBA::SystemException::_tao_minor_code (
00142                           TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
00143                           0
00144                         ),
00145                       CORBA::COMPLETED_NO
00146                     ),
00147                   0
00148                 );
00149             }
00150 
00151           // Finally, everything is created and opened successfully:
00152           // now we can assign to the member.  Otherwise, the
00153           // assignment would be premature.
00154           this->connector_registry_ = connector_registry;
00155         }
00156     }
00157 
00158   return this->connector_registry_;
00159 }
00160 
00161 
00162 TAO_Leader_Follower &
00163 TAO_Thread_Lane_Resources::leader_follower (void)
00164 {
00165   // Double check.
00166   if (this->leader_follower_ == 0)
00167     {
00168       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00169                         ace_mon,
00170                         this->lock_,
00171                         *this->leader_follower_);
00172 
00173       if (this->leader_follower_ == 0)
00174         {
00175           // Create a new Leader Follower object.
00176           ACE_NEW_RETURN (this->leader_follower_,
00177                           TAO_Leader_Follower (&this->orb_core_,
00178                                                this->new_leader_generator_),
00179                           *this->leader_follower_);
00180         }
00181     }
00182 
00183   return *this->leader_follower_;
00184 }
00185 
00186 
00187 ACE_Allocator*
00188 TAO_Thread_Lane_Resources::input_cdr_dblock_allocator (void)
00189 {
00190   if (this->input_cdr_dblock_allocator_ == 0)
00191     {
00192       // Double checked locking
00193       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00194 
00195       if (this->input_cdr_dblock_allocator_ == 0)
00196         {
00197           this->input_cdr_dblock_allocator_ =
00198             this->resource_factory ()->input_cdr_dblock_allocator ();
00199         }
00200     }
00201 
00202   return this->input_cdr_dblock_allocator_;
00203 }
00204 
00205 
00206 ACE_Allocator*
00207 TAO_Thread_Lane_Resources::input_cdr_buffer_allocator (void)
00208 {
00209   if (this->input_cdr_buffer_allocator_ == 0)
00210     {
00211       // Double checked locking
00212       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00213 
00214       if (this->input_cdr_buffer_allocator_ == 0)
00215         {
00216           this->input_cdr_buffer_allocator_ =
00217             this->resource_factory ()->input_cdr_buffer_allocator ();
00218         }
00219     }
00220 
00221   return this->input_cdr_buffer_allocator_;
00222 }
00223 
00224 
00225 ACE_Allocator*
00226 TAO_Thread_Lane_Resources::input_cdr_msgblock_allocator (void)
00227 {
00228   if (this->input_cdr_msgblock_allocator_ == 0)
00229     {
00230       // Double checked locking
00231       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00232 
00233       if (this->input_cdr_msgblock_allocator_ == 0)
00234         {
00235           this->input_cdr_msgblock_allocator_ =
00236             this->resource_factory ()->input_cdr_msgblock_allocator ();
00237         }
00238     }
00239 
00240   return this->input_cdr_msgblock_allocator_;
00241 }
00242 
00243 ACE_Allocator*
00244 TAO_Thread_Lane_Resources::transport_message_buffer_allocator (void)
00245 {
00246   if (this->transport_message_buffer_allocator_ == 0)
00247     {
00248       // Double checked locking
00249       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00250 
00251       if (this->transport_message_buffer_allocator_ == 0)
00252         {
00253           this->transport_message_buffer_allocator_ =
00254             this->resource_factory ()->input_cdr_dblock_allocator ();
00255         }
00256     }
00257 
00258   return this->transport_message_buffer_allocator_;
00259 }
00260 
00261 
00262 ACE_Allocator*
00263 TAO_Thread_Lane_Resources::output_cdr_dblock_allocator (void)
00264 {
00265   if (this->output_cdr_dblock_allocator_ == 0)
00266     {
00267       // Double checked locking
00268       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00269 
00270       if (this->output_cdr_dblock_allocator_ == 0)
00271         {
00272           this->output_cdr_dblock_allocator_ =
00273             this->resource_factory ()->output_cdr_dblock_allocator ();
00274         }
00275     }
00276 
00277   return this->output_cdr_dblock_allocator_;
00278 }
00279 
00280 
00281 ACE_Allocator*
00282 TAO_Thread_Lane_Resources::output_cdr_buffer_allocator (void)
00283 {
00284   if (this->output_cdr_buffer_allocator_ == 0)
00285     {
00286       // Double checked locking
00287       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00288 
00289       if (this->output_cdr_buffer_allocator_ == 0)
00290         {
00291           this->output_cdr_buffer_allocator_ =
00292             this->resource_factory ()->output_cdr_buffer_allocator ();
00293         }
00294     }
00295 
00296   return this->output_cdr_buffer_allocator_;
00297 }
00298 
00299 
00300 ACE_Allocator*
00301 TAO_Thread_Lane_Resources::output_cdr_msgblock_allocator (void)
00302 {
00303   if (this->output_cdr_msgblock_allocator_ == 0)
00304     {
00305       // Double checked locking
00306       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00307 
00308       if (this->output_cdr_msgblock_allocator_ == 0)
00309         {
00310           this->output_cdr_msgblock_allocator_ =
00311             this->resource_factory ()->output_cdr_msgblock_allocator ();
00312         }
00313     }
00314 
00315   return this->output_cdr_msgblock_allocator_;
00316 }
00317 
00318 ACE_Allocator*
00319 TAO_Thread_Lane_Resources::amh_response_handler_allocator (void)
00320 {
00321   if (this->amh_response_handler_allocator_ == 0)
00322     {
00323       // Double checked locking
00324       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00325 
00326       if (this->amh_response_handler_allocator_ == 0)
00327         {
00328           this->amh_response_handler_allocator_ =
00329             this->resource_factory ()->amh_response_handler_allocator ();
00330         }
00331     }
00332 
00333   return this->amh_response_handler_allocator_;
00334 }
00335 
00336 ACE_Allocator*
00337 TAO_Thread_Lane_Resources::ami_response_handler_allocator (void)
00338 {
00339   if (this->ami_response_handler_allocator_ == 0)
00340     {
00341       // Double checked locking
00342       ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00343 
00344       if (this->ami_response_handler_allocator_ == 0)
00345         {
00346           this->ami_response_handler_allocator_ =
00347             this->resource_factory ()->ami_response_handler_allocator ();
00348         }
00349     }
00350 
00351   return this->ami_response_handler_allocator_;
00352 }
00353 
00354 int
00355 TAO_Thread_Lane_Resources::open_acceptor_registry (const TAO_EndpointSet &endpoint_set,
00356                                                    bool ignore_address
00357                                                    ACE_ENV_ARG_DECL)
00358 {
00359   // Access the acceptor registry.
00360   TAO_Acceptor_Registry &ar = this->acceptor_registry ();
00361 
00362   // Open it.
00363   int result = ar.open (&this->orb_core_,
00364                         this->leader_follower ().reactor (),
00365                         endpoint_set,
00366                         ignore_address
00367                         ACE_ENV_ARG_PARAMETER);
00368   ACE_CHECK_RETURN (-1);
00369 
00370   return result;
00371 }
00372 
00373 TAO_Resource_Factory *
00374 TAO_Thread_Lane_Resources::resource_factory (void)
00375 {
00376   return this->orb_core_.resource_factory ();
00377 }
00378 
00379 void
00380 TAO_Thread_Lane_Resources::finalize (void)
00381 {
00382   // Close connectors before acceptors!
00383   // Ask the registry to close all registered connectors.
00384   if (this->connector_registry_ != 0)
00385     {
00386       this->connector_registry_->close_all ();
00387       delete this->connector_registry_;
00388       this->connector_registry_ = 0;
00389     }
00390 
00391   // Ask the registry to close all registered acceptors.
00392   if (this->acceptor_registry_ != 0)
00393     {
00394       this->acceptor_registry_->close_all ();
00395       delete this->acceptor_registry_;
00396       this->acceptor_registry_ = 0;
00397     }
00398 
00399   // Set of handlers still in the connection cache.
00400   TAO::Connection_Handler_Set handlers;
00401 
00402   // Close the transport cache and return the handlers that were still
00403   // registered.  The cache will decrease the #REFCOUNT# on the
00404   // handler when it removes the handler from cache.  However,
00405   // #REFCOUNT# is increased when the handler is placed in the handler
00406   // set.
00407   this->transport_cache_->close (handlers);
00408 
00409   // Go through the handler set, closing the connections and removing
00410   // the references.
00411   TAO_Connection_Handler **handler = 0;
00412 
00413   for (TAO::Connection_Handler_Set::iterator iter (handlers);
00414        iter.next (handler);
00415        iter.advance ())
00416     {
00417       // Connection is closed.  Potential removal from the Reactor.
00418       (*handler)->close_connection ();
00419 
00420       // #REFCOUNT# related to the handler set decreases.
00421       (*handler)->transport ()->remove_reference ();
00422     }
00423 
00424   delete this->transport_cache_;
00425   this->transport_cache_ = 0;
00426 
00427   delete this->leader_follower_;
00428   this->leader_follower_ = 0;
00429 
00430   // Delete all the allocators here.. They shouldnt be done earlier,
00431   // lest some of the contents in the above, say reactor or acceptor
00432   // may use memory from the pool..
00433   if (this->input_cdr_dblock_allocator_ != 0)
00434     {
00435       this->input_cdr_dblock_allocator_->remove ();
00436       delete this->input_cdr_dblock_allocator_;
00437       this->input_cdr_dblock_allocator_ = 0;
00438     }
00439 
00440   if (this->input_cdr_buffer_allocator_ != 0)
00441     {
00442       this->input_cdr_buffer_allocator_->remove ();
00443       delete this->input_cdr_buffer_allocator_;
00444       this->input_cdr_buffer_allocator_ = 0;
00445     }
00446 
00447   if (this->input_cdr_msgblock_allocator_ != 0)
00448     {
00449       this->input_cdr_msgblock_allocator_->remove ();
00450       delete this->input_cdr_msgblock_allocator_;
00451       this->input_cdr_msgblock_allocator_ = 0;
00452     }
00453 
00454   if (this->transport_message_buffer_allocator_ != 0)
00455     {
00456       this->transport_message_buffer_allocator_->remove ();
00457       delete this->transport_message_buffer_allocator_;
00458       this->transport_message_buffer_allocator_ = 0;
00459     }
00460 
00461   if (this->output_cdr_dblock_allocator_ != 0)
00462     {
00463       this->output_cdr_dblock_allocator_->remove ();
00464       delete this->output_cdr_dblock_allocator_;
00465       this->output_cdr_dblock_allocator_ = 0;
00466     }
00467 
00468   if (this->output_cdr_buffer_allocator_ != 0)
00469     {
00470       this->output_cdr_buffer_allocator_->remove ();
00471       delete this->output_cdr_buffer_allocator_;
00472       this->output_cdr_buffer_allocator_ = 0;
00473     }
00474 
00475   if (this->output_cdr_msgblock_allocator_ != 0)
00476     {
00477       this->output_cdr_msgblock_allocator_->remove ();
00478       delete this->output_cdr_msgblock_allocator_;
00479       this->output_cdr_msgblock_allocator_ = 0;
00480     }
00481 
00482   if (this->amh_response_handler_allocator_ != 0)
00483     {
00484       this->amh_response_handler_allocator_->remove ();
00485       delete this->amh_response_handler_allocator_;
00486       this->amh_response_handler_allocator_ = 0;
00487     }
00488 
00489   if (this->ami_response_handler_allocator_ != 0)
00490     {
00491       this->ami_response_handler_allocator_->remove ();
00492       delete this->ami_response_handler_allocator_;
00493       this->ami_response_handler_allocator_ = 0;
00494     }
00495 }
00496 
00497 void
00498 TAO_Thread_Lane_Resources::shutdown_reactor (void)
00499 {
00500   TAO_Leader_Follower &leader_follower = this->leader_follower ();
00501 
00502   ACE_GUARD (TAO_SYNCH_MUTEX,
00503              ace_mon,
00504              leader_follower.lock ());
00505 
00506   ACE_Reactor *reactor = leader_follower.reactor ();
00507 
00508   // Wakeup all the threads waiting blocked in the event loop, this
00509   // does not guarantee that they will all go away, but reduces the
00510   // load on the POA....
00511 
00512   // If there are some client threads running we have to wait until
00513   // they finish, when the last one does it will shutdown the reactor
00514   // for us.  Meanwhile no new requests will be accepted because the
00515   // POA will not process them.
00516   if (!this->orb_core_.resource_factory ()->drop_replies_during_shutdown () &&
00517       leader_follower.has_clients ())
00518     {
00519       reactor->wakeup_all_threads ();
00520       return;
00521     }
00522 
00523   // End the reactor if we want shutdown dropping replies along the
00524   // way.
00525   reactor->end_reactor_event_loop ();
00526 }
00527 
00528 void
00529 TAO_Thread_Lane_Resources::cleanup_rw_transports (void)
00530 {
00531   // If we have no-drop-reply strategy or already fininalized simply return.
00532   if (!this->orb_core_.resource_factory ()->drop_replies_during_shutdown () ||
00533        this->transport_cache_ == 0)
00534     return;
00535 
00536   // Set of handlers still in the connection cache.
00537   TAO::Connection_Handler_Set handlers;
00538 
00539   this->transport_cache_->blockable_client_transports (handlers);
00540 
00541   // Go through the handler set, closing the connections and removing
00542   // the references.
00543   TAO_Connection_Handler **handler = 0;
00544 
00545   for (TAO::Connection_Handler_Set::iterator iter (handlers);
00546        iter.next (handler);
00547        iter.advance ())
00548     {
00549       // Connection is closed. There will be a double closure but that
00550       // is okay.
00551       (*handler)->release_os_resources ();
00552 
00553       // #REFCOUNT# related to the handler set decreases.
00554       (*handler)->transport ()->remove_reference ();
00555     }
00556 }
00557 
00558 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 11:54:25 2006 for TAO by doxygen 1.3.6