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

Print this page




 248  *   }
 249  *   public boolean tryLock(long timeout, TimeUnit unit)
 250  *       throws InterruptedException {
 251  *     return sync.tryAcquireNanos(1, unit.toNanos(timeout));
 252  *   }
 253  * }
 254  * </pre>
 255  *
 256  * <p>Here is a latch class that is like a {@link CountDownLatch}
 257  * except that it only requires a single <tt>signal</tt> to
 258  * fire. Because a latch is non-exclusive, it uses the <tt>shared</tt>
 259  * acquire and release methods.
 260  *
 261  * <pre>
 262  * class BooleanLatch {
 263  *
 264  *   private static class Sync extends AbstractQueuedSynchronizer {
 265  *     boolean isSignalled() { return getState() != 0; }
 266  *
 267  *     protected int tryAcquireShared(int ignore) {
 268  *       return isSignalled()? 1 : -1;
 269  *     }
 270  *
 271  *     protected boolean tryReleaseShared(int ignore) {
 272  *       setState(1);
 273  *       return true;
 274  *     }
 275  *   }
 276  *
 277  *   private final Sync sync = new Sync();
 278  *   public boolean isSignalled() { return sync.isSignalled(); }
 279  *   public void signal()         { sync.releaseShared(1); }
 280  *   public void await() throws InterruptedException {
 281  *     sync.acquireSharedInterruptibly(1);
 282  *   }
 283  * }
 284  * </pre>
 285  *
 286  * @since 1.5
 287  * @author Doug Lea
 288  */


