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 250 public abstract class AbstractQueuedSynchronizer implements java.io.Serializable { 251 private static final long serialVersionUID = 7373984972572414691L; 252 253 257 protected AbstractQueuedSynchronizer() { } 258 259 337 static final class Node { 338 339 static final int CANCELLED = 1; 340 341 static final int SIGNAL = -1; 342 343 static final int CONDITION = -2; 344 345 static final Node SHARED = new Node(); 346 347 static final Node EXCLUSIVE = null; 348 349 377 volatile int waitStatus; 378 379 390 volatile Node prev; 391 392 404 volatile Node next; 405 406 410 volatile Thread thread; 411 412 422 Node nextWaiter; 423 424 427 final boolean isShared() { 428 return nextWaiter == SHARED; 429 } 430 431 436 final Node predecessor() throws NullPointerException { 437 Node p = prev; 438 if (p == null) 439 throw new NullPointerException (); 440 else 441 return p; 442 } 443 444 Node() { } 446 447 Node(Thread thread, Node mode) { this.nextWaiter = mode; 449 this.thread = thread; 450 } 451 452 Node(Thread thread, int waitStatus) { this.waitStatus = waitStatus; 454 this.thread = thread; 455 } 456 } 457 458 464 private transient volatile Node head; 465 466 470 private transient volatile Node tail; 471 472 475 private volatile int state; 476 477 482 protected final int getState() { 483 return state; 484 } 485 486 491 protected final void setState(int newState) { 492 state = newState; 493 } 494 495 505 protected final boolean compareAndSetState(int expect, int update) { 506 return unsafe.compareAndSwapInt(this, stateOffset, expect, update); 508 } 509 510 512 517 private Node enq(final Node node) { 518 for (;;) { 519 Node t = tail; 520 if (t == null) { Node h = new Node(); h.next = node; 523 node.prev = h; 524 if (compareAndSetHead(h)) { 525 tail = node; 526 return h; 527 } 528 } 529 else { 530 node.prev = t; 531 if (compareAndSetTail(t, node)) { 532 t.next = node; 533 return t; 534 } 535 } 536 } 537 } 538 539 545 private Node addWaiter(Node mode) { 546 Node node = new Node(Thread.currentThread(), mode); 547 Node pred = tail; 549 if (pred != null) { 550 node.prev = pred; 551 if (compareAndSetTail(pred, node)) { 552 pred.next = node; 553 return node; 554 } 555 } 556 enq(node); 557 return node; 558 } 559 560 566 private void setHead(Node node) { 567 head = node; 568 node.thread = null; 569 node.prev = null; 570 } 571 572 576 private void unparkSuccessor(Node node) { 577 581 compareAndSetWaitStatus(node, Node.SIGNAL, 0); 582 583 589 Thread thread; 590 Node s = node.next; 591 if (s != null && s.waitStatus <= 0) 592 thread = s.thread; 593 else { 594 thread = null; 595 for (s = tail; s != null && s != node; s = s.prev) 596 if (s.waitStatus <= 0) 597 thread = s.thread; 598 } 599 LockSupport.unpark(thread); 600 } 601 602 609 private void setHeadAndPropagate(Node node, int propagate) { 610 setHead(node); 611 if (propagate > 0 && node.waitStatus != 0) { 612 616 Node s = node.next; 617 if (s == null || s.isShared()) 618 unparkSuccessor(node); 619 } 620 } 621 622 624 628 private void cancelAcquire(Node node) { 629 if (node == null) 631 return; 632 633 node.thread = null; 634 635 Node pred = node.prev; 637 while (pred.waitStatus > 0) 638 node.prev = pred = pred.prev; 639 640 Node predNext = pred.next; 642 643 node.waitStatus = Node.CANCELLED; 645 646 if (node == tail && compareAndSetTail(node, pred)) { 648 compareAndSetNext(pred, predNext, null); 649 } else { 650 if (pred != head 652 && (pred.waitStatus == Node.SIGNAL 653 || compareAndSetWaitStatus(pred, 0, Node.SIGNAL)) 654 && pred.thread != null) { 655 656 Node next = node.next; 658 if (next != null && next.waitStatus <= 0) 659 compareAndSetNext(pred, predNext, next); 660 } else { 661 unparkSuccessor(node); 662 } 663 664 node.next = node; } 666 } 667 668 676 private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) { 677 int s = pred.waitStatus; 678 if (s < 0) 679 683 return true; 684 if (s > 0) { 685 689 do { 690 node.prev = pred = pred.prev; 691 } while (pred.waitStatus > 0); 692 pred.next = node; 693 } 694 else 695 700 compareAndSetWaitStatus(pred, 0, Node.SIGNAL); 701 return false; 702 } 703 704 707 private static void selfInterrupt() { 708 Thread.currentThread().interrupt(); 709 } 710 711 715 private static boolean parkAndCheckInterrupt() { 716 LockSupport.park(); 717 return Thread.interrupted(); 718 } 719 720 728 729 736 final boolean acquireQueued(final Node node, int arg) { 737 try { 738 boolean interrupted = false; 739 for (;;) { 740 final Node p = node.predecessor(); 741 if (p == head && tryAcquire(arg)) { 742 setHead(node); 743 p.next = null; return interrupted; 745 } 746 if (shouldParkAfterFailedAcquire(p, node) && 747 parkAndCheckInterrupt()) 748 interrupted = true; 749 } 750 } catch (RuntimeException ex) { 751 cancelAcquire(node); 752 throw ex; 753 } 754 } 755 756 760 private void doAcquireInterruptibly(int arg) 761 throws InterruptedException { 762 final Node node = addWaiter(Node.EXCLUSIVE); 763 try { 764 for (;;) { 765 final Node p = node.predecessor(); 766 if (p == head && tryAcquire(arg)) { 767 setHead(node); 768 p.next = null; return; 770 } 771 if (shouldParkAfterFailedAcquire(p, node) && 772 parkAndCheckInterrupt()) 773 break; 774 } 775 } catch (RuntimeException ex) { 776 cancelAcquire(node); 777 throw ex; 778 } 779 cancelAcquire(node); 781 throw new InterruptedException (); 782 } 783 784 790 private boolean doAcquireNanos(int arg, long nanosTimeout) 791 throws InterruptedException { 792 long lastTime = System.nanoTime(); 793 final Node node = addWaiter(Node.EXCLUSIVE); 794 try { 795 for (;;) { 796 final Node p = node.predecessor(); 797 if (p == head && tryAcquire(arg)) { 798 setHead(node); 799 p.next = null; return true; 801 } 802 if (nanosTimeout <= 0) { 803 cancelAcquire(node); 804 return false; 805 } 806 if (shouldParkAfterFailedAcquire(p, node)) { 807 LockSupport.parkNanos(nanosTimeout); 808 if (Thread.interrupted()) 809 break; 810 long now = System.nanoTime(); 811 nanosTimeout -= now - lastTime; 812 lastTime = now; 813 } 814 } 815 } catch (RuntimeException ex) { 816 cancelAcquire(node); 817 throw ex; 818 } 819 cancelAcquire(node); 821 throw new InterruptedException (); 822 } 823 824 828 private void doAcquireShared(int arg) { 829 final Node node = addWaiter(Node.SHARED); 830 try { 831 boolean interrupted = false; 832 for (;;) { 833 final Node p = node.predecessor(); 834 if (p == head) { 835 int r = tryAcquireShared(arg); 836 if (r >= 0) { 837 setHeadAndPropagate(node, r); 838 p.next = null; if (interrupted) 840 selfInterrupt(); 841 return; 842 } 843 } 844 if (shouldParkAfterFailedAcquire(p, node) && 845 parkAndCheckInterrupt()) 846 interrupted = true; 847 } 848 } catch (RuntimeException ex) { 849 cancelAcquire(node); 850 throw ex; 851 } 852 } 853 854 858 private void doAcquireSharedInterruptibly(int arg) 859 throws InterruptedException { 860 final Node node = addWaiter(Node.SHARED); 861 try { 862 for (;;) { 863 final Node p = node.predecessor(); 864 if (p == head) { 865 int r = tryAcquireShared(arg); 866 if (r >= 0) { 867 setHeadAndPropagate(node, r); 868 p.next = null; return; 870 } 871 } 872 if (shouldParkAfterFailedAcquire(p, node) && 873 parkAndCheckInterrupt()) 874 break; 875 } 876 } catch (RuntimeException ex) { 877 cancelAcquire(node); 878 throw ex; 879 } 880 cancelAcquire(node); 882 throw new InterruptedException (); 883 } 884 885 891 private boolean doAcquireSharedNanos(int arg, long nanosTimeout) 892 throws InterruptedException { 893 894 long lastTime = System.nanoTime(); 895 final Node node = addWaiter(Node.SHARED); 896 try { 897 for (;;) { 898 final Node p = node.predecessor(); 899 if (p == head) { 900 int r = tryAcquireShared(arg); 901 if (r >= 0) { 902 setHeadAndPropagate(node, r); 903 p.next = null; return true; 905 } 906 } 907 if (nanosTimeout <= 0) { 908 cancelAcquire(node); 909 return false; 910 } 911 if (shouldParkAfterFailedAcquire(p, node)) { 912 LockSupport.parkNanos(nanosTimeout); 913 if (Thread.interrupted()) 914 break; 915 long now = System.nanoTime(); 916 nanosTimeout -= now - lastTime; 917 lastTime = now; 918 } 919 } 920 } catch (RuntimeException ex) { 921 cancelAcquire(node); 922 throw ex; 923 } 924 cancelAcquire(node); 926 throw new InterruptedException (); 927 } 928 929 931 958 protected boolean tryAcquire(int arg) { 959 throw new UnsupportedOperationException (); 960 } 961 962 983 protected boolean tryRelease(int arg) { 984 throw new UnsupportedOperationException (); 985 } 986 987 1018 protected int tryAcquireShared(int arg) { 1019 throw new UnsupportedOperationException (); 1020 } 1021 1022 1041 protected boolean tryReleaseShared(int arg) { 1042 throw new UnsupportedOperationException (); 1043 } 1044 1045 1059 protected boolean isHeldExclusively() { 1060 throw new UnsupportedOperationException (); 1061 } 1062 1063 1075 public final void acquire(int arg) { 1076 if (!tryAcquire(arg) && 1077 acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) 1078 selfInterrupt(); 1079 } 1080 1081 1095 public final void acquireInterruptibly(int arg) throws InterruptedException { 1096 if (Thread.interrupted()) 1097 throw new InterruptedException (); 1098 if (!tryAcquire(arg)) 1099 doAcquireInterruptibly(arg); 1100 } 1101 1102 1119 public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException { 1120 if (Thread.interrupted()) 1121 throw new InterruptedException (); 1122 return tryAcquire(arg) || 1123 doAcquireNanos(arg, nanosTimeout); 1124 } 1125 1126 1136 public final boolean release(int arg) { 1137 if (tryRelease(arg)) { 1138 Node h = head; 1139 if (h != null && h.waitStatus != 0) 1140 unparkSuccessor(h); 1141 return true; 1142 } 1143 return false; 1144 } 1145 1146 1157 public final void acquireShared(int arg) { 1158 if (tryAcquireShared(arg) < 0) 1159 doAcquireShared(arg); 1160 } 1161 1162 1175 public final void acquireSharedInterruptibly(int arg) throws InterruptedException { 1176 if (Thread.interrupted()) 1177 throw new InterruptedException (); 1178 if (tryAcquireShared(arg) < 0) 1179 doAcquireSharedInterruptibly(arg); 1180 } 1181 1182 1198 public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException { 1199 if (Thread.interrupted()) 1200 throw new InterruptedException (); 1201 return tryAcquireShared(arg) >= 0 || 1202 doAcquireSharedNanos(arg, nanosTimeout); 1203 } 1204 1205 1214 public final boolean releaseShared(int arg) { 1215 if (tryReleaseShared(arg)) { 1216 Node h = head; 1217 if (h != null && h.waitStatus != 0) 1218 unparkSuccessor(h); 1219 return true; 1220 } 1221 return false; 1222 } 1223 1224 1226 1238 public final boolean hasQueuedThreads() { 1239 return head != tail; 1240 } 1241 1242 1251 public final boolean hasContended() { 1252 return head != null; 1253 } 1254 1255 1266 public final Thread getFirstQueuedThread() { 1267 return (head == tail)? null : fullGetFirstQueuedThread(); 1269 } 1270 1271 1274 private Thread fullGetFirstQueuedThread() { 1275 Node h = head; 1276 if (h == null) return null; 1278 1279 1286 Node s = h.next; 1287 if (s != null) { 1288 Thread st = s.thread; 1289 Node sp = s.prev; 1290 if (st != null && sp == head) 1291 return st; 1292 } 1293 1294 1301 1302 Node t = tail; 1303 Thread firstThread = null; 1304 while (t != null && t != head) { 1305 Thread tt = t.thread; 1306 if (tt != null) 1307 firstThread = tt; 1308 t = t.prev; 1309 } 1310 return firstThread; 1311 } 1312 1313 1323 public final boolean isQueued(Thread thread) { 1324 if (thread == null) 1325 throw new NullPointerException (); 1326 for (Node p = tail; p != null; p = p.prev) 1327 if (p.thread == thread) 1328 return true; 1329 return false; 1330 } 1331 1332 1334 1344 public final int getQueueLength() { 1345 int n = 0; 1346 for (Node p = tail; p != null; p = p.prev) { 1347 if (p.thread != null) 1348 ++n; 1349 } 1350 return n; 1351 } 1352 1353 1363 public final Collection<Thread > getQueuedThreads() { 1364 ArrayList<Thread > list = new ArrayList<Thread >(); 1365 for (Node p = tail; p != null; p = p.prev) { 1366 Thread t = p.thread; 1367 if (t != null) 1368 list.add(t); 1369 } 1370 return list; 1371 } 1372 1373 1380 public final Collection<Thread > getExclusiveQueuedThreads() { 1381 ArrayList<Thread > list = new ArrayList<Thread >(); 1382 for (Node p = tail; p != null; p = p.prev) { 1383 if (!p.isShared()) { 1384 Thread t = p.thread; 1385 if (t != null) 1386 list.add(t); 1387 } 1388 } 1389 return list; 1390 } 1391 1392 1399 public final Collection<Thread > getSharedQueuedThreads() { 1400 ArrayList<Thread > list = new ArrayList<Thread >(); 1401 for (Node p = tail; p != null; p = p.prev) { 1402 if (p.isShared()) { 1403 Thread t = p.thread; 1404 if (t != null) 1405 list.add(t); 1406 } 1407 } 1408 return list; 1409 } 1410 1411 1420 public String toString() { 1421 int s = getState(); 1422 String q = hasQueuedThreads()? "non" : ""; 1423 return super.toString() + 1424 "[State = " + s + ", " + q + "empty queue]"; 1425 } 1426 1427 1428 1430 1436 final boolean isOnSyncQueue(Node node) { 1437 if (node.waitStatus == Node.CONDITION || node.prev == null) 1438 return false; 1439 if (node.next != null) return true; 1441 1449 return findNodeFromTail(node); 1450 } 1451 1452 1457 private boolean findNodeFromTail(Node node) { 1458 Node t = tail; 1459 for (;;) { 1460 if (t == node) 1461 return true; 1462 if (t == null) 1463 return false; 1464 t = t.prev; 1465 } 1466 } 1467 1468 1475 final boolean transferForSignal(Node node) { 1476 1479 if (!compareAndSetWaitStatus(node, Node.CONDITION, 0)) 1480 return false; 1481 1482 1488 Node p = enq(node); 1489 int c = p.waitStatus; 1490 if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL)) 1491 LockSupport.unpark(node.thread); 1492 return true; 1493 } 1494 1495 1503 final boolean transferAfterCancelledWait(Node node) { 1504 if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) { 1505 enq(node); 1506 return true; 1507 } 1508 1514 while (!isOnSyncQueue(node)) 1515 Thread.yield(); 1516 return false; 1517 } 1518 1519 1525 final int fullyRelease(Node node) { 1526 try { 1527 int savedState = getState(); 1528 if (release(savedState)) 1529 return savedState; 1530 } catch(RuntimeException ex) { 1531 node.waitStatus = Node.CANCELLED; 1532 throw ex; 1533 } 1534 node.waitStatus = Node.CANCELLED; 1536 throw new IllegalMonitorStateException (); 1537 } 1538 1539 1541 1548 public final boolean owns(ConditionObject condition) { 1549 if (condition == null) 1550 throw new NullPointerException (); 1551 return condition.isOwnedBy(this); 1552 } 1553 1554 1569 public final boolean hasWaiters(ConditionObject condition) { 1570 if (!owns(condition)) 1571 throw new IllegalArgumentException ("Not owner"); 1572 return condition.hasWaiters(); 1573 } 1574 1575 1590 public final int getWaitQueueLength(ConditionObject condition) { 1591 if (!owns(condition)) 1592 throw new IllegalArgumentException ("Not owner"); 1593 return condition.getWaitQueueLength(); 1594 } 1595 1596 1611 public final Collection<Thread > getWaitingThreads(ConditionObject condition) { 1612 if (!owns(condition)) 1613 throw new IllegalArgumentException ("Not owner"); 1614 return condition.getWaitingThreads(); 1615 } 1616 1617 1632 public class ConditionObject implements Condition , java.io.Serializable { 1633 private static final long serialVersionUID = 1173984872572414699L; 1634 1635 private transient Node firstWaiter; 1636 1637 private transient Node lastWaiter; 1638 1639 1642 public ConditionObject() { } 1643 1644 1646 1650 private Node addConditionWaiter() { 1651 Node t = lastWaiter; 1652 if (t != null && t.waitStatus != Node.CONDITION) { 1654 unlinkCancelledWaiters(); 1655 t = lastWaiter; 1656 } 1657 Node node = new Node(Thread.currentThread(), Node.CONDITION); 1658 if (t == null) 1659 firstWaiter = node; 1660 else 1661 t.nextWaiter = node; 1662 lastWaiter = node; 1663 return node; 1664 } 1665 1666 1672 private void doSignal(Node first) { 1673 do { 1674 if ( (firstWaiter = first.nextWaiter) == null) 1675 lastWaiter = null; 1676 first.nextWaiter = null; 1677 } while (!transferForSignal(first) && 1678 (first = firstWaiter) != null); 1679 } 1680 1681 1685 private void doSignalAll(Node first) { 1686 lastWaiter = firstWaiter = null; 1687 do { 1688 Node next = first.nextWaiter; 1689 first.nextWaiter = null; 1690 transferForSignal(first); 1691 first = next; 1692 } while (first != null); 1693 } 1694 1695 1697 1704 public final void signal() { 1705 if (!isHeldExclusively()) 1706 throw new IllegalMonitorStateException (); 1707 Node first = firstWaiter; 1708 if (first != null) 1709 doSignal(first); 1710 } 1711 1712 1718 public final void signalAll() { 1719 if (!isHeldExclusively()) 1720 throw new IllegalMonitorStateException (); 1721 Node first = firstWaiter; 1722 if (first != null) 1723 doSignalAll(first); 1724 } 1725 1726 1740 private void unlinkCancelledWaiters() { 1741 Node t = firstWaiter; 1742 Node trail = null; 1743 while (t != null) { 1744 Node next = t.nextWaiter; 1745 if (t.waitStatus != Node.CONDITION) { 1746 t.nextWaiter = null; 1747 if (trail == null) 1748 firstWaiter = next; 1749 else 1750 trail.nextWaiter = next; 1751 if (next == null) 1752 lastWaiter = trail; 1753 } 1754 else 1755 trail = t; 1756 t = next; 1757 } 1758 } 1759 1760 1772 public final void awaitUninterruptibly() { 1773 Node node = addConditionWaiter(); 1774 int savedState = fullyRelease(node); 1775 boolean interrupted = false; 1776 while (!isOnSyncQueue(node)) { 1777 LockSupport.park(); 1778 if (Thread.interrupted()) 1779 interrupted = true; 1780 } 1781 if (acquireQueued(node, savedState) || interrupted) 1782 selfInterrupt(); 1783 } 1784 1785 1791 1792 1793 private static final int REINTERRUPT = 1; 1794 1795 private static final int THROW_IE = -1; 1796 1797 1802 private int checkInterruptWhileWaiting(Node node) { 1803 return (Thread.interrupted()) ? 1804 ((transferAfterCancelledWait(node))? THROW_IE : REINTERRUPT) : 1805 0; 1806 } 1807 1808 1812 private void reportInterruptAfterWait(int interruptMode) 1813 throws InterruptedException { 1814 if (interruptMode == THROW_IE) 1815 throw new InterruptedException (); 1816 else if (interruptMode == REINTERRUPT) 1817 Thread.currentThread().interrupt(); 1818 } 1819 1820 1834 public final void await() throws InterruptedException { 1835 if (Thread.interrupted()) 1836 throw new InterruptedException (); 1837 Node node = addConditionWaiter(); 1838 int savedState = fullyRelease(node); 1839 int interruptMode = 0; 1840 while (!isOnSyncQueue(node)) { 1841 LockSupport.park(); 1842 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1843 break; 1844 } 1845 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1846 interruptMode = REINTERRUPT; 1847 if (node.nextWaiter != null) 1848 unlinkCancelledWaiters(); 1849 if (interruptMode != 0) 1850 reportInterruptAfterWait(interruptMode); 1851 } 1852 1853 1867 public final long awaitNanos(long nanosTimeout) throws InterruptedException { 1868 if (Thread.interrupted()) 1869 throw new InterruptedException (); 1870 Node node = addConditionWaiter(); 1871 int savedState = fullyRelease(node); 1872 long lastTime = System.nanoTime(); 1873 int interruptMode = 0; 1874 while (!isOnSyncQueue(node)) { 1875 if (nanosTimeout <= 0L) { 1876 transferAfterCancelledWait(node); 1877 break; 1878 } 1879 LockSupport.parkNanos(nanosTimeout); 1880 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1881 break; 1882 long now = System.nanoTime(); 1883 nanosTimeout -= now - lastTime; 1884 lastTime = now; 1885 } 1886 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1887 interruptMode = REINTERRUPT; 1888 if (node.nextWaiter != null) 1889 unlinkCancelledWaiters(); 1890 if (interruptMode != 0) 1891 reportInterruptAfterWait(interruptMode); 1892 return nanosTimeout - (System.nanoTime() - lastTime); 1893 } 1894 1895 1910 public final boolean awaitUntil(Date deadline) throws InterruptedException { 1911 if (deadline == null) 1912 throw new NullPointerException (); 1913 long abstime = deadline.getTime(); 1914 if (Thread.interrupted()) 1915 throw new InterruptedException (); 1916 Node node = addConditionWaiter(); 1917 int savedState = fullyRelease(node); 1918 boolean timedout = false; 1919 int interruptMode = 0; 1920 while (!isOnSyncQueue(node)) { 1921 if (System.currentTimeMillis() > abstime) { 1922 timedout = transferAfterCancelledWait(node); 1923 break; 1924 } 1925 LockSupport.parkUntil(abstime); 1926 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1927 break; 1928 } 1929 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1930 interruptMode = REINTERRUPT; 1931 if (node.nextWaiter != null) 1932 unlinkCancelledWaiters(); 1933 if (interruptMode != 0) 1934 reportInterruptAfterWait(interruptMode); 1935 return !timedout; 1936 } 1937 1938 1953 public final boolean await(long time, TimeUnit unit) throws InterruptedException { 1954 if (unit == null) 1955 throw new NullPointerException (); 1956 long nanosTimeout = unit.toNanos(time); 1957 if (Thread.interrupted()) 1958 throw new InterruptedException (); 1959 Node node = addConditionWaiter(); 1960 int savedState = fullyRelease(node); 1961 long lastTime = System.nanoTime(); 1962 boolean timedout = false; 1963 int interruptMode = 0; 1964 while (!isOnSyncQueue(node)) { 1965 if (nanosTimeout <= 0L) { 1966 timedout = transferAfterCancelledWait(node); 1967 break; 1968 } 1969 LockSupport.parkNanos(nanosTimeout); 1970 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0) 1971 break; 1972 long now = System.nanoTime(); 1973 nanosTimeout -= now - lastTime; 1974 lastTime = now; 1975 } 1976 if (acquireQueued(node, savedState) && interruptMode != THROW_IE) 1977 interruptMode = REINTERRUPT; 1978 if (node.nextWaiter != null) 1979 unlinkCancelledWaiters(); 1980 if (interruptMode != 0) 1981 reportInterruptAfterWait(interruptMode); 1982 return !timedout; 1983 } 1984 1985 1987 1992 final boolean isOwnedBy(AbstractQueuedSynchronizer sync) { 1993 return sync == AbstractQueuedSynchronizer.this; 1994 } 1995 1996 2003 protected final boolean hasWaiters() { 2004 if (!isHeldExclusively()) 2005 throw new IllegalMonitorStateException (); 2006 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 2007 if (w.waitStatus == Node.CONDITION) 2008 return true; 2009 } 2010 return false; 2011 } 2012 2013 2021 protected final int getWaitQueueLength() { 2022 if (!isHeldExclusively()) 2023 throw new IllegalMonitorStateException (); 2024 int n = 0; 2025 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 2026 if (w.waitStatus == Node.CONDITION) 2027 ++n; 2028 } 2029 return n; 2030 } 2031 2032 2040 protected final Collection<Thread > getWaitingThreads() { 2041 if (!isHeldExclusively()) 2042 throw new IllegalMonitorStateException (); 2043 ArrayList<Thread > list = new ArrayList<Thread >(); 2044 for (Node w = firstWaiter; w != null; w = w.nextWaiter) { 2045 if (w.waitStatus == Node.CONDITION) { 2046 Thread t = w.thread; 2047 if (t != null) 2048 list.add(t); 2049 } 2050 } 2051 return list; 2052 } 2053 } 2054 2055 2064 private static final Unsafe unsafe = Unsafe.getUnsafe(); 2065 private static final long stateOffset; 2066 private static final long headOffset; 2067 private static final long tailOffset; 2068 private static final long waitStatusOffset; 2069 private static final long nextOffset; 2070 2071 static { 2072 try { 2073 stateOffset = unsafe.objectFieldOffset 2074 (AbstractQueuedSynchronizer .class.getDeclaredField("state")); 2075 headOffset = unsafe.objectFieldOffset 2076 (AbstractQueuedSynchronizer .class.getDeclaredField("head")); 2077 tailOffset = unsafe.objectFieldOffset 2078 (AbstractQueuedSynchronizer .class.getDeclaredField("tail")); 2079 waitStatusOffset = unsafe.objectFieldOffset 2080 (Node.class.getDeclaredField("waitStatus")); 2081 nextOffset = unsafe.objectFieldOffset 2082 (Node.class.getDeclaredField("next")); 2083 2084 } catch(Exception ex) { throw new Error (ex); } 2085 } 2086 2087 2090 private final boolean compareAndSetHead(Node update) { 2091 return unsafe.compareAndSwapObject(this, headOffset, null, update); 2092 } 2093 2094 2097 private final boolean compareAndSetTail(Node expect, Node update) { 2098 return unsafe.compareAndSwapObject(this, tailOffset, expect, update); 2099 } 2100 2101 2104 private final static boolean compareAndSetWaitStatus(Node node, 2105 int expect, 2106 int update) { 2107 return unsafe.compareAndSwapInt(node, waitStatusOffset, 2108 expect, update); 2109 } 2110 2111 2114 private final static boolean compareAndSetNext(Node node, 2115 Node expect, 2116 Node update) { 2117 return unsafe.compareAndSwapObject(node, nextOffset, expect, update); 2118 } 2119 2120} 2121 | Popular Tags |