src/java.base/share/classes/java/util/concurrent/FutureTask.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


 467             retry:
 468             for (;;) {          // restart on removeWaiter race
 469                 for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
 470                     s = q.next;
 471                     if (q.thread != null)
 472                         pred = q;
 473                     else if (pred != null) {
 474                         pred.next = s;
 475                         if (pred.thread == null) // check for race
 476                             continue retry;
 477                     }
 478                     else if (!U.compareAndSwapObject(this, WAITERS, q, s))
 479                         continue retry;
 480                 }
 481                 break;
 482             }
 483         }
 484     }
 485 
 486     // Unsafe mechanics
 487     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 488     private static final long STATE;
 489     private static final long RUNNER;
 490     private static final long WAITERS;
 491     static {
 492         try {
 493             STATE = U.objectFieldOffset
 494                 (FutureTask.class.getDeclaredField("state"));
 495             RUNNER = U.objectFieldOffset
 496                 (FutureTask.class.getDeclaredField("runner"));
 497             WAITERS = U.objectFieldOffset
 498                 (FutureTask.class.getDeclaredField("waiters"));
 499         } catch (ReflectiveOperationException e) {
 500             throw new Error(e);
 501         }
 502 
 503         // Reduce the risk of rare disastrous classloading in first call to
 504         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
 505         Class<?> ensureLoaded = LockSupport.class;
 506     }
 507 


 467             retry:
 468             for (;;) {          // restart on removeWaiter race
 469                 for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
 470                     s = q.next;
 471                     if (q.thread != null)
 472                         pred = q;
 473                     else if (pred != null) {
 474                         pred.next = s;
 475                         if (pred.thread == null) // check for race
 476                             continue retry;
 477                     }
 478                     else if (!U.compareAndSwapObject(this, WAITERS, q, s))
 479                         continue retry;
 480                 }
 481                 break;
 482             }
 483         }
 484     }
 485 
 486     // Unsafe mechanics
 487     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 488     private static final long STATE;
 489     private static final long RUNNER;
 490     private static final long WAITERS;
 491     static {
 492         try {
 493             STATE = U.objectFieldOffset
 494                 (FutureTask.class.getDeclaredField("state"));
 495             RUNNER = U.objectFieldOffset
 496                 (FutureTask.class.getDeclaredField("runner"));
 497             WAITERS = U.objectFieldOffset
 498                 (FutureTask.class.getDeclaredField("waiters"));
 499         } catch (ReflectiveOperationException e) {
 500             throw new Error(e);
 501         }
 502 
 503         // Reduce the risk of rare disastrous classloading in first call to
 504         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
 505         Class<?> ensureLoaded = LockSupport.class;
 506     }
 507