1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
//=============================================================================
/**
* @file Async_Timer_Queue_Test.cpp
*
* $Id: Async_Timer_Queue_Test.cpp 91671 2010-09-08 18:39:23Z johnnyw $
*
* This test exercises the <ACE_Asynch_Timer_Queue_Adapter>
* using an <ACE_Timer_Heap>.
*
*
* @author Douglas C. Schmidt <schmidt@cs.wustl.edu> and Sergio Flores-Gaitan <sergio@cs.wustl.edu>
*/
//=============================================================================
#include "ace/OS_NS_sys_time.h"
#include "ace/Signal.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Queue_Adapters.h"
#include "Async_Timer_Queue_Test.h"
// Hook method that is called to handle the expiration of a timer.
int
Async_Timer_Handler::handle_timeout (const ACE_Time_Value &tv,
const void *arg)
{
// Print some information here (note that this is not strictly
// signal-safe since the ACE logging mechanism uses functions that
// aren't guaranteed to work in all signal handlers).
ACE_DEBUG ((LM_DEBUG,
"handle_timeout() = (%d, %d) %d\n",
tv.sec (),
tv.usec (),
arg));
// Commit suicide!
delete this;
return 0;
}
// Initialize the Singleton pointer.
Async_Timer_Queue *Async_Timer_Queue::instance_ = 0;
// Implement the Singleton logic.
Async_Timer_Queue *
Async_Timer_Queue::instance (void)
{
if (Async_Timer_Queue::instance_ == 0)
{
// Initialize with all signals enabled.
ACE_Sig_Set ss (1);
// But, don't block out SIGQUIT since we always want that
// signal to interrupt the program.
ss.sig_del (SIGQUIT);
ACE_NEW_RETURN (Async_Timer_Queue::instance_, Async_Timer_Queue (&ss), 0);
}
return Async_Timer_Queue::instance_;
}
// Sets the signal set to mask, for the timer queue.
Async_Timer_Queue::Async_Timer_Queue (ACE_Sig_Set *ss) :
tq_ (ss)
{
}
// Dump the contents of the queue when we receive ^C.
void
Async_Timer_Queue::dump (void)
{
ACE_DEBUG ((LM_DEBUG, "begin dumping timer queue\n"));
// This iterator is implicitly protected since SIGINT and SIGALRM
// signals cannot occur while it is running.
for (ACE_Timer_Heap_Iterator iter (this->tq_.timer_queue ());
iter.item () != 0;
iter.next ())
iter.item ()->dump ();
ACE_DEBUG ((LM_DEBUG, "end dumping timer queue\n"));
}
// Schedule a timer.
void
Async_Timer_Queue::schedule (u_int microsecs)
{
ACE_Time_Value tv (0, microsecs);
// Create a new Event_Handler for our timer.
ACE_Event_Handler *eh;
ACE_NEW (eh, Async_Timer_Handler);
// Schedule the timer to run in the future.
long tid = this->tq_.schedule(eh,
0, // Note that our "magic cookie" ACT is always NULL.
ACE_OS::gettimeofday () + tv);
if (tid == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "schedule_timer"));
}
// Cancel a timer.
void
Async_Timer_Queue::cancel (long timer_id)
{
ACE_DEBUG ((LM_DEBUG, "canceling %d\n", timer_id));
const void *act = 0;
if (this->tq_.cancel (timer_id, &act) == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "cancel_timer"));
// In this case, the act will be 0, but it could be a real pointer
// in other cases.
delete (ACE_Event_Handler *) act;
}
// Schedule timer hook method. This method is called from the driver.
int
Async_Timer_Queue::schedule_timer (void *argument)
{
u_long useconds = *(int *)argument;
// Schedule a timer.
Async_Timer_Queue::instance ()->schedule (useconds);
return 0;
}
// Cancel timer hook method. Is called from the driver class.
int
Async_Timer_Queue::cancel_timer (void *argument)
{
u_long id = *(int *)argument;
// Cancel a timer.
Async_Timer_Queue::instance ()->cancel (id);
return 0;
}
// Dummy list timer hook method. The listing of timers is done from a
// signal handler using SIGINT, not from the driver.
int
Async_Timer_Queue::list_timer (void *)
{
// Display an error message.
ACE_ERROR_RETURN ((LM_ERROR, "invalid input\n"), 0);
}
// Dummy shutdown timer hook method. The shutdown of the timer queue
// is done with a signal handler using SIGQUIT, not from the driver.
int
Async_Timer_Queue::shutdown_timer (void *)
{
// Display an error message.
ACE_ERROR_RETURN ((LM_ERROR, "invalid input\n"), 0);
}
// Handler for the SIGINT and SIGQUIT signals.
static void
signal_handler (int signum)
{
ACE_DEBUG ((LM_DEBUG, "handling signal %S\n", signum));
switch (signum)
{
case SIGINT:
Async_Timer_Queue::instance ()->dump ();
break;
/* NOTREACHED */
#if !defined (ACE_LACKS_UNIX_SIGNALS)
case SIGQUIT:
ACE_ERROR ((LM_ERROR, "shutting down on SIGQUIT%a\n", 1));
/* NOTREACHED */
break;
#endif
}
}
// Register the signal handlers for SIGQUIT and SIGINT. We must
// ensure that the SIGINT handler isn't interrupted by SIGALRM.
// However, SIGQUIT is never blocked...
static void
register_signal_handlers (void)
{
#if !defined (ACE_LACKS_UNIX_SIGNALS)
// Register SIGQUIT (never blocked).
ACE_Sig_Action sigquit ((ACE_SignalHandler) signal_handler, SIGQUIT);
ACE_UNUSED_ARG (sigquit);
#endif
// Don't let the SIGALRM interrupt the SIGINT handler!
ACE_Sig_Set ss;
ss.sig_add (SIGALRM);
// Register SIGINT (note that system calls will be restarted
// automatically).
ACE_Sig_Action sigint ((ACE_SignalHandler) signal_handler,
SIGINT,
ss,
SA_RESTART);
ACE_UNUSED_ARG (sigint);
}
// constructor
Async_Timer_Queue_Test_Driver::Async_Timer_Queue_Test_Driver (void)
{
}
// displays the menu of options.
int
Async_Timer_Queue_Test_Driver::display_menu (void)
{
// The menu of options provided to the user.
static char menu[] =
"****\n"
"1) schedule timer <usecs>\n"
"2) cancel timer <timer_id>\n"
"^C list timers\n"
"^\\ exit program\n";
ACE_DEBUG ((LM_DEBUG, "%s", menu));
return 0;
}
// Initializes the test driver.
int
Async_Timer_Queue_Test_Driver::init (void)
{
typedef Command<Async_Timer_Queue, Async_Timer_Queue::ACTION> CMD;
// Initialize <Command> objects with their corresponding <Input_Task> methods.
ACE_NEW_RETURN (schedule_cmd_,
CMD (*Async_Timer_Queue::instance (),
&Async_Timer_Queue::schedule_timer),
-1);
ACE_NEW_RETURN (cancel_cmd_,
CMD (*Async_Timer_Queue::instance (),
&Async_Timer_Queue::cancel_timer),
-1);
ACE_NEW_RETURN (list_cmd_,
CMD (*Async_Timer_Queue::instance (),
&Async_Timer_Queue::list_timer),
-1);
ACE_NEW_RETURN (shutdown_cmd_,
CMD (*Async_Timer_Queue::instance (),
&Async_Timer_Queue::shutdown_timer),
-1);
register_signal_handlers ();
return 0;
}
|