1196     public final void acquire(int arg) {
1197         if (!tryAcquire(arg) &&
1198             acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
1199             selfInterrupt();
1200     }
1201 
1202     /**
1203      * Acquires in exclusive mode, aborting if interrupted.
1204      * Implemented by first checking interrupt status, then invoking
1205      * at least once {@link #tryAcquire}, returning on
1206      * success.  Otherwise the thread is queued, possibly repeatedly
1207      * blocking and unblocking, invoking {@link #tryAcquire}
1208      * until success or the thread is interrupted.  This method can be
1209      * used to implement method {@link Lock#lockInterruptibly}.
1210      *
1211      * @param arg the acquire argument.  This value is conveyed to
1212      *        {@link #tryAcquire} but is otherwise uninterpreted and
1213      *        can represent anything you like.
1214      * @throws InterruptedException if the current thread is interrupted
1215      */
1216     public final void acquireInterruptibly(int arg) throws InterruptedException {

1217         if (Thread.interrupted())
1218             throw new InterruptedException();
1219         if (!tryAcquire(arg))
1220             doAcquireInterruptibly(arg);
1221     }
1222 
1223     /**
1224      * Attempts to acquire in exclusive mode, aborting if interrupted,
1225      * and failing if the given timeout elapses.  Implemented by first
1226      * checking interrupt status, then invoking at least once {@link
1227      * #tryAcquire}, returning on success.  Otherwise, the thread is
1228      * queued, possibly repeatedly blocking and unblocking, invoking
1229      * {@link #tryAcquire} until success or the thread is interrupted
1230      * or the timeout elapses.  This method can be used to implement
1231      * method {@link Lock#tryLock(long, TimeUnit)}.
1232      *
1233      * @param arg the acquire argument.  This value is conveyed to
1234      *        {@link #tryAcquire} but is otherwise uninterpreted and
1235      *        can represent anything you like.
1236      * @param nanosTimeout the maximum number of nanoseconds to wait
1237      * @return {@code true} if acquired; {@code false} if timed out
1238      * @throws InterruptedException if the current thread is interrupted
1239      */
1240     public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException {

1241         if (Thread.interrupted())
1242             throw new InterruptedException();
1243         return tryAcquire(arg) ||
1244             doAcquireNanos(arg, nanosTimeout);
1245     }
1246 
1247     /**
1248      * Releases in exclusive mode.  Implemented by unblocking one or
1249      * more threads if {@link #tryRelease} returns true.
1250      * This method can be used to implement method {@link Lock#unlock}.
1251      *
1252      * @param arg the release argument.  This value is conveyed to
1253      *        {@link #tryRelease} but is otherwise uninterpreted and
1254      *        can represent anything you like.
1255      * @return the value returned from {@link #tryRelease}
1256      */
1257     public final boolean release(int arg) {
1258         if (tryRelease(arg)) {
1259             Node h = head;
1260             if (h != null && h.waitStatus != 0)


1276      *        and can represent anything you like.
1277      */
1278     public final void acquireShared(int arg) {
1279         if (tryAcquireShared(arg) < 0)
1280             doAcquireShared(arg);
1281     }
1282 
1283     /**
1284      * Acquires in shared mode, aborting if interrupted.  Implemented
1285      * by first checking interrupt status, then invoking at least once
1286      * {@link #tryAcquireShared}, returning on success.  Otherwise the
1287      * thread is queued, possibly repeatedly blocking and unblocking,
1288      * invoking {@link #tryAcquireShared} until success or the thread
1289      * is interrupted.
1290      * @param arg the acquire argument
1291      * This value is conveyed to {@link #tryAcquireShared} but is
1292      * otherwise uninterpreted and can represent anything
1293      * you like.
1294      * @throws InterruptedException if the current thread is interrupted
1295      */
1296     public final void acquireSharedInterruptibly(int arg) throws InterruptedException {

1297         if (Thread.interrupted())
1298             throw new InterruptedException();
1299         if (tryAcquireShared(arg) < 0)
1300             doAcquireSharedInterruptibly(arg);
1301     }
1302 
1303     /**
1304      * Attempts to acquire in shared mode, aborting if interrupted, and
1305      * failing if the given timeout elapses.  Implemented by first
1306      * checking interrupt status, then invoking at least once {@link
1307      * #tryAcquireShared}, returning on success.  Otherwise, the
1308      * thread is queued, possibly repeatedly blocking and unblocking,
1309      * invoking {@link #tryAcquireShared} until success or the thread
1310      * is interrupted or the timeout elapses.
1311      *
1312      * @param arg the acquire argument.  This value is conveyed to
1313      *        {@link #tryAcquireShared} but is otherwise uninterpreted
1314      *        and can represent anything you like.
1315      * @param nanosTimeout the maximum number of nanoseconds to wait
1316      * @return {@code true} if acquired; {@code false} if timed out
1317      * @throws InterruptedException if the current thread is interrupted
1318      */
1319     public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException {

1320         if (Thread.interrupted())
1321             throw new InterruptedException();
1322         return tryAcquireShared(arg) >= 0 ||
1323             doAcquireSharedNanos(arg, nanosTimeout);
1324     }
1325 
1326     /**
1327      * Releases in shared mode.  Implemented by unblocking one or more
1328      * threads if {@link #tryReleaseShared} returns true.
1329      *
1330      * @param arg the release argument.  This value is conveyed to
1331      *        {@link #tryReleaseShared} but is otherwise uninterpreted
1332      *        and can represent anything you like.
1333      * @return the value returned from {@link #tryReleaseShared}
1334      */
1335     public final boolean releaseShared(int arg) {
1336         if (tryReleaseShared(arg)) {
1337             doReleaseShared();
1338             return true;
1339         }


2045             if (node.nextWaiter != null) // clean up if cancelled
2046                 unlinkCancelledWaiters();
2047             if (interruptMode != 0)
2048                 reportInterruptAfterWait(interruptMode);
2049         }
2050 
2051         /**
2052          * Implements timed condition wait.
2053          * <ol>
2054          * <li> If current thread is interrupted, throw InterruptedException.
2055          * <li> Save lock state returned by {@link #getState}.
2056          * <li> Invoke {@link #release} with
2057          *      saved state as argument, throwing
2058          *      IllegalMonitorStateException if it fails.
2059          * <li> Block until signalled, interrupted, or timed out.
2060          * <li> Reacquire by invoking specialized version of
2061          *      {@link #acquire} with saved state as argument.
2062          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2063          * </ol>
2064          */
2065         public final long awaitNanos(long nanosTimeout) throws InterruptedException {

2066             if (Thread.interrupted())
2067                 throw new InterruptedException();
2068             Node node = addConditionWaiter();
2069             int savedState = fullyRelease(node);
2070             long lastTime = System.nanoTime();
2071             int interruptMode = 0;
2072             while (!isOnSyncQueue(node)) {
2073                 if (nanosTimeout <= 0L) {
2074                     transferAfterCancelledWait(node);
2075                     break;
2076                 }
2077                 LockSupport.parkNanos(this, nanosTimeout);
2078                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2079                     break;
2080 
2081                 long now = System.nanoTime();
2082                 nanosTimeout -= now - lastTime;
2083                 lastTime = now;
2084             }
2085             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)


2089             if (interruptMode != 0)
2090                 reportInterruptAfterWait(interruptMode);
2091             return nanosTimeout - (System.nanoTime() - lastTime);
2092         }
2093 
2094         /**
2095          * Implements absolute timed condition wait.
2096          * <ol>
2097          * <li> If current thread is interrupted, throw InterruptedException.
2098          * <li> Save lock state returned by {@link #getState}.
2099          * <li> Invoke {@link #release} with
2100          *      saved state as argument, throwing
2101          *      IllegalMonitorStateException if it fails.
2102          * <li> Block until signalled, interrupted, or timed out.
2103          * <li> Reacquire by invoking specialized version of
2104          *      {@link #acquire} with saved state as argument.
2105          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2106          * <li> If timed out while blocked in step 4, return false, else true.
2107          * </ol>
2108          */
2109         public final boolean awaitUntil(Date deadline) throws InterruptedException {

2110             if (deadline == null)
2111                 throw new NullPointerException();
2112             long abstime = deadline.getTime();
2113             if (Thread.interrupted())
2114                 throw new InterruptedException();
2115             Node node = addConditionWaiter();
2116             int savedState = fullyRelease(node);
2117             boolean timedout = false;
2118             int interruptMode = 0;
2119             while (!isOnSyncQueue(node)) {
2120                 if (System.currentTimeMillis() > abstime) {
2121                     timedout = transferAfterCancelledWait(node);
2122                     break;
2123                 }
2124                 LockSupport.parkUntil(this, abstime);
2125                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2126                     break;
2127             }
2128             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
2129                 interruptMode = REINTERRUPT;


2132             if (interruptMode != 0)
2133                 reportInterruptAfterWait(interruptMode);
2134             return !timedout;
2135         }
2136 
2137         /**
2138          * Implements timed condition wait.
2139          * <ol>
2140          * <li> If current thread is interrupted, throw InterruptedException.
2141          * <li> Save lock state returned by {@link #getState}.
2142          * <li> Invoke {@link #release} with
2143          *      saved state as argument, throwing
2144          *      IllegalMonitorStateException if it fails.
2145          * <li> Block until signalled, interrupted, or timed out.
2146          * <li> Reacquire by invoking specialized version of
2147          *      {@link #acquire} with saved state as argument.
2148          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2149          * <li> If timed out while blocked in step 4, return false, else true.
2150          * </ol>
2151          */
2152         public final boolean await(long time, TimeUnit unit) throws InterruptedException {

2153             if (unit == null)
2154                 throw new NullPointerException();
2155             long nanosTimeout = unit.toNanos(time);
2156             if (Thread.interrupted())
2157                 throw new InterruptedException();
2158             Node node = addConditionWaiter();
2159             int savedState = fullyRelease(node);
2160             long lastTime = System.nanoTime();
2161             boolean timedout = false;
2162             int interruptMode = 0;
2163             while (!isOnSyncQueue(node)) {
2164                 if (nanosTimeout <= 0L) {
2165                     timedout = transferAfterCancelledWait(node);
2166                     break;
2167                 }
2168                 if (nanosTimeout >= spinForTimeoutThreshold)
2169                     LockSupport.parkNanos(this, nanosTimeout);
2170                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2171                     break;
2172                 long now = System.nanoTime();


2288         } catch (Exception ex) { throw new Error(ex); }
2289     }
2290 
2291     /**
2292      * CAS head field. Used only by enq.
2293      */
2294     private final boolean compareAndSetHead(Node update) {
2295         return unsafe.compareAndSwapObject(this, headOffset, null, update);
2296     }
2297 
2298     /**
2299      * CAS tail field. Used only by enq.
2300      */
2301     private final boolean compareAndSetTail(Node expect, Node update) {
2302         return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
2303     }
2304 
2305     /**
2306      * CAS waitStatus field of a node.
2307      */
2308     private final static boolean compareAndSetWaitStatus(Node node,
2309                                                          int expect,
2310                                                          int update) {
2311         return unsafe.compareAndSwapInt(node, waitStatusOffset,
2312                                         expect, update);
2313     }
2314 
2315     /**
2316      * CAS next field of a node.
2317      */
2318     private final static boolean compareAndSetNext(Node node,
2319                                                    Node expect,
2320                                                    Node update) {
2321         return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
2322     }
2323 }


 248  *   }
 249  *   public boolean tryLock(long timeout, TimeUnit unit)
 250  *       throws InterruptedException {
 251  *     return sync.tryAcquireNanos(1, unit.toNanos(timeout));
 252  *   }
 253  * }
 254  * </pre>
 255  *
 256  * <p>Here is a latch class that is like a {@link CountDownLatch}
 257  * except that it only requires a single <tt>signal</tt> to
 258  * fire. Because a latch is non-exclusive, it uses the <tt>shared</tt>
 259  * acquire and release methods.
 260  *
 261  * <pre>
 262  * class BooleanLatch {
 263  *
 264  *   private static class Sync extends AbstractQueuedSynchronizer {
 265  *     boolean isSignalled() { return getState() != 0; }
 266  *
 267  *     protected int tryAcquireShared(int ignore) {
 268  *       return isSignalled() ? 1 : -1;
 269  *     }
 270  *
 271  *     protected boolean tryReleaseShared(int ignore) {
 272  *       setState(1);
 273  *       return true;
 274  *     }
 275  *   }
 276  *
 277  *   private final Sync sync = new Sync();
 278  *   public boolean isSignalled() { return sync.isSignalled(); }
 279  *   public void signal()         { sync.releaseShared(1); }
 280  *   public void await() throws InterruptedException {
 281  *     sync.acquireSharedInterruptibly(1);
 282  *   }
 283  * }
 284  * </pre>
 285  *
 286  * @since 1.5
 287  * @author Doug Lea
 288  */


