diff options
Diffstat (limited to 'src/nativewindow')
-rw-r--r-- | src/nativewindow/classes/com/jogamp/nativewindow/impl/RecursiveToolkitLock.java | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/impl/RecursiveToolkitLock.java b/src/nativewindow/classes/com/jogamp/nativewindow/impl/RecursiveToolkitLock.java index 52c2116..236ef07 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/impl/RecursiveToolkitLock.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/impl/RecursiveToolkitLock.java @@ -5,11 +5,12 @@ import javax.media.nativewindow.*; // // Reentrance locking toolkit // -public class RecursiveToolkitLock implements ToolkitLock { +public class RecursiveToolkitLock { private Thread owner = null; private int recursionCount = 0; private Exception lockedStack = null; private static final long timeout = 3000; // maximum wait 3s + private static final boolean TRACE_LOCK = false; public Exception getLockedStack() { return lockedStack; @@ -35,11 +36,27 @@ public class RecursiveToolkitLock implements ToolkitLock { return recursionCount; } + public synchronized void validateLocked() { + if ( !isLocked() ) { + throw new RuntimeException(Thread.currentThread()+": Not locked"); + } + if ( !isOwner() ) { + getLockedStack().printStackTrace(); + throw new RuntimeException(Thread.currentThread()+": Not owner, owner is "+owner); + } + } + /** Recursive and blocking lockSurface() implementation */ public synchronized void lock() { Thread cur = Thread.currentThread(); + if(TRACE_LOCK) { + System.out.println("... LOCK 0 ["+this+"], recursions "+recursionCount+", "+cur); + } if (owner == cur) { ++recursionCount; + if(TRACE_LOCK) { + System.out.println("+++ LOCK 1 ["+this+"], recursions "+recursionCount+", "+cur); + } return; } @@ -55,6 +72,9 @@ public class RecursiveToolkitLock implements ToolkitLock { lockedStack.printStackTrace(); throw new RuntimeException("Waited "+timeout+"ms for: "+owner+" - "+cur+", with recursionCount "+recursionCount+", lock: "+this); } + if(TRACE_LOCK) { + System.out.println("+++ LOCK X ["+this+"], recursions "+recursionCount+", "+cur); + } owner = cur; lockedStack = new Exception("Previously locked by "+owner+", lock: "+this); } @@ -67,13 +87,13 @@ public class RecursiveToolkitLock implements ToolkitLock { /** Recursive and unblocking unlockSurface() implementation */ public synchronized void unlock(Runnable taskAfterUnlockBeforeNotify) { - Thread cur = Thread.currentThread(); - if (owner != cur) { - lockedStack.printStackTrace(); - throw new RuntimeException(cur+": Not owner, owner is "+owner); - } + validateLocked(); + if (recursionCount > 0) { --recursionCount; + if(TRACE_LOCK) { + System.out.println("--- LOCK 1 ["+this+"], recursions "+recursionCount+", "+Thread.currentThread()); + } return; } owner = null; @@ -81,6 +101,9 @@ public class RecursiveToolkitLock implements ToolkitLock { if(null!=taskAfterUnlockBeforeNotify) { taskAfterUnlockBeforeNotify.run(); } + if(TRACE_LOCK) { + System.out.println("--- LOCK X ["+this+"], recursions "+recursionCount+", "+Thread.currentThread()); + } notifyAll(); } } |