1 //=============================================================================
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
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"
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 ()),
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
38 Custom_Handler_Input_Task::svc ()
41 // call back to the driver's implementation on how to read and
43 if (this->driver_
.get_next_request () == -1)
47 this->queue_
->deactivate ();
49 "terminating input thread\n"));
53 // schedule a new timer. This method will be called from inside the
54 // <Timer_Queue_Test_Driver> class. (see Command pattern)
57 Custom_Handler_Input_Task::add_timer (void *argument
)
59 u_long useconds
= *reinterpret_cast<int *> (argument
);
60 ACE_Time_Value
interval (useconds
/ usecs_
,
62 ACE_Time_Value expire_at
= ACE_OS::gettimeofday () + interval
;
67 Custom_Handler (expire_at
),
70 int id
= queue_
->schedule (h
, 0, expire_at
);
73 ACE_ERROR_RETURN ((LM_ERROR
,
77 // We store the id into the handler, this is only used to produce
81 ACE_OS::printf ("scheduling timer %d\n",
86 // Cancel a timer. This method will be called from inside the
87 // <Timer_Queue_Test_Driver> class. (see Command pattern)
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)
100 Custom_Handler_Input_Task::list_timer (void *)
102 // Dump the timer queue contents.
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 ();
118 // Cancel the thread timer queue task "preemptively."
119 if (ACE_Thread::cancel (this->queue_
->thr_id ()) == -1)
120 ACE_ERROR ((LM_ERROR
,
123 #endif /* ACE_LACKS_PTHREAD_CANCEL */
125 // -1 indicates we are shutting down the application.
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 ());
142 ACE_DEBUG ((LM_DEBUG
,
143 "end dumping timer queue\n"));
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 ()
165 Thread_Timer_Queue_Custom_Handler_Test::display_menu ()
169 "1 <microseconds>: setups a new timer\n"
170 "2 <timerid>: removes a timer\n"
171 "3 : prints timer queue\n"
174 ACE_DEBUG ((LM_DEBUG
,
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
),
191 ACE_NEW_RETURN (cancel_cmd_
,
192 CMD (input_task_
, &Custom_Handler_Input_Task::cancel_timer
),
195 ACE_NEW_RETURN (list_cmd_
,
196 CMD (input_task_
, &Custom_Handler_Input_Task::list_timer
),
199 ACE_NEW_RETURN (shutdown_cmd_
,
200 CMD (input_task_
, &Custom_Handler_Input_Task::shutdown_timer
),
203 if (this->input_task_
.activate () == -1)
204 ACE_ERROR_RETURN ((LM_ERROR
,
205 "cannot activate input task"),
207 else if (this->timer_queue_
.activate () == -1)
208 ACE_ERROR_RETURN ((LM_ERROR
,
209 "cannot activate timer queue"),
211 else if (ACE_Thread_Manager::instance ()->wait () == -1)
212 ACE_ERROR_RETURN ((LM_ERROR
,
213 "wait on Thread_Manager failed"),