Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / CollocationLockup / CollocationLockup.cpp
blobb71122bc23b3a2ca43208ca4009b5ac92e7fb47a
1 /**
2 * CollocationLockup.cpp
3 * This is regression test against bug #2130.
4 * It tests a deadlock between a thread making a collocated invocation
5 * (which holds a lock on the ORB core and attempts to lock the POA) and a
6 * thread activating a servant (which locks the POA and attempts to lock the
7 * ORB core).
9 * This test was developed from a test submitted by Rick Marlborough for bug
10 * #2297 and refined by Paul Calabrese.
13 #include "ABS.h"
14 #include "SimpleNamingServiceC.h"
16 #include "ace/Thread_Manager.h"
17 #include "ace/SString.h"
19 namespace
21 #if defined (ACE_OPENVMS)
22 // less threads on OpenVMS otherwise this test
23 // (although working correctly) takes far too long.
24 const size_t N_THREADS = 10;
25 #else
26 const size_t N_THREADS = 20;
27 #endif
28 const size_t N_ITERATIONS = 100;
31 class A_i : public virtual POA_A
35 class B_i : public virtual POA_C
37 public:
38 B_i (PortableServer::POA_ptr poa)
39 : poa_ (PortableServer::POA::_duplicate (poa))
42 virtual A_ptr
43 makeA ()
45 PortableServer::ServantBase_var servant = new A_i;
46 PortableServer::ObjectId_var tmp = this->poa_->activate_object (servant.in());
47 CORBA::Object_var obj = this->poa_->servant_to_reference (servant.in());
48 return A::_narrow (obj.in ());
51 private:
52 PortableServer::POA_var poa_;
55 // Thread for ORB->run()
56 ACE_THR_FUNC_RETURN OrbRunThread (void*);
58 // Thread to run the test
59 ACE_THR_FUNC_RETURN TestThread (void*);
61 int
62 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
64 try
66 // Normal corba init
67 CORBA::ORB_var Orb = CORBA::ORB_init (argc, argv);
69 CORBA::Object_var pPoaObj =
70 Orb->resolve_initial_references ("RootPOA");
71 PortableServer::POA_var Poa = PortableServer::POA::_narrow (pPoaObj.in ());
72 PortableServer::POAManager_var pMgr = Poa->the_POAManager ();
73 pMgr->activate ();
75 CORBA::Object_var pNSObj =
76 Orb->resolve_initial_references ("SimpleNamingService");
77 SimpleNamingService_var NameService =
78 SimpleNamingService::_narrow (pNSObj.in ());
80 if (CORBA::is_nil (NameService.in ()))
82 ACE_DEBUG ((LM_ERROR,
83 "ERROR: Could not locate the Simple Naming Service\n"));
84 return 1;
87 ACE_Thread_Manager orb_thread;
88 orb_thread.spawn (OrbRunThread, Orb.in (), THR_NEW_LWP | THR_DETACHED);
90 // Setup
91 PortableServer::ServantBase_var servant = new B_i (Poa.in ());
92 PortableServer::ObjectId_var tmp = Poa->activate_object (servant.in());
93 CORBA::Object_var b = Poa->servant_to_reference (servant.in());
95 NameService->bind (b.in ());
97 //Start threads
98 ACE_Thread_Manager threads;
99 threads.spawn_n (N_THREADS, TestThread, NameService.in ());
100 ACE_DEBUG ((LM_INFO, "All threads spawned.\n"));
102 threads.wait ();
104 Orb->shutdown (0);
105 orb_thread.wait();
106 Orb->destroy ();
107 } //destructor of ACE_Thread_Manager = implicit join
108 catch (const CORBA::Exception& ex)
110 ACE_DEBUG ((LM_ERROR, "Corba Exception: %s\n", ex._info ().c_str ()));
111 return 1;
114 return 0;
117 ACE_THR_FUNC_RETURN
118 OrbRunThread (void *arg)
120 CORBA::ORB_var Orb =
121 CORBA::ORB::_duplicate (reinterpret_cast<CORBA::ORB_ptr> (arg));
125 Orb->run ();
127 catch (const CORBA::Exception& ex)
129 ACE_DEBUG ((LM_ERROR,
130 "In OrbRunThread: Corba Exception: %s\n",
131 ex._info ().c_str ()));
133 return 0;
136 ACE_THR_FUNC_RETURN
137 TestThread (void *arg)
139 SimpleNamingService_var NameService =
140 SimpleNamingService::_duplicate (reinterpret_cast<SimpleNamingService_ptr> (arg));
144 for (size_t i (0); i < N_ITERATIONS; ++i)
146 CORBA::Object_var obj = NameService->resolve ();
147 C_var b = C::_narrow (obj.in ());
148 A_var tmp = b->makeA ();
149 if (i % 50 == 0)
150 ACE_DEBUG ((LM_INFO, "(%t) collocated call returned\n"));
153 catch (const CORBA::Exception& ex)
155 ACE_DEBUG ((LM_ERROR,
156 "In TestThread: Corba Exception: %s\n",
157 ex._info ().c_str ()));
159 return 0;