Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Timer_Queue / Thread_Timer_Queue_Custom_Handler_Test.cpp
blob8f6bae445ad52ed3038effdb078763a7cadab10c
1 //=============================================================================
2 /**
3 * @file Thread_Timer_Queue_Custom_Handler_Test.cpp
5 * This test exercises the <ACE_Thread_Timer_Queue_Adapter>
6 * using an <ACE_Timer_Heap>. It also demonstrates using a custom handler for
7 * timer events.
9 * @author Carlos O'Ryan <coryan@cs.wustl.edu> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu> and Alon Diamant <diamant.alon@gmail.com>
11 //=============================================================================
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_sys_time.h"
16 #include "ace/Task.h"
17 #include "ace/Timer_Heap_T.h"
18 #include "ace/Timer_Queue_Adapters.h"
20 #include "Thread_Timer_Queue_Custom_Handler_Test.h"
22 #include "ace/Condition_T.h"
23 #include "ace/Thread_Mutex.h"
25 Custom_Handler_Input_Task::Custom_Handler_Input_Task (Thread_Timer_Queue *queue,
26 Thread_Timer_Queue_Custom_Handler_Test &timer_queue_driver)
27 : ACE_Task_Base (ACE_Thread_Manager::instance ()),
28 queue_ (queue),
29 usecs_ (ACE_ONE_SECOND_IN_USECS),
30 driver_ (timer_queue_driver)
34 // Svc method is called from the thread library to read input from the
35 // user.
37 int
38 Custom_Handler_Input_Task::svc ()
40 for (;;)
41 // call back to the driver's implementation on how to read and
42 // parse input.
43 if (this->driver_.get_next_request () == -1)
44 break;
46 // we are done.
47 this->queue_->deactivate ();
48 ACE_DEBUG ((LM_DEBUG,
49 "terminating input thread\n"));
50 return 0;
53 // schedule a new timer. This method will be called from inside the
54 // <Timer_Queue_Test_Driver> class. (see Command pattern)
56 int
57 Custom_Handler_Input_Task::add_timer (void *argument)
59 u_long useconds = *reinterpret_cast<int *> (argument);
60 ACE_Time_Value interval (useconds / usecs_,
61 useconds % usecs_);
62 ACE_Time_Value expire_at = ACE_OS::gettimeofday () + interval;
64 Custom_Handler *h;
66 ACE_NEW_RETURN (h,
67 Custom_Handler (expire_at),
68 -1);
70 int id = queue_->schedule (h, 0, expire_at);
72 if (id == -1)
73 ACE_ERROR_RETURN ((LM_ERROR,
74 "schedule failed"),
75 -1);
77 // We store the id into the handler, this is only used to produce
78 // nicer messages.
79 h->set_id (id);
81 ACE_OS::printf ("scheduling timer %d\n",
82 id);
83 return 0;
86 // Cancel a timer. This method will be called from inside the
87 // <Timer_Queue_Test_Driver> class. (see Command pattern)
89 int
90 Custom_Handler_Input_Task::cancel_timer (void *argument)
92 return this->queue_->cancel (*reinterpret_cast<int *> (argument));
95 // Lists the timers in the queue. Ignores the argument. This method
96 // will be called from inside the <Timer_Queue_Test_Driver> class.
97 // (see Command pattern)
99 int
100 Custom_Handler_Input_Task::list_timer (void *)
102 // Dump the timer queue contents.
103 this->dump ();
105 return 0;
108 // Shutdown the timer queue. Return -1 indicates to the
109 // <Timer_Queue_Test_Driver> class that we are done.
112 Custom_Handler_Input_Task::shutdown_timer (void *)
114 #if defined (ACE_LACKS_PTHREAD_CANCEL)
115 // Cancel the thread timer queue task "voluntarily."
116 this->queue_->deactivate ();
117 #else
118 // Cancel the thread timer queue task "preemptively."
119 if (ACE_Thread::cancel (this->queue_->thr_id ()) == -1)
120 ACE_ERROR ((LM_ERROR,
121 "%p\n",
122 "cancel"));
123 #endif /* ACE_LACKS_PTHREAD_CANCEL */
125 // -1 indicates we are shutting down the application.
126 return -1;
129 void
130 Custom_Handler_Input_Task::dump ()
132 ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, ace_mon, this->queue_->mutex ());
134 ACE_DEBUG ((LM_DEBUG,
135 "begin dumping timer queue\n"));
137 for (Timer_Heap_Iterator i (*this->queue_->timer_queue ());
138 i.item () != 0;
139 i.next ())
140 i.item ()->dump ();
142 ACE_DEBUG ((LM_DEBUG,
143 "end dumping timer queue\n"));
146 // constructor
148 Thread_Timer_Queue_Custom_Handler_Test::Thread_Timer_Queue_Custom_Handler_Test ()
149 : input_task_ (&timer_queue_, *this)
153 Thread_Timer_Queue_Custom_Handler_Test::~Thread_Timer_Queue_Custom_Handler_Test ()
158 Thread_Timer_Queue_Custom_Handler_Test::run_test ()
160 this->init ();
161 return 0;
165 Thread_Timer_Queue_Custom_Handler_Test::display_menu ()
167 static char menu[] =
168 "Usage:\n"
169 "1 <microseconds>: setups a new timer\n"
170 "2 <timerid>: removes a timer\n"
171 "3 : prints timer queue\n"
172 "4 : exit\n";
174 ACE_DEBUG ((LM_DEBUG,
175 "%s",
176 menu));
177 return 0;
181 Thread_Timer_Queue_Custom_Handler_Test::init ()
183 typedef Command<Custom_Handler_Input_Task, Custom_Handler_Input_Task::ACTION> CMD;
185 // initialize the <Command> objects with their corresponding
186 // methods from <Custom_Handler_Input_Task>
187 ACE_NEW_RETURN (schedule_cmd_,
188 CMD (input_task_, &Custom_Handler_Input_Task::add_timer),
189 -1);
191 ACE_NEW_RETURN (cancel_cmd_,
192 CMD (input_task_, &Custom_Handler_Input_Task::cancel_timer),
193 -1);
195 ACE_NEW_RETURN (list_cmd_,
196 CMD (input_task_, &Custom_Handler_Input_Task::list_timer),
197 -1);
199 ACE_NEW_RETURN (shutdown_cmd_,
200 CMD (input_task_, &Custom_Handler_Input_Task::shutdown_timer),
201 -1);
203 if (this->input_task_.activate () == -1)
204 ACE_ERROR_RETURN ((LM_ERROR,
205 "cannot activate input task"),
206 -1);
207 else if (this->timer_queue_.activate () == -1)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 "cannot activate timer queue"),
210 -1);
211 else if (ACE_Thread_Manager::instance ()->wait () == -1)
212 ACE_ERROR_RETURN ((LM_ERROR,
213 "wait on Thread_Manager failed"),
214 -1);
215 return 0;