Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / RTCORBA / Thread_Pool.cpp
blob72b409d0167e53c6462e97801fc8348e08ddff61
1 #include "tao/RTCORBA/Thread_Pool.h"
3 #if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0
5 #if ! defined (__ACE_INLINE__)
6 #include "tao/RTCORBA/Thread_Pool.inl"
7 #endif /* __ACE_INLINE__ */
9 #include "tao/Exception.h"
10 #include "tao/ORB_Core.h"
11 #include "tao/ORB_Core_TSS_Resources.h"
12 #include "tao/TSS_Resources.h"
13 #include "tao/ORB.h"
14 #include "tao/Acceptor_Registry.h"
15 #include "tao/debug.h"
16 #include "tao/RTCORBA/Priority_Mapping_Manager.h"
17 #include "tao/LF_Follower.h"
18 #include "tao/Leader_Follower.h"
19 #include <memory>
21 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
23 TAO_RT_New_Leader_Generator::TAO_RT_New_Leader_Generator (
24 TAO_Thread_Lane &lane)
25 : lane_ (lane)
29 bool
30 TAO_RT_New_Leader_Generator::no_leaders_available ()
32 // Request a new dynamic thread from the Thread Lane
33 return this->lane_.new_dynamic_thread ();
36 TAO_Thread_Pool_Threads::TAO_Thread_Pool_Threads (TAO_Thread_Lane &lane)
37 : ACE_Task_Base (lane.pool ().manager ().orb_core ().thr_mgr ()),
38 lane_ (lane)
42 int
43 TAO_Thread_Pool_Threads::svc ()
45 TAO_ORB_Core &orb_core =
46 this->lane ().pool ().manager ().orb_core ();
48 if (orb_core.has_shutdown ())
49 return 0;
51 // Set TSS resources for this thread.
52 TAO_Thread_Pool_Threads::set_tss_resources (orb_core, this->lane_);
54 try
56 // Do the work
57 this->run (orb_core);
59 catch (const ::CORBA::Exception& ex)
61 // No point propagating this exception. Print it out.
62 TAOLIB_ERROR ((LM_ERROR,
63 "orb->run() raised exception for thread %t\n"));
65 ex._tao_print_exception ("");
68 return 0;
71 int
72 TAO_Thread_Pool_Threads::run (TAO_ORB_Core &orb_core)
74 CORBA::ORB_ptr orb = orb_core.orb ();
76 // Run the ORB.
77 orb->run ();
79 return 0;
82 void
83 TAO_Thread_Pool_Threads::set_tss_resources (TAO_ORB_Core &orb_core,
84 TAO_Thread_Lane &thread_lane)
86 /// Get the ORB_Core's TSS resources.
87 TAO_ORB_Core_TSS_Resources &tss =
88 *orb_core.get_tss_resources ();
90 /// Set the lane attribute in TSS.
91 tss.lane_ = &thread_lane;
93 TAO_TSS_Resources::instance ()->rtcorba_current_priority_
94 = thread_lane.lane_priority ();
97 TAO_Dynamic_Thread_Pool_Threads::TAO_Dynamic_Thread_Pool_Threads (TAO_Thread_Lane &lane)
98 : TAO_Thread_Pool_Threads (lane)
103 TAO_Dynamic_Thread_Pool_Threads::run (TAO_ORB_Core &orb_core)
105 CORBA::ORB_ptr orb = orb_core.orb ();
107 switch (this->lane_.lifespan ())
109 case TAO_RT_ORBInitializer::TAO_RTCORBA_DT_FIXED :
111 ACE_Time_Value tv_run (this->lane_.dynamic_thread_time ());
112 orb->run (tv_run);
114 break;
115 case TAO_RT_ORBInitializer::TAO_RTCORBA_DT_IDLE :
117 // A timeout is specified, run the ORB in an idle loop, if we
118 // don't handle any operations for the given timeout we just
119 // exit the loop and this thread ends itself.
120 ACE_Time_Value tv (this->lane_.dynamic_thread_time ());
121 while (!orb_core.has_shutdown () && orb->work_pending (tv))
123 // Run the ORB for the specified timeout, this prevents looping
124 // between work_pending/handle_events
125 tv = this->lane_.dynamic_thread_time ();
126 orb->run (tv);
127 // Reset the idle timeout
128 tv = this->lane_.dynamic_thread_time ();
131 break;
132 case TAO_RT_ORBInitializer::TAO_RTCORBA_DT_INFINITIVE :
134 // No timeout specified, run the ORB until it shutdowns
135 orb->run ();
137 break;
140 if (TAO_debug_level > 7)
142 TAOLIB_DEBUG ((LM_DEBUG,
143 ACE_TEXT ("TAO Process %P Pool %d Lane %d Thread %t\n")
144 ACE_TEXT ("Current number of dynamic threads left = %d; ")
145 ACE_TEXT ("RTCorba worker thread is ending!\n"),
146 this->lane_.pool ().id (),
147 this->lane_.id (),
148 this->thr_count () - 1));
151 return 0;
154 TAO_Thread_Lane::TAO_Thread_Lane (TAO_Thread_Pool &pool,
155 CORBA::ULong id,
156 CORBA::Short lane_priority,
157 CORBA::ULong static_threads,
158 CORBA::ULong dynamic_threads,
159 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
160 ACE_Time_Value const &dynamic_thread_time)
161 : pool_ (pool),
162 id_ (id),
163 lane_priority_ (lane_priority),
164 shutdown_ (false),
165 static_threads_number_ (static_threads),
166 dynamic_threads_number_ (dynamic_threads),
167 static_threads_ (*this),
168 dynamic_threads_ (*this),
169 new_thread_generator_ (*this),
170 resources_ (pool.manager ().orb_core (),
171 &new_thread_generator_),
172 native_priority_ (TAO_INVALID_PRIORITY),
173 lifespan_ (lifespan),
174 dynamic_thread_time_ (dynamic_thread_time)
178 bool
179 TAO_Thread_Lane::new_dynamic_thread ()
181 // Note that we are checking this condition below without the lock
182 // held.
183 if (this->dynamic_threads_.thr_count () >= this->dynamic_threads_number_)
184 return false;
186 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
187 mon,
188 this->lock_,
189 false);
191 TAO_Thread_Pool_Manager &manager = this->pool_.manager ();
193 if (!manager.orb_core ().has_shutdown () && !this->shutdown_&&
194 this->dynamic_threads_.thr_count () < this->dynamic_threads_number_)
196 if (TAO_debug_level > 0)
197 TAOLIB_DEBUG ((LM_DEBUG,
198 ACE_TEXT ("TAO Process %P Pool %d Lane %d Thread %t\n")
199 ACE_TEXT ("Current number of dynamic threads = %d; ")
200 ACE_TEXT ("static threads = %d; max dynamic threads = %d\n")
201 ACE_TEXT ("No leaders available; creating new leader!\n"),
202 this->pool_.id (),
203 this->id_,
204 this->dynamic_threads_.thr_count (),
205 this->static_threads_number_,
206 this->dynamic_threads_number_));
208 int result =
209 this->create_threads_i (this->dynamic_threads_,
211 THR_BOUND | THR_DETACHED);
213 if (result != 0)
214 TAOLIB_ERROR_RETURN ((LM_ERROR,
215 ACE_TEXT ("Pool %d Lane %d Thread %t: ")
216 ACE_TEXT ("cannot create dynamic thread\n"),
217 this->pool_.id (),
218 this->id_),
219 false);
222 return true;
225 void
226 TAO_Thread_Lane::shutting_down ()
228 ACE_GUARD (TAO_SYNCH_MUTEX,
229 mon,
230 this->lock_);
232 // We are shutting down, this way we are not creating any more new dynamic
233 // threads
234 this->shutdown_ = true;
237 void
238 TAO_Thread_Lane::validate_and_map_priority ()
240 // Make sure that static_threads_number_ is not zero.
241 if (this->static_threads_number_ == 0)
242 throw ::CORBA::BAD_PARAM ();
244 // Check that the priority is in bounds.
245 if (this->lane_priority_ < RTCORBA::minPriority
246 // The line below will always be false unless the value of
247 // RTCORBA::maxPriority, which is now assigned the value of
248 // 32767, is changed in RTCORBA.pidl.
249 // || this->lane_priority_ > RTCORBA::maxPriority
252 throw ::CORBA::BAD_PARAM ();
255 CORBA::ORB_ptr orb = this->pool_.manager ().orb_core ().orb ();
257 // Get the priority mapping manager.
258 CORBA::Object_var obj =
259 orb->resolve_initial_references (TAO_OBJID_PRIORITYMAPPINGMANAGER);
261 TAO_Priority_Mapping_Manager_var mapping_manager =
262 TAO_Priority_Mapping_Manager::_narrow (obj.in ());
264 RTCORBA::PriorityMapping *pm = mapping_manager.in ()->mapping ();
266 // Map CORBA priority to native priority.
267 CORBA::Boolean const result =
268 pm->to_native (this->lane_priority_, this->native_priority_);
270 if (!result)
271 throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);
273 if (TAO_debug_level > 3)
275 TAOLIB_DEBUG ((LM_DEBUG,
276 ACE_TEXT ("TAO (%P|%t) - creating thread at ")
277 ACE_TEXT ("(corba:native) priority %d:%d\n"),
278 this->lane_priority_,
279 this->native_priority_));
283 void
284 TAO_Thread_Lane::open ()
286 // Validate and map priority.
287 this->validate_and_map_priority ();
289 char pool_lane_id[10];
290 TAO_ORB_Parameters *params =
291 this->pool ().manager ().orb_core ().orb_params ();
292 TAO_EndpointSet endpoint_set;
294 // Create a string just *:* which means all pools all thread id's
295 ACE_OS::sprintf (pool_lane_id,
296 "*:*");
298 // Get the endpoints for all
299 params->get_endpoint_set (pool_lane_id, endpoint_set);
301 // Create a string with pool:* which means all lanes for this pool
302 ACE_OS::sprintf (pool_lane_id,
303 "%d:*",
304 this->pool ().id ());
306 // Get the endpoints for this pool.
307 params->get_endpoint_set (pool_lane_id, endpoint_set);
309 // Create a string with *:lane which means a lan of all pools
310 ACE_OS::sprintf (pool_lane_id,
311 "*:%d",
312 this->id ());
314 // Get the endpoints for this lane.
315 params->get_endpoint_set (pool_lane_id, endpoint_set);
317 // Create a string with the pool:thread id.
318 ACE_OS::sprintf (pool_lane_id,
319 "%d:%d",
320 this->pool ().id (),
321 this->id ());
323 // Get the endpoints for this lane.
324 params->get_endpoint_set (pool_lane_id, endpoint_set);
326 bool ignore_address = false;
328 if (endpoint_set.is_empty ())
330 // If endpoints are not specified for this lane, use the
331 // endpoints specified for the default lane but ignore their
332 // addresses.
333 params->get_endpoint_set (TAO_DEFAULT_LANE, endpoint_set);
335 ignore_address = true;
337 else
339 // If endpoints are specified for this lane, use them with their
340 // addresses.
341 ignore_address = false;
344 // Open the acceptor registry.
345 int const result =
346 this->resources_.open_acceptor_registry (endpoint_set, ignore_address);
348 if (result == -1)
349 throw ::CORBA::INTERNAL (
350 CORBA::SystemException::_tao_minor_code (
351 TAO_ACCEPTOR_REGISTRY_OPEN_LOCATION_CODE,
353 CORBA::COMPLETED_NO);
356 TAO_Thread_Lane::~TAO_Thread_Lane ()
360 void
361 TAO_Thread_Lane::finalize ()
363 // Finalize resources.
364 this->resources_.finalize ();
367 void
368 TAO_Thread_Lane::shutdown_reactor ()
370 this->resources_.shutdown_reactor ();
373 void
374 TAO_Thread_Lane::wait ()
376 this->static_threads_.wait ();
377 this->dynamic_threads_.wait ();
381 TAO_Thread_Lane::is_collocated (const TAO_MProfile &mprofile)
383 return this->resources_.is_collocated (mprofile);
386 CORBA::ULong
387 TAO_Thread_Lane::current_threads () const
389 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
390 mon,
391 this->lock_,
394 return static_cast<CORBA::ULong> (this->static_threads_.thr_count () +
395 this->dynamic_threads_.thr_count ());
400 TAO_Thread_Lane::create_static_threads ()
402 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
403 mon,
404 this->lock_,
407 // Create static threads.
408 return this->create_threads_i (this->static_threads_,
409 this->static_threads_number_,
410 THR_NEW_LWP | THR_JOINABLE);
414 TAO_Thread_Lane::create_dynamic_threads (CORBA::ULong number_of_threads)
416 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
417 mon,
418 this->lock_,
421 return this->create_threads_i (this->dynamic_threads_,
422 number_of_threads,
423 THR_BOUND | THR_DETACHED);
427 TAO_Thread_Lane::create_threads_i (TAO_Thread_Pool_Threads &thread_pool,
428 CORBA::ULong number_of_threads,
429 long thread_flags)
431 // Overwritten parameters.
432 int force_active = 1;
434 // Default parameters.
435 int default_grp_id = -1;
436 ACE_Task_Base *default_task = 0;
437 ACE_hthread_t *default_thread_handles = 0;
438 void **default_stack = 0;
440 // Setting stack size.
441 size_t *stack_size_array = 0;
442 ACE_NEW_RETURN (stack_size_array,
443 size_t[number_of_threads],
444 -1);
445 size_t index;
446 for (index = 0;
447 index != number_of_threads;
448 ++index)
449 stack_size_array[index] =
450 this->pool ().stack_size ();
452 // Make sure the dynamically created stack size array is properly
453 // deleted.
454 std::unique_ptr<size_t[]> auto_stack_size_array (stack_size_array);
456 TAO_ORB_Core &orb_core =
457 this->pool ().manager ().orb_core ();
459 long flags =
460 thread_flags |
461 orb_core.orb_params ()->thread_creation_flags ();
463 // Activate the threads.
464 int result =
465 thread_pool.activate (flags,
466 number_of_threads,
467 force_active,
468 this->native_priority_,
469 default_grp_id,
470 default_task,
471 default_thread_handles,
472 default_stack,
473 stack_size_array);
475 if (result != 0)
476 return result;
478 return result;
481 TAO_Thread_Pool::TAO_Thread_Pool (TAO_Thread_Pool_Manager &manager,
482 CORBA::ULong id,
483 CORBA::ULong stack_size,
484 CORBA::ULong static_threads,
485 CORBA::ULong dynamic_threads,
486 CORBA::Short default_priority,
487 CORBA::Boolean allow_request_buffering,
488 CORBA::ULong max_buffered_requests,
489 CORBA::ULong max_request_buffer_size,
490 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
491 ACE_Time_Value const &dynamic_thread_time)
492 : manager_ (manager),
493 id_ (id),
494 stack_size_ (stack_size),
495 allow_borrowing_ (0),
496 allow_request_buffering_ (allow_request_buffering),
497 max_buffered_requests_ (max_buffered_requests),
498 max_request_buffer_size_ (max_request_buffer_size),
499 // lifespan_ (lifespan),
500 dynamic_thread_time_ (dynamic_thread_time),
501 lanes_ (0),
502 number_of_lanes_ (1),
503 with_lanes_ (false)
505 // No support for buffering.
506 if (allow_request_buffering)
507 throw ::CORBA::NO_IMPLEMENT ();
509 // Create one lane.
510 ACE_NEW (this->lanes_,
511 TAO_Thread_Lane *[this->number_of_lanes_]);
512 ACE_NEW (this->lanes_[0],
513 TAO_Thread_Lane (*this,
515 default_priority,
516 static_threads,
517 dynamic_threads,
518 lifespan,
519 dynamic_thread_time));
522 TAO_Thread_Pool::TAO_Thread_Pool (TAO_Thread_Pool_Manager &manager,
523 CORBA::ULong id,
524 CORBA::ULong stack_size,
525 const RTCORBA::ThreadpoolLanes &lanes,
526 CORBA::Boolean allow_borrowing,
527 CORBA::Boolean allow_request_buffering,
528 CORBA::ULong max_buffered_requests,
529 CORBA::ULong max_request_buffer_size,
530 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
531 ACE_Time_Value const &dynamic_thread_time)
532 : manager_ (manager),
533 id_ (id),
534 stack_size_ (stack_size),
535 allow_borrowing_ (allow_borrowing),
536 allow_request_buffering_ (allow_request_buffering),
537 max_buffered_requests_ (max_buffered_requests),
538 max_request_buffer_size_ (max_request_buffer_size),
539 // lifespan_ (lifespan),
540 dynamic_thread_time_ (dynamic_thread_time),
541 lanes_ (0),
542 number_of_lanes_ (lanes.length ()),
543 with_lanes_ (true)
545 // No support for buffering or borrowing.
546 if (allow_borrowing ||
547 allow_request_buffering)
548 throw ::CORBA::NO_IMPLEMENT ();
550 // Create multiple lane.
551 ACE_NEW (this->lanes_,
552 TAO_Thread_Lane *[this->number_of_lanes_]);
553 for (CORBA::ULong i = 0;
554 i != this->number_of_lanes_;
555 ++i)
556 ACE_NEW (this->lanes_[i],
557 TAO_Thread_Lane (*this,
559 lanes[i].lane_priority,
560 lanes[i].static_threads,
561 lanes[i].dynamic_threads,
562 lifespan,
563 dynamic_thread_time));
566 void
567 TAO_Thread_Pool::open ()
569 // Open all the lanes.
570 for (CORBA::ULong i = 0;
571 i != this->number_of_lanes_;
572 ++i)
574 this->lanes_[i]->open ();
578 TAO_Thread_Pool::~TAO_Thread_Pool ()
580 // Delete all the lanes.
581 for (CORBA::ULong i = 0;
582 i != this->number_of_lanes_;
583 ++i)
584 delete this->lanes_[i];
586 delete[] this->lanes_;
589 void
590 TAO_Thread_Pool::finalize ()
592 // Finalize all the lanes.
593 for (CORBA::ULong i = 0;
594 i != this->number_of_lanes_;
595 ++i)
596 this->lanes_[i]->finalize ();
599 void
600 TAO_Thread_Pool::shutdown_reactor ()
602 // Finalize all the lanes.
603 for (CORBA::ULong i = 0;
604 i != this->number_of_lanes_;
605 ++i)
606 this->lanes_[i]->shutdown_reactor ();
609 void
610 TAO_Thread_Pool::shutting_down ()
612 // Finalize all the lanes.
613 for (CORBA::ULong i = 0;
614 i != this->number_of_lanes_;
615 ++i)
616 this->lanes_[i]->shutting_down ();
620 void
621 TAO_Thread_Pool::wait ()
623 // Finalize all the lanes.
624 for (CORBA::ULong i = 0;
625 i != this->number_of_lanes_;
626 ++i)
627 this->lanes_[i]->wait ();
631 TAO_Thread_Pool::is_collocated (const TAO_MProfile &mprofile)
633 // Finalize all the lanes.
634 for (CORBA::ULong i = 0;
635 i != this->number_of_lanes_;
636 ++i)
638 int result =
639 this->lanes_[i]->is_collocated (mprofile);
641 if (result)
642 return result;
645 return 0;
649 TAO_Thread_Pool::create_static_threads ()
651 for (CORBA::ULong i = 0;
652 i != this->number_of_lanes_;
653 ++i)
655 // Ask each lane to create its set of static threads.
656 int const result = this->lanes_[i]->create_static_threads ();
658 // Return on failure.
659 if (result != 0)
660 return result;
663 // Success.
664 return 0;
667 #define TAO_THREAD_POOL_MANAGER_GUARD \
668 ACE_GUARD_THROW_EX ( \
669 TAO_SYNCH_MUTEX, \
670 mon, \
671 this->lock_, \
672 CORBA::INTERNAL ( \
673 CORBA::SystemException::_tao_minor_code ( \
674 TAO_GUARD_FAILURE, \
675 0), \
676 CORBA::COMPLETED_NO));
678 TAO_Thread_Pool_Manager::TAO_Thread_Pool_Manager (TAO_ORB_Core &orb_core)
679 : orb_core_ (orb_core),
680 thread_pools_ (),
681 thread_pool_id_counter_ (1),
682 lock_ ()
686 TAO_Thread_Pool_Manager::~TAO_Thread_Pool_Manager ()
688 // Delete all the pools.
689 for (THREAD_POOLS::ITERATOR iterator = this->thread_pools_.begin ();
690 iterator != this->thread_pools_.end ();
691 ++iterator)
692 delete (*iterator).int_id_;
695 void
696 TAO_Thread_Pool_Manager::finalize ()
698 // Finalize all the pools.
699 for (THREAD_POOLS::ITERATOR iterator = this->thread_pools_.begin ();
700 iterator != this->thread_pools_.end ();
701 ++iterator)
702 (*iterator).int_id_->finalize ();
705 void
706 TAO_Thread_Pool_Manager::shutdown_reactor ()
708 // Finalize all the pools.
709 for (THREAD_POOLS::ITERATOR iterator = this->thread_pools_.begin ();
710 iterator != this->thread_pools_.end ();
711 ++iterator)
712 (*iterator).int_id_->shutdown_reactor ();
715 void
716 TAO_Thread_Pool_Manager::wait ()
718 // Finalize all the pools.
719 for (THREAD_POOLS::ITERATOR iterator = this->thread_pools_.begin ();
720 iterator != this->thread_pools_.end ();
721 ++iterator)
722 (*iterator).int_id_->wait ();
726 TAO_Thread_Pool_Manager::is_collocated (const TAO_MProfile &mprofile)
728 // Finalize all the pools.
729 for (THREAD_POOLS::ITERATOR iterator = this->thread_pools_.begin ();
730 iterator != this->thread_pools_.end ();
731 ++iterator)
733 int const result = (*iterator).int_id_->is_collocated (mprofile);
735 if (result)
736 return result;
739 return 0;
742 RTCORBA::ThreadpoolId
743 TAO_Thread_Pool_Manager::create_threadpool (CORBA::ULong stacksize,
744 CORBA::ULong static_threads,
745 CORBA::ULong dynamic_threads,
746 RTCORBA::Priority default_priority,
747 CORBA::Boolean allow_request_buffering,
748 CORBA::ULong max_buffered_requests,
749 CORBA::ULong max_request_buffer_size,
750 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
751 ACE_Time_Value const &dynamic_thread_time)
753 TAO_THREAD_POOL_MANAGER_GUARD;
755 return this->create_threadpool_i (stacksize,
756 static_threads,
757 dynamic_threads,
758 default_priority,
759 allow_request_buffering,
760 max_buffered_requests,
761 max_request_buffer_size,
762 lifespan,
763 dynamic_thread_time);
766 RTCORBA::ThreadpoolId
767 TAO_Thread_Pool_Manager::create_threadpool_with_lanes (CORBA::ULong stacksize,
768 const RTCORBA::ThreadpoolLanes & lanes,
769 CORBA::Boolean allow_borrowing,
770 CORBA::Boolean allow_request_buffering,
771 CORBA::ULong max_buffered_requests,
772 CORBA::ULong max_request_buffer_size,
773 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
774 ACE_Time_Value const &dynamic_thread_time)
776 TAO_THREAD_POOL_MANAGER_GUARD;
778 return this->create_threadpool_with_lanes_i (stacksize,
779 lanes,
780 allow_borrowing,
781 allow_request_buffering,
782 max_buffered_requests,
783 max_request_buffer_size,
784 lifespan,
785 dynamic_thread_time);
788 void
789 TAO_Thread_Pool_Manager::destroy_threadpool (RTCORBA::ThreadpoolId threadpool)
791 TAO_Thread_Pool *tao_thread_pool = 0;
793 // The guard is just for the map, don't do a wait inside the guard, because
794 // during the wait other threads can try to access the thread pool manager
795 // also, this can be one of the threads we are waiting for, which then
796 // results in a deadlock
798 TAO_THREAD_POOL_MANAGER_GUARD;
800 // Unbind the thread pool from the map.
801 int const result = this->thread_pools_.unbind (threadpool, tao_thread_pool);
803 // If the thread pool is not found in our map.
804 if (result != 0)
805 throw RTCORBA::RTORB::InvalidThreadpool ();
808 // Mark the thread pool that we are shutting down.
809 tao_thread_pool->shutting_down ();
811 // Shutdown reactor.
812 tao_thread_pool->shutdown_reactor ();
814 // Wait for the threads.
815 tao_thread_pool->wait ();
817 // Finalize resources.
818 tao_thread_pool->finalize ();
820 // Delete the thread pool.
821 delete tao_thread_pool;
824 RTCORBA::ThreadpoolId
825 TAO_Thread_Pool_Manager::create_threadpool_i (CORBA::ULong stacksize,
826 CORBA::ULong static_threads,
827 CORBA::ULong dynamic_threads,
828 RTCORBA::Priority default_priority,
829 CORBA::Boolean allow_request_buffering,
830 CORBA::ULong max_buffered_requests,
831 CORBA::ULong max_request_buffer_size,
832 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
833 ACE_Time_Value const &dynamic_thread_time)
835 // Create the thread pool.
836 TAO_Thread_Pool *thread_pool = 0;
838 ACE_NEW_THROW_EX (thread_pool,
839 TAO_Thread_Pool (*this,
840 this->thread_pool_id_counter_,
841 stacksize,
842 static_threads,
843 dynamic_threads,
844 default_priority,
845 allow_request_buffering,
846 max_buffered_requests,
847 max_request_buffer_size,
848 lifespan,
849 dynamic_thread_time
851 CORBA::NO_MEMORY ());
853 return this->create_threadpool_helper (thread_pool);
856 RTCORBA::ThreadpoolId
857 TAO_Thread_Pool_Manager::create_threadpool_with_lanes_i (CORBA::ULong stacksize,
858 const RTCORBA::ThreadpoolLanes &lanes,
859 CORBA::Boolean allow_borrowing,
860 CORBA::Boolean allow_request_buffering,
861 CORBA::ULong max_buffered_requests,
862 CORBA::ULong max_request_buffer_size,
863 TAO_RT_ORBInitializer::TAO_RTCORBA_DT_LifeSpan lifespan,
864 ACE_Time_Value const &dynamic_thread_time)
866 // Create the thread pool.
867 TAO_Thread_Pool *thread_pool = 0;
869 ACE_NEW_THROW_EX (thread_pool,
870 TAO_Thread_Pool (*this,
871 this->thread_pool_id_counter_,
872 stacksize,
873 lanes,
874 allow_borrowing,
875 allow_request_buffering,
876 max_buffered_requests,
877 max_request_buffer_size,
878 lifespan,
879 dynamic_thread_time
881 CORBA::NO_MEMORY ());
883 return this->create_threadpool_helper (thread_pool);
886 RTCORBA::ThreadpoolId
887 TAO_Thread_Pool_Manager::create_threadpool_helper (TAO_Thread_Pool *thread_pool)
889 // Make sure of safe deletion in case of errors.
890 std::unique_ptr<TAO_Thread_Pool> safe_thread_pool (thread_pool);
892 // Open the pool.
893 thread_pool->open ();
895 // Create the static threads.
896 int result = thread_pool->create_static_threads ();
898 // Throw exception in case of errors.
899 if (result != 0)
901 // Finalize thread pool related resources.
902 thread_pool->finalize ();
904 throw ::CORBA::INTERNAL (
905 CORBA::SystemException::_tao_minor_code (
906 TAO_RTCORBA_THREAD_CREATION_LOCATION_CODE,
907 errno),
908 CORBA::COMPLETED_NO);
911 // Bind thread to internal table.
912 result =
913 this->thread_pools_.bind (this->thread_pool_id_counter_,
914 thread_pool);
916 // Throw exceptin in case of errors.
917 if (result != 0)
918 throw ::CORBA::INTERNAL ();
921 // Success.
924 // No need to delete thread pool.
925 safe_thread_pool.release ();
927 // Return current counter and perform post-increment.
928 return this->thread_pool_id_counter_++;
931 TAO_Thread_Pool *
932 TAO_Thread_Pool_Manager::get_threadpool (RTCORBA::ThreadpoolId thread_pool_id )
934 TAO_THREAD_POOL_MANAGER_GUARD;
936 TAO_Thread_Pool *thread_pool = 0;
937 int const result = thread_pools_.find (thread_pool_id, thread_pool);
939 ACE_UNUSED_ARG (result);
941 return thread_pool;
944 TAO_ORB_Core &
945 TAO_Thread_Pool_Manager::orb_core () const
947 return this->orb_core_;
950 TAO_END_VERSIONED_NAMESPACE_DECL
952 #endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */