Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_3486_Regression / DllORB.cpp
blob6be97192212b10bec6508981d5464c7ece0af781
1 #include "DllORB.h"
2 #include "ace/Barrier.h"
3 #include "ace/Arg_Shifter.h"
4 #include "ace/Argv_Type_Converter.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "tao/TAO_Singleton_Manager.h"
9 DllORB::DllORB ()
10 : failPrePostInit_ (3),
11 mp_barrier_ (0),
12 mv_orb_ (),
13 mv_rootPOA_ ()
18 DllORB::~DllORB ()
20 delete mp_barrier_;
24 int
25 DllORB::init (int argc, ACE_TCHAR *argv[])
27 int threadCnt = 1;
29 try
31 ACE_Arg_Shifter as (argc, argv);
32 const ACE_TCHAR *currentArg = 0;
33 while (as.is_anything_left ())
35 if ((currentArg = as.get_the_parameter (ACE_TEXT ("-NumThreads"))))
37 int num = ACE_OS::atoi (currentArg);
38 if (num >= 1)
39 threadCnt = num;
40 as.consume_arg ();
42 else
43 as.ignore_arg ();
46 if (failPrePostInit_ < 3)
48 ACE_DEBUG ((LM_INFO,
49 ACE_TEXT ("Pre-ORB initialization ...\n")));
50 // -----------------------------------------------------------------
51 // Pre-ORB initialization steps necessary for proper DLL ORB
52 // support.
53 // -----------------------------------------------------------------
54 // Make sure TAO's singleton manager is initialized, and set to not
55 // register itself with the ACE_Object_Manager since it is under the
56 // control of the Service Configurator. If we register with the
57 // ACE_Object_Manager, then the ACE_Object_Manager will still hold
58 // (dangling) references to instances of objects created by this
59 // module and destroyed by this object when it is dynamically
60 // unloaded.
61 int register_with_object_manager = 0;
62 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance ();
63 int result = p_tsm->init (register_with_object_manager);
65 if (result == -1)
67 if (failPrePostInit_ == 0)
69 ACE_DEBUG ((LM_ERROR,
70 ACE_TEXT ("Pre-ORB initialization failed.\n")));
71 return -1;
73 else if (failPrePostInit_ < 2)
75 ACE_DEBUG ((LM_WARNING,
76 ACE_TEXT ("Pre-ORB initialization failed (ignored due to FailPrePostInit setting).\n")));
78 else
80 ACE_DEBUG ((LM_INFO,
81 ACE_TEXT ("Pre-ORB initialization failed (ignored due to FailPrePostInit setting).\n")));
84 else
86 ACE_DEBUG ((LM_INFO,
87 ACE_TEXT ("Pre-ORB initialization done.\n")));
91 // Initialize the ORB
92 ACE_Argv_Type_Converter argcon (argc, argv);
93 mv_orb_ = CORBA::ORB_init (argcon.get_argc (), argcon.get_TCHAR_argv ());
94 if (CORBA::is_nil (mv_orb_.in ()))
96 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil ORB\n")));
97 return -1;
100 CORBA::Object_var v_poa =
101 mv_orb_->resolve_initial_references ("RootPOA");
103 mv_rootPOA_ = PortableServer::POA::_narrow (v_poa.in ());
104 if (CORBA::is_nil (mv_rootPOA_.in ()))
106 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil RootPOA\n")));
107 return -1;
110 mv_poaManager_ = mv_rootPOA_->the_POAManager ();
111 if (CORBA::is_nil (mv_poaManager_.in ()))
113 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil POAManager\n")));
114 return -1;
117 mv_poaManager_->activate ();
119 catch (...)
121 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
122 return -1;
125 mp_barrier_ = new ACE_Thread_Barrier (threadCnt + 1);
127 this->activate (THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED, threadCnt);
128 mp_barrier_->wait ();
130 return 0;
135 DllORB::fini ()
139 mv_poaManager_->deactivate (1, 1);
140 mv_poaManager_ = PortableServer::POAManager::_nil ();
142 // attempt to protect against sporadic BAD_INV_ORDER exceptions
143 ACE_OS::sleep (ACE_Time_Value (0, 500));
145 mv_orb_->shutdown (1);
147 catch (...)
149 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
150 return -1;
153 // wait for our threads to finish
154 this->wait ();
156 delete mp_barrier_;
157 mp_barrier_ = 0;
161 mv_orb_->destroy ();
162 mv_orb_ = CORBA::ORB::_nil ();
164 catch (const CORBA::Exception& ex)
166 ex._tao_print_exception ("Exception caught:");
167 return -1;
170 if (failPrePostInit_ < 3)
172 // -----------------------------------------------------------------
173 // Post-ORB finalization steps necessary for proper DLL ORB
174 // support.
175 // -----------------------------------------------------------------
176 // Explicitly clean up singletons created by TAO before
177 // unloading this module.
178 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance ();
179 int result = p_tsm->fini ();
180 if (result == -1)
182 if (failPrePostInit_ == 0)
184 ACE_DEBUG ((LM_ERROR,
185 ACE_TEXT ("Post-ORB finalization failed.\n")));
186 return -1;
188 else if (failPrePostInit_ < 2)
190 ACE_DEBUG ((LM_WARNING,
191 ACE_TEXT ("Post-ORB finalization failed (ignored due to FailPrePostInit setting).\n")));
193 else
195 ACE_DEBUG ((LM_INFO,
196 ACE_TEXT ("Post-ORB finalization failed (ignored due to FailPrePostInit setting).\n")));
199 else
201 ACE_DEBUG ((LM_INFO,
202 ACE_TEXT ("Post-ORB finalization done.\n")));
206 return 0;
210 int DllORB::svc ()
212 mp_barrier_->wait ();
214 int result = 0;
218 mv_orb_->run ();
220 catch (...)
222 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
223 result = 1;
226 return result;
230 ACE_FACTORY_DEFINE (bug3486, DllORB)