ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.60 by jsr166, Mon May 9 20:00:19 2011 UTC vs.
Revision 1.84 by jsr166, Thu Sep 26 20:48:53 2019 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.concurrent.atomic.AtomicBoolean;
11 import java.util.concurrent.locks.*;
12 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import java.io.*;
15 import java.util.*;
10  
11 + import java.util.Arrays;
12 + import java.util.Collection;
13 + import java.util.HashSet;
14 + import java.util.concurrent.Callable;
15 + import java.util.concurrent.CountDownLatch;
16 + import java.util.concurrent.atomic.AtomicBoolean;
17 + import java.util.concurrent.locks.Condition;
18 + import java.util.concurrent.locks.Lock;
19 + import java.util.concurrent.locks.ReentrantReadWriteLock;
20 +
21 + import junit.framework.Test;
22 + import junit.framework.TestSuite;
23 +
24 + @SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
25   public class ReentrantReadWriteLockTest extends JSR166TestCase {
26      public static void main(String[] args) {
27 <        junit.textui.TestRunner.run(suite());
27 >        main(suite(), args);
28      }
29      public static Test suite() {
30          return new TestSuite(ReentrantReadWriteLockTest.class);
# Line 67 | Line 75 | public class ReentrantReadWriteLockTest
75       */
76      void releaseWriteLock(PublicReentrantReadWriteLock lock) {
77          ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
78 <        assertWriteLockedBy(lock, Thread.currentThread());
78 >        assertWriteLockedByMoi(lock);
79          assertEquals(1, lock.getWriteHoldCount());
80          writeLock.unlock();
81          assertNotWriteLocked(lock);
# Line 84 | Line 92 | public class ReentrantReadWriteLockTest
92              Thread.yield();
93          }
94          assertTrue(t.isAlive());
95 <        assertTrue(lock.getOwner() != t);
95 >        assertNotSame(t, lock.getOwner());
96      }
97  
98      /**
# Line 94 | Line 102 | public class ReentrantReadWriteLockTest
102          assertFalse(lock.isWriteLocked());
103          assertFalse(lock.isWriteLockedByCurrentThread());
104          assertFalse(lock.writeLock().isHeldByCurrentThread());
97        assertNull(lock.getOwner());
105          assertEquals(0, lock.getWriteHoldCount());
106 +        assertEquals(0, lock.writeLock().getHoldCount());
107 +        assertNull(lock.getOwner());
108      }
109  
110      /**
# Line 110 | Line 119 | public class ReentrantReadWriteLockTest
119                       lock.writeLock().isHeldByCurrentThread());
120          assertEquals(t == Thread.currentThread(),
121                       lock.getWriteHoldCount() > 0);
122 +        assertEquals(t == Thread.currentThread(),
123 +                     lock.writeLock().getHoldCount() > 0);
124          assertEquals(0, lock.getReadLockCount());
125      }
126  
127      /**
128 +     * Checks that lock is write-locked by the current thread.
129 +     */
130 +    void assertWriteLockedByMoi(PublicReentrantReadWriteLock lock) {
131 +        assertWriteLockedBy(lock, Thread.currentThread());
132 +    }
133 +
134 +    /**
135       * Checks that condition c has no waiters.
136       */
137      void assertHasNoWaiters(PublicReentrantReadWriteLock lock, Condition c) {
# Line 135 | Line 153 | public class ReentrantReadWriteLockTest
153          lock.writeLock().unlock();
154      }
155  
156 <    enum AwaitMethod { await, awaitNanos, awaitUntil };
156 >    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
157  
158      /**
159 <     * Awaits condition using the specified AwaitMethod
159 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
160       */
161      void await(Condition c, AwaitMethod awaitMethod)
162              throws InterruptedException {
163 +        long timeoutMillis = 2 * LONG_DELAY_MS;
164          switch (awaitMethod) {
165          case await:
166              c.await();
167              break;
168 +        case awaitTimed:
169 +            assertTrue(c.await(timeoutMillis, MILLISECONDS));
170 +            break;
171          case awaitNanos:
172 <            long nanosRemaining = c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
173 <            assertTrue(nanosRemaining > 0);
172 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
173 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
174 >            assertTrue(nanosRemaining > timeoutNanos / 2);
175 >            assertTrue(nanosRemaining <= timeoutNanos);
176              break;
177          case awaitUntil:
178 <            java.util.Date d = new java.util.Date();
155 <            assertTrue(c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)));
178 >            assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
179              break;
180 +        default:
181 +            throw new AssertionError();
182          }
183      }
184  
# Line 189 | Line 214 | public class ReentrantReadWriteLockTest
214              new PublicReentrantReadWriteLock(fair);
215          assertNotWriteLocked(lock);
216          lock.writeLock().lock();
217 <        assertWriteLockedBy(lock, Thread.currentThread());
217 >        assertWriteLockedByMoi(lock);
218          lock.writeLock().unlock();
219          assertNotWriteLocked(lock);
220          assertEquals(0, lock.getReadLockCount());
# Line 207 | Line 232 | public class ReentrantReadWriteLockTest
232      public void testGetWriteHoldCount()      { testGetWriteHoldCount(false); }
233      public void testGetWriteHoldCount_fair() { testGetWriteHoldCount(true); }
234      public void testGetWriteHoldCount(boolean fair) {
235 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
235 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
236          for (int i = 1; i <= SIZE; i++) {
237              lock.writeLock().lock();
238              assertEquals(i,lock.getWriteHoldCount());
239          }
240          for (int i = SIZE; i > 0; i--) {
241              lock.writeLock().unlock();
242 <            assertEquals(i-1,lock.getWriteHoldCount());
242 >            assertEquals(i - 1,lock.getWriteHoldCount());
243          }
244      }
245  
# Line 224 | Line 249 | public class ReentrantReadWriteLockTest
249      public void testGetHoldCount()      { testGetHoldCount(false); }
250      public void testGetHoldCount_fair() { testGetHoldCount(true); }
251      public void testGetHoldCount(boolean fair) {
252 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
252 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
253          for (int i = 1; i <= SIZE; i++) {
254              lock.writeLock().lock();
255              assertEquals(i,lock.writeLock().getHoldCount());
256          }
257          for (int i = SIZE; i > 0; i--) {
258              lock.writeLock().unlock();
259 <            assertEquals(i-1,lock.writeLock().getHoldCount());
259 >            assertEquals(i - 1,lock.writeLock().getHoldCount());
260          }
261      }
262  
# Line 241 | Line 266 | public class ReentrantReadWriteLockTest
266      public void testGetReadHoldCount()      { testGetReadHoldCount(false); }
267      public void testGetReadHoldCount_fair() { testGetReadHoldCount(true); }
268      public void testGetReadHoldCount(boolean fair) {
269 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
269 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
270          for (int i = 1; i <= SIZE; i++) {
271              lock.readLock().lock();
272              assertEquals(i,lock.getReadHoldCount());
273          }
274          for (int i = SIZE; i > 0; i--) {
275              lock.readLock().unlock();
276 <            assertEquals(i-1,lock.getReadHoldCount());
276 >            assertEquals(i - 1,lock.getReadHoldCount());
277          }
278      }
279  
# Line 258 | Line 283 | public class ReentrantReadWriteLockTest
283      public void testWriteUnlock_IMSE()      { testWriteUnlock_IMSE(false); }
284      public void testWriteUnlock_IMSE_fair() { testWriteUnlock_IMSE(true); }
285      public void testWriteUnlock_IMSE(boolean fair) {
286 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
286 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
287          try {
288              lock.writeLock().unlock();
289              shouldThrow();
# Line 271 | Line 296 | public class ReentrantReadWriteLockTest
296      public void testReadUnlock_IMSE()      { testReadUnlock_IMSE(false); }
297      public void testReadUnlock_IMSE_fair() { testReadUnlock_IMSE(true); }
298      public void testReadUnlock_IMSE(boolean fair) {
299 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
299 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
300          try {
301              lock.readLock().unlock();
302              shouldThrow();
# Line 359 | Line 384 | public class ReentrantReadWriteLockTest
384      }
385  
386      /**
387 +     * write-tryLock on an unlocked lock succeeds
388 +     */
389 +    public void testWriteTryLock()      { testWriteTryLock(false); }
390 +    public void testWriteTryLock_fair() { testWriteTryLock(true); }
391 +    public void testWriteTryLock(boolean fair) {
392 +        final PublicReentrantReadWriteLock lock =
393 +            new PublicReentrantReadWriteLock(fair);
394 +        assertTrue(lock.writeLock().tryLock());
395 +        assertWriteLockedByMoi(lock);
396 +        assertTrue(lock.writeLock().tryLock());
397 +        assertWriteLockedByMoi(lock);
398 +        lock.writeLock().unlock();
399 +        releaseWriteLock(lock);
400 +    }
401 +
402 +    /**
403       * write-tryLock fails if locked
404       */
405      public void testWriteTryLockWhenLocked()      { testWriteTryLockWhenLocked(false); }
# Line 478 | Line 519 | public class ReentrantReadWriteLockTest
519  
520      /**
521       * A thread that tries to acquire a fair read lock (non-reentrantly)
522 <     * will block if there is a waiting writer thread.
522 >     * will block if there is a waiting writer thread
523       */
524      public void testReaderWriterReaderFairFifo() {
525          final PublicReentrantReadWriteLock lock =
# Line 555 | Line 596 | public class ReentrantReadWriteLockTest
596      }
597  
598      /**
599 <     * Read trylock succeeds (barging) even in the presence of waiting readers and/or writers.
599 >     * Read trylock succeeds (barging) even in the presence of waiting
600 >     * readers and/or writers
601       */
602      public void testReadTryLockBarging()      { testReadTryLockBarging(false); }
603      public void testReadTryLockBarging_fair() { testReadTryLockBarging(true); }
# Line 622 | Line 664 | public class ReentrantReadWriteLockTest
664  
665          waitForQueuedThread(lock, t1);
666          waitForQueuedThread(lock, t2);
667 <        assertWriteLockedBy(lock, Thread.currentThread());
667 >        assertWriteLockedByMoi(lock);
668          lock.readLock().lock();
669          lock.readLock().unlock();
670          releaseWriteLock(lock);
# Line 656 | Line 698 | public class ReentrantReadWriteLockTest
698  
699          waitForQueuedThread(lock, t1);
700          waitForQueuedThread(lock, t2);
701 <        assertWriteLockedBy(lock, Thread.currentThread());
701 >        assertWriteLockedByMoi(lock);
702          lock.readLock().lock();
703          lock.readLock().unlock();
704 <        assertWriteLockedBy(lock, Thread.currentThread());
704 >        assertWriteLockedByMoi(lock);
705          lock.writeLock().unlock();
706          awaitTermination(t1);
707          awaitTermination(t2);
# Line 691 | Line 733 | public class ReentrantReadWriteLockTest
733  
734          waitForQueuedThread(lock, t1);
735          waitForQueuedThread(lock, t2);
736 <        assertWriteLockedBy(lock, Thread.currentThread());
736 >        assertWriteLockedByMoi(lock);
737          assertEquals(1, lock.getWriteHoldCount());
738          lock.writeLock().lock();
739 <        assertWriteLockedBy(lock, Thread.currentThread());
739 >        assertWriteLockedByMoi(lock);
740          assertEquals(2, lock.getWriteHoldCount());
741          lock.writeLock().unlock();
742 <        assertWriteLockedBy(lock, Thread.currentThread());
742 >        assertWriteLockedByMoi(lock);
743          assertEquals(1, lock.getWriteHoldCount());
744          lock.writeLock().unlock();
745          awaitTermination(t1);
# Line 745 | Line 787 | public class ReentrantReadWriteLockTest
787      public void testWriteTryLock_Timeout()      { testWriteTryLock_Timeout(false); }
788      public void testWriteTryLock_Timeout_fair() { testWriteTryLock_Timeout(true); }
789      public void testWriteTryLock_Timeout(boolean fair) {
790 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
790 >        final PublicReentrantReadWriteLock lock =
791 >            new PublicReentrantReadWriteLock(fair);
792 >        final long timeoutMillis = timeoutMillis();
793          lock.writeLock().lock();
794          Thread t = newStartedThread(new CheckedRunnable() {
795              public void realRun() throws InterruptedException {
796                  long startTime = System.nanoTime();
753                long timeoutMillis = 10;
797                  assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS));
798                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
799              }});
800  
801          awaitTermination(t);
802 <        assertTrue(lock.writeLock().isHeldByCurrentThread());
760 <        lock.writeLock().unlock();
802 >        releaseWriteLock(lock);
803      }
804  
805      /**
# Line 771 | Line 813 | public class ReentrantReadWriteLockTest
813          Thread t = newStartedThread(new CheckedRunnable() {
814              public void realRun() throws InterruptedException {
815                  long startTime = System.nanoTime();
816 <                long timeoutMillis = 10;
816 >                long timeoutMillis = timeoutMillis();
817                  assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS));
818                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
819              }});
# Line 782 | Line 824 | public class ReentrantReadWriteLockTest
824      }
825  
826      /**
827 <     * write lockInterruptibly succeeds if lock free else is interruptible
827 >     * write lockInterruptibly succeeds if unlocked, else is interruptible
828       */
829      public void testWriteLockInterruptibly()      { testWriteLockInterruptibly(false); }
830      public void testWriteLockInterruptibly_fair() { testWriteLockInterruptibly(true); }
# Line 791 | Line 833 | public class ReentrantReadWriteLockTest
833              new PublicReentrantReadWriteLock(fair);
834          try {
835              lock.writeLock().lockInterruptibly();
836 <        } catch (Throwable t) {
795 <            threadUnexpectedException(t);
796 <        }
836 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
837          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
838              public void realRun() throws InterruptedException {
839                  lock.writeLock().lockInterruptibly();
# Line 801 | Line 841 | public class ReentrantReadWriteLockTest
841  
842          waitForQueuedThread(lock, t);
843          t.interrupt();
844 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
845          awaitTermination(t);
846          releaseWriteLock(lock);
847      }
# Line 817 | Line 858 | public class ReentrantReadWriteLockTest
858              lock.readLock().lockInterruptibly();
859              lock.readLock().unlock();
860              lock.writeLock().lockInterruptibly();
861 <        } catch (Throwable t) {
821 <            threadUnexpectedException(t);
822 <        }
861 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
862          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
863              public void realRun() throws InterruptedException {
864                  lock.readLock().lockInterruptibly();
# Line 839 | Line 878 | public class ReentrantReadWriteLockTest
878      public void testAwait_IMSE(boolean fair) {
879          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
880          final Condition c = lock.writeLock().newCondition();
881 <        long startTime = System.nanoTime();
882 <        try {
844 <            try {
845 <                c.await();
846 <                shouldThrow();
847 <            } catch (IllegalMonitorStateException success) {}
848 <            try {
849 <                c.await(LONG_DELAY_MS, MILLISECONDS);
850 <                shouldThrow();
851 <            } catch (IllegalMonitorStateException success) {}
852 <            try {
853 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
854 <                shouldThrow();
855 <            } catch (IllegalMonitorStateException success) {}
881 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
882 >            long startTime = System.nanoTime();
883              try {
884 <                c.awaitUninterruptibly();
884 >                await(c, awaitMethod);
885                  shouldThrow();
886 <            } catch (IllegalMonitorStateException success) {}
887 <        } catch (Throwable t) {
888 <            threadUnexpectedException(t);
886 >            } catch (IllegalMonitorStateException success) {
887 >            } catch (InterruptedException fail) {
888 >                threadUnexpectedException(fail);
889 >            }
890 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
891          }
863        assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS);
892      }
893  
894      /**
# Line 897 | Line 925 | public class ReentrantReadWriteLockTest
925      public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
926      public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
927      public void testAwaitNanos_Timeout(boolean fair) {
928 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
929 +        final Condition c = lock.writeLock().newCondition();
930 +        final long timeoutMillis = timeoutMillis();
931 +        lock.writeLock().lock();
932 +        final long startTime = System.nanoTime();
933 +        final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
934          try {
901            final ReentrantReadWriteLock lock =
902                new ReentrantReadWriteLock(fair);
903            final Condition c = lock.writeLock().newCondition();
904            lock.writeLock().lock();
905            long startTime = System.nanoTime();
906            long timeoutMillis = 10;
907            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
935              long nanosRemaining = c.awaitNanos(timeoutNanos);
936              assertTrue(nanosRemaining <= 0);
937 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
938 <            lock.writeLock().unlock();
939 <        } catch (InterruptedException e) {
913 <            threadUnexpectedException(e);
914 <        }
937 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
938 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
939 >        lock.writeLock().unlock();
940      }
941  
942      /**
# Line 920 | Line 945 | public class ReentrantReadWriteLockTest
945      public void testAwait_Timeout()      { testAwait_Timeout(false); }
946      public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
947      public void testAwait_Timeout(boolean fair) {
948 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
949 +        final Condition c = lock.writeLock().newCondition();
950 +        final long timeoutMillis = timeoutMillis();
951 +        lock.writeLock().lock();
952 +        final long startTime = System.nanoTime();
953          try {
924            final ReentrantReadWriteLock lock =
925                new ReentrantReadWriteLock(fair);
926            final Condition c = lock.writeLock().newCondition();
927            lock.writeLock().lock();
928            long startTime = System.nanoTime();
929            long timeoutMillis = 10;
954              assertFalse(c.await(timeoutMillis, MILLISECONDS));
955 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
956 <            lock.writeLock().unlock();
957 <        } catch (InterruptedException e) {
934 <            threadUnexpectedException(e);
935 <        }
955 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
956 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
957 >        lock.writeLock().unlock();
958      }
959  
960      /**
# Line 941 | Line 963 | public class ReentrantReadWriteLockTest
963      public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
964      public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
965      public void testAwaitUntil_Timeout(boolean fair) {
966 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
967 +        final Condition c = lock.writeLock().newCondition();
968 +        lock.writeLock().lock();
969 +        // We shouldn't assume that nanoTime and currentTimeMillis
970 +        // use the same time source, so don't use nanoTime here.
971 +        final java.util.Date delayedDate = delayedDate(timeoutMillis());
972          try {
973 <            final ReentrantReadWriteLock lock =
974 <                new ReentrantReadWriteLock(fair);
975 <            final Condition c = lock.writeLock().newCondition();
976 <            lock.writeLock().lock();
949 <            long startTime = System.nanoTime();
950 <            long timeoutMillis = 10;
951 <            java.util.Date d = new java.util.Date();
952 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
953 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
954 <            lock.writeLock().unlock();
955 <        } catch (InterruptedException e) {
956 <            threadUnexpectedException(e);
957 <        }
973 >            assertFalse(c.awaitUntil(delayedDate));
974 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
975 >        assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
976 >        lock.writeLock().unlock();
977      }
978  
979      /**
# Line 986 | Line 1005 | public class ReentrantReadWriteLockTest
1005      }
1006  
1007      /**
1008 <     * awaitUninterruptibly doesn't abort on interrupt
1008 >     * awaitUninterruptibly is uninterruptible
1009       */
1010      public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
1011      public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
1012      public void testAwaitUninterruptibly(boolean fair) {
1013 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1014 <        final Condition c = lock.writeLock().newCondition();
1015 <        final CountDownLatch locked = new CountDownLatch(1);
1016 <        Thread t = newStartedThread(new CheckedRunnable() {
1013 >        final Lock lock = new ReentrantReadWriteLock(fair).writeLock();
1014 >        final Condition condition = lock.newCondition();
1015 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
1016 >
1017 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1018              public void realRun() {
1019 <                lock.writeLock().lock();
1020 <                locked.countDown();
1021 <                c.awaitUninterruptibly();
1019 >                // Interrupt before awaitUninterruptibly
1020 >                lock.lock();
1021 >                pleaseInterrupt.countDown();
1022 >                Thread.currentThread().interrupt();
1023 >                condition.awaitUninterruptibly();
1024                  assertTrue(Thread.interrupted());
1025 <                lock.writeLock().unlock();
1025 >                lock.unlock();
1026              }});
1027  
1028 <        await(locked);
1029 <        lock.writeLock().lock();
1030 <        lock.writeLock().unlock();
1031 <        t.interrupt();
1032 <        long timeoutMillis = 10;
1033 <        assertThreadJoinTimesOut(t, timeoutMillis);
1034 <        lock.writeLock().lock();
1035 <        c.signal();
1036 <        lock.writeLock().unlock();
1037 <        awaitTermination(t);
1028 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1029 >            public void realRun() {
1030 >                // Interrupt during awaitUninterruptibly
1031 >                lock.lock();
1032 >                pleaseInterrupt.countDown();
1033 >                condition.awaitUninterruptibly();
1034 >                assertTrue(Thread.interrupted());
1035 >                lock.unlock();
1036 >            }});
1037 >
1038 >        await(pleaseInterrupt);
1039 >        t2.interrupt();
1040 >        lock.lock();
1041 >        lock.unlock();
1042 >        assertThreadBlocks(t1, Thread.State.WAITING);
1043 >        assertThreadBlocks(t2, Thread.State.WAITING);
1044 >
1045 >        lock.lock();
1046 >        condition.signalAll();
1047 >        lock.unlock();
1048 >
1049 >        awaitTermination(t1);
1050 >        awaitTermination(t2);
1051      }
1052  
1053      /**
# Line 1020 | Line 1055 | public class ReentrantReadWriteLockTest
1055       */
1056      public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
1057      public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
1058 +    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
1059 +    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
1060      public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
1061      public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
1062      public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
# Line 1032 | Line 1069 | public class ReentrantReadWriteLockTest
1069          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1070              public void realRun() throws InterruptedException {
1071                  lock.writeLock().lock();
1072 <                assertWriteLockedBy(lock, Thread.currentThread());
1072 >                assertWriteLockedByMoi(lock);
1073                  assertHasNoWaiters(lock, c);
1074                  locked.countDown();
1075                  try {
1076                      await(c, awaitMethod);
1077                  } finally {
1078 <                    assertWriteLockedBy(lock, Thread.currentThread());
1078 >                    assertWriteLockedByMoi(lock);
1079                      assertHasNoWaiters(lock, c);
1080                      lock.writeLock().unlock();
1081                      assertFalse(Thread.interrupted());
# Line 1057 | Line 1094 | public class ReentrantReadWriteLockTest
1094       */
1095      public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
1096      public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
1097 +    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
1098 +    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
1099      public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
1100      public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
1101      public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
# Line 1090 | Line 1129 | public class ReentrantReadWriteLockTest
1129      }
1130  
1131      /**
1132 <     * signal wakes up waiting threads in FIFO order.
1132 >     * signal wakes up waiting threads in FIFO order
1133       */
1134      public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1135      public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
# Line 1150 | Line 1189 | public class ReentrantReadWriteLockTest
1189          Thread t1 = newStartedThread(new CheckedRunnable() {
1190              public void realRun() throws InterruptedException {
1191                  lock.writeLock().lock();
1192 <                assertWriteLockedBy(lock, Thread.currentThread());
1192 >                assertWriteLockedByMoi(lock);
1193                  assertEquals(1, lock.writeLock().getHoldCount());
1194                  locked.countDown();
1195                  c.await();
1196 <                assertWriteLockedBy(lock, Thread.currentThread());
1196 >                assertWriteLockedByMoi(lock);
1197                  assertEquals(1, lock.writeLock().getHoldCount());
1198                  lock.writeLock().unlock();
1199              }});
# Line 1163 | Line 1202 | public class ReentrantReadWriteLockTest
1202              public void realRun() throws InterruptedException {
1203                  lock.writeLock().lock();
1204                  lock.writeLock().lock();
1205 <                assertWriteLockedBy(lock, Thread.currentThread());
1205 >                assertWriteLockedByMoi(lock);
1206                  assertEquals(2, lock.writeLock().getHoldCount());
1207                  locked.countDown();
1208                  c.await();
1209 <                assertWriteLockedBy(lock, Thread.currentThread());
1209 >                assertWriteLockedByMoi(lock);
1210                  assertEquals(2, lock.writeLock().getHoldCount());
1211                  lock.writeLock().unlock();
1212                  lock.writeLock().unlock();
# Line 1189 | Line 1228 | public class ReentrantReadWriteLockTest
1228      public void testSerialization()      { testSerialization(false); }
1229      public void testSerialization_fair() { testSerialization(true); }
1230      public void testSerialization(boolean fair) {
1231 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1231 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1232          lock.writeLock().lock();
1233          lock.readLock().lock();
1234  
# Line 1205 | Line 1244 | public class ReentrantReadWriteLockTest
1244          assertEquals(1, clone.getReadLockCount());
1245          clone.readLock().unlock();
1246          clone.writeLock().unlock();
1247 +        assertFalse(clone.isWriteLocked());
1248 +        assertEquals(1, lock.getReadLockCount());
1249 +        assertEquals(0, clone.getReadLockCount());
1250      }
1251  
1252      /**
# Line 1248 | Line 1290 | public class ReentrantReadWriteLockTest
1290      }
1291  
1292      /**
1293 <     * hasQueuedThread reports whether a thread is queued.
1293 >     * hasQueuedThread reports whether a thread is queued
1294       */
1295      public void testHasQueuedThread()      { testHasQueuedThread(false); }
1296      public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
# Line 1588 | Line 1630 | public class ReentrantReadWriteLockTest
1630      public void testToString()      { testToString(false); }
1631      public void testToString_fair() { testToString(true); }
1632      public void testToString(boolean fair) {
1633 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1634 <        String us = lock.toString();
1635 <        assertTrue(us.indexOf("Write locks = 0") >= 0);
1636 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1637 <        lock.writeLock().lock();
1638 <        String ws = lock.toString();
1639 <        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1640 <        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1633 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1634 >        assertTrue(lock.toString().contains("Write locks = 0"));
1635 >        assertTrue(lock.toString().contains("Read locks = 0"));
1636 >        lock.writeLock().lock();
1637 >        assertTrue(lock.toString().contains("Write locks = 1"));
1638 >        assertTrue(lock.toString().contains("Read locks = 0"));
1639 >        lock.writeLock().lock();
1640 >        assertTrue(lock.toString().contains("Write locks = 2"));
1641 >        assertTrue(lock.toString().contains("Read locks = 0"));
1642 >        lock.writeLock().unlock();
1643          lock.writeLock().unlock();
1644          lock.readLock().lock();
1645 +        assertTrue(lock.toString().contains("Write locks = 0"));
1646 +        assertTrue(lock.toString().contains("Read locks = 1"));
1647          lock.readLock().lock();
1648 <        String rs = lock.toString();
1649 <        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1604 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1648 >        assertTrue(lock.toString().contains("Write locks = 0"));
1649 >        assertTrue(lock.toString().contains("Read locks = 2"));
1650      }
1651  
1652      /**
# Line 1610 | Line 1655 | public class ReentrantReadWriteLockTest
1655      public void testReadLockToString()      { testReadLockToString(false); }
1656      public void testReadLockToString_fair() { testReadLockToString(true); }
1657      public void testReadLockToString(boolean fair) {
1658 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1659 <        String us = lock.readLock().toString();
1615 <        assertTrue(us.indexOf("Read locks = 0") >= 0);
1658 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1659 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1660          lock.readLock().lock();
1661 +        assertTrue(lock.readLock().toString().contains("Read locks = 1"));
1662          lock.readLock().lock();
1663 <        String rs = lock.readLock().toString();
1664 <        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1663 >        assertTrue(lock.readLock().toString().contains("Read locks = 2"));
1664 >        lock.readLock().unlock();
1665 >        assertTrue(lock.readLock().toString().contains("Read locks = 1"));
1666 >        lock.readLock().unlock();
1667 >        assertTrue(lock.readLock().toString().contains("Read locks = 0"));
1668      }
1669  
1670      /**
# Line 1625 | Line 1673 | public class ReentrantReadWriteLockTest
1673      public void testWriteLockToString()      { testWriteLockToString(false); }
1674      public void testWriteLockToString_fair() { testWriteLockToString(true); }
1675      public void testWriteLockToString(boolean fair) {
1676 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1677 <        String us = lock.writeLock().toString();
1630 <        assertTrue(us.indexOf("Unlocked") >= 0);
1676 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
1677 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1678          lock.writeLock().lock();
1679 <        String ls = lock.writeLock().toString();
1680 <        assertTrue(ls.indexOf("Locked") >= 0);
1679 >        assertTrue(lock.writeLock().toString().contains("Locked by"));
1680 >        lock.writeLock().unlock();
1681 >        assertTrue(lock.writeLock().toString().contains("Unlocked"));
1682      }
1683  
1684 +    /**
1685 +     * ThreadMXBean reports the blockers that we expect.
1686 +     */
1687 +    public void testBlockers() {
1688 +        if (!testImplementationDetails) return;
1689 +        final boolean fair = randomBoolean();
1690 +        final boolean timedAcquire = randomBoolean();
1691 +        final boolean timedAwait = randomBoolean();
1692 +        final String syncClassName = fair
1693 +            ? "ReentrantReadWriteLock$FairSync"
1694 +            : "ReentrantReadWriteLock$NonfairSync";
1695 +        final String conditionClassName
1696 +            = "AbstractQueuedSynchronizer$ConditionObject";
1697 +        final Thread.State expectedAcquireState = timedAcquire
1698 +            ? Thread.State.TIMED_WAITING
1699 +            : Thread.State.WAITING;
1700 +        final Thread.State expectedAwaitState = timedAwait
1701 +            ? Thread.State.TIMED_WAITING
1702 +            : Thread.State.WAITING;
1703 +        final Lock lock = new ReentrantReadWriteLock(fair).writeLock();
1704 +        final Condition condition = lock.newCondition();
1705 +        final AtomicBoolean conditionSatisfied = new AtomicBoolean(false);
1706 +        lock.lock();
1707 +        final Thread thread = newStartedThread((Action) () -> {
1708 +            if (timedAcquire)
1709 +                lock.tryLock(LONGER_DELAY_MS, MILLISECONDS);
1710 +            else
1711 +                lock.lock();
1712 +            while (!conditionSatisfied.get())
1713 +                if (timedAwait)
1714 +                    condition.await(LONGER_DELAY_MS, MILLISECONDS);
1715 +                else
1716 +                    condition.await();
1717 +        });
1718 +        Callable<Boolean> waitingForLock = () -> {
1719 +            String className;
1720 +            return thread.getState() == expectedAcquireState
1721 +            && (className = blockerClassName(thread)) != null
1722 +            && className.endsWith(syncClassName);
1723 +        };
1724 +        waitForThreadToEnterWaitState(thread, waitingForLock);
1725 +
1726 +        lock.unlock();
1727 +        Callable<Boolean> waitingForCondition = () -> {
1728 +            String className;
1729 +            return thread.getState() == expectedAwaitState
1730 +            && (className = blockerClassName(thread)) != null
1731 +            && className.endsWith(conditionClassName);
1732 +        };
1733 +        waitForThreadToEnterWaitState(thread, waitingForCondition);
1734 +
1735 +        // politely release the waiter
1736 +        conditionSatisfied.set(true);
1737 +        lock.lock();
1738 +        try {
1739 +            condition.signal();
1740 +        } finally { lock.unlock(); }
1741 +
1742 +        awaitTermination(thread);
1743 +    }
1744   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines