Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / Thread_Lane_Resources.cpp
blob32bc8c072df6b5a270346d0ded654e0f516489d9
1 #include "tao/Thread_Lane_Resources.h"
2 #include "tao/Acceptor_Registry.h"
3 #include "tao/LF_Follower.h"
4 #include "tao/Leader_Follower.h"
5 #include "tao/Connection_Handler.h"
6 #include "tao/Transport.h"
7 #include "tao/Connector_Registry.h"
8 #include "tao/SystemException.h"
9 #include "tao/ORB_Core.h"
10 #include "tao/Transport_Descriptor_Interface.h"
12 #include "ace/Reactor.h"
15 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
17 TAO_Thread_Lane_Resources::TAO_Thread_Lane_Resources (
18 TAO_ORB_Core &orb_core,
19 TAO_New_Leader_Generator *new_leader_generator)
20 : orb_core_ (orb_core),
21 acceptor_registry_ (nullptr),
22 connector_registry_ (nullptr),
23 transport_cache_ (nullptr),
24 leader_follower_ (nullptr),
25 new_leader_generator_ (new_leader_generator),
26 input_cdr_dblock_allocator_ (nullptr),
27 input_cdr_buffer_allocator_ (nullptr),
28 input_cdr_msgblock_allocator_ (nullptr),
29 transport_message_buffer_allocator_ (nullptr),
30 output_cdr_dblock_allocator_ (nullptr),
31 output_cdr_buffer_allocator_ (nullptr),
32 output_cdr_msgblock_allocator_ (nullptr),
33 amh_response_handler_allocator_ (nullptr),
34 ami_response_handler_allocator_ (nullptr)
36 // Create the transport cache.
37 ACE_NEW (this->transport_cache_,
38 TAO::Transport_Cache_Manager (
39 orb_core.resource_factory ()->purge_percentage (),
40 orb_core.resource_factory ()->create_purging_strategy (),
41 orb_core.resource_factory ()->cache_maximum (),
42 orb_core.resource_factory ()->locked_transport_cache (),
43 orb_core.orbid ()));
46 TAO_Thread_Lane_Resources::~TAO_Thread_Lane_Resources ()
50 TAO::Transport_Cache_Manager &
51 TAO_Thread_Lane_Resources::transport_cache ()
53 return *this->transport_cache_;
56 int
57 TAO_Thread_Lane_Resources::has_acceptor_registry_been_created () const
59 return this->acceptor_registry_ != nullptr;
62 int
63 TAO_Thread_Lane_Resources::is_collocated (const TAO_MProfile& mprofile)
65 if (!this->has_acceptor_registry_been_created ())
67 return 0;
70 return this->acceptor_registry ().is_collocated (mprofile);
73 TAO_Acceptor_Registry &
74 TAO_Thread_Lane_Resources::acceptor_registry ()
76 // Double check.
77 if (this->acceptor_registry_ == nullptr)
79 // @todo: Wouldnt this crash big time if you happen to
80 // dereference a null-pointer? Needs fixing.
81 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
82 ace_mon,
83 this->lock_,
84 *this->acceptor_registry_);
86 if (this->acceptor_registry_ == nullptr)
88 // @@ Not exception safe code
89 // Get the resource factory.
90 TAO_Resource_Factory &resource_factory =
91 *this->orb_core_.resource_factory ();
93 // Ask it to create a new acceptor registry.
94 this->acceptor_registry_ =
95 resource_factory.get_acceptor_registry ();
99 return *this->acceptor_registry_;
102 TAO_Connector_Registry *
103 TAO_Thread_Lane_Resources::connector_registry ()
105 // Double check.
106 if (this->connector_registry_ == nullptr)
108 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
109 ace_mon,
110 this->lock_,
111 nullptr);
113 if (this->connector_registry_ == nullptr)
115 // Ask it to create a new acceptor registry.
116 TAO_Connector_Registry *connector_registry =
117 this->orb_core_.resource_factory ()->get_connector_registry ();
119 if (connector_registry == nullptr)
121 throw ::CORBA::INITIALIZE (
122 CORBA::SystemException::_tao_minor_code (
123 TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
125 CORBA::COMPLETED_NO);
128 if (connector_registry->open (&this->orb_core_) != 0)
130 throw ::CORBA::INITIALIZE (
131 CORBA::SystemException::_tao_minor_code (
132 TAO_CONNECTOR_REGISTRY_INIT_LOCATION_CODE,
134 CORBA::COMPLETED_NO);
137 // Finally, everything is created and opened successfully:
138 // now we can assign to the member. Otherwise, the
139 // assignment would be premature.
140 this->connector_registry_ = connector_registry;
144 return this->connector_registry_;
148 TAO_Leader_Follower &
149 TAO_Thread_Lane_Resources::leader_follower ()
151 // Double check.
152 if (this->leader_follower_ == nullptr)
154 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
155 ace_mon,
156 this->lock_,
157 *this->leader_follower_);
159 if (this->leader_follower_ == nullptr)
161 // Create a new Leader Follower object.
162 ACE_NEW_RETURN (this->leader_follower_,
163 TAO_Leader_Follower (&this->orb_core_,
164 this->new_leader_generator_),
165 *this->leader_follower_);
169 return *this->leader_follower_;
173 ACE_Allocator*
174 TAO_Thread_Lane_Resources::input_cdr_dblock_allocator ()
176 if (this->input_cdr_dblock_allocator_ == nullptr)
178 // Double checked locking
179 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
181 if (this->input_cdr_dblock_allocator_ == nullptr)
183 this->input_cdr_dblock_allocator_ =
184 this->resource_factory ()->input_cdr_dblock_allocator ();
188 return this->input_cdr_dblock_allocator_;
192 ACE_Allocator*
193 TAO_Thread_Lane_Resources::input_cdr_buffer_allocator ()
195 if (this->input_cdr_buffer_allocator_ == nullptr)
197 // Double checked locking
198 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
200 if (this->input_cdr_buffer_allocator_ == nullptr)
202 this->input_cdr_buffer_allocator_ =
203 this->resource_factory ()->input_cdr_buffer_allocator ();
207 return this->input_cdr_buffer_allocator_;
211 ACE_Allocator*
212 TAO_Thread_Lane_Resources::input_cdr_msgblock_allocator ()
214 if (this->input_cdr_msgblock_allocator_ == nullptr)
216 // Double checked locking
217 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
219 if (this->input_cdr_msgblock_allocator_ == nullptr)
221 this->input_cdr_msgblock_allocator_ =
222 this->resource_factory ()->input_cdr_msgblock_allocator ();
226 return this->input_cdr_msgblock_allocator_;
229 ACE_Allocator*
230 TAO_Thread_Lane_Resources::transport_message_buffer_allocator ()
232 if (this->transport_message_buffer_allocator_ == nullptr)
234 // Double checked locking
235 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
237 if (this->transport_message_buffer_allocator_ == nullptr)
239 this->transport_message_buffer_allocator_ =
240 this->resource_factory ()->input_cdr_dblock_allocator ();
244 return this->transport_message_buffer_allocator_;
248 ACE_Allocator*
249 TAO_Thread_Lane_Resources::output_cdr_dblock_allocator ()
251 if (this->output_cdr_dblock_allocator_ == nullptr)
253 // Double checked locking
254 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
256 if (this->output_cdr_dblock_allocator_ == nullptr)
258 this->output_cdr_dblock_allocator_ =
259 this->resource_factory ()->output_cdr_dblock_allocator ();
263 return this->output_cdr_dblock_allocator_;
267 ACE_Allocator*
268 TAO_Thread_Lane_Resources::output_cdr_buffer_allocator ()
270 if (this->output_cdr_buffer_allocator_ == nullptr)
272 // Double checked locking
273 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
275 if (this->output_cdr_buffer_allocator_ == nullptr)
277 this->output_cdr_buffer_allocator_ =
278 this->resource_factory ()->output_cdr_buffer_allocator ();
282 return this->output_cdr_buffer_allocator_;
286 ACE_Allocator*
287 TAO_Thread_Lane_Resources::output_cdr_msgblock_allocator ()
289 if (this->output_cdr_msgblock_allocator_ == nullptr)
291 // Double checked locking
292 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
294 if (this->output_cdr_msgblock_allocator_ == nullptr)
296 this->output_cdr_msgblock_allocator_ =
297 this->resource_factory ()->output_cdr_msgblock_allocator ();
301 return this->output_cdr_msgblock_allocator_;
304 ACE_Allocator*
305 TAO_Thread_Lane_Resources::amh_response_handler_allocator ()
307 if (this->amh_response_handler_allocator_ == nullptr)
309 // Double checked locking
310 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
312 if (this->amh_response_handler_allocator_ == nullptr)
314 this->amh_response_handler_allocator_ =
315 this->resource_factory ()->amh_response_handler_allocator ();
319 return this->amh_response_handler_allocator_;
322 ACE_Allocator*
323 TAO_Thread_Lane_Resources::ami_response_handler_allocator ()
325 if (this->ami_response_handler_allocator_ == nullptr)
327 // Double checked locking
328 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, nullptr);
330 if (this->ami_response_handler_allocator_ == nullptr)
332 this->ami_response_handler_allocator_ =
333 this->resource_factory ()->ami_response_handler_allocator ();
337 return this->ami_response_handler_allocator_;
341 TAO_Thread_Lane_Resources::open_acceptor_registry (const TAO_EndpointSet &endpoint_set,
342 bool ignore_address)
344 // Access the acceptor registry.
345 TAO_Acceptor_Registry &ar = this->acceptor_registry ();
347 // Open it.
348 return ar.open (&this->orb_core_,
349 this->leader_follower ().reactor (),
350 endpoint_set,
351 ignore_address);
354 TAO_Resource_Factory *
355 TAO_Thread_Lane_Resources::resource_factory ()
357 return this->orb_core_.resource_factory ();
360 void
361 TAO_Thread_Lane_Resources::finalize ()
363 // Close connectors before acceptors!
364 // Ask the registry to close all registered connectors.
365 if (this->connector_registry_ != nullptr)
367 this->connector_registry_->close_all ();
368 delete this->connector_registry_;
369 this->connector_registry_ = nullptr;
372 // Ask the registry to close all registered acceptors.
373 if (this->acceptor_registry_ != nullptr)
375 this->acceptor_registry_->close_all ();
376 delete this->acceptor_registry_;
377 this->acceptor_registry_ = nullptr;
380 // Set of handlers still in the connection cache.
381 TAO::Connection_Handler_Set handlers;
383 // Close the transport cache and return the handlers that were still
384 // registered. The cache will decrease the #REFCOUNT# on the
385 // handler when it removes the handler from cache. However,
386 // #REFCOUNT# is increased when the handler is placed in the handler
387 // set.
388 this->transport_cache_->close (handlers);
390 // Go through the handler set, closing the connections and removing
391 // the references.
392 TAO_Connection_Handler **handler = nullptr;
394 for (TAO::Connection_Handler_Set::iterator iter (handlers);
395 iter.next (handler);
396 iter.advance ())
398 // Connection is closed. Potential removal from the Reactor.
399 (*handler)->close_connection ();
401 // #REFCOUNT# related to the handler set decreases.
402 (*handler)->transport ()->remove_reference ();
405 delete this->transport_cache_;
406 this->transport_cache_ = nullptr;
408 delete this->leader_follower_;
409 this->leader_follower_ = nullptr;
411 // Delete all the allocators here.. They shouldnt be done earlier,
412 // lest some of the contents in the above, say reactor or acceptor
413 // may use memory from the pool..
414 if (this->input_cdr_dblock_allocator_ != nullptr)
416 this->input_cdr_dblock_allocator_->remove ();
417 delete this->input_cdr_dblock_allocator_;
418 this->input_cdr_dblock_allocator_ = nullptr;
421 if (this->input_cdr_buffer_allocator_ != nullptr)
423 this->input_cdr_buffer_allocator_->remove ();
424 delete this->input_cdr_buffer_allocator_;
425 this->input_cdr_buffer_allocator_ = nullptr;
428 if (this->input_cdr_msgblock_allocator_ != nullptr)
430 this->input_cdr_msgblock_allocator_->remove ();
431 delete this->input_cdr_msgblock_allocator_;
432 this->input_cdr_msgblock_allocator_ = nullptr;
435 if (this->transport_message_buffer_allocator_ != nullptr)
437 this->transport_message_buffer_allocator_->remove ();
438 delete this->transport_message_buffer_allocator_;
439 this->transport_message_buffer_allocator_ = nullptr;
442 if (this->output_cdr_dblock_allocator_ != nullptr)
444 this->output_cdr_dblock_allocator_->remove ();
445 delete this->output_cdr_dblock_allocator_;
446 this->output_cdr_dblock_allocator_ = nullptr;
449 if (this->output_cdr_buffer_allocator_ != nullptr)
451 this->output_cdr_buffer_allocator_->remove ();
452 delete this->output_cdr_buffer_allocator_;
453 this->output_cdr_buffer_allocator_ = nullptr;
456 if (this->output_cdr_msgblock_allocator_ != nullptr)
458 this->output_cdr_msgblock_allocator_->remove ();
459 delete this->output_cdr_msgblock_allocator_;
460 this->output_cdr_msgblock_allocator_ = nullptr;
463 if (this->amh_response_handler_allocator_ != nullptr)
465 this->amh_response_handler_allocator_->remove ();
466 delete this->amh_response_handler_allocator_;
467 this->amh_response_handler_allocator_ = nullptr;
470 if (this->ami_response_handler_allocator_ != nullptr)
472 this->ami_response_handler_allocator_->remove ();
473 delete this->ami_response_handler_allocator_;
474 this->ami_response_handler_allocator_ = nullptr;
478 void
479 TAO_Thread_Lane_Resources::shutdown_reactor ()
481 TAO_Leader_Follower &leader_follower = this->leader_follower ();
483 ACE_GUARD (TAO_SYNCH_MUTEX,
484 ace_mon,
485 leader_follower.lock ());
487 ACE_Reactor *reactor = leader_follower.reactor ();
489 // Wakeup all the threads waiting blocked in the event loop, this
490 // does not guarantee that they will all go away, but reduces the
491 // load on the POA....
493 // If there are some client threads running we have to wait until
494 // they finish, when the last one does it will shutdown the reactor
495 // for us. Meanwhile no new requests will be accepted because the
496 // POA will not process them.
497 if (!this->orb_core_.resource_factory ()->drop_replies_during_shutdown () &&
498 leader_follower.has_clients ())
500 reactor->wakeup_all_threads ();
502 else
504 // End the reactor if we want shutdown dropping replies along the
505 // way.
506 reactor->end_reactor_event_loop ();
510 void
511 TAO_Thread_Lane_Resources::close_all_transports ()
513 // If we have no-drop-reply strategy or already fininalized simply return.
514 if (!this->orb_core_.resource_factory ()->drop_replies_during_shutdown () ||
515 this->transport_cache_ == nullptr)
516 return;
518 // Set of handlers still in the connection cache.
519 TAO::Connection_Handler_Set handlers;
521 // Close the transport cache and return the handlers that were still
522 // registered. The cache will decrease the #REFCOUNT# on the
523 // handler when it removes the handler from cache. However,
524 // #REFCOUNT# is increased when the handler is placed in the handler
525 // set.
526 this->transport_cache_->close (handlers);
528 // Go through the handler set, closing the connections and removing
529 // the references.
530 TAO_Connection_Handler **handler = nullptr;
532 for (TAO::Connection_Handler_Set::iterator iter (handlers);
533 iter.next (handler);
534 iter.advance ())
536 // Connection is closed. Potential removal from the Reactor.
537 (*handler)->close_connection ();
539 // #REFCOUNT# related to the handler set decreases.
540 (*handler)->transport ()->remove_reference ();
544 TAO_END_VERSIONED_NAMESPACE_DECL