Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / examples / Kokyu_dsrt_schedulers / fp_example / client.cpp
blobfec3e6ec1a527a2ea98a41a7c7eb23b32bef5760
1 #include "ace/Get_Opt.h"
2 #include "ace/Task.h"
3 #include "ace/High_Res_Timer.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "tao/RTScheduling/RTScheduler_Manager.h"
6 #include "testC.h"
7 #include "FP_Scheduler.h"
9 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
10 int niterations = 5;
11 int do_shutdown = 0;
12 int enable_dynamic_scheduling = 0;
13 int enable_yield = 1;
15 /**
16 * Run a server thread
18 * Use the ACE_Task_Base class to run server threads
20 class Worker : public ACE_Task_Base
22 public:
23 Worker (CORBA::ORB_ptr orb,
24 Simple_Server_ptr server_ptr,
25 RTScheduling::Current_ptr current,
26 Fixed_Priority_Scheduler* scheduler,
27 CORBA::Short importance,
28 CORBA::Long server_load);
29 // int sleep_time);
30 // ctor
32 virtual int svc ();
33 // The thread entry point.
35 private:
36 CORBA::ORB_var orb_;
37 // The orb
39 Simple_Server_var server_;
40 RTScheduling::Current_var scheduler_current_;
41 Fixed_Priority_Scheduler* scheduler_;
42 RTCORBA::Priority prio_;
43 CORBA::Long server_load_;
44 int sleep_time_;
47 int
48 parse_args (int argc, ACE_TCHAR *argv[])
50 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:i:ds"));
51 int c;
53 while ((c = get_opts ()) != -1)
54 switch (c)
56 case 'x':
57 do_shutdown = 1;
58 break;
60 case 'k':
61 ior = get_opts.opt_arg ();
62 break;
64 case 'i':
65 niterations = ACE_OS::atoi (get_opts.opt_arg ());
66 break;
68 case 'd':
69 enable_dynamic_scheduling = 1;
70 break;
72 case 's':
73 enable_yield = 0;
74 break;
76 case '?':
77 default:
78 ACE_ERROR_RETURN ((LM_ERROR,
79 "usage: %s "
80 "-k <ior> "
81 "-i <niterations> "
82 "-d (enable dynamic scheduling)"
83 "-s (disable yield)"
84 "\n",
85 argv [0]),
86 -1);
88 // Indicates successful parsing of the command line
89 return 0;
92 int
93 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
95 Fixed_Priority_Scheduler* scheduler=0;
96 RTScheduling::Current_var current;
97 int prio;
98 int max_prio;
99 ACE_Sched_Params::Policy sched_policy = ACE_SCHED_RR;
100 int sched_scope = ACE_SCOPE_THREAD;
101 long flags;
103 if (sched_policy == ACE_SCHED_RR)
104 flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_RR;
105 else
106 flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_FIFO;
108 ACE_hthread_t main_thr_handle;
109 ACE_Thread::self (main_thr_handle);
111 max_prio = ACE_Sched_Params::priority_max (sched_policy,
112 sched_scope);
114 //FUZZ: disable check_for_lack_ACE_OS
115 ACE_Sched_Params sched_params (sched_policy, max_prio);
116 //FUZZ: enable check_for_lack_ACE_OS
118 ACE_OS::sched_params (sched_params);
120 if (ACE_Thread::getprio (main_thr_handle, prio) == -1)
122 if (errno == ENOTSUP)
124 ACE_ERROR((LM_ERROR,
125 ACE_TEXT ("getprio not supported\n")));
127 else
129 ACE_ERROR ((LM_ERROR,
130 ACE_TEXT ("%p\n")
131 ACE_TEXT ("thr_getprio failed")));
135 ACE_DEBUG ((LM_DEBUG, "(%t): main thread prio is %d\n", prio));
139 RTScheduling::Scheduler_var sched_owner;
141 CORBA::ORB_var orb =
142 CORBA::ORB_init (argc, argv);
144 if (parse_args (argc, argv) != 0)
145 return 1;
147 CORBA::Object_var object =
148 orb->string_to_object (ior);
150 Simple_Server_var server =
151 Simple_Server::_narrow (object.in ());
153 if (CORBA::is_nil (server.in ()))
155 ACE_ERROR_RETURN ((LM_ERROR,
156 "Object reference <%s> is nil.\n",
157 ior),
161 if (enable_dynamic_scheduling)
163 ACE_DEBUG ((LM_DEBUG, "Dyn Sched enabled\n"));
164 CORBA::Object_var manager_obj =
165 orb->resolve_initial_references ("RTSchedulerManager");
167 TAO_RTScheduler_Manager_var manager =
168 TAO_RTScheduler_Manager::_narrow (manager_obj.in ());
170 Kokyu::DSRT_Dispatcher_Impl_t disp_impl_type;
171 if (enable_yield)
173 disp_impl_type = Kokyu::DSRT_CV_BASED;
175 else
177 disp_impl_type = Kokyu::DSRT_OS_BASED;
180 ACE_NEW_RETURN (scheduler,
181 Fixed_Priority_Scheduler (orb.in (),
182 disp_impl_type,
183 sched_policy,
184 sched_scope), -1);
185 sched_owner = scheduler;
187 manager->rtscheduler (scheduler);
189 CORBA::Object_var object =
190 orb->resolve_initial_references ("RTScheduler_Current");
192 current =
193 RTScheduling::Current::_narrow (object.in ());
196 Worker worker1 (orb.in (),
197 server.in (),
198 current.in (),
199 scheduler, 10, 15);
201 if (worker1.activate (flags, 1, 0, max_prio) != 0)
203 ACE_ERROR ((LM_ERROR,
204 "(%t|%T) cannot activate worker thread.\n"));
207 ACE_OS::sleep(2);
209 Worker worker2 (orb.in (),
210 server.in (),
211 current.in (),
212 scheduler, 12, 5);
214 if (worker2.activate (flags, 1, 0, max_prio) != 0)
216 ACE_ERROR ((LM_ERROR,
217 "(%t|%T) cannot activate scheduler thread in RT mode.\n"));
220 worker1.wait ();
221 worker2.wait ();
223 ACE_DEBUG ((LM_DEBUG,
224 "(%t): wait for worker threads done in main thread\n"));
226 if (do_shutdown)
228 if (enable_dynamic_scheduling)
230 FP_Scheduling::SegmentSchedulingParameter sched_param;
231 sched_param.base_priority = 0;
232 CORBA::Policy_var sched_param_policy
233 = scheduler->create_segment_scheduling_parameter (sched_param);
234 CORBA::Policy_ptr implicit_sched_param = 0;
235 current->begin_scheduling_segment (0,
236 sched_param_policy.in (),
237 implicit_sched_param);
240 ACE_DEBUG ((LM_DEBUG, "(%t): about to call server shutdown\n"));
241 server->shutdown ();
243 ACE_DEBUG ((LM_DEBUG, "after shutdown call in main thread\n"));
246 if (enable_dynamic_scheduling)
248 current->end_scheduling_segment (0);
252 scheduler->shutdown ();
253 ACE_DEBUG ((LM_DEBUG, "scheduler shutdown done\n"));
255 orb->destroy ();
257 catch (const CORBA::Exception& ex)
259 ex._tao_print_exception ("Exception caught:");
260 return 1;
263 return 0;
266 // ****************************************************************
268 Worker::Worker (CORBA::ORB_ptr orb,
269 Simple_Server_ptr server_ptr,
270 RTScheduling::Current_ptr current,
271 Fixed_Priority_Scheduler* scheduler,
272 RTCORBA::Priority prio,
273 CORBA::Long server_load)
274 // int sleep_time)
275 : orb_ (CORBA::ORB::_duplicate (orb)),
276 server_ (Simple_Server::_duplicate (server_ptr)),
277 scheduler_current_ (RTScheduling::Current::_duplicate (current)),
278 scheduler_ (scheduler),
279 prio_ (prio),
280 server_load_ (server_load)
281 // sleep_time_ (sleep_time)
286 Worker::svc ()
288 const char * name = 0;
290 ACE_DEBUG ((LM_DEBUG, "(%t|%T):about to sleep for %d sec\n", sleep_time_));
291 ACE_OS::sleep (sleep_time_);
292 ACE_DEBUG ((LM_DEBUG, "(%t|%T):woke up from sleep for %d sec\n", sleep_time_));
294 ACE_hthread_t thr_handle;
295 ACE_Thread::self (thr_handle);
296 int prio;
298 if (ACE_Thread::getprio (thr_handle, prio) == -1)
300 if (errno == ENOTSUP)
302 ACE_ERROR((LM_ERROR,
303 ACE_TEXT ("getprio not supported\n")));
305 else
307 ACE_ERROR ((LM_ERROR,
308 ACE_TEXT ("%p\n")
309 ACE_TEXT ("thr_getprio failed")));
313 ACE_DEBUG ((LM_DEBUG, "(%t|%T) worker activated with prio %d\n", prio));
315 if (enable_dynamic_scheduling)
317 FP_Scheduling::SegmentSchedulingParameter sched_param;
318 sched_param.base_priority = prio_;
319 CORBA::Policy_var sched_param_policy
320 = scheduler_->create_segment_scheduling_parameter (sched_param);
322 CORBA::Policy_ptr implicit_sched_param = 0;
323 ACE_DEBUG ((LM_DEBUG, "(%t|%T):before begin_sched_segment\n"));
324 scheduler_current_->begin_scheduling_segment (name,
325 sched_param_policy.in (),
326 implicit_sched_param);
328 ACE_DEBUG ((LM_DEBUG, "(%t|%T):after begin_sched_segment\n"));
331 ACE_DEBUG ((LM_DEBUG, "(%t|%T):about to make two way call\n"));
332 server_->test_method (server_load_);
334 ACE_DEBUG ((LM_DEBUG, "(%t|%T):two way call done\n"));
336 if (enable_dynamic_scheduling)
338 scheduler_current_->end_scheduling_segment (name);
341 ACE_DEBUG ((LM_DEBUG, "client worker thread (%t) done\n"));
343 return 0;