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
|
/*
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
/**
* The J3dThread is the super class of all slave threads in Java 3D. It implements
* all of the common flow control constructs.
*/
abstract class J3dThread extends Thread {
/**
* These are the thread types that a message may affect
*/
static final int BEHAVIOR_SCHEDULER = 0x01;
static final int SOUND_SCHEDULER = 0x02;
static final int INPUT_DEVICE_SCHEDULER = 0x04;
static final int RENDER_THREAD = 0x10;
// static final int COLLISION_THREAD = 0x20;
static final int UPDATE_GEOMETRY = 0x40;
static final int UPDATE_RENDER = 0x80;
static final int UPDATE_BEHAVIOR = 0x100;
static final int UPDATE_SOUND = 0x200;
static final int UPDATE_RENDERING_ATTRIBUTES = 0x400;
static final int UPDATE_RENDERING_ENVIRONMENT = 0x1000;
static final int UPDATE_TRANSFORM = 0x2000;
/**
* The classification types.
*/
static final int WORK_THREAD = 0x01;
static final int UPDATE_THREAD = 0x02;
/**
* This runMonitor action puts the thread into an initial wait state
*/
static final int WAIT = 0;
/**
* This runMonitor action notifies MasterControl that this thread
* has completed and wait.
*/
static final int NOTIFY_AND_WAIT = 1;
/**
* This is used by Canvas3D Renderer to notify user thread
* that swap is completed.
*/
static final int NOTIFY = 2;
/**
* This runMonitor action tells the thread to run N number of
* iterations.
*/
static final int RUN = 2;
/**
* This runMonitor action tells the thread to stop running
*/
static final int STOP = 3;
/**
* This indicates that this thread has been activated by MC
*/
boolean active = false;
/**
* This indicates that this thread is alive and running
*/
private volatile boolean running = true;
/**
* This flag is set by the RUN action of runMonitor to indicate that the
* waiting thread has work to do.
*/
private volatile boolean ready = false;
/**
* The thread data for this thread
*/
private J3dThreadData[] data = null;
/**
* This indicates that this thread is started and able to accept work
*/
private volatile boolean started = false;
/**
* The time values passed into this thread
*/
long referenceTime;
/**
* Use to assign threadOpts WAIT_ALL_THREADS
*/
long lastWaitTimestamp = 0;
/**
* The type of this thread. It is one of the above constants.
*/
int type;
/**
* The classification of this thread. It is one of the above constants.
*/
int classification = WORK_THREAD;
/**
* The arguments passed in for this thread
*/
Object[] args = null;
/**
* Flag to indicate that user initiate a thread stop
*/
volatile boolean userStop = false;
/**
* Flag to indicate that this thread is waiting to be notify
*/
private volatile boolean waiting = false;
/**
* Some variables used to name threads correctly
*/
private static int numInstances = 0;
private int instanceNum = -1;
private synchronized int newInstanceNum() {
return (++numInstances);
}
int getInstanceNum() {
if (instanceNum == -1)
instanceNum = newInstanceNum();
return instanceNum;
}
/**
* This method is defined by all slave threads to implement
* one iteration of work.
*/
abstract void doWork(long referenceTime);
/**
* This constructor simply assigns the given id.
*/
J3dThread(ThreadGroup t) {
super(t, "");
}
/**
* This returns the thread data for this thread.
*/
synchronized J3dThreadData getThreadData(View v, Canvas3D c) {
J3dThreadData threadData;
int i, j;
J3dThreadData[] newData;
if (type != RENDER_THREAD) { // Regular Thread
if (data == null) {
data = new J3dThreadData[1];
data[0] = new J3dThreadData();
data[0].thread = this;
data[0].threadType = type;
data[0].view = null;
data[0].canvas = null;
}
threadData = data[0];
} else { // Render thread
// Note: each renderer has multiple thread data mappings
// for its render and swap threads
if (data == null) {
data = new J3dThreadData[1];
data[0] = new J3dThreadData();
data[0].thread = this;
data[0].threadType = type;
data[0].view = v;
data[0].canvas = c;
data[0].threadArgs = new Object[4];
threadData = data[0];
} else {
for (i=0; i<data.length; i++) {
if (data[i].view == v && data[i].canvas == c) {
break;
}
}
if (i==data.length) {
newData = new J3dThreadData[data.length+1];
for (j=0; j<data.length; j++) {
newData[j] = data[j];
}
data = newData;
data[j] = new J3dThreadData();
data[j].thread = this;
data[j].threadType = type;
data[j].view = v;
data[j].canvas = c;
data[j].threadArgs = new Object[4];
threadData = data[j];
} else {
threadData = data[i];
Object args[] = (Object []) threadData.threadArgs;
args[0] = null;
args[1] = null;
args[2] = null;
args[3] = null;
}
}
}
return (threadData);
}
/**
* This initializes this thread. Once this method returns, the thread is
* able to accept work.
*/
void initialize() {
this.start();
while (!started) {
MasterControl.threadYield();
}
}
/**
* This causes the threads run method to exit.
*/
void finish() {
// NOTE: This spin loop is probably not necessary.
while (!waiting) {
MasterControl.threadYield();
}
runMonitor(STOP, 0,null);
}
/**
* This thread controls the syncing of all the canvases attached to
* this view.
*/
@Override
public void run() {
runMonitor(WAIT, 0, null);
while (running) {
doWork(referenceTime);
runMonitor(NOTIFY_AND_WAIT, 0, null);
}
// resource clean up
shutdown();
}
synchronized void runMonitor(int action, long referenceTime,
Object[] args) {
switch (action) {
case WAIT:
started = true;
// Issue 279 - loop until ready
while (!ready && running) {
waiting = true;
try {
wait();
} catch (InterruptedException e) {
System.err.println(e);
}
waiting = false;
}
ready = false;
break;
case NOTIFY_AND_WAIT:
VirtualUniverse.mc.runMonitor(MasterControl.THREAD_DONE, null,
null, null, this);
// Issue 279 - loop until ready
while (!ready && running) {
waiting = true;
try {
wait();
} catch (InterruptedException e) {
System.err.println(e);
}
waiting = false;
}
ready = false;
break;
case RUN:
this.referenceTime = referenceTime;
this.args = args;
ready = true;
if (waiting) {
notify();
}
break;
case STOP:
running = false;
if (waiting) {
notify();
}
break;
}
}
void cleanupView() {
// renderer will reconstruct threadData next time
// in getThreadData
data = null;
}
// default resource clean up method
void shutdown() {
}
void cleanup() {
active = false;
running = true;
ready = false;
data = null;
started = true;
lastWaitTimestamp = 0;
classification = WORK_THREAD;
args = null;
userStop = false;
referenceTime = 0;
}
}
|