001: /*
002: * $RCSfile: AudioEngineThread.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:02 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.audioengines;
046:
047: /*
048: * Audio Engine Thread
049: */
050:
051: import javax.media.j3d.*;
052:
053: /**
054: * The Thread Class extended for Audio Device engines that must process
055: * calls dynamically, in 'real-time" to asynchronously change engine
056: * parameters.
057: *
058: * <p>
059: * NOTE: this class is probably not needed for those Audio Device implementations
060: * that handle all dynamic parameters in the low-level audio library.
061: */
062: public class AudioEngineThread extends Thread {
063:
064: // Debug print flag
065: static final protected boolean debugFlag = false;
066:
067: protected void debugPrint(String message) {
068: if (debugFlag)
069: System.out.println(message);
070: }
071:
072: /**
073: * The classification types.
074: */
075: protected static final int WORK_THREAD = 0x01;
076: protected static final int UPDATE_THREAD = 0x02;
077:
078: /**
079: * This runMonitor action puts the thread into an initial wait state
080: */
081: protected static final int WAIT = 0;
082:
083: /**
084: * This runMonitor action notifies MasterControl that this thread
085: * has completed and wait.
086: */
087: protected static final int NOTIFY_AND_WAIT = 1;
088:
089: /**
090: * This runMonitor action tells the thread to run N number of
091: * iterations.
092: */
093: protected static final int RUN = 2;
094:
095: /**
096: * This runMonitor action tells the thread to stop running
097: */
098: protected static final int STOP = 3;
099:
100: /**
101: * This indicates that this thread has been activated by MC
102: */
103: protected boolean active = false;
104:
105: /**
106: * This indicates that this thread is alive and running
107: */
108: protected boolean running = true;
109:
110: /**
111: * This indicates that this thread is ready
112: */
113: protected boolean started = false;
114:
115: /**
116: * The time values passed into this thread
117: */
118: protected long referenceTime;
119:
120: /**
121: * Use to assign threadOpts WAIT_ALL_THREADS
122: */
123: protected long lastWaitTimestamp = 0;
124:
125: /**
126: * The type of this thread. It is one of the above constants.
127: */
128: protected int type;
129:
130: /**
131: * The classification of this thread. It is one of the above constants.
132: */
133: protected int classification = WORK_THREAD;
134:
135: /**
136: * The arguments passed in for this thread
137: */
138: protected Object[] args = null;
139:
140: /**
141: * Flag to indicate that user initiate a thread stop
142: */
143: protected boolean userStop = false;
144:
145: /**
146: * Flag to indicate that this thread is waiting to be notify
147: */
148: protected boolean waiting = false;
149:
150: /**
151: * Some variables used to name threads correctly
152: */
153: protected static int numInstances = 0;
154: protected int instanceNum = -1;
155:
156: /**
157: * This constructor simply assigns the given id.
158: */
159: public AudioEngineThread(ThreadGroup t, String threadName) {
160: super (t, threadName);
161: if (debugFlag)
162: debugPrint("AudioEngineThread.constructor(" + threadName
163: + ")");
164: }
165:
166: synchronized int newInstanceNum() {
167: return (++numInstances);
168: }
169:
170: int getInstanceNum() {
171: if (instanceNum == -1)
172: instanceNum = newInstanceNum();
173: return instanceNum;
174: }
175:
176: /**
177: * This method is defined by all slave threads to implement
178: * one iteration of work.
179: */
180: synchronized public void doWork() {
181: if (debugFlag)
182: debugPrint("AudioEngineThread.doWork()");
183: }
184:
185: /**
186: * This initializes this thread. Once this method returns, the thread is
187: * ready to do work.
188: */
189: public void initialize() {
190: if (debugFlag)
191: debugPrint("AudioEngineThread.initialize()");
192: this .start();
193: while (!started) {
194: try {
195: Thread.currentThread().sleep(1, 0);
196: } catch (InterruptedException e) {
197: }
198: }
199: }
200:
201: /**
202: * This causes the threads run method to exit.
203: */
204: public void finish() {
205: while (!waiting) {
206: try {
207: Thread.sleep(10);
208: } catch (InterruptedException e) {
209: }
210: }
211: runMonitor(STOP, 0, null);
212: }
213:
214: /*
215: * This thread controls the syncing of all the canvases attached to
216: * this view.
217: */
218: public void run() {
219: if (debugFlag)
220: debugPrint("AudioEngineThread.run");
221: runMonitor(WAIT, 0, null);
222: while (running) {
223: doWork();
224: runMonitor(WAIT, 0, null);
225: }
226: // resource clean up
227: shutdown();
228: }
229:
230: synchronized public void runMonitor(int action, long referenceTime,
231: Object[] args) {
232: switch (action) {
233: case WAIT:
234: if (debugFlag)
235: debugPrint("AudioEngineThread.runMonitor(WAIT)");
236: try {
237: started = true;
238: waiting = true;
239: wait();
240: } catch (InterruptedException e) {
241: System.err.println(e);
242: }
243: waiting = false;
244: break;
245: case RUN:
246: if (debugFlag)
247: debugPrint("AudioEngineThread.runMonitor(RUN)");
248: this .referenceTime = referenceTime;
249: this .args = args;
250: notify();
251: break;
252: case STOP:
253: if (debugFlag)
254: debugPrint("AudioEngineThread.runMonitor(STOP)");
255: running = false;
256: notify();
257: break;
258: }
259: }
260:
261: public void shutdown() {
262: }
263:
264: // default resource clean up method
265: public void cleanup() {
266: active = false;
267: running = true;
268: started = true;
269: lastWaitTimestamp = 0;
270: classification = WORK_THREAD;
271: args = null;
272: userStop = false;
273: referenceTime = 0;
274:
275: }
276: }
|