Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Single_Threaded_POA / Single_Threaded_POA.cpp
blob31b271efa0ebe9b66707921d6aef002545012f0f
1 //=============================================================================
2 /**
3 * @file Single_Threaded_POA.cpp
5 * This program tests to make sure that two threads cannot call
6 * servants in a single threaded POA simultaneously. At the same
7 * time, it makes sure that a servant can call itself or other
8 * servants in the same POA while in an upcall.
10 * @author Irfan Pyarali
12 //=============================================================================
14 #include "testS.h"
15 #include "ace/Task.h"
16 #include "ace/OS_NS_unistd.h"
18 class test_i : public virtual POA_test
20 public:
21 test_i (PortableServer::POA_ptr poa);
23 void method ();
25 PortableServer::POA_ptr _default_POA ();
27 private:
28 PortableServer::POA_var poa_;
29 int called_self_;
32 test_i::test_i (PortableServer::POA_ptr poa)
33 : poa_ (PortableServer::POA::_duplicate (poa)),
34 called_self_ (0)
38 void
39 test_i::method ()
41 ACE_DEBUG ((LM_DEBUG,
42 "Entering Worker::svc from %t and sleeping....\n"));
44 ACE_OS::sleep (2);
46 ACE_DEBUG ((LM_DEBUG,
47 "Done resting from %t\n"));
49 if (this->called_self_ == 0)
51 this->called_self_ = 1;
53 ACE_DEBUG ((LM_DEBUG,
54 "Calling self from %t\n"));
56 PortableServer::ObjectId_var id =
57 this->poa_->activate_object (this);
59 CORBA::Object_var object = this->poa_->id_to_reference (id.in ());
61 test_var self = test::_narrow (object.in ());
63 self->method ();
67 PortableServer::POA_ptr
68 test_i::_default_POA ()
70 return PortableServer::POA::_duplicate (this->poa_.in ());
73 class Worker : public ACE_Task_Base
75 public:
76 Worker (test_ptr t);
77 int svc () override;
79 private:
80 test_var test_;
83 Worker::Worker (test_ptr t)
84 : test_ (test::_duplicate (t))
88 int
89 Worker::svc ()
91 try
93 this->test_->method ();
95 catch (const CORBA::Exception& ex)
97 ex._tao_print_exception ("Exception caught in thread");
98 return -1;
101 return 0;
105 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
109 // Initialize the ORB first.
110 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
112 // Obtain the RootPOA.
113 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
115 // Get the POA_var object from Object_var.
116 PortableServer::POA_var root_poa =
117 PortableServer::POA::_narrow (obj.in ());
119 // Get the POAManager of the RootPOA.
120 PortableServer::POAManager_var poa_manager =
121 root_poa->the_POAManager ();
123 // Policies for the new POA.
124 CORBA::PolicyList policies (2);
125 policies.length (2);
127 policies[0] =
128 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
130 policies[1] =
131 root_poa->create_thread_policy (PortableServer::SINGLE_THREAD_MODEL);
133 // Creation of the child POA.
134 PortableServer::POA_var child_poa =
135 root_poa->create_POA ("child",
136 poa_manager.in (),
137 policies);
139 // Destroy the policies
140 for (CORBA::ULong i = 0;
141 i < policies.length ();
142 ++i)
144 policies[i]->destroy ();
147 poa_manager->activate ();
149 test_i servant1 (child_poa.in ());
150 test_i servant2 (child_poa.in ());
152 PortableServer::ObjectId_var id =
153 root_poa->activate_object (&servant1);
155 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
157 test_var object1 = test::_narrow (object_act.in ());
159 id = root_poa->activate_object (&servant2);
161 object_act = root_poa->id_to_reference (id.in ());
163 test_var object2 = test::_narrow (object_act.in ());
165 Worker worker1 (object1.in ());
166 Worker worker2 (object2.in ());
168 int result =
169 worker1.activate () != 0 ||
170 worker2.activate () != 0;
171 ACE_ASSERT (result == 0);
173 result = ACE_Thread_Manager::instance ()->wait ();
174 ACE_ASSERT (result == 0);
176 // In non-debug compiles, asserts will disappear.
177 ACE_UNUSED_ARG (result);
179 root_poa->destroy (true, true);
181 orb->destroy ();
183 catch (const CORBA::Exception& ex)
185 ex._tao_print_exception ("Exception caught");
186 return -1;
189 return 0;