From e4baba27507ce78e64a150ec6f69fb96f5721a34 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 27 Sep 2011 11:52:53 +0200 Subject: Lock ChangeSet (fin): Cleanup, fix and enhance RecursiveLock implementation - RecursiveLock _is_ interface. - Use LockFactory to create a RecursiveLock. - Impl: RecursiveLockImpl01Unfairish - just using notify w/o any queue: fast - still enqueuing new lock-applicants if queue full (nice) - lock's sync extends AbstractOwnableSynchronizer and uses it (monitor) - Impl: RecursiveLockImpl01CompleteFair - using queue and interrupt for correctness (slow) - lock's sync extends AbstractOwnableSynchronizer and uses it (monitor) - Impl: RecursiveLockImplJava5 for using Java5's concurrency impl. - to verify correctness, performance and deviation of locking time TestRecursiveLock01 new performance measurements incl. simple avrg and deviation shows best combined performance-deviation w/ our RecursiveLockImpl01Unfairish os Linux and MacOSX. RecursiveLockImpl01Unfairish is the default in LockFactory. Adding 'private' LockDebugUtil, allowing validating all holdings locks of one thread as stack traces (Throwable). Besides the AbstractOwnableSynchronizer utilization, this helps debugging deadlocks and starvation very well. --- .../common/util/locks/RecursiveLockImplJava5.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java (limited to 'src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java') diff --git a/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java new file mode 100644 index 0000000..b4e6ce0 --- /dev/null +++ b/src/java/jogamp/common/util/locks/RecursiveLockImplJava5.java @@ -0,0 +1,81 @@ +package jogamp.common.util.locks; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; + +import com.jogamp.common.util.locks.RecursiveLock; + +public class RecursiveLockImplJava5 implements RecursiveLock { + + volatile Thread owner = null; + ReentrantLock lock; + + public RecursiveLockImplJava5(boolean fair) { + lock = new ReentrantLock(fair); + } + + public void lock() { + try { + if(!tryLock(TIMEOUT)) { + throw new RuntimeException("Waited "+TIMEOUT+"ms for: "+threadName(owner)+" - "+threadName(Thread.currentThread())+", with count "+getHoldCount()+", lock: "+this); + } + } catch (InterruptedException e) { + throw new RuntimeException("Interrupted", e); + } + owner = Thread.currentThread(); + } + + public boolean tryLock(long timeout) throws InterruptedException { + if(lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { + owner = Thread.currentThread(); + return true; + } + return false; + } + + public void unlock() throws RuntimeException { + validateLocked(); + owner = null; + lock.unlock(); + } + + public boolean isLocked() { + return lock.isLocked(); + } + + public Thread getOwner() { + return owner; + } + + public boolean isLockedByOtherThread() { + return lock.isLocked() && !lock.isHeldByCurrentThread(); + } + + public boolean isOwner() { + return lock.isHeldByCurrentThread(); + } + + public boolean isOwner(Thread thread) { + return lock.isLocked() && owner == thread; + } + + public void validateLocked() { + if ( !lock.isHeldByCurrentThread() ) { + if ( !lock.isLocked() ) { + throw new RuntimeException(Thread.currentThread()+": Not locked"); + } else { + throw new RuntimeException(Thread.currentThread()+": Not owner, owner is "+owner); + } + } + } + + public int getHoldCount() { + return lock.getHoldCount(); + } + + public int getQueueLength() { + return lock.getQueueLength(); + } + + private String threadName(Thread t) { return null!=t ? "<"+t.getName()+">" : "" ; } +} -- cgit v1.2.3