1 //=============================================================================
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 //=============================================================================
16 #include "ace/OS_NS_unistd.h"
18 class test_i
: public virtual POA_test
21 test_i (PortableServer::POA_ptr poa
);
25 PortableServer::POA_ptr
_default_POA ();
28 PortableServer::POA_var poa_
;
32 test_i::test_i (PortableServer::POA_ptr poa
)
33 : poa_ (PortableServer::POA::_duplicate (poa
)),
42 "Entering Worker::svc from %t and sleeping....\n"));
47 "Done resting from %t\n"));
49 if (this->called_self_
== 0)
51 this->called_self_
= 1;
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 ());
67 PortableServer::POA_ptr
68 test_i::_default_POA ()
70 return PortableServer::POA::_duplicate (this->poa_
.in ());
73 class Worker
: public ACE_Task_Base
83 Worker::Worker (test_ptr t
)
84 : test_ (test::_duplicate (t
))
93 this->test_
->method ();
95 catch (const CORBA::Exception
& ex
)
97 ex
._tao_print_exception ("Exception caught in thread");
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);
128 root_poa
->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION
);
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",
139 // Destroy the policies
140 for (CORBA::ULong i
= 0;
141 i
< policies
.length ();
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 ());
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);
183 catch (const CORBA::Exception
& ex
)
185 ex
._tao_print_exception ("Exception caught");