Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / ORB_Local_Config / Bug_2612 / DllOrb.cpp
blob946e30b80305508ffb1a7b2a93ec867ca236f17f
1 /*
2 * @author Iliyan jeliazkov <iliyan@ociweb.com>
3 * @author Lothar Werzinger <lothar@tradescape.biz>
4 */
6 #include "ace/Arg_Shifter.h"
7 #include "ace/SString.h"
8 #include "ace/OS_NS_unistd.h"
10 #include "tao/corba.h"
11 #include "tao/TAO_Singleton_Manager.h"
13 #include "DllOrb.h"
15 DllOrb::DllOrb (int nthreads)
17 m_nthreads_ (nthreads),
18 m_failPrePostInit(0),
19 #if defined (ACE_HAS_THREADS)
20 mp_barrier(0),
21 #endif
22 mv_orb(),
23 mv_rootPOA()
27 DllOrb::~DllOrb () noexcept
29 #if defined (ACE_HAS_THREADS)
30 delete mp_barrier;
31 #endif
34 int DllOrb::init (int argc, ACE_TCHAR *argv[])
36 int result = 0;
37 int threadCnt = this->m_nthreads_;
39 try
41 ACE_Arg_Shifter as (argc, argv);
42 const ACE_TCHAR *currentArg = 0;
43 while (as.is_anything_left ())
45 if (0 != (currentArg = as.get_the_parameter (ACE_TEXT ("-t"))))
47 int num = ACE_OS::atoi (currentArg);
48 if (num >= 1)
49 threadCnt = num;
50 as.consume_arg ();
52 else
53 as.ignore_arg ();
56 if (m_failPrePostInit < 3)
58 ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Pre-ORB initialization ...\n"));
60 // -----------------------------------------------------------------
61 // Pre-ORB initialization steps necessary for proper DLL ORB
62 // support.
63 // -----------------------------------------------------------------
64 // Make sure TAO's singleton manager is initialized, and set to not
65 // register itself with the ACE_Object_Manager since it is under the
66 // control of the Service Configurator. If we register with the
67 // ACE_Object_Manager, then the ACE_Object_Manager will still hold
68 // (dangling) references to instances of objects created by this
69 // module and destroyed by this object when it is dynamically
70 // unloaded.
71 int register_with_object_manager = 0;
72 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance ();
73 result = p_tsm->init (register_with_object_manager);
75 if (result == -1 && m_failPrePostInit == 0)
76 return -1;
79 // Initialize the ORB
80 mv_orb = CORBA::ORB_init (argc, argv);
81 if (CORBA::is_nil (mv_orb.in ()))
82 return -1;
84 CORBA::Object_var v_poa = mv_orb->resolve_initial_references ("RootPOA");
86 mv_rootPOA = PortableServer::POA::_narrow (v_poa.in ());
87 if (CORBA::is_nil (mv_rootPOA.in ()))
88 return -1;
90 mv_poaManager = mv_rootPOA->the_POAManager ();
91 if (CORBA::is_nil (mv_poaManager.in ()))
92 return -1;
94 mv_poaManager->activate ();
96 catch(CORBA::Exception& ex)
98 ex._tao_print_exception (ACE_TEXT ("(%P|%t) init failed:"));
99 return -1;
101 catch (...)
103 ACE_ERROR_RETURN ((LM_ERROR,
104 ACE_TEXT ("(%P|%t) init failed\n")),
105 -1);
108 #if defined (ACE_HAS_THREADS)
109 mp_barrier = new ACE_Thread_Barrier (threadCnt + 1);
111 this->activate(
112 THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED,
113 threadCnt);
114 mp_barrier->wait ();
115 #endif
117 return 0;
120 int DllOrb::fini ()
122 int result;
126 #if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO) && !defined (TAO_HAS_MINIMUM_POA)
127 mv_poaManager->deactivate(1, 1);
128 #endif
129 mv_poaManager = PortableServer::POAManager::_nil();
131 // attempt to protect against sporadic BAD_INV_ORDER exceptions
132 ACE_OS::sleep(ACE_Time_Value(0, 500));
134 mv_orb->shutdown(true);
136 catch(CORBA::Exception& ex)
138 ex._tao_print_exception (ACE_TEXT ("(%P|%t) fini failed:"));
139 return -1;
141 catch(...)
143 ACE_ERROR_RETURN ((LM_ERROR,
144 ACE_TEXT ("(%P|%t) fini failed to deactivate/shutdown\n")),
145 -1);
149 // wait for our threads to finish
150 this->wait();
152 #if defined (ACE_HAS_THREADS)
153 delete mp_barrier;
154 mp_barrier = 0;
155 #endif
159 mv_orb->destroy();
160 mv_orb = CORBA::ORB::_nil();
162 catch(CORBA::Exception& ex)
164 ex._tao_print_exception (
165 ACE_TEXT (
166 "(%P|%t) init failed to destroy the orb:"));
167 return -1;
170 if (m_failPrePostInit < 3)
172 ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Post-ORB finalization ...\n"));
174 // -----------------------------------------------------------------
175 // Post-ORB finalization steps necessary for proper DLL ORB
176 // support.
177 // -----------------------------------------------------------------
178 // Explicitly clean up singletons created by TAO before
179 // unloading this module.
180 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance();
181 result = p_tsm->fini();
182 if (result == -1 && m_failPrePostInit == 0)
183 ACE_ERROR_RETURN ((LM_ERROR,
184 ACE_TEXT ("(%P|%t) fini failed to destroy TAO_Singleton_Manager\n")),
185 -1);
186 } /* end of if */
188 return 0;
191 int DllOrb::svc ()
193 #if defined (ACE_HAS_THREADS)
194 mp_barrier->wait();
195 #endif
199 mv_orb->run();
201 catch(CORBA::BAD_INV_ORDER const & rc_ex)
203 const CORBA::ULong VMCID = rc_ex.minor() & 0xFFFFF000U;
204 const CORBA::ULong minorCode = rc_ex.minor() & 0xFFFU;
205 if (VMCID != CORBA::OMGVMCID || minorCode != 4)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 ACE_TEXT ("(%P|%t) svc exits (-1)\n")),
208 -1);
210 catch(CORBA::Exception& ex)
212 ex._tao_print_exception (ACE_TEXT ("(%P|%t) svc - orb->run() failed:"));
213 return -1;
215 catch(...)
217 ACE_ERROR_RETURN ((LM_ERROR,
218 ACE_TEXT ("(%P|%t) svc got some exception\n")),
219 -1);
221 return 0;
224 ACE_FACTORY_DEFINE (DllOrb, DllOrb)