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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Async_Timer_Queue_Test.cpp
//
// = DESCRIPTION
// This test exercises the <ACE_Asynch_Timer_Queue_Adapter>
// using an <ACE_Timer_List>.
//
// = AUTHORS
// Douglas C. Schmidt
//
// ============================================================================
#include "test_config.h"
#include "ace/Signal.h"
#include "ace/Timer_List.h"
class Timer_Handler : public ACE_Event_Handler
// = TITLE
// Target of the asynchronous timeout operation.
{
public:
virtual int handle_timeout (const ACE_Time_Value &tv,
const void *arg);
// Callback hook invoked by the <Timer_Queue>.
};
int
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;
}
class Async_Timer_Queue
// = TITLE
// Asynchronous Timer Queue Singleton.
{
public:
static Async_Timer_Queue *instance (void);
// Singleton access point.
void schedule (u_int microsecs);
// Schedule a timer to expire <microsecs> in the future.
void cancel (long timer_id);
// Cancel a timer with <timer_id>.
void dump (void);
// Dump the contents of the queue.
private:
Async_Timer_Queue (ACE_Sig_Set *);
// Private constructor enforces the Singleton.
static Async_Timer_Queue *instance_;
// Pointer to the timer queue.
ACE_Async_Timer_Queue_Adapter<ACE_Timer_List> tq_;
// The adapter is instantiated by an <ACE_Timer_List>.
};
// 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 to be able
// to interrupt the program with that signal.
ss.sig_del (SIGQUIT);
ACE_NEW_RETURN (Async_Timer_Queue::instance_,
Async_Timer_Queue (&ss),
0);
}
return Async_Timer_Queue::instance_;
}
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_List_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, Timer_Handler);
// Schedule the timer to run in the future.
long tid = this->tq_.schedule
(eh, 0, 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;
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;
}
// Command-line API.
static int
parse_commands (const char *buf)
{
u_int choice;
long value;
if (sscanf (buf, "%u %ld", &choice, &value) != 2)
ACE_ERROR_RETURN ((LM_ERROR, "invalid input %s", buf), -1);
switch (choice)
{
case 1: // Schedule a timer.
Async_Timer_Queue::instance ()->schedule (value);
break;
/* NOTREACHED */
case 2: // Cancel a timer.
Async_Timer_Queue::instance ()->cancel (value);
break;
/* NOTREACHED */
}
return 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 */
case SIGQUIT:
ACE_DEBUG ((LM_DEBUG, "shutting down on SIGQUIT%a\n", 1));
/* NOTREACHED */
break;
}
}
// 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)
{
// Register SIGQUIT (never blocked).
ACE_Sig_Action sigquit ((ACE_SignalHandler) signal_handler,
SIGQUIT);
ACE_UNUSED_ARG (sigquit);
// 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);
}
// 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"
"please enter your choice: ";
int
main (int, char *[])
{
// ACE_START_TEST ("Timer_Queue_Test");
register_signal_handlers ();
// Run until the user types ^\.
for (;;)
{
ACE_DEBUG ((LM_DEBUG, "%s", menu));
char buf[BUFSIZ];
// Wait for user to type commands. This call is automatically
// restarted when SIGINT or SIGALRM signals occur.
if (ACE_OS::read (ACE_STDIN, buf, sizeof buf) <= 0)
break;
// Run the command.
parse_commands (buf);
}
// ACE_END_TEST;
return 0;
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Async_Timer_Queue_Adapter<ACE_Timer_List>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Async_Timer_Queue_Adapter<ACE_Timer_List>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
|