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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Message_Queue_Notification_Test.cpp
//
// = DESCRIPTION
// There are two tests that test 2 different notification
// mechanisms in Message Queue.
//
// The first test illustrates the notification mechanisms in
// Message_Queue and its integration with Reactor.
//
// Note the following things about this part of the test:
//
// 1. Multiple threads are not required.
// 2. You do not have to explicitly notify the Reactor
// 3. This code will work the same with any Reactor Implementation
// 4. handle_input, handle_exception, handle_output are the only
// callbacks supported by this mechanism
// 5. The notification mechanism need not notify the Reactor. You can
// write your own strategy classes that can do whatever application
// specific behavior you want.
//
// The second test also makes sure the high/low water mark
// signaling mechanism works flawlessly.
//
// = AUTHOR
// Irfan Pyarali and Nanbor Wang.
//
// ============================================================================
#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/Strategies.h"
#include "ace/Task.h"
ACE_RCSID(tests, Message_Queue_Notifications_Test, "$Id$")
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */
static int iterations = 10;
static const size_t worker_threads = 2;
static const char * default_message = "ACE RULES";
static const size_t default_high_water_mark = 20;
static const size_t default_low_water_mark = 10;
static const int watermark_iterations = 2 * default_high_water_mark;
class Message_Handler : public ACE_Task<ACE_NULL_SYNCH>
{
// = TITLE
// This class implements a notification strategy for the Reactor.
public:
// = Initialization and termination.
Message_Handler (ACE_Reactor &reactor);
// Constructor.
// = Demuxing hooks.
virtual int handle_input (ACE_HANDLE);
virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);
virtual int handle_exception (ACE_HANDLE fd = ACE_INVALID_HANDLE);
private:
int process_message (void);
void make_message (void);
ACE_Reactor_Notification_Strategy notification_strategy_;
};
class Watermark_Test : public ACE_Task<ACE_SYNCH>
{
// = TITLE
// This class test the correct functioning of build-in flow
// control machanism in ACE_Task.
public:
Watermark_Test (void);
virtual int svc (void);
int consumer (void);
int producer (void);
int put_message (ACE_Time_Value* timeout = 0);
int get_message (void);
void print_producer_debug_message (void);
private:
const size_t len_;
const size_t hwm_;
const size_t lwm_;
ACE_Atomic_Op <ACE_SYNCH_MUTEX, int> role_;
ACE_MT (ACE_Barrier mq_full_);
ACE_MT (ACE_Barrier mq_low_water_mark_hit_);
};
Message_Handler::Message_Handler (ACE_Reactor &reactor)
// First time handle_input will be called
: notification_strategy_ (&reactor,
this,
ACE_Event_Handler::READ_MASK)
{
this->msg_queue ()->notification_strategy (&this->notification_strategy_);
this->make_message ();
}
int
Message_Handler::handle_input (ACE_HANDLE)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("Message_Handler::handle_input\n")));
// Next time handle_output will be called.
this->notification_strategy_.mask (ACE_Event_Handler::WRITE_MASK);
return process_message ();
}
int
Message_Handler::handle_output (ACE_HANDLE fd)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("Message_Handler::handle_output\n")));
ACE_UNUSED_ARG (fd);
// Next time handle_exception will be called.
this->notification_strategy_.mask (ACE_Event_Handler::EXCEPT_MASK);
return process_message ();
}
int
Message_Handler::handle_exception (ACE_HANDLE fd)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("Message_Handler::handle_exception\n")));
ACE_UNUSED_ARG (fd);
// Next time handle_input will be called.
this->notification_strategy_.mask (ACE_Event_Handler::READ_MASK);
return this->process_message ();
}
int
Message_Handler::process_message (void)
{
ACE_Message_Block *mb;
if (this->getq (mb,
(ACE_Time_Value *) &ACE_Time_Value::zero) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("dequeue_head")),
-1);
else
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("message received = %s\n"),
mb->rd_ptr ()));
mb->release ();
}
this->make_message ();
return 0;
}
void
Message_Handler::make_message (void)
{
if (--iterations > 0)
{
ACE_Message_Block *mb;
ACE_NEW (mb,
ACE_Message_Block ((char *) ASYS_TEXT ("hello")));
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("sending message\n")));
this->putq (mb);
}
}
Watermark_Test::Watermark_Test (void)
: len_ (ACE_OS::strlen (default_message) + 1),
hwm_ (this->len_ * default_high_water_mark),
lwm_ (this->len_ * default_low_water_mark),
role_ (0)
#if defined (ACE_HAS_THREADS)
, mq_full_ (worker_threads),
mq_low_water_mark_hit_ (worker_threads)
#endif /* ACE_HAS_THREADS */
{
this->water_marks (ACE_IO_Cntl_Msg::SET_LWM,
this->lwm_);
this->water_marks (ACE_IO_Cntl_Msg::SET_HWM,
this->hwm_);
}
int
Watermark_Test::producer (void)
{
int i = watermark_iterations;
for (int hwm = this->hwm_;
hwm >= 0 ;
hwm -= this->len_)
{
this->put_message ();
this->print_producer_debug_message ();
i--;
}
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) Producer: High water mark hit ---- \n"));
ACE_MT (this->mq_full_.wait ());
// The following put_message should block until the message queue
// has dropped under the lwm.
this->put_message ();
ACE_ASSERT (this->msg_queue ()-> message_bytes () <= this->lwm_ + this->len_);
this->print_producer_debug_message ();
for (i--; i >= 0 ; i--)
{
this->put_message ();
this->print_producer_debug_message ();
}
return 0;
}
int
Watermark_Test::consumer (void)
{
ACE_MT (this->mq_full_.wait ());
ACE_OS::sleep (1);
// Let producer proceed and block in putq.
for (int i = watermark_iterations; i >= 0; i--)
{
this->get_message ();
ACE_OS::sleep (0);
}
return 0;
}
int
Watermark_Test::get_message (void)
{
ACE_Message_Block *mb;
if (this->getq (mb) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("dequeue_head")),
-1);
else
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%P|%t) Consumer: message size = %3d, ")
ASYS_TEXT ("message count = %3d\n"),
this->msg_queue ()-> message_bytes (),
this->msg_queue ()-> message_count ()));
mb->release ();
}
return 0;
}
int
Watermark_Test::put_message (ACE_Time_Value *timeout)
{
ACE_Message_Block *mb;
ACE_NEW_RETURN (mb,
ACE_Message_Block (default_message,
this->len_),
-1);
return this->putq (mb, timeout);
}
void
Watermark_Test::print_producer_debug_message (void)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%P|%t) Producer: message size = %3d, ")
ASYS_TEXT ("message count = %3d\n"),
this->msg_queue ()-> message_bytes (),
this->msg_queue ()-> message_count ()));
}
int
Watermark_Test::svc (void)
{
// this->role_ is an Atomic_Op object.
int role = this->role_++;
switch (role)
{
case 0:
this->producer ();
break;
case 1:
this->consumer ();
break;
default:
break;
}
return 0;
}
int
main (int, ASYS_TCHAR *[])
{
ACE_START_TEST (ASYS_TEXT ("Message_Queue_Notifications_Test"));
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("Starting message queue reactive notification test...\n")));
ACE_Reactor reactor;
Message_Handler mh (reactor);
while (iterations > 0)
reactor.handle_events ();
#if defined (ACE_HAS_THREADS)
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("Starting message queue watermark test...\n")));
Watermark_Test watermark_test;
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("High water mark is %d\n")
ASYS_TEXT ("Low water mark is %d\n"),
default_high_water_mark,
default_low_water_mark));
watermark_test.activate (THR_NEW_LWP,
worker_threads);
ACE_Thread_Manager::instance ()->wait ();
#else
ACE_DEBUG ((LM_INFO,
ASYS_TEXT ("Message queue watermark test not performed because threads are not supported\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return 0;
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Atomic_Op<ACE_SYNCH_MUTEX, int>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Atomic_Op<ACE_SYNCH_MUTEX, int>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
|