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

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


 608      * @throws TimeoutException if the specified waiting time elapses
 609      *         before another thread enters the exchange
 610      */
 611     @SuppressWarnings("unchecked")
 612     public V exchange(V x, long timeout, TimeUnit unit)
 613         throws InterruptedException, TimeoutException {
 614         Object v;
 615         Object item = (x == null) ? NULL_ITEM : x;
 616         long ns = unit.toNanos(timeout);
 617         if ((arena != null ||
 618              (v = slotExchange(item, true, ns)) == null) &&
 619             ((Thread.interrupted() ||
 620               (v = arenaExchange(item, true, ns)) == null)))
 621             throw new InterruptedException();
 622         if (v == TIMED_OUT)
 623             throw new TimeoutException();
 624         return (v == NULL_ITEM) ? null : (V)v;
 625     }
 626 
 627     // Unsafe mechanics
 628     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
 629     private static final long BOUND;
 630     private static final long SLOT;
 631     private static final long MATCH;
 632     private static final long BLOCKER;
 633     private static final int ABASE;
 634     static {
 635         try {
 636             BOUND = U.objectFieldOffset
 637                 (Exchanger.class.getDeclaredField("bound"));
 638             SLOT = U.objectFieldOffset
 639                 (Exchanger.class.getDeclaredField("slot"));
 640 
 641             MATCH = U.objectFieldOffset
 642                 (Node.class.getDeclaredField("match"));
 643 
 644             BLOCKER = U.objectFieldOffset
 645                 (Thread.class.getDeclaredField("parkBlocker"));
 646 
 647             int scale = U.arrayIndexScale(Node[].class);
 648             if ((scale & (scale - 1)) != 0 || scale > (1 << ASHIFT))


 608      * @throws TimeoutException if the specified waiting time elapses
 609      *         before another thread enters the exchange
 610      */
 611     @SuppressWarnings("unchecked")
 612     public V exchange(V x, long timeout, TimeUnit unit)
 613         throws InterruptedException, TimeoutException {
 614         Object v;
 615         Object item = (x == null) ? NULL_ITEM : x;
 616         long ns = unit.toNanos(timeout);
 617         if ((arena != null ||
 618              (v = slotExchange(item, true, ns)) == null) &&
 619             ((Thread.interrupted() ||
 620               (v = arenaExchange(item, true, ns)) == null)))
 621             throw new InterruptedException();
 622         if (v == TIMED_OUT)
 623             throw new TimeoutException();
 624         return (v == NULL_ITEM) ? null : (V)v;
 625     }
 626 
 627     // Unsafe mechanics
 628     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
 629     private static final long BOUND;
 630     private static final long SLOT;
 631     private static final long MATCH;
 632     private static final long BLOCKER;
 633     private static final int ABASE;
 634     static {
 635         try {
 636             BOUND = U.objectFieldOffset
 637                 (Exchanger.class.getDeclaredField("bound"));
 638             SLOT = U.objectFieldOffset
 639                 (Exchanger.class.getDeclaredField("slot"));
 640 
 641             MATCH = U.objectFieldOffset
 642                 (Node.class.getDeclaredField("match"));
 643 
 644             BLOCKER = U.objectFieldOffset
 645                 (Thread.class.getDeclaredField("parkBlocker"));
 646 
 647             int scale = U.arrayIndexScale(Node[].class);
 648             if ((scale & (scale - 1)) != 0 || scale > (1 << ASHIFT))