Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tests / RTCORBA / Dynamic_Thread_Pool / client.cpp
blobf57bdda98c69829882ec2ce5c68b89215caeb94d
1 #include "ace/Get_Opt.h"
2 #include "testC.h"
3 #include "tao/RTCORBA/RTCORBA.h"
4 #include "tao/ORB_Core.h"
5 #include "ace/Task.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "../check_supported_priorities.cpp"
9 const ACE_TCHAR *ior = ACE_TEXT("file://ior_1");
10 int iterations = 6;
11 int shutdown_server = 0;
12 bool decreased = false;
14 int
15 parse_args (int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:i:"));
18 int c;
20 while ((c = get_opts ()) != -1)
21 switch (c)
23 case 'x':
24 shutdown_server = 1;
25 break;
27 case 'k':
28 ior = get_opts.opt_arg ();
29 break;
31 case 'i':
32 iterations = ACE_OS::atoi (get_opts.opt_arg ());
33 break;
35 case '?':
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "-k <ior> "
40 "-i <iterations> "
41 "-x [shutdown server] "
42 "\n",
43 argv [0]),
44 -1);
47 // Indicates successful parsing of the command line
48 return 0;
51 class Task : public ACE_Task_Base
53 public:
54 Task (ACE_Thread_Manager &thread_manager,
55 CORBA::ORB_ptr orb);
57 int svc ();
59 CORBA::ORB_var orb_;
62 Task::Task (ACE_Thread_Manager &thread_manager,
63 CORBA::ORB_ptr orb)
64 : ACE_Task_Base (&thread_manager),
65 orb_ (CORBA::ORB::_duplicate (orb))
69 int
70 Task::svc ()
72 try
74 CORBA::Object_var object =
75 this->orb_->string_to_object (ior);
77 test_var test =
78 test::_narrow (object.in ());
80 pid_t pid =
81 ACE_OS::getpid ();
83 object =
84 this->orb_->resolve_initial_references ("RTCurrent");
86 RTCORBA::Current_var current =
87 RTCORBA::Current::_narrow (object.in ());
89 // We need to set the client thread CORBA priority
90 current->the_priority (get_implicit_thread_CORBA_priority (this->orb_.in ()));
92 CORBA::Long tc = 0;
94 for (int i = 0; i != iterations; ++i)
96 CORBA::Long mtc = 0;
97 CORBA::Long r =
98 test->method (pid,
100 mtc);
102 // Each 2 iterations sleep 5 seconds
103 if (i % 2 == 0)
104 ACE_OS::sleep (5);
106 ACE_ASSERT (r == i);
107 // Assert disappears on with optimizations on.
108 ACE_UNUSED_ARG (r);
110 if (mtc > tc)
112 // Number of threads increased, so store this.
113 ACE_DEBUG ((LM_DEBUG, "Thread count increased to %d\n", mtc));
114 tc = mtc;
116 else if (mtc < tc)
118 // Number of threads decreased!
119 ACE_DEBUG ((LM_DEBUG, "Thread count decreased to %d\n", mtc));
120 decreased = true;
121 tc = mtc;
125 ACE_OS::sleep (20);
127 CORBA::Long end = 0;
128 CORBA::Long re =
129 test->method (pid,
131 end);
133 ACE_ASSERT (re == 0);
134 // Assert disappears on with optimizations on.
135 ACE_UNUSED_ARG (re);
137 if (end != 0)
139 ACE_ERROR ((LM_ERROR, "Dynamic thread count should be 0, not %d\n", end));
142 catch (const CORBA::Exception& ex)
144 ex._tao_print_exception ("Exception caught:");
145 return -1;
148 return 0;
152 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
156 CORBA::ORB_var orb =
157 CORBA::ORB_init (argc, argv);
159 int result =
160 parse_args (argc, argv);
161 if (result != 0)
162 return result;
164 if (shutdown_server)
166 CORBA::Object_var object =
167 orb->string_to_object (ior);
169 test_var test =
170 test::_narrow (object.in ());
172 test->shutdown ();
174 else
176 // Thread Manager for managing task.
177 ACE_Thread_Manager thread_manager;
179 // Create task.
180 Task task (thread_manager,
181 orb.in ());
183 // Task activation flags.
184 long flags =
185 THR_NEW_LWP |
186 THR_JOINABLE |
187 orb->orb_core ()->orb_params ()->thread_creation_flags ();
189 // Activate task.
190 result =
191 task.activate (flags);
192 if (result == -1)
194 if (errno == EPERM)
196 ACE_ERROR_RETURN ((LM_ERROR,
197 "Cannot create thread with scheduling policy %s\n"
198 "because the user does not have the appropriate privileges, terminating program....\n"
199 "Check svc.conf options and/or run as root\n",
200 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
203 else
204 // Unexpected error.
205 ACE_ASSERT (0);
208 // Wait for task to exit.
209 result =
210 thread_manager.wait ();
211 ACE_ASSERT (result != -1);
213 if (decreased == false)
215 ACE_ERROR_RETURN ((LM_ERROR, "ERROR: Number of threads didn't decrease\n"), -1);
219 catch (const CORBA::Exception& ex)
221 ex._tao_print_exception ("Exception caught");
222 return -1;
225 return 0;