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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
//=============================================================================
/**
* @file Monotonic_Message_Queue_Test.cpp
*
* This is a test that verifies the time policy features of the
* ACE_Message_Queue template.
* A template instantiation based on the ACE_Monotonic_Time_Policy
* is used to demonstrate the ability for making the message queue
* timeouts independent from system time changes (time shift).
*
* @author Martin Corino <mcorino@remedy.nl>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/Timer_Queue.h"
#include "ace/Thread_Manager.h"
#include "ace/Message_Queue.h"
#include "ace/Monotonic_Time_Policy.h"
#include "ace/Synch_Traits.h"
#include "ace/Timer_Heap_T.h"
#include "ace/Event_Handler_Handle_Timeout_Upcall.h"
#include "ace/TP_Reactor.h"
#include "ace/Task_T.h"
#include "ace/Truncate.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_unistd.h"
#if defined (ACE_HAS_MONOTONIC_TIME_POLICY) && defined (ACE_HAS_MONOTONIC_CONDITIONS)
# if defined (ACE_WIN32)
# include "ace/Date_Time.h"
# endif
# if defined (ACE_HAS_THREADS)
typedef ACE_Message_Queue<ACE_MT_SYNCH, ACE_Monotonic_Time_Policy> SYNCH_QUEUE;
// Create timer queue with hr support
ACE_Timer_Queue *
create_timer_queue ()
{
ACE_Timer_Queue * tmq = 0;
typedef ACE_Timer_Heap_T<ACE_Event_Handler *,
ACE_Event_Handler_Handle_Timeout_Upcall,
ACE_SYNCH_RECURSIVE_MUTEX,
ACE_HR_Time_Policy> timer_queue_type;
ACE_NEW_RETURN (tmq, timer_queue_type (), 0);
return tmq;
}
class MyTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
MyTask () : my_reactor_ (0), my_tq_ (0) {}
virtual ~MyTask () { stop (); }
virtual int svc ();
int start (int num_threads);
int stop ();
ACE_Reactor* get_reactor ();
int create_reactor ();
private:
int delete_reactor ();
ACE_SYNCH_RECURSIVE_MUTEX lock_;
ACE_Reactor *my_reactor_;
ACE_Timer_Queue *my_tq_;
};
ACE_Reactor*
MyTask::get_reactor ()
{
return my_reactor_;
}
int
MyTask::create_reactor ()
{
ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX,
monitor,
this->lock_,
-1);
ACE_TEST_ASSERT (this->my_reactor_ == 0);
this->my_tq_ = create_timer_queue ();
ACE_TP_Reactor * pImpl = 0;
ACE_NEW_RETURN (pImpl,ACE_TP_Reactor (0, this->my_tq_), -1);
ACE_NEW_RETURN (my_reactor_,
ACE_Reactor (pImpl ,1),
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT (" (%t) Create TP_Reactor\n")));
this->reactor (my_reactor_);
return 0;
}
int
MyTask::delete_reactor ()
{
ACE_GUARD_RETURN (ACE_SYNCH_RECURSIVE_MUTEX,
monitor,
this->lock_,
-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT (" (%t) Delete TP_Reactor\n")));
this->reactor (0);
delete this->my_reactor_;
this->my_reactor_ = 0;
delete this->my_tq_;
this->my_tq_ = 0;
return 0;
}
int
MyTask::start (int num_threads)
{
if (this->activate (THR_NEW_LWP, num_threads) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to activate thread pool")),
-1);
return 0;
}
int
MyTask::stop ()
{
if (this->my_reactor_ != 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("End TP_Reactor event loop\n")));
this->my_reactor_->end_reactor_event_loop ();
}
if (this->wait () == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to stop thread pool")));
if (this->delete_reactor () == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p.\n"),
ACE_TEXT ("unable to delete reactor")));
return 0;
}
int
MyTask::svc ()
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%P|%t) MyTask started\n")));
while (this->my_reactor_->reactor_event_loop_done () == 0)
this->my_reactor_->run_reactor_event_loop ();
ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) MyTask finished\n")));
return 0;
}
class TestHandler
: public ACE_Event_Handler
{
public:
TestHandler (ACE_Reactor* reactor, SYNCH_QUEUE &mq)
: reactor_ (reactor),
mq_ (mq)
{}
virtual int handle_timeout (const ACE_Time_Value &tv,
const void *arg);
bool trigger_in(const ACE_Time_Value &delay);
private:
ACE_Reactor* reactor_;
SYNCH_QUEUE& mq_;
};
int TestHandler::handle_timeout (const ACE_Time_Value &,
const void *)
{
const char S1[] = "message";
ACE_DEBUG ((LM_DEBUG, "(%P|%t) TestHandler::handle_timeout - timeout triggered\n"));
ACE_Message_Block* mb1 = new ACE_Message_Block(S1, sizeof S1);
this->mq_.enqueue_tail (mb1);
return 0;
}
bool TestHandler::trigger_in(const ACE_Time_Value &delay)
{
ACE_DEBUG ((LM_DEBUG, "(%P|%t) TestHandler::trigger_in - scheduling timer\n"));
return -1 != reactor_->schedule_timer (this, 0, delay, ACE_Time_Value (0));
}
void set_system_time(const ACE_Time_Value& tv)
{
# if defined (ACE_WIN32)
ACE_Date_Time curdt (tv);
SYSTEMTIME sys_time;
sys_time.wDay = ACE_Utils::truncate_cast <WORD> (curdt.day ());
sys_time.wMonth = ACE_Utils::truncate_cast <WORD> (curdt.month ());
sys_time.wYear = ACE_Utils::truncate_cast <WORD> (curdt.year ());
sys_time.wHour = ACE_Utils::truncate_cast <WORD> (curdt.hour ());
sys_time.wMinute = ACE_Utils::truncate_cast <WORD> (curdt.minute ());
sys_time.wSecond = ACE_Utils::truncate_cast <WORD> (curdt.second ());
sys_time.wMilliseconds = ACE_Utils::truncate_cast <WORD> (curdt.microsec () / 1000);
if (!::SetLocalTime (&sys_time))
# else
timespec_t curts;
curts = tv;
if (ACE_OS::clock_settime (CLOCK_REALTIME, &curts) != 0)
# endif
{
ACE_DEBUG((LM_INFO,
"(%P|%t) Unable to reset OS time. Insufficient privileges or not supported.\n"));
}
}
// Ensure that the timedout dequeue_head() keeps working in case of timeshift when using monotonic timer.
static bool
timeout_test ()
{
bool status = true;
SYNCH_QUEUE mq;
MyTask task1;
task1.create_reactor ();
task1.start (1);
TestHandler test_handler (task1.get_reactor (), mq);
// The reactor of taks1 that uses a hrtimer will trigger a timeout in
// 5 seconds which will enqueue a message block in the queue. At the
// same moment we calculate a timeout for the dequeue operation for
// 3 seconds in the future. Than we set the system time 4 seconds back.
// The condition should timeout because the queue is empty and the trigger
// only fires after the condition has timed out.
// Next we start another dequeue for 3 seconds in the future which should
// return before timing out because by then the trigger should have fired.
// In case of using regular system time policy for message queue and
// dequeue timeouts the first dequeue would not have timed out because
// between calculating the timeout and starting the dequeue the system time
// shifted back 4 sec causing the trigger to fire before the timeout elapsed.
// In case timeshifting does not work because of priority problems or such
// the test should succeed.
if (!test_handler.trigger_in (ACE_Time_Value (5, 0)))
ACE_ERROR_RETURN ((LM_ERROR,
"(%P|%t) Unable to schedule trigger.\n"),
false);
if (!mq.is_empty ())
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("New queue is not empty!\n")));
status = false;
}
else
{
ACE_Message_Block *b = 0;
ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv;
tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec
// shift back in time 4 sec
set_system_time (ACE_OS::gettimeofday () - ACE_Time_Value (4, 0));
if (mq.dequeue_head (b, &tv) != -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Dequeued before timeout elapsed!\n")));
status = false;
}
else if (errno != EWOULDBLOCK)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Dequeue timeout should be EWOULDBLOCK, got")));
status = false;
}
else
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("First dequeue timed out: OK\n")));
tv = (tv.now () + ACE_Time_Value (3,0)); // Now (monotonic time) + 3 sec
if (mq.dequeue_head (b, &tv) != -1)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Second dequeue succeeded: OK\n")));
delete b;
}
else
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Second dequeue timed out!\n")));
status = false;
}
}
// restore time
set_system_time (ACE_OS::gettimeofday () + ACE_Time_Value (4, 0));
}
ACE_DEBUG((LM_INFO,
"(%P|%t) Asking worker thread to finish.\n"));
task1.stop ();
ACE_Thread_Manager::instance ()->wait ();
return status;
}
# endif /* ACE_HAS_THREADS */
int
run_main (int , ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Monotonic_Message_Queue_Test"));
int status = 0;
# if defined (ACE_HAS_THREADS)
if (!timeout_test ())
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("test failed")));
status = 1;
}
# endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return status;
}
#else
int
run_main (int , ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Monotonic_Message_Queue_Test"));
ACE_DEBUG((LM_INFO,
"(%P|%t) ACE not compiled with monotonic time.\n"));
ACE_END_TEST;
return 0;
}
#endif
|