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

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


 507             this.nextWaiter = nextWaiter;
 508             U.putObject(this, THREAD, Thread.currentThread());
 509         }
 510 
 511         /** Constructor used by addConditionWaiter. */
 512         Node(int waitStatus) {
 513             U.putInt(this, WAITSTATUS, waitStatus);
 514             U.putObject(this, THREAD, Thread.currentThread());
 515         }
 516 
 517         /** CASes waitStatus field. */
 518         final boolean compareAndSetWaitStatus(int expect, int update) {
 519             return U.compareAndSwapInt(this, WAITSTATUS, expect, update);
 520         }
 521 
 522         /** CASes next field. */
 523         final boolean compareAndSetNext(Node expect, Node update) {
 524             return U.compareAndSwapObject(this, NEXT, expect, update);
 525         }
 526 
 527         private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 528         private static final long NEXT;
 529         static final long PREV;
 530         private static final long THREAD;
 531         private static final long WAITSTATUS;
 532         static {
 533             try {
 534                 NEXT = U.objectFieldOffset
 535                     (Node.class.getDeclaredField("next"));
 536                 PREV = U.objectFieldOffset
 537                     (Node.class.getDeclaredField("prev"));
 538                 THREAD = U.objectFieldOffset
 539                     (Node.class.getDeclaredField("thread"));
 540                 WAITSTATUS = U.objectFieldOffset
 541                     (Node.class.getDeclaredField("waitStatus"));
 542             } catch (ReflectiveOperationException e) {
 543                 throw new Error(e);
 544             }
 545         }
 546     }
 547 


2268             for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
2269                 if (w.waitStatus == Node.CONDITION) {
2270                     Thread t = w.thread;
2271                     if (t != null)
2272                         list.add(t);
2273                 }
2274             }
2275             return list;
2276         }
2277     }
2278 
2279     /**
2280      * Setup to support compareAndSet. We need to natively implement
2281      * this here: For the sake of permitting future enhancements, we
2282      * cannot explicitly subclass AtomicInteger, which would be
2283      * efficient and useful otherwise. So, as the lesser of evils, we
2284      * natively implement using hotspot intrinsics API. And while we
2285      * are at it, we do the same for other CASable fields (which could
2286      * otherwise be done with atomic field updaters).
2287      */
2288     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
2289     private static final long STATE;
2290     private static final long HEAD;
2291     private static final long TAIL;
2292 
2293     static {
2294         try {
2295             STATE = U.objectFieldOffset
2296                 (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
2297             HEAD = U.objectFieldOffset
2298                 (AbstractQueuedSynchronizer.class.getDeclaredField("head"));
2299             TAIL = U.objectFieldOffset
2300                 (AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
2301         } catch (ReflectiveOperationException e) {
2302             throw new Error(e);
2303         }
2304 
2305         // Reduce the risk of rare disastrous classloading in first call to
2306         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
2307         Class<?> ensureLoaded = LockSupport.class;
2308     }


 507             this.nextWaiter = nextWaiter;
 508             U.putObject(this, THREAD, Thread.currentThread());
 509         }
 510 
 511         /** Constructor used by addConditionWaiter. */
 512         Node(int waitStatus) {
 513             U.putInt(this, WAITSTATUS, waitStatus);
 514             U.putObject(this, THREAD, Thread.currentThread());
 515         }
 516 
 517         /** CASes waitStatus field. */
 518         final boolean compareAndSetWaitStatus(int expect, int update) {
 519             return U.compareAndSwapInt(this, WAITSTATUS, expect, update);
 520         }
 521 
 522         /** CASes next field. */
 523         final boolean compareAndSetNext(Node expect, Node update) {
 524             return U.compareAndSwapObject(this, NEXT, expect, update);
 525         }
 526 
 527         private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 528         private static final long NEXT;
 529         static final long PREV;
 530         private static final long THREAD;
 531         private static final long WAITSTATUS;
 532         static {
 533             try {
 534                 NEXT = U.objectFieldOffset
 535                     (Node.class.getDeclaredField("next"));
 536                 PREV = U.objectFieldOffset
 537                     (Node.class.getDeclaredField("prev"));
 538                 THREAD = U.objectFieldOffset
 539                     (Node.class.getDeclaredField("thread"));
 540                 WAITSTATUS = U.objectFieldOffset
 541                     (Node.class.getDeclaredField("waitStatus"));
 542             } catch (ReflectiveOperationException e) {
 543                 throw new Error(e);
 544             }
 545         }
 546     }
 547 


2268             for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
2269                 if (w.waitStatus == Node.CONDITION) {
2270                     Thread t = w.thread;
2271                     if (t != null)
2272                         list.add(t);
2273                 }
2274             }
2275             return list;
2276         }
2277     }
2278 
2279     /**
2280      * Setup to support compareAndSet. We need to natively implement
2281      * this here: For the sake of permitting future enhancements, we
2282      * cannot explicitly subclass AtomicInteger, which would be
2283      * efficient and useful otherwise. So, as the lesser of evils, we
2284      * natively implement using hotspot intrinsics API. And while we
2285      * are at it, we do the same for other CASable fields (which could
2286      * otherwise be done with atomic field updaters).
2287      */
2288     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
2289     private static final long STATE;
2290     private static final long HEAD;
2291     private static final long TAIL;
2292 
2293     static {
2294         try {
2295             STATE = U.objectFieldOffset
2296                 (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
2297             HEAD = U.objectFieldOffset
2298                 (AbstractQueuedSynchronizer.class.getDeclaredField("head"));
2299             TAIL = U.objectFieldOffset
2300                 (AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
2301         } catch (ReflectiveOperationException e) {
2302             throw new Error(e);
2303         }
2304 
2305         // Reduce the risk of rare disastrous classloading in first call to
2306         // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
2307         Class<?> ensureLoaded = LockSupport.class;
2308     }