1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import junit.framework.Test;
import junit.framework.TestSuite;
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom
public class ReentrantLockTest extends JSR166TestCase {
public static void main(String[] args) {
main(suite(), args);
}
public static Test suite() {
return new TestSuite(ReentrantLockTest.class);
}
/**
* A checked runnable calling lockInterruptibly
*/
class InterruptibleLockRunnable extends CheckedRunnable {
final ReentrantLock lock;
InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
public void realRun() throws InterruptedException {
lock.lockInterruptibly();
}
}
/**
* A checked runnable calling lockInterruptibly that expects to be
* interrupted
*/
class InterruptedLockRunnable extends CheckedInterruptedRunnable {
final ReentrantLock lock;
InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
public void realRun() throws InterruptedException {
lock.lockInterruptibly();
}
}
/**
* Subclass to expose protected methods
*/
static class PublicReentrantLock extends ReentrantLock {
PublicReentrantLock() { super(); }
PublicReentrantLock(boolean fair) { super(fair); }
public Thread getOwner() {
return super.getOwner();
}
public Collection<Thread> getQueuedThreads() {
return super.getQueuedThreads();
}
public Collection<Thread> getWaitingThreads(Condition c) {
return super.getWaitingThreads(c);
}
}
/**
* Releases write lock, checking that it had a hold count of 1.
*/
void releaseLock(PublicReentrantLock lock) {
assertLockedByMoi(lock);
lock.unlock();
assertFalse(lock.isHeldByCurrentThread());
assertNotLocked(lock);
}
/**
* Spin-waits until lock.hasQueuedThread(t) becomes true.
*/
void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
long startTime = System.nanoTime();
while (!lock.hasQueuedThread(t)) {
if (millisElapsedSince(startTime) > LONG_DELAY_MS)
throw new AssertionError("timed out");
Thread.yield();
}
assertTrue(t.isAlive());
assertNotSame(t, lock.getOwner());
}
/**
* Checks that lock is not locked.
*/
void assertNotLocked(PublicReentrantLock lock) {
assertFalse(lock.isLocked());
assertFalse(lock.isHeldByCurrentThread());
assertNull(lock.getOwner());
assertEquals(0, lock.getHoldCount());
}
/**
* Checks that lock is locked by the given thread.
*/
void assertLockedBy(PublicReentrantLock lock, Thread t) {
assertTrue(lock.isLocked());
assertSame(t, lock.getOwner());
assertEquals(t == Thread.currentThread(),
lock.isHeldByCurrentThread());
assertEquals(t == Thread.currentThread(),
lock.getHoldCount() > 0);
}
/**
* Checks that lock is locked by the current thread.
*/
void assertLockedByMoi(PublicReentrantLock lock) {
assertLockedBy(lock, Thread.currentThread());
}
/**
* Checks that condition c has no waiters.
*/
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
assertHasWaiters(lock, c, new Thread[] {});
}
/**
* Checks that condition c has exactly the given waiter threads.
*/
void assertHasWaiters(PublicReentrantLock lock, Condition c,
Thread... threads) {
lock.lock();
assertEquals(threads.length > 0, lock.hasWaiters(c));
assertEquals(threads.length, lock.getWaitQueueLength(c));
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
assertEquals(threads.length, lock.getWaitingThreads(c).size());
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
new HashSet<Thread>(Arrays.asList(threads)));
lock.unlock();
}
enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
static AwaitMethod randomAwaitMethod() {
AwaitMethod[] awaitMethods = AwaitMethod.values();
return awaitMethods[ThreadLocalRandom.current().nextInt(awaitMethods.length)];
}
/**
* Awaits condition "indefinitely" using the specified AwaitMethod.
*/
void await(Condition c, AwaitMethod awaitMethod)
throws InterruptedException {
long timeoutMillis = 2 * LONG_DELAY_MS;
switch (awaitMethod) {
case await:
c.await();
break;
case awaitTimed:
assertTrue(c.await(timeoutMillis, MILLISECONDS));
break;
case awaitNanos:
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
long nanosRemaining = c.awaitNanos(timeoutNanos);
assertTrue(nanosRemaining > timeoutNanos / 2);
assertTrue(nanosRemaining <= timeoutNanos);
break;
case awaitUntil:
assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
break;
default:
throw new AssertionError();
}
}
/**
* Constructor sets given fairness, and is in unlocked state
*/
public void testConstructor() {
PublicReentrantLock lock;
lock = new PublicReentrantLock();
assertFalse(lock.isFair());
assertNotLocked(lock);
lock = new PublicReentrantLock(true);
assertTrue(lock.isFair());
assertNotLocked(lock);
lock = new PublicReentrantLock(false);
assertFalse(lock.isFair());
assertNotLocked(lock);
}
/**
* locking an unlocked lock succeeds
*/
public void testLock() { testLock(false); }
public void testLock_fair() { testLock(true); }
public void testLock(boolean fair) {
PublicReentrantLock lock = new PublicReentrantLock(fair);
lock.lock();
assertLockedByMoi(lock);
releaseLock(lock);
}
/**
* Unlocking an unlocked lock throws IllegalMonitorStateException
*/
public void testUnlock_IMSE() { testUnlock_IMSE(false); }
public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
public void testUnlock_IMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
try {
lock.unlock();
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
/**
* tryLock on an unlocked lock succeeds
*/
public void testTryLock() { testTryLock(false); }
public void testTryLock_fair() { testTryLock(true); }
public void testTryLock(boolean fair) {
PublicReentrantLock lock = new PublicReentrantLock(fair);
assertTrue(lock.tryLock());
assertLockedByMoi(lock);
assertTrue(lock.tryLock());
assertLockedByMoi(lock);
lock.unlock();
releaseLock(lock);
}
/**
* hasQueuedThreads reports whether there are waiting threads
*/
public void testHasQueuedThreads() { testHasQueuedThreads(false); }
public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
public void testHasQueuedThreads(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
assertFalse(lock.hasQueuedThreads());
lock.lock();
assertFalse(lock.hasQueuedThreads());
t1.start();
waitForQueuedThread(lock, t1);
assertTrue(lock.hasQueuedThreads());
t2.start();
waitForQueuedThread(lock, t2);
assertTrue(lock.hasQueuedThreads());
t1.interrupt();
awaitTermination(t1);
assertTrue(lock.hasQueuedThreads());
lock.unlock();
awaitTermination(t2);
assertFalse(lock.hasQueuedThreads());
}
/**
* getQueueLength reports number of waiting threads
*/
public void testGetQueueLength() { testGetQueueLength(false); }
public void testGetQueueLength_fair() { testGetQueueLength(true); }
public void testGetQueueLength(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
assertEquals(0, lock.getQueueLength());
lock.lock();
t1.start();
waitForQueuedThread(lock, t1);
assertEquals(1, lock.getQueueLength());
t2.start();
waitForQueuedThread(lock, t2);
assertEquals(2, lock.getQueueLength());
t1.interrupt();
awaitTermination(t1);
assertEquals(1, lock.getQueueLength());
lock.unlock();
awaitTermination(t2);
assertEquals(0, lock.getQueueLength());
}
/**
* hasQueuedThread(null) throws NPE
*/
public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
public void testHasQueuedThreadNPE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
try {
lock.hasQueuedThread(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* hasQueuedThread reports whether a thread is queued
*/
public void testHasQueuedThread() { testHasQueuedThread(false); }
public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
public void testHasQueuedThread(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
assertFalse(lock.hasQueuedThread(t1));
assertFalse(lock.hasQueuedThread(t2));
lock.lock();
t1.start();
waitForQueuedThread(lock, t1);
assertTrue(lock.hasQueuedThread(t1));
assertFalse(lock.hasQueuedThread(t2));
t2.start();
waitForQueuedThread(lock, t2);
assertTrue(lock.hasQueuedThread(t1));
assertTrue(lock.hasQueuedThread(t2));
t1.interrupt();
awaitTermination(t1);
assertFalse(lock.hasQueuedThread(t1));
assertTrue(lock.hasQueuedThread(t2));
lock.unlock();
awaitTermination(t2);
assertFalse(lock.hasQueuedThread(t1));
assertFalse(lock.hasQueuedThread(t2));
}
/**
* getQueuedThreads includes waiting threads
*/
public void testGetQueuedThreads() { testGetQueuedThreads(false); }
public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
public void testGetQueuedThreads(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
Thread t1 = new Thread(new InterruptedLockRunnable(lock));
Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
assertTrue(lock.getQueuedThreads().isEmpty());
lock.lock();
assertTrue(lock.getQueuedThreads().isEmpty());
t1.start();
waitForQueuedThread(lock, t1);
assertEquals(1, lock.getQueuedThreads().size());
assertTrue(lock.getQueuedThreads().contains(t1));
t2.start();
waitForQueuedThread(lock, t2);
assertEquals(2, lock.getQueuedThreads().size());
assertTrue(lock.getQueuedThreads().contains(t1));
assertTrue(lock.getQueuedThreads().contains(t2));
t1.interrupt();
awaitTermination(t1);
assertFalse(lock.getQueuedThreads().contains(t1));
assertTrue(lock.getQueuedThreads().contains(t2));
assertEquals(1, lock.getQueuedThreads().size());
lock.unlock();
awaitTermination(t2);
assertTrue(lock.getQueuedThreads().isEmpty());
}
/**
* timed tryLock is interruptible
*/
public void testTryLock_Interruptible() { testTryLock_Interruptible(false); }
public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
public void testTryLock_Interruptible(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
lock.lock();
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
}});
waitForQueuedThread(lock, t);
t.interrupt();
awaitTermination(t);
releaseLock(lock);
}
/**
* tryLock on a locked lock fails
*/
public void testTryLockWhenLocked() { testTryLockWhenLocked(false); }
public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
public void testTryLockWhenLocked(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
lock.lock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertFalse(lock.tryLock());
}});
awaitTermination(t);
releaseLock(lock);
}
/**
* Timed tryLock on a locked lock times out
*/
public void testTryLock_Timeout() { testTryLock_Timeout(false); }
public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
public void testTryLock_Timeout(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final long timeoutMillis = timeoutMillis();
lock.lock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}});
awaitTermination(t);
releaseLock(lock);
}
/**
* getHoldCount returns number of recursive holds
*/
public void testGetHoldCount() { testGetHoldCount(false); }
public void testGetHoldCount_fair() { testGetHoldCount(true); }
public void testGetHoldCount(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
for (int i = 1; i <= SIZE; i++) {
lock.lock();
assertEquals(i, lock.getHoldCount());
}
for (int i = SIZE; i > 0; i--) {
lock.unlock();
assertEquals(i - 1, lock.getHoldCount());
}
}
/**
* isLocked is true when locked and false when not
*/
public void testIsLocked() { testIsLocked(false); }
public void testIsLocked_fair() { testIsLocked(true); }
public void testIsLocked(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
try {
assertFalse(lock.isLocked());
lock.lock();
assertTrue(lock.isLocked());
lock.lock();
assertTrue(lock.isLocked());
lock.unlock();
assertTrue(lock.isLocked());
lock.unlock();
assertFalse(lock.isLocked());
final CyclicBarrier barrier = new CyclicBarrier(2);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
lock.lock();
assertTrue(lock.isLocked());
barrier.await();
barrier.await();
lock.unlock();
}});
barrier.await();
assertTrue(lock.isLocked());
barrier.await();
awaitTermination(t);
assertFalse(lock.isLocked());
} catch (Exception fail) { threadUnexpectedException(fail); }
}
/**
* lockInterruptibly succeeds when unlocked, else is interruptible
*/
public void testLockInterruptibly() { testLockInterruptibly(false); }
public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
public void testLockInterruptibly(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
try {
lock.lockInterruptibly();
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
assertLockedByMoi(lock);
Thread t = newStartedThread(new InterruptedLockRunnable(lock));
waitForQueuedThread(lock, t);
t.interrupt();
assertTrue(lock.isLocked());
assertTrue(lock.isHeldByCurrentThread());
awaitTermination(t);
releaseLock(lock);
}
/**
* Calling await without holding lock throws IllegalMonitorStateException
*/
public void testAwait_IMSE() { testAwait_IMSE(false); }
public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
public void testAwait_IMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
for (AwaitMethod awaitMethod : AwaitMethod.values()) {
long startTime = System.nanoTime();
try {
await(c, awaitMethod);
shouldThrow();
} catch (IllegalMonitorStateException success) {
} catch (InterruptedException e) { threadUnexpectedException(e); }
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
/**
* Calling signal without holding lock throws IllegalMonitorStateException
*/
public void testSignal_IMSE() { testSignal_IMSE(false); }
public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
public void testSignal_IMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
try {
c.signal();
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
/**
* awaitNanos without a signal times out
*/
public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); }
public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
public void testAwaitNanos_Timeout(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
final long timeoutMillis = timeoutMillis();
final long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
lock.lock();
final long startTime = System.nanoTime();
try {
long nanosRemaining = c.awaitNanos(timeoutNanos);
assertTrue(nanosRemaining <= 0);
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
lock.unlock();
}
/**
* timed await without a signal times out
*/
public void testAwait_Timeout() { testAwait_Timeout(false); }
public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
public void testAwait_Timeout(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
final long timeoutMillis = timeoutMillis();
lock.lock();
final long startTime = System.nanoTime();
try {
assertFalse(c.await(timeoutMillis, MILLISECONDS));
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
lock.unlock();
}
/**
* awaitUntil without a signal times out
*/
public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
public void testAwaitUntil_Timeout(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
lock.lock();
// We shouldn't assume that nanoTime and currentTimeMillis
// use the same time source, so don't use nanoTime here.
final java.util.Date delayedDate = delayedDate(timeoutMillis());
try {
assertFalse(c.awaitUntil(delayedDate));
} catch (InterruptedException fail) { threadUnexpectedException(fail); }
assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
lock.unlock();
}
/**
* await returns when signalled
*/
public void testAwait() { testAwait(false); }
public void testAwait_fair() { testAwait(true); }
public void testAwait(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
locked.countDown();
c.await();
lock.unlock();
}});
await(locked);
lock.lock();
assertHasWaiters(lock, c, t);
c.signal();
assertHasNoWaiters(lock, c);
assertTrue(t.isAlive());
lock.unlock();
awaitTermination(t);
}
/**
* hasWaiters throws NPE if null
*/
public void testHasWaitersNPE() { testHasWaitersNPE(false); }
public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
public void testHasWaitersNPE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
try {
lock.hasWaiters(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* getWaitQueueLength throws NPE if null
*/
public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
public void testGetWaitQueueLengthNPE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
try {
lock.getWaitQueueLength(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* getWaitingThreads throws NPE if null
*/
public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
public void testGetWaitingThreadsNPE(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
try {
lock.getWaitingThreads(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* hasWaiters throws IllegalArgumentException if not owned
*/
public void testHasWaitersIAE() { testHasWaitersIAE(false); }
public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
public void testHasWaitersIAE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
final ReentrantLock lock2 = new ReentrantLock(fair);
try {
lock2.hasWaiters(c);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* hasWaiters throws IllegalMonitorStateException if not locked
*/
public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
public void testHasWaitersIMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
try {
lock.hasWaiters(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
/**
* getWaitQueueLength throws IllegalArgumentException if not owned
*/
public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
public void testGetWaitQueueLengthIAE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
final ReentrantLock lock2 = new ReentrantLock(fair);
try {
lock2.getWaitQueueLength(c);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* getWaitQueueLength throws IllegalMonitorStateException if not locked
*/
public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
public void testGetWaitQueueLengthIMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
try {
lock.getWaitQueueLength(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
/**
* getWaitingThreads throws IllegalArgumentException if not owned
*/
public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
public void testGetWaitingThreadsIAE(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
try {
lock2.getWaitingThreads(c);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* getWaitingThreads throws IllegalMonitorStateException if not locked
*/
public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
public void testGetWaitingThreadsIMSE(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
try {
lock.getWaitingThreads(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
/**
* hasWaiters returns true when a thread is waiting, else false
*/
public void testHasWaiters() { testHasWaiters(false); }
public void testHasWaiters_fair() { testHasWaiters(true); }
public void testHasWaiters(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseSignal = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertHasNoWaiters(lock, c);
assertFalse(lock.hasWaiters(c));
pleaseSignal.countDown();
c.await();
assertHasNoWaiters(lock, c);
assertFalse(lock.hasWaiters(c));
lock.unlock();
}});
await(pleaseSignal);
lock.lock();
assertHasWaiters(lock, c, t);
assertTrue(lock.hasWaiters(c));
c.signal();
assertHasNoWaiters(lock, c);
assertFalse(lock.hasWaiters(c));
lock.unlock();
awaitTermination(t);
assertHasNoWaiters(lock, c);
}
/**
* getWaitQueueLength returns number of waiting threads
*/
public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
public void testGetWaitQueueLength(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.hasWaiters(c));
assertEquals(0, lock.getWaitQueueLength(c));
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.hasWaiters(c));
assertEquals(1, lock.getWaitQueueLength(c));
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertEquals(0, lock.getWaitQueueLength(c));
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertEquals(1, lock.getWaitQueueLength(c));
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertEquals(2, lock.getWaitQueueLength(c));
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
/**
* getWaitingThreads returns only and all waiting threads
*/
public void testGetWaitingThreads() { testGetWaitingThreads(false); }
public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
public void testGetWaitingThreads(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.getWaitingThreads(c).isEmpty());
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertFalse(lock.getWaitingThreads(c).contains(t2));
assertEquals(1, lock.getWaitingThreads(c).size());
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertTrue(lock.getWaitingThreads(c).contains(t2));
assertEquals(2, lock.getWaitingThreads(c).size());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
/**
* awaitUninterruptibly is uninterruptible
*/
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
public void testAwaitUninterruptibly(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition condition = lock.newCondition();
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt before awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
Thread.currentThread().interrupt();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt during awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
await(pleaseInterrupt);
t2.interrupt();
lock.lock();
lock.unlock();
assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.WAITING);
lock.lock();
condition.signalAll();
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
/**
* await/awaitNanos/awaitUntil is interruptible
*/
public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
final PublicReentrantLock lock =
new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertLockedByMoi(lock);
assertHasNoWaiters(lock, c);
pleaseInterrupt.countDown();
try {
await(c, awaitMethod);
} finally {
assertLockedByMoi(lock);
assertHasNoWaiters(lock, c);
lock.unlock();
assertFalse(Thread.interrupted());
}
}});
await(pleaseInterrupt);
assertHasWaiters(lock, c, t);
t.interrupt();
awaitTermination(t);
assertNotLocked(lock);
}
/**
* signalAll wakes up all threads
*/
public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseSignal = new CountDownLatch(2);
class Awaiter extends CheckedRunnable {
public void realRun() throws InterruptedException {
lock.lock();
pleaseSignal.countDown();
await(c, awaitMethod);
lock.unlock();
}
}
Thread t1 = newStartedThread(new Awaiter());
Thread t2 = newStartedThread(new Awaiter());
await(pleaseSignal);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
/**
* signal wakes up waiting threads in FIFO order
*/
public void testSignalWakesFifo() { testSignalWakesFifo(false); }
public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
public void testSignalWakesFifo(boolean fair) {
final PublicReentrantLock lock =
new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
locked1.countDown();
c.await();
lock.unlock();
}});
await(locked1);
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
locked2.countDown();
c.await();
lock.unlock();
}});
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertFalse(lock.hasQueuedThreads());
c.signal();
assertHasWaiters(lock, c, t2);
assertTrue(lock.hasQueuedThread(t1));
assertFalse(lock.hasQueuedThread(t2));
c.signal();
assertHasNoWaiters(lock, c);
assertTrue(lock.hasQueuedThread(t1));
assertTrue(lock.hasQueuedThread(t2));
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
/**
* await after multiple reentrant locking preserves lock count
*/
public void testAwaitLockCount() { testAwaitLockCount(false); }
public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
public void testAwaitLockCount(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseSignal = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertLockedByMoi(lock);
assertEquals(1, lock.getHoldCount());
pleaseSignal.countDown();
c.await();
assertLockedByMoi(lock);
assertEquals(1, lock.getHoldCount());
lock.unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
lock.lock();
assertLockedByMoi(lock);
assertEquals(2, lock.getHoldCount());
pleaseSignal.countDown();
c.await();
assertLockedByMoi(lock);
assertEquals(2, lock.getHoldCount());
lock.unlock();
lock.unlock();
}});
await(pleaseSignal);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertEquals(1, lock.getHoldCount());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
/**
* A serialized lock deserializes as unlocked
*/
public void testSerialization() { testSerialization(false); }
public void testSerialization_fair() { testSerialization(true); }
public void testSerialization(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
lock.lock();
ReentrantLock clone = serialClone(lock);
assertEquals(lock.isFair(), clone.isFair());
assertTrue(lock.isLocked());
assertFalse(clone.isLocked());
assertEquals(1, lock.getHoldCount());
assertEquals(0, clone.getHoldCount());
clone.lock();
clone.lock();
assertTrue(clone.isLocked());
assertEquals(2, clone.getHoldCount());
assertEquals(1, lock.getHoldCount());
clone.unlock();
clone.unlock();
assertTrue(lock.isLocked());
assertFalse(clone.isLocked());
}
/**
* toString indicates current lock state
*/
public void testToString() { testToString(false); }
public void testToString_fair() { testToString(true); }
public void testToString(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
assertTrue(lock.toString().contains("Unlocked"));
lock.lock();
assertTrue(lock.toString().contains("Locked by"));
lock.unlock();
assertTrue(lock.toString().contains("Unlocked"));
}
/**
* Tests scenario for JDK-8187408
* AbstractQueuedSynchronizer wait queue corrupted when thread awaits without holding the lock
*/
public void testBug8187408() throws InterruptedException {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final AwaitMethod awaitMethod = randomAwaitMethod();
final int nThreads = rnd.nextInt(2, 10);
final ReentrantLock lock = new ReentrantLock();
final Condition cond = lock.newCondition();
final CountDownLatch done = new CountDownLatch(nThreads);
final ArrayList<Thread> threads = new ArrayList<>();
Runnable rogue = () -> {
while (done.getCount() > 0) {
try {
// call await without holding lock?!
await(cond, awaitMethod);
throw new AssertionError("should throw");
}
catch (IllegalMonitorStateException expected) {}
catch (Throwable fail) { threadUnexpectedException(fail); }}};
Thread rogueThread = new Thread(rogue, "rogue");
threads.add(rogueThread);
rogueThread.start();
Runnable waiter = () -> {
lock.lock();
try {
done.countDown();
cond.await();
} catch (Throwable fail) {
threadUnexpectedException(fail);
} finally {
lock.unlock();
}};
for (int i = 0; i < nThreads; i++) {
Thread thread = new Thread(waiter, "waiter");
threads.add(thread);
thread.start();
}
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
lock.lock();
try {
assertEquals(nThreads, lock.getWaitQueueLength(cond));
} finally {
cond.signalAll();
lock.unlock();
}
for (Thread thread : threads) {
thread.join(LONG_DELAY_MS);
assertFalse(thread.isAlive());
}
}
}
|