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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*************************************************
*
* = PACKAGE
* JACE.ASX
*
* = FILENAME
* Task.java
*
*@author Prashant Jain
*
*************************************************/
package JACE.ASX;
import JACE.OS.*;
import JACE.Reactor.*;
import JACE.Concurrency.*;
public abstract class Task implements Runnable, EventHandler
{
// = Initialization/termination methods.
/**
* Initialize a Task. Note, we allocate a message queue ourselves.
*/
public Task ()
{
this.msgQueue_ = new MessageQueue ();
this.thrMgr_ = null;
}
/**
* Initialize a Task. Note, we use the message queue and thread
* manager supplied by the user.
*@param mq Message Queue to hold list of messages on the Task
*@param thrMgr Thread Manager that manages all the spawned threads
*/
public Task (MessageQueue mq,
ThreadManager thrMgr)
{
this.msgQueue_ = mq;
this.thrMgr_ = thrMgr;
}
/**
* Not meant to be invoked by the user directly!. This needs to be
* in the public interface in order to get invoked by Thread
* class.
*/
public void run ()
{
this.svc ();
}
// = Initialization and termination hooks (note that these *must* be
// defined by subclasses).
/**
* Hook called to open a Task.
*@param obj used to pass arbitrary information
*/
public abstract int open (Object obj);
/**
* Hook called to close a Task.
*/
public abstract int close (long flags);
// = Immediate and deferred processing methods, respectively.
/**
* Transfer a message into the queue to handle immediate
* processing.
*@param mb Message Block to handle immediately
*@param tv amount of time to wait for
*/
public abstract int put (MessageBlock mb, TimeValue tv);
/**
* Run by a daemon thread to handle deferred processing. Note, that
* to do anything useful, this method should be overriden by the
* subclass.
*@return default implementation always returns 0.
*/
public int svc ()
{
return 0;
}
/**
* Set the underlying Thread Manager.
*@param t Thread Manager to use
*/
public synchronized void thrMgr (ThreadManager t)
{
this.thrMgr_ = t;
}
/**
* Get the Thread Manager.
*@return Underlying Thread Manager
*/
public synchronized ThreadManager thrMgr ()
{
return this.thrMgr_;
}
// = Active object method.
/**
* Turn the task into an active object. That is, having <nThreads>
* separate threads of control that all invoke Task::svc.
*@param flags Task Flags
*@param nThreads number of threads to spawn
*@param forceActive whether to force creation of new threads or not
*@return -1 if failure occurs, 1 if Task is already an active
* object and <forceActive> is false (doesn't *not* create a new
* thread in this case), and 0 if Task was not already an active
* object and a thread is created successfully or thread is an active
* object and <forceActive> is true.
*/
public synchronized int activate (long flags, int nThreads, boolean forceActive)
{
// Create a Thread Manager if we do not already have one
if (this.thrMgr_ == null)
this.thrMgr_ = new ThreadManager ();
if (this.thrCount () > 0 && forceActive == false)
return 1; // Already active.
this.flags_ = flags;
if (ACE.BIT_ENABLED (flags, TaskFlags.THR_DAEMON))
this.thrMgr_.spawnN (nThreads, this, true); // Spawn off all threads as daemon threads
else // Spawn off all threads as normal threads
this.thrMgr_.spawnN (nThreads, this, false);
return 0;
}
// = Suspend/resume a Task
/**
* Suspend a task.
*/
public synchronized void suspend ()
{
// Suspend all threads
if (this.thrMgr_ != null)
this.thrMgr_.thrGrp ().suspend ();
}
/**
* Resume a suspended task.
*/
public synchronized void resume ()
{
// Resume all threads
if (this.thrMgr_ != null)
this.thrMgr_.thrGrp ().resume ();
}
/**
* Get the current group name.
*@return name of the current thread group
*/
public synchronized String grpName ()
{
if (this.thrMgr_ != null)
return this.thrMgr_.thrGrp ().getName ();
else
return null;
}
/**
* Get the message queue associated with this task.
*@return the message queue associated with this task.
*/
public MessageQueue msgQueue ()
{
return this.msgQueue_;
}
/**
* Set the message queue associated with this task.
*@param mq Message Queue to use with this Task.
*/
public void msgQueue (MessageQueue mq)
{
this.msgQueue_ = mq;
}
/**
* Get the number of threads currently running within the Task.
*@return the number of threads currently running within the Task.
* 0 if we're a passive object, else > 0.
*/
public synchronized int thrCount ()
{
if (this.thrMgr_ != null)
return this.thrMgr_.thrGrp ().activeCount ();
else
return 0;
}
/**
* Set the Task flags
*@param flags Task Flags
*/
public synchronized void flags (long flags)
{
this.flags_ = flags;
}
/**
* Get the Task flags
*@return Task Flags
*/
public synchronized long flags ()
{
return this.flags_;
}
// = Message queue manipulation methods.
/*
* Dump debug information.
*/
public void dump ()
{
}
/**
* Insert message into the message queue.
*@param mb Message Block to insert into the Message Queue
*@param tv amount of time to wait for
*/
protected int putq (MessageBlock mb, TimeValue tv) throws InterruptedException
{
return this.msgQueue_.enqueueTail (mb, tv);
}
/**
* Extract the first message from the queue. Note that the call is blocking.
*@return the first Message Block from the Message Queue.
*@param tv amount of time to wait for
*/
protected MessageBlock getq (TimeValue tv) throws InterruptedException
{
return this.msgQueue_.dequeueHead (tv);
}
/**
* Return a message back to the queue.
*@param mb Message Block to return back to the Message Queue
*@param tv amount of time to wait for
*/
protected int ungetq (MessageBlock mb, TimeValue tv) throws InterruptedException
{
return this.msgQueue_.enqueueHead (mb, tv);
}
/**
* Transfer message to the adjacent ACETask in an ACEStream.
*@param mb Message Block to transfer to the adjacent Task
*@param tv amount of time to wait for
*@return -1 if there is no adjacent Task, else the return value of
* trying to put the Message Block on that Task's Message Queue.
*/
protected int putNext (MessageBlock mb, TimeValue tv)
{
return this.next_ == null ? -1 : this.next_.put (mb, tv);
}
/**
* Turn the message back around. Puts the message in the sibling's
* Message Queue.
*@param mb Message Block to put into sibling's Message Queue
*@param tv amount of time to wait for
*@return -1 if there is no adjacent Task to the sibling, else the
* return value of trying to put the Message Block on sibling's
* Message Queue.
*/
protected int reply (MessageBlock mb, TimeValue tv)
{
return this.sibling ().putNext (mb, tv);
}
// = ACE_Task utility routines to identify names et al.
/**
* Get the name of the enclosing Module.
*@return the name of the enclosing Module if there's one associated
* with the Task, else null.
*/
protected String name ()
{
if (this.mod_ == null)
return null;
else
return this.mod_.name ();
}
/**
* Get the Task's sibling.
*@return the Task's sibling if there's one associated with the
* Task's Module, else null.
*/
protected Task sibling ()
{
if (this.mod_ == null)
return null;
else
return this.mod_.sibling (this);
}
/**
* Set the Task's module.
*@param mod the Task's Module.
*/
protected void module (Module mod)
{
this.mod_ = mod;
}
/**
* Get the Task's module.
*@return the Task's Module if there is one, else null.
*/
protected Module module ()
{
return this.mod_;
}
/**
* Check if queue is a reader.
*@return true if queue is a reader, else false.
*/
protected boolean isReader ()
{
return (ACE.BIT_ENABLED (this.flags_, TaskFlags.ACE_READER));
}
/**
* Check if queue is a writer.
*@return true if queue is a writer, else false.
*/
protected boolean isWriter ()
{
return (ACE.BIT_DISABLED (this.flags_, TaskFlags.ACE_READER));
}
// = Pointers to next ACE_Queue (if ACE is part of an ACE_Stream).
/**
* Get next Task pointer.
*@return pointer to the next Task
*/
protected Task next ()
{
return this.next_;
}
/**
* Set next Task pointer.
*@param task next task pointer
*/
protected void next (Task task)
{
this.next_ = task;
}
// Special routines corresponding to certain message types.
/**
* Flush the Message Queue
*@return 0 if Message Queue is null, 1 if flush succeeds, -1 if
* ACE_FLUSHALL bit is not enabled in flags.
*/
protected int flush (long flag)
{
if (ACE.BIT_ENABLED (flag, TaskFlags.ACE_FLUSHALL))
return (this.msgQueue_ == null ? 0 : 1);
else
return -1;
}
/**
* Manipulate watermarks.
*@param cmd IOCntlCmd
*@param size watermark
*/
protected void waterMarks (int cmd, int size)
{
if (cmd == IOCntlCmds.SET_LWM)
this.msgQueue_.lowWaterMark (size);
else /* cmd == IOCntlMsg.SET_HWM */
this.msgQueue_.highWaterMark (size);
}
private ThreadManager thrMgr_ = null;
// Thread_Manager that manages all the spawned threads
private long flags_;
// Task flags.
private MessageQueue msgQueue_;
// List of messages on the Task..
private Task next_;
// Adjacent ACE_Task.
private Module mod_;
// Back-pointer to the enclosing module.
}
|