1 7 8 package java.util.concurrent.locks; 9 import java.util.*; 10 import java.util.concurrent.*; 11 import java.util.concurrent.atomic.*; 12 import sun.misc.Unsafe; 13 14 31 public abstract class AbstractQueuedLongSynchronizer 32 extends AbstractOwnableSynchronizer 33 implements java.io.Serializable { 34 35 private static final long serialVersionUID = 7373984972572414692L; 36 37 43 44 48 protected AbstractQueuedLongSynchronizer() { } 49 50 128 static final class Node { 129 130 static final int CANCELLED = 1; 131 132 static final int SIGNAL = -1; 133 134 static final int CONDITION = -2; 135 136 static final Node SHARED = new Node(); 137 138 static final Node EXCLUSIVE = null; 139 140 168 volatile int waitStatus; 169 170 181 volatile Node prev; 182 183 195 volatile Node next; 196 197 201 volatile Thread thread; 202 203 213 Node nextWaiter; 214 215 218 final boolean isShared() { 219 return nextWaiter == SHARED; 220 } 221 222 227 final Node predecessor() throws NullPointerException { 228 Node p = prev; 229 if (p == null) 230 throw new NullPointerException (); 231 else 232 return p; 233 } 234 235 Node() { } 237 238 Node(Thread thread, Node mode) { this.nextWaiter = mode; 240 this.thread = thread; 241 } 242 243 Node(Thread thread, int waitStatus) { this.waitStatus = waitStatus; 245 this.thread = thread; 246 } 247 } 248 249 255 private transient volatile Node head; 256 257 261 private transient volatile Node tail; 262 263 266 private volatile long state; 267 268 273 protected final long getState() { 274 return state; 275 } 276 277 282 protected final void setState(long newState) { 283 state = newState; 284 } 285 286 297 protected final boolean compareAndSetState(long expect, long update) { 298 return unsafe.compareAndSwapLong(this, stateOffset, expect, update); 300 } 301 302 304 309 static final long spinForTimeoutThreshold = 1000L; 310 311 316 private Node enq(final Node node) { 317 for (;;) { 318 Node t = tail; 319 if (t == null) { Node h = new Node(); h.next = node; 322 node.prev = h; 323 if (compareAndSetHead(h)) { 324 tail = node; 325 return h; 326 } 327 } 328 else { 329 node.prev = t; 330 if (compareAndSetTail(t, node)) { 331 t.next = node; 332 return t; 333 } 334 } 335 } 336 } 337 338 345 private Node addWaiter(Node mode) { 346 Node node = new Node(Thread.currentThread(), mode); 347 Node pred = tail; 349 if (pred != null) { 350 node.prev = pred; 351 if (compareAndSetTail(pred, node)) { 352 pred.next = node; 353 return node; 354 } 355 } 356 enq(node); 357 return node; 358 } 359 360 367 private void setHead(Node node) { 368 head = node; 369 node.thread = null; 370 node.prev = null; 371 } 372 373 378 private void unparkSuccessor(Node node) { 379 383 compareAndSetWaitStatus(node, Node.SIGNAL, 0); 384 385 391 Node s = node.next; 392 if (s == null || s.waitStatus > 0) { 393 s = null; 394 for (Node t = tail; t != null && t != node; t = t.prev) 395 if (t.waitStatus <= 0) 396 s = t; 397 } 398 if (s != null) 399 LockSupport.unpark(s.thread); 400 } 401 402 410 private void setHeadAndPropagate(Node node, long propagate) { 411 setHead(node); 412 if (propagate > 0 && node.waitStatus != 0) { 413 417 Node s = node.next; 418 if (s == null || s.isShared()) 419 unparkSuccessor(node); 420 } 421 } 422 423 425 430 private void cancelAcquire(Node node) { 431 if (node == null) 433 return; 434 435 node.thread = null; 436 437 Node pred = node.prev; 439 while (pred.waitStatus > 0) 440 node.prev = pred = pred.prev; 441 442 Node predNext = pred.next; 444 445 node.waitStatus = Node.CANCELLED; 447 448 if (node == tail && compareAndSetTail(node, pred)) { 450 compareAndSetNext(pred, predNext, null); 451 } else { 452 if (pred != head 454 && (pred.waitStatus == Node.SIGNAL 455 || compareAndSetWaitStatus(pred, 0, Node.SIGNAL)) 456 && pred.thread != null) { 457 458 Node next = node.next; 460 if (next != null && next.waitStatus <= 0) 461 compareAndSetNext(pred, predNext, next); 462 } else { 463 unparkSuccessor(node); 464 } 465 466 node.next = node; } 468 } 469 470 479 private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { 480 int s = pred.waitStatus; 481 if (s < 0) 482 486 return true; 487 if (s > 0) { 488 492 do { 493 node.prev = pred = pred.prev; 494 } while (pred.waitStatus > 0); 495 pred.next = node; 496 } 497 else 498 503 compareAndSetWaitStatus(pred, 0, Node.SIGNAL); 504 return false; 505 } 506 507 510 private static void selfInterrupt() { 511 Thread.currentThread().interrupt(); 512 } 513 514 519 private final boolean parkAndCheckInterrupt() { 520 LockSupport.park(this); 521 return Thread.interrupted(); 522 } 523 524 532 533 541 final boolean acquireQueued(final Node node, long arg) { 542 try { 543 boolean interrupted = false; 544 for (;;) { 545 final Node p = node.predecessor(); 546 if (p == head && tryAcquire(arg)) { 547 setHead(node); 548 p.next = null; return interrupted; 550 } 551 if (shouldParkAfterFailedAcquire(p, node) && 552 parkAndCheckInterrupt()) 553 interrupted = true; 554 } 555 } catch (RuntimeException ex) { 556 cancelAcquire(node); 557 throw ex; 558 } 559 } 560 561 565 private void doAcquireInterruptibly(long arg) 566 throws InterruptedException { 567 final Node node = addWaiter(Node.EXCLUSIVE); 568 try { 569 for (;;) { 570 final Node p = node.predecessor(); 571 if (p == head && tryAcquire(arg)) { 572 setHead(node); 573 p.next = null; return; 575 } 576 if (shouldParkAfterFailedAcquire(p, node) && 577 parkAndCheckInterrupt()) 578 break; 579 } 580 } catch (RuntimeException ex) { 581 cancelAcquire(node); 582 throw ex; 583 } 584 cancelAcquire(node); 586 throw new InterruptedException (); 587 } 588 589 596 private boolean doAcquireNanos(long arg, long nanosTimeout) 597 throws InterruptedException { 598 long lastTime = System.nanoTime(); 599 final Node node = addWaiter(Node.EXCLUSIVE); 600 try { 601 for (;;) { 602 final Node p = node.predecessor(); 603 if (p == head && tryAcquire(arg)) { 604 setHead(node); 605 p.next = null; return true; 607 } 608 if (nanosTimeout <= 0) { 609 cancelAcquire(node); 610 return false; 611 } 612 if (nanosTimeout > spinForTimeoutThreshold && 613 shouldParkAfterFailedAcquire(p, node)) 614 LockSupport.parkNanos(this, nanosTimeout); 615 long now = System.nanoTime(); 616 nanosTimeout -= now - lastTime; 617 lastTime = now; 618 if (Thread.interrupted()) 619 break; 620 } 621 } catch (RuntimeException ex) { 622 cancelAcquire(node); 623 throw ex; 624 } 625 cancelAcquire(node); 627 throw new InterruptedException (); 628 } 629 630 634 private void doAcquireShared(long arg) { 635 final Node node = addWaiter(Node.SHARED); 636 try { 637 boolean interrupted = false; 638 for (;;) { 639 final Node p = node.predecessor(); 640 if (p == head) { 641 long r = tryAcquireShared(arg); 642 if (r >= 0) { 643 setHeadAndPropagate(node, r); 644 p.next = null; if (interrupted) 646 selfInterrupt(); 647 return; 648 } 649 } 650 if (shouldParkAfterFailedAcquire(p, node) && 651 parkAndCheckInterrupt()) 652 interrupted = true; 653 } 654 } catch (RuntimeException ex) { 655 cancelAcquire(node); 656 throw ex; 657 } 658 } 659 660 664 private void doAcquireSharedInterruptibly(long arg) 665 throws InterruptedException { 666 final Node node = addWaiter(Node.SHARED); 667 try { 668 for (;;) { 669 final Node p = node.predecessor(); 670 if (p == head) { 671 long r = tryAcquireShared(arg); 672 if (r >= 0) { 673 setHeadAndPropagate(node, r); 674 p.next = null; return; 676 } 677 } 678 if (shouldParkAfterFailedAcquire(p, node) && 679 parkAndCheckInterrupt()) 680 break; 681 } 682 } catch (RuntimeException ex) { 683 cancelAcquire(node); 684 throw ex; 685 } 686 cancelAcquire(node); 688 throw new InterruptedException (); 689 } 690 691 698 private boolean doAcquireSharedNanos(long arg, long nanosTimeout) 699 throws InterruptedException { 700 701 long lastTime = System.nanoTime(); 702 final Node node = addWaiter(Node.SHARED); 703 try { 704 for (;;) { 705 final Node p = node.predecessor(); 706 if (p == head) { 707 long r = tryAcquireShared(arg); 708 if (r >= 0) { 709 setHeadAndPropagate(node, r); 710 p.next = null; return true; 712 } 713 } 714 if (nanosTimeout <= 0) { 715 cancelAcquire(node); 716 return false; 717 } 718 if (nanosTimeout > spinForTimeoutThreshold && 719 shouldParkAfterFailedAcquire(p, node)) 720 LockSupport.parkNanos(this, nanosTimeout); 721 long now = System.nanoTime(); 722 nanosTimeout -= now - lastTime; 723 lastTime = now; 724 if (Thread.interrupted()) 725 break; 726 } 727 } catch (RuntimeException ex) { 728 cancelAcquire(node); 729 throw ex; 730 } 731 cancelAcquire(node); 733 throw new InterruptedException (); 734 } 735 736 738 764 protected boolean tryAcquire(long arg) { 765 throw new UnsupportedOperationException (); 766 } 767 768 790 protected boolean tryRelease(long arg) { 791 throw new UnsupportedOperationException (); 792 } 793 794 826 protected long tryAcquireShared(long arg) { 827 throw new UnsupportedOperationException (); 828 } 829 830 851 protected boolean tryReleaseShared(long arg) { 852 throw new UnsupportedOperationException (); 853 } 854 855 870 protected boolean isHeldExclusively() { 871 throw new UnsupportedOperationException (); 872 } 873 874 886 public final void acquire(long arg) { 887 if (!tryAcquire(arg) && 888 acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) 889 selfInterrupt(); 890 } 891 892 906 public final void acquireInterruptibly(long arg) throws InterruptedException { 907 if (Thread.interrupted()) 908 throw new InterruptedException (); 909 if (!tryAcquire(arg)) 910 doAcquireInterruptibly(arg); 911 } 912 913 930 public final boolean tryAcquireNanos(long arg, long nanosTimeout) throws InterruptedException { 931 if (Thread.interrupted()) 932 throw new InterruptedException (); 933 return tryAcquire(arg) || 934 doAcquireNanos(arg, nanosTimeout); 935 } 936 937 947 public final boolean release(long arg) { 948 if (tryRelease(arg)) { 949 Node h = head; 950 if (h != null && h.waitStatus != 0) 951 unparkSuccessor(h); 952 return true; 953 } 954 return false; 955 } 956 957 968 public final void acquireShared(long arg) { 969 if (tryAcquireShared(arg) < 0) 970 doAcquireShared(arg); 971 } 972 973 986 public final void acquireSharedInterruptibly(long arg) throws InterruptedException { 987 if (Thread.interrupted()) 988 throw new InterruptedException (); 989 if (tryAcquireShared(arg) < 0) 990 doAcquireSharedInterruptibly(arg); 991 } 992 993 1009 public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) throws InterruptedException { 1010 if (Thread.interrupted()) 1011 throw new InterruptedException (); 1012 return tryAcquireShared(arg) >= 0 || 1013 doAcquireSharedNanos(arg, nanosTimeout); 1014 } 1015 1016 1025 public final boolean releaseShared(long arg) { 1026 if (tryReleaseShared(arg)) { 1027 Node h = head; 1028 if (h != null && h.waitStatus != 0) 1029 unparkSuccessor(h); 1030 return true; 1031 } 1032 return false; 1033 } 1034 1035 1037 1048 public final boolean hasQueuedThreads() { 1049 return head != tail; 1050 } 1051 1052 1061 public final boolean hasContended() { 1062 return head != null; 1063 } 1064 1065 1076 public final Thread getFirstQueuedThread() { 1077 return (head == tail)? null : fullGetFirstQueuedThread(); 1079 } 1080 1081 1084 private Thread fullGetFirstQueuedThread() { 1085 1093 Node h, s; 1094 Thread st; 1095 if (((h = head) != null && (s = h.next) != null && 1096 s.prev == head && (st = s.thread) != null) || 1097 ((h = head) != null && (s = h.next) != null && 1098 s.prev == head && (st = s.thread) != null)) 1099 return st; 1100 1101 1108 1109 Node t = tail; 1110 Thread firstThread = null; 1111 while (t != null && t != head) { 1112 Thread tt = t.thread; 1113 if (tt != null) 1114 firstThread = tt; 1115 t = t.prev; 1116 } 1117 return firstThread; 1118 } 1119 1120 1130 public final boolean isQueued(Thread thread) { 1131 if (thread == null) 1132 throw new NullPointerException (); 1133 for (Node p = tail; p != null; p = p.prev) 1134 if (p.thread == thread) 1135 return true; 1136 return false; 1137 } 1138 1139 1144 final boolean apparentlyFirstQueuedIsExclusive() { 1145 Node h, s; 1146 return ((h = head) != null && (s = h.next) != null && 1147 s.nextWaiter != Node.SHARED); 1148 } 1149 1150 1155 final boolean isFirst(Thread current) { 1156 Node h, s; 1157 return ((h = head) == null || 1158 ((s = h.next) != null && s.thread == current) || 1159 fullIsFirst(current)); 1160 } 1161 1162 final boolean fullIsFirst(Thread current) { 1163 Node h, s; 1165 Thread firstThread = null; 1166 if (((h = head) != null && (s = h.next) != null && 1167 s.prev == head && (firstThread = s.thread) != null)) 1168 return firstThread == current; 1169 Node t = tail; 1170 while (t != null && t != head) { 1171 Thread tt = t.thread; 1172 if (tt != null) 1173 firstThread = tt; 1174 t = t.prev; 1175 } 1176 return firstThread == current || firstThread == null; 1177 } 1178 1179 1180 1182 1192 public final int getQueueLength() { 1193 int n = 0; 1194 for (Node p = tail; p != null; p = p.prev) { 1195 if (p.thread != null) 1196 ++n; 1197 } 1198 return n; 1199 } 1200 1201 1212 public final Collection<Thread > getQueuedThreads() { 1213 ArrayList<Thread > list = new ArrayList<Thread >(); 1214 for (Node p = tail; p != null; p = p.prev) { 1215 Thread t = p.thread; 1216 if (t != null) 1217 list.add(t); 1218 } 1219 return list; 1220 } 1221 1222 1230 public final Collection<Thread > getExclusiveQueuedThreads() { 1231 ArrayList<Thread > list = new ArrayList<Thread >(); 1232 for (Node p = tail; p != null; p = p.prev) { 1233 if (!p.isShared()) { 1234 Thread t = p.thread; 1235 if (t != null) 1236 list.add(t); 1237 } 1238 } 1239 return list; 1240 } 1241 1242 1250 public final Collection<Thread > getSharedQueuedThreads() { 1251 ArrayList<Thread > list = new ArrayList<Thread >(); 1252 for (Node p = tail; p != null; p = p.prev) { 1253 if (p.isShared()) { 1254 Thread t = p.thread; 1255 if (t != null) 1256 list.add(t); 1257 } 1258 } 1259 return list; 1260 } 1261 1262 1271 public String toString() { 1272 long s = getState(); 1273 String q = hasQueuedThreads()? "non" : ""; 1274 return super.toString() + 1275 "[State = " + s + ", " + q + "empty queue]"; 1276 } 1277 1278 1279 1281 1287 final boolean isOnSyncQueue(Node node) { 1288 if (node.waitStatus == Node.CONDITION || node.prev == null) 1289 return false; 1290 if (node.next != null) return true; 1292 1300 return findNodeFromTail(node); 1301 } 1302 1303 1308 private boolean findNodeFromTail(Node node) { 1309 Node t = tail; 1310 for (;;) { 1311 if (t == node) 1312 return true; 1313 if (t == null) 1314 return false; 1315 t = t.prev; 1316 } 1317 } 1318 1319 1326 final boolean transferForSignal(Node node) { 1327 1330 if (!compareAndSetWaitStatus(node, Node.CONDITION, 0)) 1331 return false; 1332 1333 1339 Node p = enq(node); 1340 int c = p.waitStatus; 1341 if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL)) 1342 LockSupport.unpark(node.thread); 1343 return true; 1344 } 1345 1346 1354 final boolean transferAfterCancelledWait(Node node) { 1355 if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) { 1356 enq(node); 1357 return true; 1358 } 1359 1365 while (!isOnSyncQueue(node)) 1366 Thread.yield(); 1367 return false; 1368 } 1369 1370 1376 final long fullyRelease(Node node) { 1377 try { 1378 long savedState = getState(); 1379 if (release(savedState)) 1380 return savedState; 1381 } catch (RuntimeException ex) { 1382 node.waitStatus = Node.CANCELLED; 1383 throw ex; 1384 } 1385 node.waitStatus = Node.CANCELLED; 1387 throw new IllegalMonitorStateException (); 1388 } 1389 1390 1392 1400 public final boolean owns(ConditionObject condition) { 1401 if (condition == null) 1402 throw new NullPointerException (); 1403 return condition.isOwnedBy(this); 1404 } 1405 1406 1422 public final boolean hasWaiters(ConditionObject condition) { 1423 if (!owns(condition)) 1424 throw new IllegalArgumentException ("Not owner"); 1425 return condition.hasWaiters(); 1426 } 1427 1428 1444 public final int getWaitQueueLength(ConditionObject condition) { 1445 if (!owns(condition)) 1446 throw new IllegalArgumentException ("Not owner"); 1447 return condition.getWaitQueueLength(); 1448 } 1449 1450 1466 public final Collection<Thread > getWaitingThreads(ConditionObject condition) { 1467 if (!owns(condition)) 1468 throw new IllegalArgumentException ("Not owner"); 1469 return condition.getWaitingThreads(); 1470 } 1471 1472 1489 public class ConditionObject implements Condition , java.io.Serializable { 1490 private static final long serialVersionUID = 1173984872572414699L; 1491 1492 private transient Node firstWaiter; 1493 1494 private transient Node lastWaiter; 1495 1496 1499 public ConditionObject() { } 1500 1501 1503 1507 private Node addConditionWaiter() { 1508 Node t = lastWaiter; 1509 if (t != null && t.waitStatus != Node.CONDITION) { 1511 unlinkCancelledWaiters(); 1512 t = lastWaiter; 1513 } 1514 Node node = new Node(Thread.currentThread(), Node.CONDITION); 1515 if (t == null) 1516 firstWaiter = node; 1517 else 1518 t.nextWaiter = node; 1519 lastWaiter = node; 1520 return node; 1521 } 1522 1523 1529 private void doSignal(Node first) { 1530 do { 1531 if ( (firstWaiter = first.nextWaiter) == null) 1532 lastWaiter = null; 1533 first.nextWaiter = null; 1534 } while (!transferForSignal(first) && 1535 (first = firstWaiter) != null); 1536 } 1537 1538 1542 private void doSignalAll(Node first) { 1543 lastWaiter = firstWaiter = null; 1544 do { 1545 Node next = first.nextWaiter; 1546 first.nextWaiter = null; 1547 transferForSignal(first); 1548 first = next; 1549 } while (first != null); 1550 } 1551 1552 1566 private void unlinkCancelledWaiters() { 1567 Node t = firstWaiter; 1568 Node trail = null; 1569 while (t != null) { 1570 Node next = t.nextWaiter; 1571 if (t.waitStatus != Node.CONDITION) { 1572 t.nextWaiter = null; 1573 if (trail == null) 1574 firstWaiter = next; 1575 else 1576 trail.nextWaiter = next; 1577 if (next == null) 1578 lastWaiter = trail; 1579 } 1580 else 1581 trail = t; 1582 t = next; 1583 } 1584 } 1585 1586 1588 1596 public final void signal() { 1597 if (!isHeldExclusively()) 1598 throw new IllegalMonitorStateException (); 1599 Node first = firstWaiter; 1600 if (first != null) 1601 doSignal(first); 1602 } 1603 1604 1611 public final void signalAll() { 1612 if (!isHeldExclusively()) 1613 throw new IllegalMonitorStateException (); 1614 Node first = firstWaiter; 1615 if (first != null) 1616 doSignalAll(first); 1617 } 1618 1619 1631 public final void awaitUninterruptibly() { 1632 Node node = addConditionWaiter(); 1633 long savedState = fullyRelease(node); 1634 boolean interrupted = false; 1635 while (!isOnSyncQueue(node)) { 1636 LockSupport.park(this); 1637 if (Thread.interrupted()) 1638 interrupted = true; 1639 } 1640 if (acquireQueued(node, savedState) || interrupted) 1641 selfInterrupt(); 1642 } 1643 1644 1650 1651 1652 private static final int REINTERRUPT = 1; 1653 1654 private static final int THROW_IE = -1; 1655 1656 1661 private int checkInterruptWhileWaiting(Node node) { 1662 return (Thread.interrupted()) ? 1663 ((transferAfterCancelledWait(node))? THROW_IE : REINTERRUPT) : 1664 0; 1665 } 1666 1667 1671 private void reportInterruptAfterWait(int interruptMode) 1672 throws InterruptedException { 1673 if (interruptMode == THROW_IE) 1674 throw new InterruptedException (); 1675 else if (interruptMode == REINTERRUPT) 1676 selfInterrupt(); 1677 } 1678 1679 1693 public final void await() throws InterruptedException { 1694 if (Thread.interrupted()) 1695 throw new InterruptedException (); 1696 Node node = addConditionWaiter(); 1697 long savedState = fullyRelease(node); 1698 int interruptMode = 0; 1699 while (!isOnSyncQueue(node)) { 1700 LockSupport.park(this); 1701 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1702 break; 1703 } 1704 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1705 interruptMode = REINTERRUPT; 1706 if (node.nextWaiter != null) 1707 unlinkCancelledWaiters(); 1708 if (interruptMode != 0) 1709 reportInterruptAfterWait(interruptMode); 1710 } 1711 1712 1726 public final long awaitNanos(long nanosTimeout) throws InterruptedException { 1727 if (Thread.interrupted()) 1728 throw new InterruptedException (); 1729 Node node = addConditionWaiter(); 1730 long savedState = fullyRelease(node); 1731 long lastTime = System.nanoTime(); 1732 int interruptMode = 0; 1733 while (!isOnSyncQueue(node)) { 1734 if (nanosTimeout <= 0L) { 1735 transferAfterCancelledWait(node); 1736 break; 1737 } 1738 LockSupport.parkNanos(this, nanosTimeout); 1739 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1740 break; 1741 1742 long now = System.nanoTime(); 1743 nanosTimeout -= now - lastTime; 1744 lastTime = now; 1745 } 1746 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1747 interruptMode = REINTERRUPT; 1748 if (node.nextWaiter != null) 1749 unlinkCancelledWaiters(); 1750 if (interruptMode != 0) 1751 reportInterruptAfterWait(interruptMode); 1752 return nanosTimeout - (System.nanoTime() - lastTime); 1753 } 1754 1755 1770 public final boolean awaitUntil(Date deadline) throws InterruptedException { 1771 if (deadline == null) 1772 throw new NullPointerException (); 1773 long abstime = deadline.getTime(); 1774 if (Thread.interrupted()) 1775 throw new InterruptedException (); 1776 Node node = addConditionWaiter(); 1777 long savedState = fullyRelease(node); 1778 boolean timedout = false; 1779 int interruptMode = 0; 1780 while (!isOnSyncQueue(node)) { 1781 if (System.currentTimeMillis() > abstime) { 1782 timedout = transferAfterCancelledWait(node); 1783 break; 1784 } 1785 LockSupport.parkUntil(this, abstime); 1786 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1787 break; 1788 } 1789 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1790 interruptMode = REINTERRUPT; 1791 if (node.nextWaiter != null) 1792 unlinkCancelledWaiters(); 1793 if (interruptMode != 0) 1794 reportInterruptAfterWait(interruptMode); 1795 return !timedout; 1796 } 1797 1798 1813 public final boolean await(long time, TimeUnit unit) throws InterruptedException { 1814 if (unit == null) 1815 throw new NullPointerException (); 1816 long nanosTimeout = unit.toNanos(time); 1817 if (Thread.interrupted()) 1818 throw new InterruptedException (); 1819 Node node = addConditionWaiter(); 1820 long savedState = fullyRelease(node); 1821 long lastTime = System.nanoTime(); 1822 boolean timedout = false; 1823 int interruptMode = 0; 1824 while (!isOnSyncQueue(node)) { 1825 if (nanosTimeout <= 0L) { 1826 timedout = transferAfterCancelledWait(node); 1827 break; 1828 } 1829 LockSupport.parkNanos(this, nanosTimeout); 1830 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1831 break; 1832 long now = System.nanoTime(); 1833 nanosTimeout -= now - lastTime; 1834 lastTime = now; 1835 } 1836 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1837 interruptMode = REINTERRUPT; 1838 if (node.nextWaiter != null) 1839 unlinkCancelledWaiters(); 1840 if (interruptMode != 0) 1841 reportInterruptAfterWait(interruptMode); 1842 return !timedout; 1843 } 1844 1845 1847 1853 final boolean isOwnedBy(AbstractQueuedLongSynchronizer sync) { 1854 return sync == AbstractQueuedLongSynchronizer.this; 1855 } 1856 1857 1865 protected final boolean hasWaiters() { 1866 if (!isHeldExclusively()) 1867 throw new IllegalMonitorStateException (); 1868 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 1869 if (w.waitStatus == Node.CONDITION) 1870 return true; 1871 } 1872 return false; 1873 } 1874 1875 1884 protected final int getWaitQueueLength() { 1885 if (!isHeldExclusively()) 1886 throw new IllegalMonitorStateException (); 1887 int n = 0; 1888 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 1889 if (w.waitStatus == Node.CONDITION) 1890 ++n; 1891 } 1892 return n; 1893 } 1894 1895 1904 protected final Collection<Thread > getWaitingThreads() { 1905 if (!isHeldExclusively()) 1906 throw new IllegalMonitorStateException (); 1907 ArrayList<Thread > list = new ArrayList<Thread >(); 1908 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 1909 if (w.waitStatus == Node.CONDITION) { 1910 Thread t = w.thread; 1911 if (t != null) 1912 list.add(t); 1913 } 1914 } 1915 return list; 1916 } 1917 } 1918 1919 1928 private static final Unsafe unsafe = Unsafe.getUnsafe(); 1929 private static final long stateOffset; 1930 private static final long headOffset; 1931 private static final long tailOffset; 1932 private static final long waitStatusOffset; 1933 private static final long nextOffset; 1934 1935 static { 1936 try { 1937 stateOffset = unsafe.objectFieldOffset 1938 (AbstractQueuedLongSynchronizer .class.getDeclaredField("state")); 1939 headOffset = unsafe.objectFieldOffset 1940 (AbstractQueuedLongSynchronizer .class.getDeclaredField("head")); 1941 tailOffset = unsafe.objectFieldOffset 1942 (AbstractQueuedLongSynchronizer .class.getDeclaredField("tail")); 1943 waitStatusOffset = unsafe.objectFieldOffset 1944 (Node.class.getDeclaredField("waitStatus")); 1945 nextOffset = unsafe.objectFieldOffset 1946 (Node.class.getDeclaredField("next")); 1947 1948 } catch (Exception ex) { throw new Error (ex); } 1949 } 1950 1951 1954 private final boolean compareAndSetHead(Node update) { 1955 return unsafe.compareAndSwapObject(this, headOffset, null, update); 1956 } 1957 1958 1961 private final boolean compareAndSetTail(Node expect, Node update) { 1962 return unsafe.compareAndSwapObject(this, tailOffset, expect, update); 1963 } 1964 1965 1968 private final static boolean compareAndSetWaitStatus(Node node, 1969 int expect, 1970 int update) { 1971 return unsafe.compareAndSwapInt(node, waitStatusOffset, 1972 expect, update); 1973 } 1974 1975 1978 private final static boolean compareAndSetNext(Node node, 1979 Node expect, 1980 Node update) { 1981 return unsafe.compareAndSwapObject(node, nextOffset, expect, update); 1982 } 1983} 1984 | Popular Tags |