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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Priority_Buffer_Test.cpp
//
// = DESCRIPTION
// This is a simple test to illustrate the priority mechanism of
// <ACE_Message_Queue>s. The producer uses an <ACE_Message_Queue>
// to enqueue a bunch of messages with different priorities which
// are then dequeued by the consumer.
//
// = AUTHOR
// Prashant Jain <pjain@cs.wustl.edu> and
// Douglas C. Schmidt <schmidt@cs.wustl.edu>
//
// ============================================================================
#include "test_config.h"
#include "ace/Message_Queue.h"
#include "ace/Thread_Manager.h"
ACE_RCSID(tests, Priority_Buffer_Test, "$Id$")
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */
#if defined (ACE_HAS_THREADS)
// Global message count.
static int count = 0;
// Make the queue be capable of being *very* large.
static const long max_queue = LONG_MAX;
// The consumer dequeues a message from the ACE_Message_Queue, writes
// the message to the stderr stream, and deletes the message. The
// producer sends a 0-sized message to inform the consumer to stop
// reading and exit.
static void *
consumer (void *args)
{
ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue =
ACE_reinterpret_cast (ACE_Message_Queue<ACE_MT_SYNCH> *,
args);
u_long cur_priority = 27;
ACE_UNUSED_ARG (cur_priority);
// To suppress ghs warning about unused local variable
// "cur_priority".
int local_count = 0;
// Keep looping, reading a message out of the queue, until we get a
// message with a length == 0, which signals us to quit.
for (char c = 'z'; ; c--)
{
ACE_Message_Block *mb = 0;
int result = msg_queue->dequeue_head (mb);
if (result == -1)
break;
local_count++;
int length = mb->length ();
if (length > 0)
{
// This isn't a "shutdown" message, so process it
// "normally."
ACE_ASSERT (c == *mb->rd_ptr ());
ACE_ASSERT (mb->msg_priority () < cur_priority);
cur_priority = mb->msg_priority ();
}
// Free up the buffer memory and the Message_Block. Note that
// the destructor of Message Block will delete the the actual
// buffer.
mb->release ();
if (length == 0)
// This was a "shutdown" message, so break out of the loop.
break;
}
ACE_ASSERT (local_count == count);
return 0;
}
// The producer reads data from the stdin stream, creates a message,
// and then queues the message in the message list, where it is
// removed by the consumer thread. A 0-sized message is enqueued when
// there is no more data to read. The consumer uses this as a flag to
// know when to exit.
static void *
producer (void *args)
{
ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue =
ACE_reinterpret_cast (ACE_Message_Queue<ACE_MT_SYNCH> *,
args);
ACE_Message_Block *mb;
for (char *c = ACE_ALPHABET; *c != '\0'; c++)
{
count++;
// Allocate a new message
ACE_NEW_RETURN (mb,
ACE_Message_Block (1),
0);
*mb->wr_ptr () = *c;
// Set the priority.
mb->msg_priority (count);
mb->wr_ptr (1);
// Enqueue in priority order.
if (msg_queue->enqueue_prio (mb) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("put_next")),
0);
}
// Now send a 0-sized shutdown message to the other thread
ACE_NEW_RETURN (mb,
ACE_Message_Block ((size_t) 0),
0);
if (msg_queue->enqueue_tail (mb) == -1)
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("put_next")));
count++;
// Now read all the items out in priority order (i.e., ordered by
// the size of the lines!).
consumer (msg_queue);
return 0;
}
#endif /* ACE_HAS_THREADS */
// Spawn off one thread that copies stdin to stdout in order of the
// size of each line.
int
main (int, ASYS_TCHAR *[])
{
ACE_START_TEST (ASYS_TEXT ("Priority_Buffer_Test"));
#if defined (ACE_HAS_THREADS)
// Message queue.
ACE_Message_Queue<ACE_MT_SYNCH> msg_queue (max_queue);
if (ACE_Thread_Manager::instance ()->spawn
(ACE_THR_FUNC (producer),
(void *) &msg_queue,
THR_NEW_LWP | THR_DETACHED) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("spawn")),
1);
// Wait for producer and consumer threads to exit.
ACE_Thread_Manager::instance ()->wait ();
#else
ACE_ERROR ((LM_INFO,
ASYS_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return 0;
}
|