287 |
|
lock.writeLock().lock(); |
288 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
289 |
|
public void realRun() throws InterruptedException { |
290 |
< |
lock.writeLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
290 |
> |
lock.writeLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
291 |
|
}}); |
292 |
|
|
293 |
|
waitForQueuedThread(lock, t); |
321 |
|
lock.writeLock().lock(); |
322 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
323 |
|
public void realRun() throws InterruptedException { |
324 |
< |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
324 |
> |
lock.readLock().tryLock(2 * LONG_DELAY_MS, MILLISECONDS); |
325 |
|
}}); |
326 |
|
|
327 |
|
waitForQueuedThread(lock, t); |
774 |
|
lock.writeLock().lock(); |
775 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
776 |
|
public void realRun() throws InterruptedException { |
777 |
< |
assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
777 |
> |
long startTime = System.nanoTime(); |
778 |
> |
long timeoutMillis = 10; |
779 |
> |
assertFalse(lock.writeLock().tryLock(timeoutMillis, MILLISECONDS)); |
780 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
781 |
|
}}); |
782 |
|
|
783 |
|
awaitTermination(t); |
793 |
|
lock.writeLock().lock(); |
794 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
795 |
|
public void realRun() throws InterruptedException { |
796 |
< |
assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
796 |
> |
long startTime = System.nanoTime(); |
797 |
> |
long timeoutMillis = 10; |
798 |
> |
assertFalse(lock.readLock().tryLock(timeoutMillis, MILLISECONDS)); |
799 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
800 |
|
}}); |
801 |
|
|
802 |
|
awaitTermination(t); |
923 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
924 |
|
final Condition c = lock.writeLock().newCondition(); |
925 |
|
lock.writeLock().lock(); |
926 |
+ |
long startTime = System.nanoTime(); |
927 |
+ |
long timeoutMillis = 10; |
928 |
|
java.util.Date d = new java.util.Date(); |
929 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
929 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis))); |
930 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
931 |
|
lock.writeLock().unlock(); |
932 |
|
} |
933 |
|
|
935 |
|
* await returns when signalled |
936 |
|
*/ |
937 |
|
public void testAwait() throws InterruptedException { |
938 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
938 |
> |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
939 |
|
final Condition c = lock.writeLock().newCondition(); |
940 |
|
final CountDownLatch locked = new CountDownLatch(1); |
941 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
948 |
|
|
949 |
|
locked.await(); |
950 |
|
lock.writeLock().lock(); |
951 |
+ |
assertHasWaiters(lock, c, t); |
952 |
|
c.signal(); |
953 |
+ |
assertHasNoWaiters(lock, c); |
954 |
|
assertTrue(t.isAlive()); |
955 |
|
lock.writeLock().unlock(); |
956 |
|
awaitTermination(t); |
963 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
964 |
|
final Condition c = lock.writeLock().newCondition(); |
965 |
|
final CountDownLatch locked = new CountDownLatch(1); |
955 |
– |
final AtomicBoolean canAwake = new AtomicBoolean(false); |
966 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
967 |
|
public void realRun() { |
968 |
|
lock.writeLock().lock(); |
976 |
|
lock.writeLock().lock(); |
977 |
|
lock.writeLock().unlock(); |
978 |
|
t.interrupt(); |
979 |
< |
t.join(10); |
980 |
< |
assertTrue(t.isAlive()); |
979 |
> |
long timeoutMillis = 10; |
980 |
> |
assertThreadJoinTimesOut(t, timeoutMillis); |
981 |
|
lock.writeLock().lock(); |
982 |
|
c.signal(); |
983 |
|
lock.writeLock().unlock(); |
1028 |
|
assertHasNoWaiters(lock, c); |
1029 |
|
locked.countDown(); |
1030 |
|
try { |
1031 |
< |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
1031 |
> |
c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS)); |
1032 |
|
} finally { |
1033 |
|
assertWriteLockedBy(lock, Thread.currentThread()); |
1034 |
|
assertHasNoWaiters(lock, c); |
1059 |
|
locked.countDown(); |
1060 |
|
java.util.Date d = new java.util.Date(); |
1061 |
|
try { |
1062 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1062 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS)); |
1063 |
|
} finally { |
1064 |
|
assertWriteLockedBy(lock, Thread.currentThread()); |
1065 |
|
assertHasNoWaiters(lock, c); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
+ |
* await after multiple reentrant locking preserves lock count |
1159 |
+ |
*/ |
1160 |
+ |
public void testAwaitLockCount() throws InterruptedException { |
1161 |
+ |
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1162 |
+ |
final Condition c = lock.writeLock().newCondition(); |
1163 |
+ |
final CountDownLatch locked = new CountDownLatch(2); |
1164 |
+ |
Thread t1 = newStartedThread(new CheckedRunnable() { |
1165 |
+ |
public void realRun() throws InterruptedException { |
1166 |
+ |
lock.writeLock().lock(); |
1167 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
1168 |
+ |
assertEquals(1, lock.writeLock().getHoldCount()); |
1169 |
+ |
locked.countDown(); |
1170 |
+ |
c.await(); |
1171 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
1172 |
+ |
assertEquals(1, lock.writeLock().getHoldCount()); |
1173 |
+ |
lock.writeLock().unlock(); |
1174 |
+ |
}}); |
1175 |
+ |
|
1176 |
+ |
Thread t2 = newStartedThread(new CheckedRunnable() { |
1177 |
+ |
public void realRun() throws InterruptedException { |
1178 |
+ |
lock.writeLock().lock(); |
1179 |
+ |
lock.writeLock().lock(); |
1180 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
1181 |
+ |
assertEquals(2, lock.writeLock().getHoldCount()); |
1182 |
+ |
locked.countDown(); |
1183 |
+ |
c.await(); |
1184 |
+ |
assertWriteLockedBy(lock, Thread.currentThread()); |
1185 |
+ |
assertEquals(2, lock.writeLock().getHoldCount()); |
1186 |
+ |
lock.writeLock().unlock(); |
1187 |
+ |
lock.writeLock().unlock(); |
1188 |
+ |
}}); |
1189 |
+ |
|
1190 |
+ |
locked.await(); |
1191 |
+ |
lock.writeLock().lock(); |
1192 |
+ |
assertHasWaiters(lock, c, t1, t2); |
1193 |
+ |
c.signalAll(); |
1194 |
+ |
assertHasNoWaiters(lock, c); |
1195 |
+ |
lock.writeLock().unlock(); |
1196 |
+ |
awaitTermination(t1); |
1197 |
+ |
awaitTermination(t2); |
1198 |
+ |
} |
1199 |
+ |
|
1200 |
+ |
/** |
1201 |
|
* A serialized lock deserializes as unlocked |
1202 |
|
*/ |
1203 |
|
public void testSerialization() throws Exception { |
1220 |
|
/** |
1221 |
|
* hasQueuedThreads reports whether there are waiting threads |
1222 |
|
*/ |
1223 |
< |
public void testhasQueuedThreads() throws InterruptedException { |
1223 |
> |
public void testHasQueuedThreads() throws InterruptedException { |
1224 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1225 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
1226 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1227 |
|
assertFalse(lock.hasQueuedThreads()); |
1228 |
|
lock.writeLock().lock(); |
1229 |
|
assertFalse(lock.hasQueuedThreads()); |
1177 |
– |
long startTime = System.nanoTime(); |
1230 |
|
t1.start(); |
1231 |
|
waitForQueuedThread(lock, t1); |
1232 |
+ |
assertTrue(lock.hasQueuedThreads()); |
1233 |
|
t2.start(); |
1234 |
|
waitForQueuedThread(lock, t2); |
1235 |
|
assertTrue(lock.hasQueuedThreads()); |
1245 |
|
* hasQueuedThread(null) throws NPE |
1246 |
|
*/ |
1247 |
|
public void testHasQueuedThreadNPE() { |
1248 |
< |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1248 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1249 |
|
try { |
1250 |
< |
sync.hasQueuedThread(null); |
1250 |
> |
lock.hasQueuedThread(null); |
1251 |
|
shouldThrow(); |
1252 |
|
} catch (NullPointerException success) {} |
1253 |
|
} |
1262 |
|
assertFalse(lock.hasQueuedThread(t1)); |
1263 |
|
assertFalse(lock.hasQueuedThread(t2)); |
1264 |
|
lock.writeLock().lock(); |
1212 |
– |
long startTime = System.nanoTime(); |
1265 |
|
t1.start(); |
1266 |
|
waitForQueuedThread(lock, t1); |
1267 |
|
assertTrue(lock.hasQueuedThread(t1)); |
1289 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1290 |
|
assertEquals(0, lock.getQueueLength()); |
1291 |
|
lock.writeLock().lock(); |
1240 |
– |
long startTime = System.nanoTime(); |
1292 |
|
t1.start(); |
1293 |
|
waitForQueuedThread(lock, t1); |
1294 |
|
assertEquals(1, lock.getQueueLength()); |
1312 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
1313 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1314 |
|
lock.writeLock().lock(); |
1264 |
– |
long startTime = System.nanoTime(); |
1315 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
1316 |
|
t1.start(); |
1317 |
|
waitForQueuedThread(lock, t1); |
1366 |
|
} |
1367 |
|
|
1368 |
|
/** |
1369 |
< |
* hasWaiters throws IAE if not owned |
1369 |
> |
* hasWaiters throws IllegalArgumentException if not owned |
1370 |
|
*/ |
1371 |
|
public void testHasWaitersIAE() { |
1372 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1379 |
|
} |
1380 |
|
|
1381 |
|
/** |
1382 |
< |
* hasWaiters throws IMSE if not locked |
1382 |
> |
* hasWaiters throws IllegalMonitorStateException if not locked |
1383 |
|
*/ |
1384 |
|
public void testHasWaitersIMSE() { |
1385 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1391 |
|
} |
1392 |
|
|
1393 |
|
/** |
1394 |
< |
* getWaitQueueLength throws IAE if not owned |
1394 |
> |
* getWaitQueueLength throws IllegalArgumentException if not owned |
1395 |
|
*/ |
1396 |
|
public void testGetWaitQueueLengthIAE() { |
1397 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1404 |
|
} |
1405 |
|
|
1406 |
|
/** |
1407 |
< |
* getWaitQueueLength throws IMSE if not locked |
1407 |
> |
* getWaitQueueLength throws IllegalMonitorStateException if not locked |
1408 |
|
*/ |
1409 |
|
public void testGetWaitQueueLengthIMSE() { |
1410 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1416 |
|
} |
1417 |
|
|
1418 |
|
/** |
1419 |
< |
* getWaitingThreads throws IAE if not owned |
1419 |
> |
* getWaitingThreads throws IllegalArgumentException if not owned |
1420 |
|
*/ |
1421 |
|
public void testGetWaitingThreadsIAE() { |
1422 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1429 |
|
} |
1430 |
|
|
1431 |
|
/** |
1432 |
< |
* getWaitingThreads throws IMSE if not locked |
1432 |
> |
* getWaitingThreads throws IllegalMonitorStateException if not locked |
1433 |
|
*/ |
1434 |
|
public void testGetWaitingThreadsIMSE() { |
1435 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1450 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1451 |
|
public void realRun() throws InterruptedException { |
1452 |
|
lock.writeLock().lock(); |
1453 |
+ |
assertHasNoWaiters(lock, c); |
1454 |
|
assertFalse(lock.hasWaiters(c)); |
1455 |
|
locked.countDown(); |
1405 |
– |
assertEquals(0, lock.getWaitQueueLength(c)); |
1456 |
|
c.await(); |
1457 |
+ |
assertHasNoWaiters(lock, c); |
1458 |
+ |
assertFalse(lock.hasWaiters(c)); |
1459 |
|
lock.writeLock().unlock(); |
1460 |
|
}}); |
1461 |
|
|
1462 |
|
locked.await(); |
1463 |
|
lock.writeLock().lock(); |
1464 |
+ |
assertHasWaiters(lock, c, t); |
1465 |
|
assertTrue(lock.hasWaiters(c)); |
1413 |
– |
assertEquals(1, lock.getWaitQueueLength(c)); |
1466 |
|
c.signal(); |
1467 |
|
assertHasNoWaiters(lock, c); |
1468 |
+ |
assertFalse(lock.hasWaiters(c)); |
1469 |
|
lock.writeLock().unlock(); |
1470 |
|
awaitTermination(t); |
1471 |
|
assertHasNoWaiters(lock, c); |