1196     public final void acquire(int arg) {
1197         if (!tryAcquire(arg) &&
1198             acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
1199             selfInterrupt();
1200     }
1201 
1202     /**
1203      * Acquires in exclusive mode, aborting if interrupted.
1204      * Implemented by first checking interrupt status, then invoking
1205      * at least once {@link #tryAcquire}, returning on
1206      * success.  Otherwise the thread is queued, possibly repeatedly
1207      * blocking and unblocking, invoking {@link #tryAcquire}
1208      * until success or the thread is interrupted.  This method can be
1209      * used to implement method {@link Lock#lockInterruptibly}.
1210      *
1211      * @param arg the acquire argument.  This value is conveyed to
1212      *        {@link #tryAcquire} but is otherwise uninterpreted and
1213      *        can represent anything you like.
1214      * @throws InterruptedException if the current thread is interrupted
1215      */
1216     public final void acquireInterruptibly(int arg)
1217             throws InterruptedException {
1218         if (Thread.interrupted())
1219             throw new InterruptedException();
1220         if (!tryAcquire(arg))
1221             doAcquireInterruptibly(arg);
1222     }
1223 
1224     /**
1225      * Attempts to acquire in exclusive mode, aborting if interrupted,
1226      * and failing if the given timeout elapses.  Implemented by first
1227      * checking interrupt status, then invoking at least once {@link
1228      * #tryAcquire}, returning on success.  Otherwise, the thread is
1229      * queued, possibly repeatedly blocking and unblocking, invoking
1230      * {@link #tryAcquire} until success or the thread is interrupted
1231      * or the timeout elapses.  This method can be used to implement
1232      * method {@link Lock#tryLock(long, TimeUnit)}.
1233      *
1234      * @param arg the acquire argument.  This value is conveyed to
1235      *        {@link #tryAcquire} but is otherwise uninterpreted and
1236      *        can represent anything you like.
1237      * @param nanosTimeout the maximum number of nanoseconds to wait
1238      * @return {@code true} if acquired; {@code false} if timed out
1239      * @throws InterruptedException if the current thread is interrupted
1240      */
1241     public final boolean tryAcquireNanos(int arg, long nanosTimeout)
1242             throws InterruptedException {
1243         if (Thread.interrupted())
1244             throw new InterruptedException();
1245         return tryAcquire(arg) ||
1246             doAcquireNanos(arg, nanosTimeout);
1247     }
1248 
1249     /**
1250      * Releases in exclusive mode.  Implemented by unblocking one or
1251      * more threads if {@link #tryRelease} returns true.
1252      * This method can be used to implement method {@link Lock#unlock}.
1253      *
1254      * @param arg the release argument.  This value is conveyed to
1255      *        {@link #tryRelease} but is otherwise uninterpreted and
1256      *        can represent anything you like.
1257      * @return the value returned from {@link #tryRelease}
1258      */
1259     public final boolean release(int arg) {
1260         if (tryRelease(arg)) {
1261             Node h = head;
1262             if (h != null && h.waitStatus != 0)


1278      *        and can represent anything you like.
1279      */
1280     public final void acquireShared(int arg) {
1281         if (tryAcquireShared(arg) < 0)
1282             doAcquireShared(arg);
1283     }
1284 
1285     /**
1286      * Acquires in shared mode, aborting if interrupted.  Implemented
1287      * by first checking interrupt status, then invoking at least once
1288      * {@link #tryAcquireShared}, returning on success.  Otherwise the
1289      * thread is queued, possibly repeatedly blocking and unblocking,
1290      * invoking {@link #tryAcquireShared} until success or the thread
1291      * is interrupted.
1292      * @param arg the acquire argument
1293      * This value is conveyed to {@link #tryAcquireShared} but is
1294      * otherwise uninterpreted and can represent anything
1295      * you like.
1296      * @throws InterruptedException if the current thread is interrupted
1297      */
1298     public final void acquireSharedInterruptibly(int arg)
1299             throws InterruptedException {
1300         if (Thread.interrupted())
1301             throw new InterruptedException();
1302         if (tryAcquireShared(arg) < 0)
1303             doAcquireSharedInterruptibly(arg);
1304     }
1305 
1306     /**
1307      * Attempts to acquire in shared mode, aborting if interrupted, and
1308      * failing if the given timeout elapses.  Implemented by first
1309      * checking interrupt status, then invoking at least once {@link
1310      * #tryAcquireShared}, returning on success.  Otherwise, the
1311      * thread is queued, possibly repeatedly blocking and unblocking,
1312      * invoking {@link #tryAcquireShared} until success or the thread
1313      * is interrupted or the timeout elapses.
1314      *
1315      * @param arg the acquire argument.  This value is conveyed to
1316      *        {@link #tryAcquireShared} but is otherwise uninterpreted
1317      *        and can represent anything you like.
1318      * @param nanosTimeout the maximum number of nanoseconds to wait
1319      * @return {@code true} if acquired; {@code false} if timed out
1320      * @throws InterruptedException if the current thread is interrupted
1321      */
1322     public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
1323             throws InterruptedException {
1324         if (Thread.interrupted())
1325             throw new InterruptedException();
1326         return tryAcquireShared(arg) >= 0 ||
1327             doAcquireSharedNanos(arg, nanosTimeout);
1328     }
1329 
1330     /**
1331      * Releases in shared mode.  Implemented by unblocking one or more
1332      * threads if {@link #tryReleaseShared} returns true.
1333      *
1334      * @param arg the release argument.  This value is conveyed to
1335      *        {@link #tryReleaseShared} but is otherwise uninterpreted
1336      *        and can represent anything you like.
1337      * @return the value returned from {@link #tryReleaseShared}
1338      */
1339     public final boolean releaseShared(int arg) {
1340         if (tryReleaseShared(arg)) {
1341             doReleaseShared();
1342             return true;
1343         }


2049             if (node.nextWaiter != null) // clean up if cancelled
2050                 unlinkCancelledWaiters();
2051             if (interruptMode != 0)
2052                 reportInterruptAfterWait(interruptMode);
2053         }
2054 
2055         /**
2056          * Implements timed condition wait.
2057          * <ol>
2058          * <li> If current thread is interrupted, throw InterruptedException.
2059          * <li> Save lock state returned by {@link #getState}.
2060          * <li> Invoke {@link #release} with
2061          *      saved state as argument, throwing
2062          *      IllegalMonitorStateException if it fails.
2063          * <li> Block until signalled, interrupted, or timed out.
2064          * <li> Reacquire by invoking specialized version of
2065          *      {@link #acquire} with saved state as argument.
2066          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2067          * </ol>
2068          */
2069         public final long awaitNanos(long nanosTimeout)
2070                 throws InterruptedException {
2071             if (Thread.interrupted())
2072                 throw new InterruptedException();
2073             Node node = addConditionWaiter();
2074             int savedState = fullyRelease(node);
2075             long lastTime = System.nanoTime();
2076             int interruptMode = 0;
2077             while (!isOnSyncQueue(node)) {
2078                 if (nanosTimeout <= 0L) {
2079                     transferAfterCancelledWait(node);
2080                     break;
2081                 }
2082                 LockSupport.parkNanos(this, nanosTimeout);
2083                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2084                     break;
2085 
2086                 long now = System.nanoTime();
2087                 nanosTimeout -= now - lastTime;
2088                 lastTime = now;
2089             }
2090             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)


2094             if (interruptMode != 0)
2095                 reportInterruptAfterWait(interruptMode);
2096             return nanosTimeout - (System.nanoTime() - lastTime);
2097         }
2098 
2099         /**
2100          * Implements absolute timed condition wait.
2101          * <ol>
2102          * <li> If current thread is interrupted, throw InterruptedException.
2103          * <li> Save lock state returned by {@link #getState}.
2104          * <li> Invoke {@link #release} with
2105          *      saved state as argument, throwing
2106          *      IllegalMonitorStateException if it fails.
2107          * <li> Block until signalled, interrupted, or timed out.
2108          * <li> Reacquire by invoking specialized version of
2109          *      {@link #acquire} with saved state as argument.
2110          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2111          * <li> If timed out while blocked in step 4, return false, else true.
2112          * </ol>
2113          */
2114         public final boolean awaitUntil(Date deadline)
2115                 throws InterruptedException {
2116             if (deadline == null)
2117                 throw new NullPointerException();
2118             long abstime = deadline.getTime();
2119             if (Thread.interrupted())
2120                 throw new InterruptedException();
2121             Node node = addConditionWaiter();
2122             int savedState = fullyRelease(node);
2123             boolean timedout = false;
2124             int interruptMode = 0;
2125             while (!isOnSyncQueue(node)) {
2126                 if (System.currentTimeMillis() > abstime) {
2127                     timedout = transferAfterCancelledWait(node);
2128                     break;
2129                 }
2130                 LockSupport.parkUntil(this, abstime);
2131                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2132                     break;
2133             }
2134             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
2135                 interruptMode = REINTERRUPT;


2138             if (interruptMode != 0)
2139                 reportInterruptAfterWait(interruptMode);
2140             return !timedout;
2141         }
2142 
2143         /**
2144          * Implements timed condition wait.
2145          * <ol>
2146          * <li> If current thread is interrupted, throw InterruptedException.
2147          * <li> Save lock state returned by {@link #getState}.
2148          * <li> Invoke {@link #release} with
2149          *      saved state as argument, throwing
2150          *      IllegalMonitorStateException if it fails.
2151          * <li> Block until signalled, interrupted, or timed out.
2152          * <li> Reacquire by invoking specialized version of
2153          *      {@link #acquire} with saved state as argument.
2154          * <li> If interrupted while blocked in step 4, throw InterruptedException.
2155          * <li> If timed out while blocked in step 4, return false, else true.
2156          * </ol>
2157          */
2158         public final boolean await(long time, TimeUnit unit)
2159                 throws InterruptedException {
2160             if (unit == null)
2161                 throw new NullPointerException();
2162             long nanosTimeout = unit.toNanos(time);
2163             if (Thread.interrupted())
2164                 throw new InterruptedException();
2165             Node node = addConditionWaiter();
2166             int savedState = fullyRelease(node);
2167             long lastTime = System.nanoTime();
2168             boolean timedout = false;
2169             int interruptMode = 0;
2170             while (!isOnSyncQueue(node)) {
2171                 if (nanosTimeout <= 0L) {
2172                     timedout = transferAfterCancelledWait(node);
2173                     break;
2174                 }
2175                 if (nanosTimeout >= spinForTimeoutThreshold)
2176                     LockSupport.parkNanos(this, nanosTimeout);
2177                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
2178                     break;
2179                 long now = System.nanoTime();


2295         } catch (Exception ex) { throw new Error(ex); }
2296     }
2297 
2298     /**
2299      * CAS head field. Used only by enq.
2300      */
2301     private final boolean compareAndSetHead(Node update) {
2302         return unsafe.compareAndSwapObject(this, headOffset, null, update);
2303     }
2304 
2305     /**
2306      * CAS tail field. Used only by enq.
2307      */
2308     private final boolean compareAndSetTail(Node expect, Node update) {
2309         return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
2310     }
2311 
2312     /**
2313      * CAS waitStatus field of a node.
2314      */
2315     private static final boolean compareAndSetWaitStatus(Node node,
2316                                                          int expect,
2317                                                          int update) {
2318         return unsafe.compareAndSwapInt(node, waitStatusOffset,
2319                                         expect, update);
2320     }
2321 
2322     /**
2323      * CAS next field of a node.
2324      */
2325     private static final boolean compareAndSetNext(Node node,
2326                                                    Node expect,
2327                                                    Node update) {
2328         return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
2329     }
2330 }