Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Bug_2936_Regression / DllORB.cpp
blobd67f1026da4719159c1b2c8f6cedc89094c5c61c
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 mv_orb_ = CORBA::ORB_init (argc, argv);
93 if (CORBA::is_nil (mv_orb_.in ()))
95 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil ORB\n")));
96 return -1;
99 CORBA::Object_var v_poa =
100 mv_orb_->resolve_initial_references ("RootPOA");
102 mv_rootPOA_ = PortableServer::POA::_narrow (v_poa.in ());
103 if (CORBA::is_nil (mv_rootPOA_.in ()))
105 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil RootPOA\n")));
106 return -1;
109 mv_poaManager_ = mv_rootPOA_->the_POAManager ();
110 if (CORBA::is_nil (mv_poaManager_.in ()))
112 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil POAManager\n")));
113 return -1;
116 mv_poaManager_->activate ();
118 catch (...)
120 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
121 return -1;
124 mp_barrier_ = new ACE_Thread_Barrier (threadCnt + 1);
126 this->activate (THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED, threadCnt);
127 mp_barrier_->wait ();
129 return 0;
134 DllORB::fini ()
138 mv_poaManager_->deactivate (1, 1);
139 mv_poaManager_ = PortableServer::POAManager::_nil ();
141 // attempt to protect against sporadic BAD_INV_ORDER exceptions
142 ACE_OS::sleep (ACE_Time_Value (0, 500));
144 mv_orb_->shutdown (true);
146 catch (...)
148 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
149 return -1;
152 // wait for our threads to finish
153 this->wait ();
155 delete mp_barrier_;
156 mp_barrier_ = 0;
160 mv_orb_->destroy ();
161 mv_orb_ = CORBA::ORB::_nil ();
163 catch (const CORBA::Exception& ex)
165 ex._tao_print_exception ("Exception caught:");
166 return -1;
169 if (failPrePostInit_ < 3)
171 // -----------------------------------------------------------------
172 // Post-ORB finalization steps necessary for proper DLL ORB
173 // support.
174 // -----------------------------------------------------------------
175 // Explicitly clean up singletons created by TAO before
176 // unloading this module.
177 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance ();
178 int result = p_tsm->fini ();
179 if (result == -1)
181 if (failPrePostInit_ == 0)
183 ACE_DEBUG ((LM_ERROR,
184 ACE_TEXT ("Post-ORB finalization failed.\n")));
185 return -1;
187 else if (failPrePostInit_ < 2)
189 ACE_DEBUG ((LM_WARNING,
190 ACE_TEXT ("Post-ORB finalization failed (ignored due to FailPrePostInit setting).\n")));
192 else
194 ACE_DEBUG ((LM_INFO,
195 ACE_TEXT ("Post-ORB finalization failed (ignored due to FailPrePostInit setting).\n")));
198 else
200 ACE_DEBUG ((LM_INFO,
201 ACE_TEXT ("Post-ORB finalization done.\n")));
205 return 0;
209 int DllORB::svc ()
211 mp_barrier_->wait ();
213 int result = 0;
217 mv_orb_->run ();
219 catch (...)
221 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
222 result = 1;
225 return result;
229 ACE_FACTORY_DEFINE (bug2936, DllORB)