8 |
|
import static java.util.concurrent.TimeUnit.DAYS; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import static java.util.concurrent.locks.StampedLock.isLockStamp; |
12 |
+ |
import static java.util.concurrent.locks.StampedLock.isOptimisticReadStamp; |
13 |
+ |
import static java.util.concurrent.locks.StampedLock.isReadLockStamp; |
14 |
+ |
import static java.util.concurrent.locks.StampedLock.isWriteLockStamp; |
15 |
+ |
|
16 |
|
import java.util.ArrayList; |
17 |
|
import java.util.List; |
18 |
|
import java.util.concurrent.CountDownLatch; |
587 |
|
long s = lock.readLock(); |
588 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
589 |
|
public void realRun() { |
590 |
< |
threadAssertEquals(0L, lock.tryWriteLock()); |
590 |
> |
assertEquals(0L, lock.tryWriteLock()); |
591 |
|
}}); |
592 |
|
|
593 |
|
awaitTermination(t); |
982 |
|
* IllegalMonitorStateException |
983 |
|
*/ |
984 |
|
public void testCannotUnlockOptimisticReadStamps() { |
985 |
< |
Runnable[] actions = { |
986 |
< |
() -> { |
987 |
< |
StampedLock sl = new StampedLock(); |
988 |
< |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
989 |
< |
sl.unlockRead(stamp); |
990 |
< |
}, |
991 |
< |
() -> { |
992 |
< |
StampedLock sl = new StampedLock(); |
993 |
< |
long stamp = sl.tryOptimisticRead(); |
994 |
< |
sl.unlock(stamp); |
995 |
< |
}, |
985 |
> |
{ |
986 |
> |
StampedLock sl = new StampedLock(); |
987 |
> |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
988 |
> |
assertThrows(IllegalMonitorStateException.class, |
989 |
> |
() -> sl.unlockRead(stamp)); |
990 |
> |
} |
991 |
> |
{ |
992 |
> |
StampedLock sl = new StampedLock(); |
993 |
> |
long stamp = sl.tryOptimisticRead(); |
994 |
> |
assertThrows(IllegalMonitorStateException.class, |
995 |
> |
() -> sl.unlock(stamp)); |
996 |
> |
} |
997 |
|
|
998 |
< |
() -> { |
999 |
< |
StampedLock sl = new StampedLock(); |
1000 |
< |
long stamp = sl.tryOptimisticRead(); |
1001 |
< |
sl.writeLock(); |
1002 |
< |
sl.unlock(stamp); |
1003 |
< |
}, |
1004 |
< |
() -> { |
1005 |
< |
StampedLock sl = new StampedLock(); |
1006 |
< |
sl.readLock(); |
1007 |
< |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
1008 |
< |
sl.unlockRead(stamp); |
1009 |
< |
}, |
1010 |
< |
() -> { |
1011 |
< |
StampedLock sl = new StampedLock(); |
1012 |
< |
sl.readLock(); |
1013 |
< |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
1014 |
< |
sl.unlock(stamp); |
1015 |
< |
}, |
1016 |
< |
|
1017 |
< |
() -> { |
1018 |
< |
StampedLock sl = new StampedLock(); |
1013 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1014 |
< |
assertValid(sl, stamp); |
1015 |
< |
sl.writeLock(); |
1016 |
< |
sl.unlockWrite(stamp); |
1017 |
< |
}, |
1018 |
< |
() -> { |
1019 |
< |
StampedLock sl = new StampedLock(); |
1020 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1021 |
< |
sl.writeLock(); |
1022 |
< |
sl.unlock(stamp); |
1023 |
< |
}, |
1024 |
< |
() -> { |
1025 |
< |
StampedLock sl = new StampedLock(); |
1026 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1027 |
< |
sl.readLock(); |
1028 |
< |
sl.unlockRead(stamp); |
1029 |
< |
}, |
1030 |
< |
() -> { |
1031 |
< |
StampedLock sl = new StampedLock(); |
1032 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1033 |
< |
sl.readLock(); |
1034 |
< |
sl.unlock(stamp); |
1035 |
< |
}, |
1036 |
< |
|
1037 |
< |
() -> { |
1038 |
< |
StampedLock sl = new StampedLock(); |
1039 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1040 |
< |
assertValid(sl, stamp); |
1041 |
< |
sl.writeLock(); |
1042 |
< |
sl.unlockWrite(stamp); |
1043 |
< |
}, |
1044 |
< |
() -> { |
1045 |
< |
StampedLock sl = new StampedLock(); |
1046 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1047 |
< |
sl.writeLock(); |
1048 |
< |
sl.unlock(stamp); |
1049 |
< |
}, |
1050 |
< |
() -> { |
1051 |
< |
StampedLock sl = new StampedLock(); |
1052 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1053 |
< |
sl.readLock(); |
1054 |
< |
sl.unlockRead(stamp); |
1055 |
< |
}, |
1056 |
< |
() -> { |
1057 |
< |
StampedLock sl = new StampedLock(); |
1058 |
< |
sl.readLock(); |
1059 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1060 |
< |
assertValid(sl, stamp); |
1061 |
< |
sl.readLock(); |
1062 |
< |
sl.unlockRead(stamp); |
1063 |
< |
}, |
1064 |
< |
() -> { |
1065 |
< |
StampedLock sl = new StampedLock(); |
1066 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1067 |
< |
sl.readLock(); |
1068 |
< |
sl.unlock(stamp); |
1069 |
< |
}, |
1070 |
< |
() -> { |
1071 |
< |
StampedLock sl = new StampedLock(); |
1072 |
< |
sl.readLock(); |
1073 |
< |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1074 |
< |
sl.readLock(); |
1075 |
< |
sl.unlock(stamp); |
1076 |
< |
}, |
1077 |
< |
}; |
998 |
> |
{ |
999 |
> |
StampedLock sl = new StampedLock(); |
1000 |
> |
long stamp = sl.tryOptimisticRead(); |
1001 |
> |
sl.writeLock(); |
1002 |
> |
assertThrows(IllegalMonitorStateException.class, |
1003 |
> |
() -> sl.unlock(stamp)); |
1004 |
> |
} |
1005 |
> |
{ |
1006 |
> |
StampedLock sl = new StampedLock(); |
1007 |
> |
sl.readLock(); |
1008 |
> |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
1009 |
> |
assertThrows(IllegalMonitorStateException.class, |
1010 |
> |
() -> sl.unlockRead(stamp)); |
1011 |
> |
} |
1012 |
> |
{ |
1013 |
> |
StampedLock sl = new StampedLock(); |
1014 |
> |
sl.readLock(); |
1015 |
> |
long stamp = assertValid(sl, sl.tryOptimisticRead()); |
1016 |
> |
assertThrows(IllegalMonitorStateException.class, |
1017 |
> |
() -> sl.unlock(stamp)); |
1018 |
> |
} |
1019 |
|
|
1020 |
< |
assertThrows(IllegalMonitorStateException.class, actions); |
1020 |
> |
{ |
1021 |
> |
StampedLock sl = new StampedLock(); |
1022 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1023 |
> |
assertValid(sl, stamp); |
1024 |
> |
sl.writeLock(); |
1025 |
> |
assertThrows(IllegalMonitorStateException.class, |
1026 |
> |
() -> sl.unlockWrite(stamp)); |
1027 |
> |
} |
1028 |
> |
{ |
1029 |
> |
StampedLock sl = new StampedLock(); |
1030 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1031 |
> |
sl.writeLock(); |
1032 |
> |
assertThrows(IllegalMonitorStateException.class, |
1033 |
> |
() -> sl.unlock(stamp)); |
1034 |
> |
} |
1035 |
> |
{ |
1036 |
> |
StampedLock sl = new StampedLock(); |
1037 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1038 |
> |
sl.readLock(); |
1039 |
> |
assertThrows(IllegalMonitorStateException.class, |
1040 |
> |
() -> sl.unlockRead(stamp)); |
1041 |
> |
} |
1042 |
> |
{ |
1043 |
> |
StampedLock sl = new StampedLock(); |
1044 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.writeLock()); |
1045 |
> |
sl.readLock(); |
1046 |
> |
assertThrows(IllegalMonitorStateException.class, |
1047 |
> |
() -> sl.unlock(stamp)); |
1048 |
> |
} |
1049 |
> |
|
1050 |
> |
{ |
1051 |
> |
StampedLock sl = new StampedLock(); |
1052 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1053 |
> |
assertValid(sl, stamp); |
1054 |
> |
sl.writeLock(); |
1055 |
> |
assertThrows(IllegalMonitorStateException.class, |
1056 |
> |
() -> sl.unlockWrite(stamp)); |
1057 |
> |
} |
1058 |
> |
{ |
1059 |
> |
StampedLock sl = new StampedLock(); |
1060 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1061 |
> |
sl.writeLock(); |
1062 |
> |
assertThrows(IllegalMonitorStateException.class, |
1063 |
> |
() -> sl.unlock(stamp)); |
1064 |
> |
} |
1065 |
> |
{ |
1066 |
> |
StampedLock sl = new StampedLock(); |
1067 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1068 |
> |
sl.readLock(); |
1069 |
> |
assertThrows(IllegalMonitorStateException.class, |
1070 |
> |
() -> sl.unlockRead(stamp)); |
1071 |
> |
} |
1072 |
> |
{ |
1073 |
> |
StampedLock sl = new StampedLock(); |
1074 |
> |
sl.readLock(); |
1075 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1076 |
> |
assertValid(sl, stamp); |
1077 |
> |
sl.readLock(); |
1078 |
> |
assertThrows(IllegalMonitorStateException.class, |
1079 |
> |
() -> sl.unlockRead(stamp)); |
1080 |
> |
} |
1081 |
> |
{ |
1082 |
> |
StampedLock sl = new StampedLock(); |
1083 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1084 |
> |
sl.readLock(); |
1085 |
> |
assertThrows(IllegalMonitorStateException.class, |
1086 |
> |
() -> sl.unlock(stamp)); |
1087 |
> |
} |
1088 |
> |
{ |
1089 |
> |
StampedLock sl = new StampedLock(); |
1090 |
> |
sl.readLock(); |
1091 |
> |
long stamp = sl.tryConvertToOptimisticRead(sl.readLock()); |
1092 |
> |
sl.readLock(); |
1093 |
> |
assertThrows(IllegalMonitorStateException.class, |
1094 |
> |
() -> sl.unlock(stamp)); |
1095 |
> |
} |
1096 |
|
} |
1097 |
|
|
1098 |
|
static long writeLockInterruptiblyUninterrupted(StampedLock sl) { |
1275 |
|
} while (stamp == 0); |
1276 |
|
return Math.hypot(currentX, currentY); |
1277 |
|
} |
1278 |
+ |
|
1279 |
+ |
double distanceFromOrigin2() { |
1280 |
+ |
long stamp = sl.tryOptimisticRead(); |
1281 |
+ |
try { |
1282 |
+ |
retryHoldingLock: |
1283 |
+ |
for (;; stamp = sl.readLock()) { |
1284 |
+ |
if (stamp == 0L) |
1285 |
+ |
continue retryHoldingLock; |
1286 |
+ |
// possibly racy reads |
1287 |
+ |
double currentX = x; |
1288 |
+ |
double currentY = y; |
1289 |
+ |
if (!sl.validate(stamp)) |
1290 |
+ |
continue retryHoldingLock; |
1291 |
+ |
return Math.hypot(currentX, currentY); |
1292 |
+ |
} |
1293 |
+ |
} finally { |
1294 |
+ |
if (StampedLock.isReadLockStamp(stamp)) |
1295 |
+ |
sl.unlockRead(stamp); |
1296 |
+ |
} |
1297 |
+ |
} |
1298 |
+ |
|
1299 |
+ |
void moveIfAtOrigin(double newX, double newY) { |
1300 |
+ |
long stamp = sl.readLock(); |
1301 |
+ |
try { |
1302 |
+ |
while (x == 0.0 && y == 0.0) { |
1303 |
+ |
long ws = sl.tryConvertToWriteLock(stamp); |
1304 |
+ |
if (ws != 0L) { |
1305 |
+ |
stamp = ws; |
1306 |
+ |
x = newX; |
1307 |
+ |
y = newY; |
1308 |
+ |
return; |
1309 |
+ |
} |
1310 |
+ |
else { |
1311 |
+ |
sl.unlockRead(stamp); |
1312 |
+ |
stamp = sl.writeLock(); |
1313 |
+ |
} |
1314 |
+ |
} |
1315 |
+ |
} finally { |
1316 |
+ |
sl.unlock(stamp); |
1317 |
+ |
} |
1318 |
+ |
} |
1319 |
|
} |
1320 |
|
|
1321 |
|
Point p = new Point(); |
1322 |
|
p.move(3.0, 4.0); |
1323 |
|
assertEquals(5.0, p.distanceFromOrigin()); |
1324 |
+ |
p.moveIfAtOrigin(5.0, 12.0); |
1325 |
+ |
assertEquals(5.0, p.distanceFromOrigin2()); |
1326 |
+ |
} |
1327 |
+ |
|
1328 |
+ |
/** |
1329 |
+ |
* Stamp inspection methods work as expected, and do not inspect |
1330 |
+ |
* the state of the lock itself. |
1331 |
+ |
*/ |
1332 |
+ |
public void testStampStateInspectionMethods() { |
1333 |
+ |
StampedLock lock = new StampedLock(); |
1334 |
+ |
|
1335 |
+ |
assertFalse(isWriteLockStamp(0L)); |
1336 |
+ |
assertFalse(isReadLockStamp(0L)); |
1337 |
+ |
assertFalse(isLockStamp(0L)); |
1338 |
+ |
assertFalse(isOptimisticReadStamp(0L)); |
1339 |
+ |
|
1340 |
+ |
{ |
1341 |
+ |
long stamp = lock.writeLock(); |
1342 |
+ |
for (int i = 0; i < 2; i++) { |
1343 |
+ |
assertTrue(isWriteLockStamp(stamp)); |
1344 |
+ |
assertFalse(isReadLockStamp(stamp)); |
1345 |
+ |
assertTrue(isLockStamp(stamp)); |
1346 |
+ |
assertFalse(isOptimisticReadStamp(stamp)); |
1347 |
+ |
if (i == 0) |
1348 |
+ |
lock.unlockWrite(stamp); |
1349 |
+ |
} |
1350 |
+ |
} |
1351 |
+ |
|
1352 |
+ |
{ |
1353 |
+ |
long stamp = lock.readLock(); |
1354 |
+ |
for (int i = 0; i < 2; i++) { |
1355 |
+ |
assertFalse(isWriteLockStamp(stamp)); |
1356 |
+ |
assertTrue(isReadLockStamp(stamp)); |
1357 |
+ |
assertTrue(isLockStamp(stamp)); |
1358 |
+ |
assertFalse(isOptimisticReadStamp(stamp)); |
1359 |
+ |
if (i == 0) |
1360 |
+ |
lock.unlockRead(stamp); |
1361 |
+ |
} |
1362 |
+ |
} |
1363 |
+ |
|
1364 |
+ |
{ |
1365 |
+ |
long optimisticStamp = lock.tryOptimisticRead(); |
1366 |
+ |
long readStamp = lock.tryConvertToReadLock(optimisticStamp); |
1367 |
+ |
long writeStamp = lock.tryConvertToWriteLock(readStamp); |
1368 |
+ |
for (int i = 0; i < 2; i++) { |
1369 |
+ |
assertFalse(isWriteLockStamp(optimisticStamp)); |
1370 |
+ |
assertFalse(isReadLockStamp(optimisticStamp)); |
1371 |
+ |
assertFalse(isLockStamp(optimisticStamp)); |
1372 |
+ |
assertTrue(isOptimisticReadStamp(optimisticStamp)); |
1373 |
+ |
|
1374 |
+ |
assertFalse(isWriteLockStamp(readStamp)); |
1375 |
+ |
assertTrue(isReadLockStamp(readStamp)); |
1376 |
+ |
assertTrue(isLockStamp(readStamp)); |
1377 |
+ |
assertFalse(isOptimisticReadStamp(readStamp)); |
1378 |
+ |
|
1379 |
+ |
assertTrue(isWriteLockStamp(writeStamp)); |
1380 |
+ |
assertFalse(isReadLockStamp(writeStamp)); |
1381 |
+ |
assertTrue(isLockStamp(writeStamp)); |
1382 |
+ |
assertFalse(isOptimisticReadStamp(writeStamp)); |
1383 |
+ |
if (i == 0) |
1384 |
+ |
lock.unlockWrite(writeStamp); |
1385 |
+ |
} |
1386 |
+ |
} |
1387 |
|
} |
1388 |
|
|
1389 |
|
} |