diff options
author | Sven Gothel <[email protected]> | 2012-06-27 04:18:53 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-06-27 04:18:53 +0200 |
commit | 834b9e530e652b7ff7c5e222720bce3ad2b11c5f (patch) | |
tree | bac459da1a84abec07f70f74204a4e1deca1d226 | |
parent | 9a71703904ebfec343fb2c7266343d37a2e4c3db (diff) |
Lock Cleanup (API Change)
- LockExt -> ThreadLock - clarifying semantics (API Change)
- ThreadLock: Remove isOwner(), use isOwner(Thread.currentThread)
- adding @Override
13 files changed, 80 insertions, 33 deletions
diff --git a/src/java/com/jogamp/common/util/locks/Lock.java b/src/java/com/jogamp/common/util/locks/Lock.java index cf3fd01..33a093b 100644 --- a/src/java/com/jogamp/common/util/locks/Lock.java +++ b/src/java/com/jogamp/common/util/locks/Lock.java @@ -77,5 +77,6 @@ public interface Lock { */ void unlock() throws RuntimeException; + /** Query if locked */ boolean isLocked(); } diff --git a/src/java/com/jogamp/common/util/locks/RecursiveLock.java b/src/java/com/jogamp/common/util/locks/RecursiveLock.java index efa9789..9eb9c8c 100644 --- a/src/java/com/jogamp/common/util/locks/RecursiveLock.java +++ b/src/java/com/jogamp/common/util/locks/RecursiveLock.java @@ -31,7 +31,7 @@ package com.jogamp.common.util.locks; /** * Reentrance capable locking toolkit. */ -public interface RecursiveLock extends LockExt { +public interface RecursiveLock extends ThreadLock { /** Return the number of locks issued to this lock by the same thread. * A hold count of 0 identifies this lock as unlocked.<br> * A hold count of 1 identifies this lock as locked.<br> diff --git a/src/java/com/jogamp/common/util/locks/RecursiveThreadGroupLock.java b/src/java/com/jogamp/common/util/locks/RecursiveThreadGroupLock.java index 5e11f29..626199f 100644 --- a/src/java/com/jogamp/common/util/locks/RecursiveThreadGroupLock.java +++ b/src/java/com/jogamp/common/util/locks/RecursiveThreadGroupLock.java @@ -123,6 +123,7 @@ public interface RecursiveThreadGroupLock extends RecursiveLock { * * {@inheritDoc} */ + @Override void unlock() throws RuntimeException; /** @@ -132,6 +133,7 @@ public interface RecursiveThreadGroupLock extends RecursiveLock { * * {@inheritDoc} */ + @Override void unlock(Runnable taskAfterUnlockBeforeNotify); } diff --git a/src/java/com/jogamp/common/util/locks/SingletonInstance.java b/src/java/com/jogamp/common/util/locks/SingletonInstance.java index c085371..5f2718b 100644 --- a/src/java/com/jogamp/common/util/locks/SingletonInstance.java +++ b/src/java/com/jogamp/common/util/locks/SingletonInstance.java @@ -68,8 +68,10 @@ public abstract class SingletonInstance implements Lock { public final long getPollPeriod() { return poll_ms; } public abstract String getName(); + @Override public final String toString() { return getName(); } + @Override public synchronized void lock() throws RuntimeException { try { do { @@ -82,6 +84,7 @@ public abstract class SingletonInstance implements Lock { } } + @Override public synchronized boolean tryLock(long maxwait) throws RuntimeException { if(locked) { return true; @@ -110,6 +113,7 @@ public abstract class SingletonInstance implements Lock { } protected abstract boolean tryLockImpl(); + @Override public void unlock() throws RuntimeException { if(locked) { locked = !unlockImpl(); @@ -121,6 +125,7 @@ public abstract class SingletonInstance implements Lock { } protected abstract boolean unlockImpl(); + @Override public synchronized boolean isLocked() { return locked; } diff --git a/src/java/com/jogamp/common/util/locks/LockExt.java b/src/java/com/jogamp/common/util/locks/ThreadLock.java index 974c11a..65260bc 100644 --- a/src/java/com/jogamp/common/util/locks/LockExt.java +++ b/src/java/com/jogamp/common/util/locks/ThreadLock.java @@ -31,19 +31,19 @@ package com.jogamp.common.util.locks; /** * Extending the {@link Lock} features with convenient functionality. */ -public interface LockExt extends Lock { +public interface ThreadLock extends Lock { + /** Query whether the lock is hold by the a thread other than the current thread. */ + boolean isLockedByOtherThread(); + + /** Query whether the lock is hold by the given thread. */ + boolean isOwner(Thread thread); + /** * @return the Thread owning this lock if locked, otherwise null */ Thread getOwner(); - boolean isLockedByOtherThread(); - - boolean isOwner(); - - boolean isOwner(Thread thread); - /** * @throws RuntimeException if current thread does not hold the lock */ diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java b/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java index c1a74fc..a26dfa4 100644 --- a/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java +++ b/src/java/jogamp/common/util/locks/RecursiveLockImpl01CompleteFair.java @@ -94,42 +94,43 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock { } } + @Override public final Thread getOwner() { synchronized(sync) { return sync.getOwner(); } } - public final boolean isOwner() { - synchronized(sync) { - return isOwner(Thread.currentThread()); - } - } - + @Override public final boolean isOwner(Thread thread) { synchronized(sync) { return sync.getOwner() == thread ; } } + @Override public final boolean isLocked() { synchronized(sync) { return null != sync.getOwner(); } } + @Override public final boolean isLockedByOtherThread() { synchronized(sync) { - return null != sync.getOwner() && Thread.currentThread() != sync.getOwner() ; + final Thread o = sync.getOwner(); + return null != o && Thread.currentThread() != o ; } } + @Override public final int getHoldCount() { synchronized(sync) { return sync.holdCount; } } + @Override public final void validateLocked() throws RuntimeException { synchronized(sync) { if ( Thread.currentThread() != sync.getOwner() ) { @@ -144,6 +145,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock { } } + @Override public final void lock() { synchronized(sync) { try { @@ -159,6 +161,7 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock { } } + @Override public final boolean tryLock(long timeout) throws InterruptedException { synchronized(sync) { final Thread cur = Thread.currentThread(); @@ -244,12 +247,14 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock { } + @Override public final void unlock() { synchronized(sync) { unlock(null); } } + @Override public final void unlock(Runnable taskAfterUnlockBeforeNotify) { synchronized(sync) { validateLocked(); @@ -293,12 +298,14 @@ public class RecursiveLockImpl01CompleteFair implements RecursiveLock { } } + @Override public final int getQueueLength() { synchronized(sync) { return sync.queue.size(); } } + @Override public String toString() { return syncName()+"[count "+sync.holdCount+ ", qsz "+sync.queue.size()+", owner "+threadName(sync.getOwner())+"]"; diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java b/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java index 326693a..5b5e42e 100644 --- a/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java +++ b/src/java/jogamp/common/util/locks/RecursiveLockImpl01Unfairish.java @@ -67,18 +67,23 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { /* package */ SingleThreadSync() { super(); } + @Override public final Thread getOwner() { return getExclusiveOwnerThread(); } + @Override public boolean isOwner(Thread t) { return getExclusiveOwnerThread()==t; } + @Override public final void setOwner(Thread t) { setExclusiveOwnerThread(t); } + @Override public final Throwable getLockedStack() { return lockedStack; } + @Override public final void setLockedStack(Throwable s) { List<Throwable> ls = LockDebugUtil.getRecursiveLockTrace(); if(s==null) { @@ -88,12 +93,18 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } lockedStack = s; } + @Override public final int getHoldCount() { return holdCount; } + @Override public void incrHoldCount(Thread t) { holdCount++; } + @Override public void decrHoldCount(Thread t) { holdCount--; } + @Override public final int getQSz() { return qsz; } + @Override public final void incrQSz() { qsz++; } + @Override public final void decrQSz() { qsz--; } // lock count by same thread @@ -124,40 +135,43 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } } + @Override public final Thread getOwner() { synchronized(sync) { return sync.getOwner(); } } - public final boolean isOwner() { - return isOwner(Thread.currentThread()); - } - + @Override public final boolean isOwner(Thread thread) { synchronized(sync) { - return sync.isOwner(thread) ; + return sync.isOwner(thread); } } + @Override public final boolean isLocked() { synchronized(sync) { return null != sync.getOwner(); } } + @Override public final boolean isLockedByOtherThread() { synchronized(sync) { - return null != sync.getOwner() && !sync.isOwner(Thread.currentThread()) ; + final Thread o = sync.getOwner(); + return null != o && Thread.currentThread() != o ; } } + @Override public final int getHoldCount() { synchronized(sync) { return sync.getHoldCount(); } } + @Override public final void validateLocked() throws RuntimeException { synchronized(sync) { if ( !sync.isOwner(Thread.currentThread()) ) { @@ -172,6 +186,7 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } } + @Override public final void lock() { synchronized(sync) { try { @@ -187,6 +202,7 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } } + @Override public final boolean tryLock(long timeout) throws InterruptedException { synchronized(sync) { final Thread cur = Thread.currentThread(); @@ -242,12 +258,14 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } + @Override public final void unlock() { synchronized(sync) { unlock(null); } } + @Override public void unlock(Runnable taskAfterUnlockBeforeNotify) { synchronized(sync) { validateLocked(); @@ -277,12 +295,14 @@ public class RecursiveLockImpl01Unfairish implements RecursiveLock { } } + @Override public final int getQueueLength() { synchronized(sync) { return sync.getQSz(); } } + @Override public String toString() { return syncName()+"[count "+sync.getHoldCount()+ ", qsz "+sync.getQSz()+", owner "+threadName(sync.getOwner())+"]"; diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java index 7c88400..d9bc3df 100644 --- a/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java +++ b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java @@ -14,6 +14,7 @@ public class RecursiveLockImplJava5 implements RecursiveLock { lock = new ReentrantLock(fair); } + @Override public void lock() { try { if(!tryLock(TIMEOUT)) { @@ -25,6 +26,7 @@ public class RecursiveLockImplJava5 implements RecursiveLock { owner = Thread.currentThread(); } + @Override public boolean tryLock(long timeout) throws InterruptedException { if(lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { owner = Thread.currentThread(); @@ -33,10 +35,12 @@ public class RecursiveLockImplJava5 implements RecursiveLock { return false; } + @Override public void unlock() throws RuntimeException { unlock(null); } + @Override public void unlock(Runnable taskAfterUnlockBeforeNotify) { validateLocked(); owner = null; @@ -46,26 +50,27 @@ public class RecursiveLockImplJava5 implements RecursiveLock { lock.unlock(); } + @Override public boolean isLocked() { return lock.isLocked(); } + @Override public Thread getOwner() { return owner; } + @Override public boolean isLockedByOtherThread() { return lock.isLocked() && !lock.isHeldByCurrentThread(); } - public boolean isOwner() { - return lock.isHeldByCurrentThread(); - } - + @Override public boolean isOwner(Thread thread) { return lock.isLocked() && owner == thread; } + @Override public void validateLocked() throws RuntimeException { if ( !lock.isHeldByCurrentThread() ) { if ( !lock.isLocked() ) { @@ -76,10 +81,12 @@ public class RecursiveLockImplJava5 implements RecursiveLock { } } + @Override public int getHoldCount() { return lock.getHoldCount(); } + @Override public int getQueueLength() { return lock.getQueueLength(); } diff --git a/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java b/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java index 695c9c3..aac9348 100644 --- a/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java +++ b/src/java/jogamp/common/util/locks/RecursiveThreadGroupLockImpl01Unfairish.java @@ -43,12 +43,14 @@ public class RecursiveThreadGroupLockImpl01Unfairish threads = null; holdCountAdditionOwner = 0; } + @Override public final void incrHoldCount(Thread t) { super.incrHoldCount(t); if(!isOriginalOwner(t)) { holdCountAdditionOwner++; } } + @Override public final void decrHoldCount(Thread t) { super.decrHoldCount(t); if(!isOriginalOwner(t)) { @@ -62,6 +64,7 @@ public class RecursiveThreadGroupLockImpl01Unfairish public final boolean isOriginalOwner(Thread t) { return super.isOwner(t); } + @Override public final boolean isOwner(Thread t) { if(getExclusiveOwnerThread()==t) { return true; @@ -136,16 +139,19 @@ public class RecursiveThreadGroupLockImpl01Unfairish super(new ThreadGroupSync()); } + @Override public final boolean isOriginalOwner() { return isOriginalOwner(Thread.currentThread()); } + @Override public final boolean isOriginalOwner(Thread thread) { synchronized(sync) { return ((ThreadGroupSync)sync).isOriginalOwner(thread) ; } } + @Override public final void addOwner(Thread t) throws RuntimeException, IllegalArgumentException { validateLocked(); final Thread cur = Thread.currentThread(); @@ -159,6 +165,7 @@ public class RecursiveThreadGroupLockImpl01Unfairish tgSync.addOwner(t); } + @Override public final void unlock(Runnable taskAfterUnlockBeforeNotify) { synchronized(sync) { final Thread cur = Thread.currentThread(); @@ -196,11 +203,13 @@ public class RecursiveThreadGroupLockImpl01Unfairish } } + @Override public final void removeOwner(Thread t) throws RuntimeException, IllegalArgumentException { validateLocked(); ((ThreadGroupSync)sync).removeOwner(t); } + @Override public String toString() { final ThreadGroupSync tgSync = (ThreadGroupSync)sync; final int hc = sync.getHoldCount(); diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java index f0bed66..f369941 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceFileLock.java @@ -71,11 +71,13 @@ public class SingletonInstanceFileLock extends SingletonInstance { setupFileCleanup(); } + @Override public final String getName() { return file.getPath(); } private void setupFileCleanup() { file.deleteOnExit(); Runtime.getRuntime().addShutdownHook(new Thread() { + @Override public void run() { unlock(); } diff --git a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java index e5ba012..14a6aaf 100644 --- a/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java +++ b/src/java/jogamp/common/util/locks/SingletonInstanceServerSocket.java @@ -81,6 +81,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { return singletonServer.getPortNumber(); } + @Override public final String getName() { return fullName; } @Override @@ -180,6 +181,7 @@ public class SingletonInstanceServerSocket extends SingletonInstance { return null; } + @Override public void run() { { final Thread currentThread = Thread.currentThread(); diff --git a/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java b/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java index 3576be7..45df7b2 100644 --- a/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java +++ b/src/junit/com/jogamp/common/util/locks/TestRecursiveLock01.java @@ -28,9 +28,7 @@ package com.jogamp.common.util.locks; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; diff --git a/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java index 2eecce1..7191a74 100644 --- a/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java +++ b/src/junit/com/jogamp/common/util/locks/TestRecursiveThreadGroupLock01.java @@ -28,13 +28,7 @@ package com.jogamp.common.util.locks; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; import org.junit.Assert; import org.junit.Test; |