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
|
// $Id$
#include "ace/config-lite.h"
#if defined (ACE_HAS_THREADS)
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_time.h"
#include "ace/Task.h"
#include "ace/Containers.h"
#include "ace/Synch.h"
#include "ace/SString.h"
#include "ace/Method_Request.h"
#include "ace/Future.h"
#include "ace/Activation_Queue.h"
class Worker;
class IManager
{
public:
virtual ~IManager (void) { }
virtual int return_to_work (Worker *worker) = 0;
};
// Listing 2 code/ch16
class Worker : public ACE_Task<ACE_MT_SYNCH>
{
public:
Worker (IManager *manager) : manager_(manager) { }
virtual int svc (void)
{
ACE_Thread_ID id;
thread_id_ = id;
while (1)
{
ACE_Message_Block *mb = 0;
if (this->getq (mb) == -1)
ACE_ERROR_BREAK
((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("getq")));
if (mb->msg_type () == ACE_Message_Block::MB_HANGUP)
{
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("(%t) Shutting down\n")));
mb->release ();
break;
}
// Process the message.
process_message (mb);
// Return to work.
this->manager_->return_to_work (this);
}
return 0;
}
// Listing 2
const ACE_Thread_ID& thread_id (void)
{
return this->thread_id_;
}
private:
void process_message (ACE_Message_Block *mb)
{
ACE_TRACE (ACE_TEXT ("Worker::process_message"));
int msgId;
ACE_OS::memcpy (&msgId, mb->rd_ptr (), sizeof(int));
mb->release ();
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Started processing message %d\n"),
msgId));
ACE_OS::sleep (3);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Finished processing message %d\n"),
msgId));
}
IManager *manager_;
ACE_Thread_ID thread_id_;
};
// Listing 1 code/ch16
class Manager: public ACE_Task<ACE_MT_SYNCH>, private IManager
{
public:
enum {POOL_SIZE = 5, MAX_TIMEOUT = 5};
Manager ()
: shutdown_(0), workers_lock_(), workers_cond_(workers_lock_)
{
ACE_TRACE (ACE_TEXT ("Manager::Manager"));
}
int svc (void)
{
ACE_TRACE (ACE_TEXT ("Manager::svc"));
ACE_DEBUG ((LM_INFO, ACE_TEXT ("(%t) Manager started\n")));
// Create pool.
create_worker_pool ();
while (!done ())
{
ACE_Message_Block *mb = 0;
ACE_Time_Value tv ((long)MAX_TIMEOUT);
tv += ACE_OS::time (0);
// Get a message request.
if (this->getq (mb, &tv) < 0)
{
shut_down ();
break;
}
// Choose a worker.
Worker *worker = 0;
{
ACE_GUARD_RETURN (ACE_Thread_Mutex,
worker_mon, this->workers_lock_, -1);
while (this->workers_.is_empty ())
workers_cond_.wait ();
this->workers_.dequeue_head (worker);
}
// Ask the worker to do the job.
worker->putq (mb);
}
return 0;
}
int shut_down (void);
const ACE_Thread_ID& thread_id (Worker *worker);
virtual int return_to_work (Worker *worker)
{
ACE_GUARD_RETURN (ACE_Thread_Mutex,
worker_mon, this->workers_lock_, -1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Worker %t returning to work.\n")));
this->workers_.enqueue_tail (worker);
this->workers_cond_.signal ();
return 0;
}
private:
int create_worker_pool (void)
{
ACE_GUARD_RETURN (ACE_Thread_Mutex,
worker_mon,
this->workers_lock_,
-1);
for (int i = 0; i < POOL_SIZE; i++)
{
Worker *worker = 0;
ACE_NEW_RETURN (worker, Worker (this), -1);
this->workers_.enqueue_tail (worker);
worker->activate ();
}
return 0;
}
int done (void);
private:
int shutdown_;
ACE_Thread_Mutex workers_lock_;
ACE_Condition<ACE_Thread_Mutex> workers_cond_;
ACE_Unbounded_Queue<Worker* > workers_;
};
// Listing 1
int Manager::done (void)
{
return (shutdown_ == 1);
}
int
Manager::shut_down (void)
{
ACE_TRACE (ACE_TEXT ("Manager::shut_down"));
ACE_Unbounded_Queue<Worker* >::ITERATOR iter =
this->workers_.begin ();
Worker **worker_ptr = 0;
do
{
iter.next (worker_ptr);
Worker *worker = (*worker_ptr);
ACE_Thread_ID id = thread_id (worker);
char buf [65];
id.to_string (buf);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Attempting shutdown of %C\n"),
buf));
// Send the hangup message.
ACE_Message_Block *mb = 0;
ACE_NEW_RETURN
(mb,
ACE_Message_Block(0,
ACE_Message_Block::MB_HANGUP),
-1);
worker->putq (mb);
// Wait for the exit.
worker->wait ();
ACE_ASSERT (worker->msg_queue ()->is_empty ());
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) Worker %C shut down.\n"),
buf));
delete worker;
}
while (iter.advance ());
shutdown_ = 1;
return 0;
}
const ACE_Thread_ID&
Manager::thread_id (Worker *worker)
{
return worker->thread_id ();
}
int ACE_TMAIN (int, ACE_TCHAR *[])
{
Manager tp;
tp.activate ();
// Wait for a moment every time you send a message.
ACE_Time_Value tv;
tv.msec (100);
ACE_Message_Block *mb = 0;
for (int i = 0; i < 30; i++)
{
ACE_NEW_RETURN
(mb, ACE_Message_Block(sizeof(int)), -1);
ACE_OS::memcpy (mb->wr_ptr (), &i, sizeof(int));
ACE_OS::sleep (tv);
// Add a new work item.
tp.putq (mb);
}
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
#else
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_OS::puts (ACE_TEXT ("This example requires threads."));
return 0;
}
#endif /* ACE_HAS_THREADS */
|