Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3542_Regression / DllOrb.cpp
blob6f155b5eb3302f639371c03905038534b29e4227
1 #include "DllOrb.h"
2 #include "ace/Arg_Shifter.h"
3 #include "ace/Argv_Type_Converter.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "tao/TAO_Singleton_Manager.h"
7 DllOrb::DllOrb (void)
9 ma_barrier_(),
10 mv_orb_ (),
11 mv_rootPOA_ ()
15 DllOrb::~DllOrb (void)
19 int
20 DllOrb::init (int argc, ACE_TCHAR *argv[])
22 int threadCnt = 1;
24 try
26 ACE_Arg_Shifter as (argc, argv);
27 const ACE_TCHAR *currentArg = 0;
28 while (as.is_anything_left ())
30 if (0 != (currentArg = as.get_the_parameter (ACE_TEXT ("-NumThreads"))))
32 int num = ACE_OS::atoi (currentArg);
33 if (num >= 1)
34 threadCnt = num;
35 as.consume_arg ();
37 else
38 as.ignore_arg ();
41 // Initialize the ORB
42 mv_orb_ = CORBA::ORB_init (argc, argv);
43 if (CORBA::is_nil (mv_orb_.in ()))
45 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil ORB\n")));
46 return -1;
49 CORBA::Object_var v_poa =
50 mv_orb_->resolve_initial_references ("RootPOA");
52 mv_rootPOA_ = PortableServer::POA::_narrow (v_poa.in ());
53 if (CORBA::is_nil (mv_rootPOA_.in ()))
55 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil RootPOA\n")));
56 return -1;
59 mv_poaManager_ = mv_rootPOA_->the_POAManager ();
60 if (CORBA::is_nil (mv_poaManager_.in ()))
62 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("nil POAManager\n")));
63 return -1;
66 mv_poaManager_->activate ();
68 catch (...)
70 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
71 return -1;
74 ACE_auto_ptr_reset (ma_barrier_, new ACE_Thread_Barrier (threadCnt + 1));
76 this->activate(
77 THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED,
78 threadCnt
80 ACE_DEBUG ((LM_INFO, ACE_TEXT ("init mp_barrier->wait() ...\n")));
81 ma_barrier_->wait();
82 ACE_DEBUG ((LM_INFO, ACE_TEXT ("init mp_barrier->wait() done\n")));
84 return 0;
88 int
89 DllOrb::fini (void)
91 try
93 mv_poaManager_->deactivate (1, 1);
94 mv_poaManager_ = PortableServer::POAManager::_nil ();
96 // attempt to protect against sporadic BAD_INV_ORDER exceptions
97 ACE_OS::sleep (ACE_Time_Value (0, 500));
99 mv_orb_->shutdown (1);
101 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("wait() ...\n")));
102 // wait for our threads to finish
103 wait();
104 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("wait() done\n")));
106 ACE_auto_ptr_reset (ma_barrier_, static_cast<ACE_Thread_Barrier *> (0));
108 catch (...)
110 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("ERROR: exception\n")));
111 return -1;
116 mv_orb_->destroy ();
117 mv_orb_ = CORBA::ORB::_nil ();
119 catch (const CORBA::Exception& ex)
121 ex._tao_print_exception ("Exception caught:");
122 return -1;
125 return 0;
129 int DllOrb::svc (void)
131 ACE_DEBUG ((LM_INFO, ACE_TEXT ("svc mp_barrier->wait() ...\n")));
132 ma_barrier_->wait();
133 ACE_DEBUG ((LM_INFO, ACE_TEXT ("svc mp_barrier->wait() done\n")));
137 ACE_DEBUG ((LM_INFO, ACE_TEXT ("mv_orb->run ...\n")));
140 mv_orb_->run();
141 ACE_DEBUG ((LM_INFO, ACE_TEXT ("mv_orb->run returned\n")));
143 catch(CORBA::BAD_INV_ORDER const & rc_ex)
145 const CORBA::ULong VMCID = rc_ex.minor() & 0xFFFFF000U;
146 const CORBA::ULong minorCode = rc_ex.minor() & 0xFFFU;
147 if (VMCID == CORBA::OMGVMCID && minorCode == 4)
149 ACE_DEBUG ((LM_INFO, ACE_TEXT ("ignored 'CORBA::BAD_INV_ORDER: ORB has shutdown.'\n")));
151 else
153 throw;
156 ACE_DEBUG ((LM_INFO, ACE_TEXT ("mv_orb->run done\n")));
158 catch(...)
160 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Exception\n")));
161 return -1;
164 return 0;
165 } /* end of DllOrb::svc ( ) */
168 ACE_FACTORY_DEFINE (bug_3542, DllOrb)