Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / CollocationLockup / CollocationLockup.cpp
blobad25d7cb7bdd0e86456c4c9c6d24e4bdbf0ac003
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 const size_t N_THREADS = 20;
22 const size_t N_ITERATIONS = 100;
25 class A_i : public virtual POA_A
29 class B_i : public virtual POA_C
31 public:
32 B_i (PortableServer::POA_ptr poa)
33 : poa_ (PortableServer::POA::_duplicate (poa))
36 virtual A_ptr
37 makeA ()
39 PortableServer::ServantBase_var servant = new A_i;
40 PortableServer::ObjectId_var tmp = this->poa_->activate_object (servant.in());
41 CORBA::Object_var obj = this->poa_->servant_to_reference (servant.in());
42 return A::_narrow (obj.in ());
45 private:
46 PortableServer::POA_var poa_;
49 // Thread for ORB->run()
50 ACE_THR_FUNC_RETURN OrbRunThread (void*);
52 // Thread to run the test
53 ACE_THR_FUNC_RETURN TestThread (void*);
55 int
56 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
58 try
60 // Normal corba init
61 CORBA::ORB_var Orb = CORBA::ORB_init (argc, argv);
63 CORBA::Object_var pPoaObj =
64 Orb->resolve_initial_references ("RootPOA");
65 PortableServer::POA_var Poa = PortableServer::POA::_narrow (pPoaObj.in ());
66 PortableServer::POAManager_var pMgr = Poa->the_POAManager ();
67 pMgr->activate ();
69 CORBA::Object_var pNSObj =
70 Orb->resolve_initial_references ("SimpleNamingService");
71 SimpleNamingService_var NameService =
72 SimpleNamingService::_narrow (pNSObj.in ());
74 if (CORBA::is_nil (NameService.in ()))
76 ACE_DEBUG ((LM_ERROR,
77 "ERROR: Could not locate the Simple Naming Service\n"));
78 return 1;
81 ACE_Thread_Manager orb_thread;
82 orb_thread.spawn (OrbRunThread, Orb.in (), THR_NEW_LWP | THR_DETACHED);
84 // Setup
85 PortableServer::ServantBase_var servant = new B_i (Poa.in ());
86 PortableServer::ObjectId_var tmp = Poa->activate_object (servant.in());
87 CORBA::Object_var b = Poa->servant_to_reference (servant.in());
89 NameService->bind (b.in ());
91 //Start threads
92 ACE_Thread_Manager threads;
93 threads.spawn_n (N_THREADS, TestThread, NameService.in ());
94 ACE_DEBUG ((LM_INFO, "All threads spawned.\n"));
96 threads.wait ();
98 Orb->shutdown (0);
99 orb_thread.wait();
100 Orb->destroy ();
101 } //destructor of ACE_Thread_Manager = implicit join
102 catch (const CORBA::Exception& ex)
104 ACE_DEBUG ((LM_ERROR, "Corba Exception: %s\n", ex._info ().c_str ()));
105 return 1;
108 return 0;
111 ACE_THR_FUNC_RETURN
112 OrbRunThread (void *arg)
114 CORBA::ORB_var Orb =
115 CORBA::ORB::_duplicate (reinterpret_cast<CORBA::ORB_ptr> (arg));
119 Orb->run ();
121 catch (const CORBA::Exception& ex)
123 ACE_DEBUG ((LM_ERROR,
124 "In OrbRunThread: Corba Exception: %s\n",
125 ex._info ().c_str ()));
127 return 0;
130 ACE_THR_FUNC_RETURN
131 TestThread (void *arg)
133 SimpleNamingService_var NameService =
134 SimpleNamingService::_duplicate (reinterpret_cast<SimpleNamingService_ptr> (arg));
138 for (size_t i (0); i < N_ITERATIONS; ++i)
140 CORBA::Object_var obj = NameService->resolve ();
141 C_var b = C::_narrow (obj.in ());
142 A_var tmp = b->makeA ();
143 if (i % 50 == 0)
144 ACE_DEBUG ((LM_INFO, "(%t) collocated call returned\n"));
147 catch (const CORBA::Exception& ex)
149 ACE_DEBUG ((LM_ERROR,
150 "In TestThread: Corba Exception: %s\n",
151 ex._info ().c_str ()));
153 return 0;