src/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java

Print this page




 973     public final void acquire(long arg) {
 974         if (!tryAcquire(arg) &&
 975             acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
 976             selfInterrupt();
 977     }
 978 
 979     /**
 980      * Acquires in exclusive mode, aborting if interrupted.
 981      * Implemented by first checking interrupt status, then invoking
 982      * at least once {@link #tryAcquire}, returning on
 983      * success.  Otherwise the thread is queued, possibly repeatedly
 984      * blocking and unblocking, invoking {@link #tryAcquire}
 985      * until success or the thread is interrupted.  This method can be
 986      * used to implement method {@link Lock#lockInterruptibly}.
 987      *
 988      * @param arg the acquire argument.  This value is conveyed to
 989      *        {@link #tryAcquire} but is otherwise uninterpreted and
 990      *        can represent anything you like.
 991      * @throws InterruptedException if the current thread is interrupted
 992      */
 993     public final void acquireInterruptibly(long arg) throws InterruptedException {

 994         if (Thread.interrupted())
 995             throw new InterruptedException();
 996         if (!tryAcquire(arg))
 997             doAcquireInterruptibly(arg);
 998     }
 999 
1000     /**
1001      * Attempts to acquire in exclusive mode, aborting if interrupted,
1002      * and failing if the given timeout elapses.  Implemented by first
1003      * checking interrupt status, then invoking at least once {@link
1004      * #tryAcquire}, returning on success.  Otherwise, the thread is
1005      * queued, possibly repeatedly blocking and unblocking, invoking
1006      * {@link #tryAcquire} until success or the thread is interrupted
1007      * or the timeout elapses.  This method can be used to implement
1008      * method {@link Lock#tryLock(long, TimeUnit)}.
1009      *
1010      * @param arg the acquire argument.  This value is conveyed to
1011      *        {@link #tryAcquire} but is otherwise uninterpreted and
1012      *        can represent anything you like.
1013      * @param nanosTimeout the maximum number of nanoseconds to wait
1014      * @return {@code true} if acquired; {@code false} if timed out
1015      * @throws InterruptedException if the current thread is interrupted
1016      */
1017     public final boolean tryAcquireNanos(long arg, long nanosTimeout) throws InterruptedException {

1018         if (Thread.interrupted())
1019             throw new InterruptedException();
1020         return tryAcquire(arg) ||
1021             doAcquireNanos(arg, nanosTimeout);
1022     }
1023 
1024     /**
1025      * Releases in exclusive mode.  Implemented by unblocking one or
1026      * more threads if {@link #tryRelease} returns true.
1027      * This method can be used to implement method {@link Lock#unlock}.
1028      *
1029      * @param arg the release argument.  This value is conveyed to
1030      *        {@link #tryRelease} but is otherwise uninterpreted and
1031      *        can represent anything you like.
1032      * @return the value returned from {@link #tryRelease}
1033      */
1034     public final boolean release(long arg) {
1035         if (tryRelease(arg)) {
1036             Node h = head;
1037             if (h != null && h.waitStatus != 0)


1053      *        and can represent anything you like.
1054      */
1055     public final void acquireShared(long arg) {
1056         if (tryAcquireShared(arg) < 0)
1057             doAcquireShared(arg);
1058     }
1059 
1060     /**
1061      * Acquires in shared mode, aborting if interrupted.  Implemented
1062      * by first checking interrupt status, then invoking at least once
1063      * {@link #tryAcquireShared}, returning on success.  Otherwise the
1064      * thread is queued, possibly repeatedly blocking and unblocking,
1065      * invoking {@link #tryAcquireShared} until success or the thread
1066      * is interrupted.
1067      * @param arg the acquire argument
1068      * This value is conveyed to {@link #tryAcquireShared} but is
1069      * otherwise uninterpreted and can represent anything
1070      * you like.
1071      * @throws InterruptedException if the current thread is interrupted
1072      */
1073     public final void acquireSharedInterruptibly(long arg) throws InterruptedException {

1074         if (Thread.interrupted())
1075             throw new InterruptedException();
1076         if (tryAcquireShared(arg) < 0)
1077             doAcquireSharedInterruptibly(arg);
1078     }
1079 
1080     /**
1081      * Attempts to acquire in shared mode, aborting if interrupted, and
1082      * failing if the given timeout elapses.  Implemented by first
1083      * checking interrupt status, then invoking at least once {@link
1084      * #tryAcquireShared}, returning on success.  Otherwise, the
1085      * thread is queued, possibly repeatedly blocking and unblocking,
1086      * invoking {@link #tryAcquireShared} until success or the thread
1087      * is interrupted or the timeout elapses.
1088      *
1089      * @param arg the acquire argument.  This value is conveyed to
1090      *        {@link #tryAcquireShared} but is otherwise uninterpreted
1091      *        and can represent anything you like.
1092      * @param nanosTimeout the maximum number of nanoseconds to wait
1093      * @return {@code true} if acquired; {@code false} if timed out
1094      * @throws InterruptedException if the current thread is interrupted
1095      */
1096     public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) throws InterruptedException {

1097         if (Thread.interrupted())
1098             throw new InterruptedException();
1099         return tryAcquireShared(arg) >= 0 ||
1100             doAcquireSharedNanos(arg, nanosTimeout);
1101     }
1102 
1103     /**
1104      * Releases in shared mode.  Implemented by unblocking one or more
1105      * threads if {@link #tryReleaseShared} returns true.
1106      *
1107      * @param arg the release argument.  This value is conveyed to
1108      *        {@link #tryReleaseShared} but is otherwise uninterpreted
1109      *        and can represent anything you like.
1110      * @return the value returned from {@link #tryReleaseShared}
1111      */
1112     public final boolean releaseShared(long arg) {
1113         if (tryReleaseShared(arg)) {
1114             doReleaseShared();
1115             return true;
1116         }


1824             if (node.nextWaiter != null) // clean up if cancelled
1825                 unlinkCancelledWaiters();
1826             if (interruptMode != 0)
1827                 reportInterruptAfterWait(interruptMode);
1828         }
1829 
1830         /**
1831          * Implements timed condition wait.
1832          * <ol>
1833          * <li> If current thread is interrupted, throw InterruptedException.
1834          * <li> Save lock state returned by {@link #getState}.
1835          * <li> Invoke {@link #release} with
1836          *      saved state as argument, throwing
1837          *      IllegalMonitorStateException if it fails.
1838          * <li> Block until signalled, interrupted, or timed out.
1839          * <li> Reacquire by invoking specialized version of
1840          *      {@link #acquire} with saved state as argument.
1841          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1842          * </ol>
1843          */
1844         public final long awaitNanos(long nanosTimeout) throws InterruptedException {

1845             if (Thread.interrupted())
1846                 throw new InterruptedException();
1847             Node node = addConditionWaiter();
1848             long savedState = fullyRelease(node);
1849             long lastTime = System.nanoTime();
1850             int interruptMode = 0;
1851             while (!isOnSyncQueue(node)) {
1852                 if (nanosTimeout <= 0L) {
1853                     transferAfterCancelledWait(node);
1854                     break;
1855                 }
1856                 LockSupport.parkNanos(this, nanosTimeout);
1857                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1858                     break;
1859 
1860                 long now = System.nanoTime();
1861                 nanosTimeout -= now - lastTime;
1862                 lastTime = now;
1863             }
1864             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)


1868             if (interruptMode != 0)
1869                 reportInterruptAfterWait(interruptMode);
1870             return nanosTimeout - (System.nanoTime() - lastTime);
1871         }
1872 
1873         /**
1874          * Implements absolute timed condition wait.
1875          * <ol>
1876          * <li> If current thread is interrupted, throw InterruptedException.
1877          * <li> Save lock state returned by {@link #getState}.
1878          * <li> Invoke {@link #release} with
1879          *      saved state as argument, throwing
1880          *      IllegalMonitorStateException if it fails.
1881          * <li> Block until signalled, interrupted, or timed out.
1882          * <li> Reacquire by invoking specialized version of
1883          *      {@link #acquire} with saved state as argument.
1884          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1885          * <li> If timed out while blocked in step 4, return false, else true.
1886          * </ol>
1887          */
1888         public final boolean awaitUntil(Date deadline) throws InterruptedException {

1889             if (deadline == null)
1890                 throw new NullPointerException();
1891             long abstime = deadline.getTime();
1892             if (Thread.interrupted())
1893                 throw new InterruptedException();
1894             Node node = addConditionWaiter();
1895             long savedState = fullyRelease(node);
1896             boolean timedout = false;
1897             int interruptMode = 0;
1898             while (!isOnSyncQueue(node)) {
1899                 if (System.currentTimeMillis() > abstime) {
1900                     timedout = transferAfterCancelledWait(node);
1901                     break;
1902                 }
1903                 LockSupport.parkUntil(this, abstime);
1904                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1905                     break;
1906             }
1907             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
1908                 interruptMode = REINTERRUPT;


1911             if (interruptMode != 0)
1912                 reportInterruptAfterWait(interruptMode);
1913             return !timedout;
1914         }
1915 
1916         /**
1917          * Implements timed condition wait.
1918          * <ol>
1919          * <li> If current thread is interrupted, throw InterruptedException.
1920          * <li> Save lock state returned by {@link #getState}.
1921          * <li> Invoke {@link #release} with
1922          *      saved state as argument, throwing
1923          *      IllegalMonitorStateException if it fails.
1924          * <li> Block until signalled, interrupted, or timed out.
1925          * <li> Reacquire by invoking specialized version of
1926          *      {@link #acquire} with saved state as argument.
1927          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1928          * <li> If timed out while blocked in step 4, return false, else true.
1929          * </ol>
1930          */
1931         public final boolean await(long time, TimeUnit unit) throws InterruptedException {

1932             if (unit == null)
1933                 throw new NullPointerException();
1934             long nanosTimeout = unit.toNanos(time);
1935             if (Thread.interrupted())
1936                 throw new InterruptedException();
1937             Node node = addConditionWaiter();
1938             long savedState = fullyRelease(node);
1939             long lastTime = System.nanoTime();
1940             boolean timedout = false;
1941             int interruptMode = 0;
1942             while (!isOnSyncQueue(node)) {
1943                 if (nanosTimeout <= 0L) {
1944                     timedout = transferAfterCancelledWait(node);
1945                     break;
1946                 }
1947                 if (nanosTimeout >= spinForTimeoutThreshold)
1948                     LockSupport.parkNanos(this, nanosTimeout);
1949                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1950                     break;
1951                 long now = System.nanoTime();


2067         } catch (Exception ex) { throw new Error(ex); }
2068     }
2069 
2070     /**
2071      * CAS head field. Used only by enq.
2072      */
2073     private final boolean compareAndSetHead(Node update) {
2074         return unsafe.compareAndSwapObject(this, headOffset, null, update);
2075     }
2076 
2077     /**
2078      * CAS tail field. Used only by enq.
2079      */
2080     private final boolean compareAndSetTail(Node expect, Node update) {
2081         return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
2082     }
2083 
2084     /**
2085      * CAS waitStatus field of a node.
2086      */
2087     private final static boolean compareAndSetWaitStatus(Node node,
2088                                                          int expect,
2089                                                          int update) {
2090         return unsafe.compareAndSwapInt(node, waitStatusOffset,
2091                                         expect, update);
2092     }
2093 
2094     /**
2095      * CAS next field of a node.
2096      */
2097     private final static boolean compareAndSetNext(Node node,
2098                                                    Node expect,
2099                                                    Node update) {
2100         return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
2101     }
2102 }


 973     public final void acquire(long arg) {
 974         if (!tryAcquire(arg) &&
 975             acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
 976             selfInterrupt();
 977     }
 978 
 979     /**
 980      * Acquires in exclusive mode, aborting if interrupted.
 981      * Implemented by first checking interrupt status, then invoking
 982      * at least once {@link #tryAcquire}, returning on
 983      * success.  Otherwise the thread is queued, possibly repeatedly
 984      * blocking and unblocking, invoking {@link #tryAcquire}
 985      * until success or the thread is interrupted.  This method can be
 986      * used to implement method {@link Lock#lockInterruptibly}.
 987      *
 988      * @param arg the acquire argument.  This value is conveyed to
 989      *        {@link #tryAcquire} but is otherwise uninterpreted and
 990      *        can represent anything you like.
 991      * @throws InterruptedException if the current thread is interrupted
 992      */
 993     public final void acquireInterruptibly(long arg)
 994             throws InterruptedException {
 995         if (Thread.interrupted())
 996             throw new InterruptedException();
 997         if (!tryAcquire(arg))
 998             doAcquireInterruptibly(arg);
 999     }
1000 
1001     /**
1002      * Attempts to acquire in exclusive mode, aborting if interrupted,
1003      * and failing if the given timeout elapses.  Implemented by first
1004      * checking interrupt status, then invoking at least once {@link
1005      * #tryAcquire}, returning on success.  Otherwise, the thread is
1006      * queued, possibly repeatedly blocking and unblocking, invoking
1007      * {@link #tryAcquire} until success or the thread is interrupted
1008      * or the timeout elapses.  This method can be used to implement
1009      * method {@link Lock#tryLock(long, TimeUnit)}.
1010      *
1011      * @param arg the acquire argument.  This value is conveyed to
1012      *        {@link #tryAcquire} but is otherwise uninterpreted and
1013      *        can represent anything you like.
1014      * @param nanosTimeout the maximum number of nanoseconds to wait
1015      * @return {@code true} if acquired; {@code false} if timed out
1016      * @throws InterruptedException if the current thread is interrupted
1017      */
1018     public final boolean tryAcquireNanos(long arg, long nanosTimeout)
1019             throws InterruptedException {
1020         if (Thread.interrupted())
1021             throw new InterruptedException();
1022         return tryAcquire(arg) ||
1023             doAcquireNanos(arg, nanosTimeout);
1024     }
1025 
1026     /**
1027      * Releases in exclusive mode.  Implemented by unblocking one or
1028      * more threads if {@link #tryRelease} returns true.
1029      * This method can be used to implement method {@link Lock#unlock}.
1030      *
1031      * @param arg the release argument.  This value is conveyed to
1032      *        {@link #tryRelease} but is otherwise uninterpreted and
1033      *        can represent anything you like.
1034      * @return the value returned from {@link #tryRelease}
1035      */
1036     public final boolean release(long arg) {
1037         if (tryRelease(arg)) {
1038             Node h = head;
1039             if (h != null && h.waitStatus != 0)


1055      *        and can represent anything you like.
1056      */
1057     public final void acquireShared(long arg) {
1058         if (tryAcquireShared(arg) < 0)
1059             doAcquireShared(arg);
1060     }
1061 
1062     /**
1063      * Acquires in shared mode, aborting if interrupted.  Implemented
1064      * by first checking interrupt status, then invoking at least once
1065      * {@link #tryAcquireShared}, returning on success.  Otherwise the
1066      * thread is queued, possibly repeatedly blocking and unblocking,
1067      * invoking {@link #tryAcquireShared} until success or the thread
1068      * is interrupted.
1069      * @param arg the acquire argument
1070      * This value is conveyed to {@link #tryAcquireShared} but is
1071      * otherwise uninterpreted and can represent anything
1072      * you like.
1073      * @throws InterruptedException if the current thread is interrupted
1074      */
1075     public final void acquireSharedInterruptibly(long arg)
1076             throws InterruptedException {
1077         if (Thread.interrupted())
1078             throw new InterruptedException();
1079         if (tryAcquireShared(arg) < 0)
1080             doAcquireSharedInterruptibly(arg);
1081     }
1082 
1083     /**
1084      * Attempts to acquire in shared mode, aborting if interrupted, and
1085      * failing if the given timeout elapses.  Implemented by first
1086      * checking interrupt status, then invoking at least once {@link
1087      * #tryAcquireShared}, returning on success.  Otherwise, the
1088      * thread is queued, possibly repeatedly blocking and unblocking,
1089      * invoking {@link #tryAcquireShared} until success or the thread
1090      * is interrupted or the timeout elapses.
1091      *
1092      * @param arg the acquire argument.  This value is conveyed to
1093      *        {@link #tryAcquireShared} but is otherwise uninterpreted
1094      *        and can represent anything you like.
1095      * @param nanosTimeout the maximum number of nanoseconds to wait
1096      * @return {@code true} if acquired; {@code false} if timed out
1097      * @throws InterruptedException if the current thread is interrupted
1098      */
1099     public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout)
1100             throws InterruptedException {
1101         if (Thread.interrupted())
1102             throw new InterruptedException();
1103         return tryAcquireShared(arg) >= 0 ||
1104             doAcquireSharedNanos(arg, nanosTimeout);
1105     }
1106 
1107     /**
1108      * Releases in shared mode.  Implemented by unblocking one or more
1109      * threads if {@link #tryReleaseShared} returns true.
1110      *
1111      * @param arg the release argument.  This value is conveyed to
1112      *        {@link #tryReleaseShared} but is otherwise uninterpreted
1113      *        and can represent anything you like.
1114      * @return the value returned from {@link #tryReleaseShared}
1115      */
1116     public final boolean releaseShared(long arg) {
1117         if (tryReleaseShared(arg)) {
1118             doReleaseShared();
1119             return true;
1120         }


1828             if (node.nextWaiter != null) // clean up if cancelled
1829                 unlinkCancelledWaiters();
1830             if (interruptMode != 0)
1831                 reportInterruptAfterWait(interruptMode);
1832         }
1833 
1834         /**
1835          * Implements timed condition wait.
1836          * <ol>
1837          * <li> If current thread is interrupted, throw InterruptedException.
1838          * <li> Save lock state returned by {@link #getState}.
1839          * <li> Invoke {@link #release} with
1840          *      saved state as argument, throwing
1841          *      IllegalMonitorStateException if it fails.
1842          * <li> Block until signalled, interrupted, or timed out.
1843          * <li> Reacquire by invoking specialized version of
1844          *      {@link #acquire} with saved state as argument.
1845          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1846          * </ol>
1847          */
1848         public final long awaitNanos(long nanosTimeout)
1849                 throws InterruptedException {
1850             if (Thread.interrupted())
1851                 throw new InterruptedException();
1852             Node node = addConditionWaiter();
1853             long savedState = fullyRelease(node);
1854             long lastTime = System.nanoTime();
1855             int interruptMode = 0;
1856             while (!isOnSyncQueue(node)) {
1857                 if (nanosTimeout <= 0L) {
1858                     transferAfterCancelledWait(node);
1859                     break;
1860                 }
1861                 LockSupport.parkNanos(this, nanosTimeout);
1862                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1863                     break;
1864 
1865                 long now = System.nanoTime();
1866                 nanosTimeout -= now - lastTime;
1867                 lastTime = now;
1868             }
1869             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)


1873             if (interruptMode != 0)
1874                 reportInterruptAfterWait(interruptMode);
1875             return nanosTimeout - (System.nanoTime() - lastTime);
1876         }
1877 
1878         /**
1879          * Implements absolute timed condition wait.
1880          * <ol>
1881          * <li> If current thread is interrupted, throw InterruptedException.
1882          * <li> Save lock state returned by {@link #getState}.
1883          * <li> Invoke {@link #release} with
1884          *      saved state as argument, throwing
1885          *      IllegalMonitorStateException if it fails.
1886          * <li> Block until signalled, interrupted, or timed out.
1887          * <li> Reacquire by invoking specialized version of
1888          *      {@link #acquire} with saved state as argument.
1889          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1890          * <li> If timed out while blocked in step 4, return false, else true.
1891          * </ol>
1892          */
1893         public final boolean awaitUntil(Date deadline)
1894                 throws InterruptedException {
1895             if (deadline == null)
1896                 throw new NullPointerException();
1897             long abstime = deadline.getTime();
1898             if (Thread.interrupted())
1899                 throw new InterruptedException();
1900             Node node = addConditionWaiter();
1901             long savedState = fullyRelease(node);
1902             boolean timedout = false;
1903             int interruptMode = 0;
1904             while (!isOnSyncQueue(node)) {
1905                 if (System.currentTimeMillis() > abstime) {
1906                     timedout = transferAfterCancelledWait(node);
1907                     break;
1908                 }
1909                 LockSupport.parkUntil(this, abstime);
1910                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1911                     break;
1912             }
1913             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
1914                 interruptMode = REINTERRUPT;


1917             if (interruptMode != 0)
1918                 reportInterruptAfterWait(interruptMode);
1919             return !timedout;
1920         }
1921 
1922         /**
1923          * Implements timed condition wait.
1924          * <ol>
1925          * <li> If current thread is interrupted, throw InterruptedException.
1926          * <li> Save lock state returned by {@link #getState}.
1927          * <li> Invoke {@link #release} with
1928          *      saved state as argument, throwing
1929          *      IllegalMonitorStateException if it fails.
1930          * <li> Block until signalled, interrupted, or timed out.
1931          * <li> Reacquire by invoking specialized version of
1932          *      {@link #acquire} with saved state as argument.
1933          * <li> If interrupted while blocked in step 4, throw InterruptedException.
1934          * <li> If timed out while blocked in step 4, return false, else true.
1935          * </ol>
1936          */
1937         public final boolean await(long time, TimeUnit unit)
1938                 throws InterruptedException {
1939             if (unit == null)
1940                 throw new NullPointerException();
1941             long nanosTimeout = unit.toNanos(time);
1942             if (Thread.interrupted())
1943                 throw new InterruptedException();
1944             Node node = addConditionWaiter();
1945             long savedState = fullyRelease(node);
1946             long lastTime = System.nanoTime();
1947             boolean timedout = false;
1948             int interruptMode = 0;
1949             while (!isOnSyncQueue(node)) {
1950                 if (nanosTimeout <= 0L) {
1951                     timedout = transferAfterCancelledWait(node);
1952                     break;
1953                 }
1954                 if (nanosTimeout >= spinForTimeoutThreshold)
1955                     LockSupport.parkNanos(this, nanosTimeout);
1956                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
1957                     break;
1958                 long now = System.nanoTime();


2074         } catch (Exception ex) { throw new Error(ex); }
2075     }
2076 
2077     /**
2078      * CAS head field. Used only by enq.
2079      */
2080     private final boolean compareAndSetHead(Node update) {
2081         return unsafe.compareAndSwapObject(this, headOffset, null, update);
2082     }
2083 
2084     /**
2085      * CAS tail field. Used only by enq.
2086      */
2087     private final boolean compareAndSetTail(Node expect, Node update) {
2088         return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
2089     }
2090 
2091     /**
2092      * CAS waitStatus field of a node.
2093      */
2094     private static final boolean compareAndSetWaitStatus(Node node,
2095                                                          int expect,
2096                                                          int update) {
2097         return unsafe.compareAndSwapInt(node, waitStatusOffset,
2098                                         expect, update);
2099     }
2100 
2101     /**
2102      * CAS next field of a node.
2103      */
2104     private static final boolean compareAndSetNext(Node node,
2105                                                    Node expect,
2106                                                    Node update) {
2107         return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
2108     }
2109 }