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
|
// $Id$
// This short program prints the contents of stdin to stdout sorted by
// the length of each line via the use of an ASX Message_Queue. It
// illustrates how priorities can be used for ACE Message_Queues.
#include "ace/OS_NS_stdio.h"
#include "ace/Malloc_Base.h" // To get ACE_Allocator
#include "ace/Message_Queue.h"
#include "ace/Read_Buffer.h"
#include "ace/Thread_Manager.h"
#include "ace/Service_Config.h"
#include "ace/Truncate.h"
ACE_RCSID(Message_Queue, priority_buffer, "$Id$")
#if defined (ACE_HAS_THREADS)
// Global thread manager.
static ACE_Thread_Manager thr_mgr;
// 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 (ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue)
{
// Keep looping, reading a message out of the queue, until we
// timeout or get a message with a length == 0, which signals us to
// quit.
for (;;)
{
ACE_Message_Block *mb = 0;
if (msg_queue->dequeue_head (mb) == -1)
break;
int length = ACE_Utils::truncate_cast<int> (mb->length ());
if (length > 0)
ACE_OS::puts (mb->rd_ptr ());
// Free up the buffer memory and the Message_Block.
ACE_Allocator::instance ()->free (mb->rd_ptr ());
mb->release ();
if (length == 0)
break;
}
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 (ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue)
{
ACE_Read_Buffer rb (ACE_STDIN);
// Keep reading stdin, until we reach EOF.
for (;;)
{
// Allocate a new buffer.
char *buffer = rb.read ('\n');
ACE_Message_Block *mb = 0;
if (buffer == 0)
{
// Send a 0-sized shutdown message to the other thread and
// exit.
ACE_NEW_RETURN (mb, ACE_Message_Block ((size_t) 0), 0);
if (msg_queue->enqueue_tail (mb) == -1)
ACE_ERROR ((LM_ERROR, "(%t) %p\n", "put_next"));
break;
}
// Enqueue the message in priority order.
else
{
// Allocate a new message, but have it "borrow" its memory
// from the buffer.
ACE_NEW_RETURN (mb,
ACE_Message_Block (rb.size (),
ACE_Message_Block::MB_DATA,
0,
buffer),
0);
mb->msg_priority (ACE_Utils::truncate_cast<unsigned long> (rb.size ()));
mb->wr_ptr (rb.size ());
ACE_DEBUG ((LM_DEBUG,
"enqueueing message of size %d\n",
mb->msg_priority ()));
// Enqueue in priority order.
if (msg_queue->enqueue_prio (mb) == -1)
ACE_ERROR ((LM_ERROR, "(%t) %p\n", "put_next"));
}
}
// Now read all the items out in priority order (i.e., ordered by
// the size of the lines!).
consumer (msg_queue);
return 0;
}
// Spawn off one thread that copies stdin to stdout in order of the
// size of each line.
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
// Message queue.
ACE_Message_Queue<ACE_MT_SYNCH> msg_queue (max_queue);
if (thr_mgr.spawn (ACE_THR_FUNC (producer), (void *) &msg_queue,
THR_NEW_LWP | THR_DETACHED) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "spawn"), 1);
// Wait for producer and consumer threads to exit.
thr_mgr.wait ();
return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
return 0;
}
#endif /* ACE_HAS_THREADS */
|