35package com.jogamp.openal.sound3d;
37import com.jogamp.common.util.locks.Lock;
38import com.jogamp.common.util.locks.LockFactory;
39import com.jogamp.common.util.locks.RecursiveLock;
40import com.jogamp.openal.*;
41import com.jogamp.openal.util.ALHelpers;
50 private final RecursiveLock lock = LockFactory.createRecursiveLock();
51 private final Device device;
53 private boolean threadContextLocked;
55 private static final ThreadLocal<Context> currentContext =
new ThreadLocal<Context>();
65 this.alCtx = realContext;
86 this( createImpl(device.
getALDevice(), attributes), device);
96 if(
null != attributes && attributes.length > 0 ) {
99 return AudioSystem3D.alc.alcCreateContext(alDevice,
null);
116 public boolean create(
final int[] attributes) {
119 if(
null == alCtx ) {
120 alCtx = createImpl(device.
getALDevice(), attributes);
121 return null != alCtx;
142 alCtx = createImpl(device.
getALDevice(), attributes);
143 if(
null != alCtx ) {
145 return makeCurrentImpl();
161 public boolean isValid() {
return null != alCtx; }
175 if( currentContext.get() ==
this ) {
176 currentContext.set(
null);
179 while(lock.getHoldCount() > 1) {
186 private void destroyImpl() {
187 if(
null != alCtx ) {
188 if( threadContextLocked ) {
191 AudioSystem3D.alc.alcMakeContextCurrent(
null);
193 AudioSystem3D.alc.alcDestroyContext(alCtx);
207 return currentContext.get();
212 return lock.getHoldCount();
215 public boolean tryMakeCurrent(
final boolean throwException,
final long timeoutMS)
throws RuntimeException {
216 return makeCurrentImpl(
false , throwException, timeoutMS);
232 return makeCurrentImpl(
true , throwException, Lock.TIMEOUT);
234 private boolean makeCurrentImpl(
final boolean throwTryLockException,
final boolean throwException,
final long timeoutMS)
throws RuntimeException {
236 if( lock.tryLock(timeoutMS) ) {
237 if(
null == alCtx ) {
239 if( throwException ) {
248 if (current !=
null) {
249 if (current ==
this) {
253 if( throwException ) {
254 throw new ALException(
"Current thread "+Thread.currentThread()+
" holds another "+current+
" while claiming this "+
this);
259 final boolean r = makeCurrentImpl();
261 currentContext.set(
this);
264 if( throwException ) {
265 throw new ALException(
"Context make current failed "+
this);
270 if( throwTryLockException ) {
271 throw new RuntimeException(
"Waited "+timeoutMS+
"ms for: "+lock.toString()+
" - "+Thread.currentThread().getName());
275 }
catch (
final InterruptedException ie) {
276 throw new RuntimeException(ie);
279 private boolean makeCurrentImpl() {
281 threadContextLocked =
true;
282 return AudioSystem3D.alExt.alcSetThreadContext(alCtx);
284 threadContextLocked =
false;
285 return AudioSystem3D.alc.alcMakeContextCurrent(alCtx);
303 if( !lock.isOwner( Thread.currentThread() ) ) {
304 if( throwException ) {
305 throw new ALException(
"Context not held on current thread "+Thread.currentThread()+
", "+
this);
309 if( lock.getHoldCount() == 1 ) {
311 if( threadContextLocked ) {
317 currentContext.set(
null);
319 if( throwException ) {
320 throw new ALException(
"Context release failed "+
this);
347 final String alCtxStr =
null != alCtx ?
"0x"+Integer.toHexString(alCtx.hashCode()) :
"null";
348 return "ALContext[this 0x"+Integer.toHexString(hashCode())+
", alCtx "+alCtxStr+
" lockCount "+lock.getHoldCount()+
", on "+device+
"]";
A generic exception for OpenAL errors used throughout the binding as a substitute for RuntimeExceptio...
The AudioSystem3D class provides a set of methods for creating and manipulating a 3D audio environmen...
This class provides a Sound3D Context associated with a specified device.
boolean release(final boolean throwException)
Releases control of this audio context from the current thread, if implementation utilizes context lo...
boolean isValid()
Returns whether getALContext() is valid, i.e.
boolean makeCurrent(final boolean throwException)
Makes the audio context current on the calling thread.
int getALCError()
Return ALC#alcGetError(ALCdevice) using getDevice().
int getLockCount()
Return the lock count of this context, i.e.
void suspend()
Suspend this context.
Context(final Device device, final int[] attributes)
Creates a new Context for a specified device including native ALCcontext creation.
boolean isDebugAvail()
Returns whether AL_EXT_debug is available for the current context.
boolean recreate(final int[] attributes)
Recreates the internal ALCcontext instance, i.e.
Device getDevice()
Gets the device associated with this context.
void destroy()
destroys this context freeing its resources.
boolean tryMakeCurrent(final boolean throwException, final long timeoutMS)
Context(final ALCcontext realContext, final Device device)
Creates a new Context for a given ALCcontext for the specified device.
ALCcontext getALContext()
Returns the OpenAL ALCcontext.
boolean create(final int[] attributes)
Creates the internal ALCcontext instance if getALContext() is null.
final boolean hasALC_thread_local_context
static Context getCurrentContext()
Returns this thread current context.
This class provides a handle to a specific audio device.
int getALCError()
Return ALC#alcGetError(ALCdevice).
ALCdevice getALDevice()
Returns the OpenAL ALCdevice.
static final String ALC_EXT_thread_local_context
static final String AL_EXT_debug
boolean alcMakeContextCurrent(ALCcontext context)
Entry point (through function pointer) to C language function: ALCboolean alcMakeContextCurrent(ALC...
void alcSuspendContext(ALCcontext context)
Entry point (through function pointer) to C language function: void alcSuspendContext(ALCcontext * ...
boolean alcIsExtensionPresent(ALCdevice device, String extname)
Entry point (through function pointer) to C language function: ALCboolean alcIsExtensionPresent(ALC...
ALCcontext alcCreateContext(ALCdevice device, IntBuffer attrlist)
Entry point (through function pointer) to C language function: ALCcontext * alcCreateContext(ALCdev...
boolean alcSetThreadContext(ALCcontext context)
Entry point (through function pointer) to C language function: ALCboolean alcSetThreadContext(ALCco...
boolean alIsExtensionPresent(String extname)
Entry point (through function pointer) to C language function: ALboolean alIsExtensionPresent(const...