summaryrefslogtreecommitdiffstats
path: root/src/classes/share/com/sun/j3d/audioengines/AudioEngineThread.java
blob: df0b653ae545bb27bd60dfee20572a028eb38cc5 (plain)
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
/*
 * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 */

package com.sun.j3d.audioengines;

/*
 * Audio Engine Thread
 */


/**
 * The Thread Class extended for Audio Device engines that must process
 * calls dynamically, in 'real-time" to asynchronously change engine
 * parameters.
 *
 * <p>
 * NOTE: this class is probably not needed for those Audio Device implementations
 * that handle all dynamic parameters in the low-level audio library.
 */
public class AudioEngineThread extends Thread {

    // Debug print flag
    static final protected boolean debugFlag = false;


    protected void debugPrint(String message) {
        if (debugFlag)
            System.out.println(message);
    }

    /**
     * The classification types.
     */
    protected static final int WORK_THREAD   = 0x01;
    protected static final int UPDATE_THREAD = 0x02;

    /**
     * This runMonitor action puts the thread into an initial wait state
     */
    protected static final int WAIT = 0;

    /**
     * This runMonitor action notifies MasterControl that this thread
     * has completed and wait.
     */
    protected static final int NOTIFY_AND_WAIT = 1;

    /**
     * This runMonitor action tells the thread to run N number of
     * iterations.
     */
    protected static final int RUN = 2;

    /**
     * This runMonitor action tells the thread to stop running
     */
    protected static final int STOP = 3;

    /**
     * This indicates that this thread has been activated by MC
     */
    protected boolean active = false;

    /**
     * This indicates that this thread is alive and running
     */
    protected boolean running = true;


    /**
     * This indicates that this thread is ready
     */
    protected boolean started = false;

    /**
     * The time values passed into this thread
     */
    protected long referenceTime;

    /**
     * Use to assign threadOpts WAIT_ALL_THREADS
     */
    protected long lastWaitTimestamp = 0;

    /**
     * The type of this thread.  It is one of the above constants.
     */
    protected int type;

    /**
     * The classification of this thread.  It is one of the above constants.
     */
    protected int classification = WORK_THREAD;

    /**
     * The arguments passed in for this thread
     */
    protected Object[] args = null;

    /**
     * Flag to indicate that user initiate a thread stop
     */
    protected boolean userStop = false;

    /**
     * Flag to indicate that this thread is waiting to be notify
     */
    protected boolean waiting = false;

    /**
     * Some variables used to name threads correctly
     */
    protected static int numInstances = 0;
    protected int instanceNum = -1;

    /**
     * This constructor simply assigns the given id.
     */
    public AudioEngineThread(ThreadGroup t, String threadName) {
	super(t, threadName);
        if (debugFlag)
            debugPrint("AudioEngineThread.constructor("+threadName +")");
    }

    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.
     */
    synchronized public void doWork() {
        if (debugFlag)
            debugPrint("AudioEngineThread.doWork()");
    }

    /**
     * This initializes this thread.  Once this method returns, the thread is
     * ready to do work.
     */
    public void initialize() {
        if (debugFlag)
            debugPrint("AudioEngineThread.initialize()");
	this.start();
	while (!started) {
	    try {
                Thread.currentThread().sleep(1, 0);
            } catch (InterruptedException e) {
            }
	}
    }

    /**
     * This causes the threads run method to exit.
     */
    public void finish() {
	while (!waiting) {
	    try {
		Thread.sleep(10);
	    } catch (InterruptedException e) {}
	}
	runMonitor(STOP, 0,null);
    }

    /*
     * This thread controls the syncing of all the canvases attached to
     * this view.
     */
    public void run() {
        if (debugFlag)
            debugPrint("AudioEngineThread.run");
	runMonitor(WAIT, 0, null);
	while (running) {
	    doWork();
	    runMonitor(WAIT, 0, null);
	}
	// resource clean up
        shutdown();
    }

    synchronized public void runMonitor(int action, long referenceTime, Object[] args){
        switch (action) {
            case WAIT:
                if (debugFlag)
                   debugPrint("AudioEngineThread.runMonitor(WAIT)");
                try {
		  started = true;
		  waiting = true;
                  wait();
                } catch (InterruptedException e) {
                   System.err.println(e);
                }
		waiting = false;
                break;
            case RUN:
                if (debugFlag)
                   debugPrint("AudioEngineThread.runMonitor(RUN)");
		this.referenceTime = referenceTime;
		this.args = args;
                notify();
                break;
            case STOP:
                if (debugFlag)
                   debugPrint("AudioEngineThread.runMonitor(STOP)");
	        running = false;
	        notify();
	        break;
        }
    }

    public void shutdown() {
    }

    // default resource clean up method
    public void cleanup() {
	active = false;
	running = true;
	started = true;
	lastWaitTimestamp = 0;
	classification = WORK_THREAD;
	args = null;
	userStop = false;
	referenceTime = 0;

    }
}