2 //=============================================================================
4 * @file Priority_Buffer_Test.cpp
6 * This is a simple test to illustrate the priority mechanism of
7 * <ACE_Message_Queue>s. The producer uses an <ACE_Message_Queue>
8 * to enqueue a bunch of messages with different priorities which
9 * are then dequeued by the consumer.
11 * @author Prashant Jain <pjain@cs.wustl.edu> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
13 //=============================================================================
16 #include "test_config.h"
17 #include "ace/Message_Queue.h"
18 #include "ace/Thread_Manager.h"
22 #if defined (ACE_HAS_THREADS)
24 static const char ACE_ALPHABET
[] = "abcdefghijklmnopqrstuvwxyz";
26 // Global message count.
27 static int message_count
= 0;
29 // Make the queue be capable of being *very* large.
30 static const long max_queue
= LONG_MAX
;
32 // The consumer dequeues a message from the ACE_Message_Queue, writes
33 // the message to the stderr stream, and deletes the message. The
34 // producer sends a 0-sized message to inform the consumer to stop
40 ACE_Message_Queue
<ACE_MT_SYNCH
> *msg_queue
=
41 reinterpret_cast<ACE_Message_Queue
<ACE_MT_SYNCH
> *> (args
);
43 u_long cur_priority
= 27;
44 ACE_UNUSED_ARG (cur_priority
);
45 // To suppress ghs warning about unused local variable
50 // Keep looping, reading a message out of the queue, until we get a
51 // message with a length == 0, which signals us to quit.
52 for (char c
= 'z'; ; c
--)
54 ACE_Message_Block
*mb
= 0;
56 int result
= msg_queue
->dequeue_head (mb
);
63 size_t length
= mb
->length ();
67 // This isn't a "shutdown" message, so process it
69 ACE_TEST_ASSERT (c
== *mb
->rd_ptr ());
70 ACE_TEST_ASSERT (mb
->msg_priority () < cur_priority
);
71 cur_priority
= mb
->msg_priority ();
74 // Free up the buffer memory and the Message_Block. Note that
75 // the destructor of Message Block will delete the the actual
80 // This was a "shutdown" message, so break out of the loop.
84 ACE_TEST_ASSERT (local_count
== message_count
);
88 // The producer reads data from the stdin stream, creates a message,
89 // and then queues the message in the message list, where it is
90 // removed by the consumer thread. A 0-sized message is enqueued when
91 // there is no more data to read. The consumer uses this as a flag to
97 ACE_Message_Queue
<ACE_MT_SYNCH
> *msg_queue
=
98 reinterpret_cast<ACE_Message_Queue
<ACE_MT_SYNCH
> *> (args
);
100 ACE_Message_Block
*mb
= 0;
102 for (const char *c
= ACE_ALPHABET
; *c
!= '\0'; c
++)
106 // Allocate a new message
109 ACE_Message_Block (1),
114 mb
->msg_priority (message_count
);
117 // Enqueue in priority order.
118 if (msg_queue
->enqueue_prio (mb
) == -1)
119 ACE_ERROR_RETURN ((LM_ERROR
,
120 ACE_TEXT ("(%t) %p\n"),
121 ACE_TEXT ("put_next")),
125 // Now send a 0-sized shutdown message to the other thread
127 ACE_Message_Block ((size_t) 0),
130 if (msg_queue
->enqueue_tail (mb
) == -1)
131 ACE_ERROR ((LM_ERROR
,
132 ACE_TEXT ("(%t) %p\n"),
133 ACE_TEXT ("put_next")));
137 // Now read all the items out in priority order (i.e., ordered by
138 // the size of the lines!).
139 consumer (msg_queue
);
144 #endif /* ACE_HAS_THREADS */
146 // Spawn off one thread that copies stdin to stdout in order of the
147 // size of each line.
150 run_main (int, ACE_TCHAR
*[])
152 ACE_START_TEST (ACE_TEXT ("Priority_Buffer_Test"));
154 #if defined (ACE_HAS_THREADS)
156 ACE_Message_Queue
<ACE_MT_SYNCH
> msg_queue (max_queue
);
158 if (ACE_Thread_Manager::instance ()->spawn
159 (ACE_THR_FUNC (producer
),
161 THR_NEW_LWP
| THR_DETACHED
) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR
,
167 // Wait for producer and consumer threads to exit.
168 ACE_Thread_Manager::instance ()->wait ();
171 ACE_TEXT ("threads not supported on this platform\n")));
172 #endif /* ACE_HAS_THREADS */