--- jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2005/08/01 19:53:01 1.23 +++ jsr166/src/test/tck/ReentrantReadWriteLockTest.java 2006/05/18 10:29:23 1.27 @@ -84,10 +84,12 @@ public class ReentrantReadWriteLockTest rl.writeLock().lock(); assertTrue(rl.isWriteLocked()); assertTrue(rl.isWriteLockedByCurrentThread()); + assertTrue(rl.writeLock().isHeldByCurrentThread()); assertEquals(0, rl.getReadLockCount()); rl.writeLock().unlock(); assertFalse(rl.isWriteLocked()); assertFalse(rl.isWriteLockedByCurrentThread()); + assertFalse(rl.writeLock().isHeldByCurrentThread()); assertEquals(0, rl.getReadLockCount()); rl.readLock().lock(); assertFalse(rl.isWriteLocked()); @@ -108,10 +110,12 @@ public class ReentrantReadWriteLockTest rl.writeLock().lock(); assertTrue(rl.isWriteLocked()); assertTrue(rl.isWriteLockedByCurrentThread()); + assertTrue(rl.writeLock().isHeldByCurrentThread()); assertEquals(0, rl.getReadLockCount()); rl.writeLock().unlock(); assertFalse(rl.isWriteLocked()); assertFalse(rl.isWriteLockedByCurrentThread()); + assertFalse(rl.writeLock().isHeldByCurrentThread()); assertEquals(0, rl.getReadLockCount()); rl.readLock().lock(); assertFalse(rl.isWriteLocked()); @@ -139,6 +143,21 @@ public class ReentrantReadWriteLockTest } /** + * WriteLock.getHoldCount returns number of recursive holds + */ + public void testGetHoldCount() { + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + for(int i = 1; i <= SIZE; i++) { + lock.writeLock().lock(); + assertEquals(i,lock.writeLock().getHoldCount()); + } + for(int i = SIZE; i > 0; i--) { + lock.writeLock().unlock(); + assertEquals(i-1,lock.writeLock().getHoldCount()); + } + } + + /** * getReadHoldCount returns number of recursive holds */ public void testGetReadHoldCount() { @@ -184,7 +203,9 @@ public class ReentrantReadWriteLockTest try { lock.writeLock().lock(); t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); lock.writeLock().unlock(); t.join(); } catch(Exception e){ @@ -230,7 +251,9 @@ public class ReentrantReadWriteLockTest }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); lock.writeLock().unlock(); t.join(); } catch(Exception e){ @@ -698,6 +721,50 @@ public class ReentrantReadWriteLockTest } } + + /** + * Fair Read tryLock succeeds if readlocked but not writelocked + */ + public void testTryLockWhenReadLockedFair() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.readLock().lock(); + Thread t = new Thread(new Runnable() { + public void run() { + threadAssertTrue(lock.readLock().tryLock()); + lock.readLock().unlock(); + } + }); + try { + t.start(); + t.join(); + lock.readLock().unlock(); + } catch(Exception e){ + unexpectedException(); + } + } + + + + /** + * Fair write tryLock fails when readlocked + */ + public void testWriteTryLockWhenReadLockedFair() { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + lock.readLock().lock(); + Thread t = new Thread(new Runnable() { + public void run() { + threadAssertFalse(lock.writeLock().tryLock()); + } + }); + try { + t.start(); + t.join(); + lock.readLock().unlock(); + } catch(Exception e){ + unexpectedException(); + } + } + /** @@ -771,7 +838,9 @@ public class ReentrantReadWriteLockTest }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); + Thread.sleep(SHORT_DELAY_MS); t.join(); lock.writeLock().unlock(); } catch(Exception e){ @@ -801,6 +870,7 @@ public class ReentrantReadWriteLockTest }); try { t.start(); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); lock.writeLock().unlock();