1152 lines · 45.1 KB · Java
RawDownload
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * This file is available under and governed by the GNU General Public * License version 2 only, as published by the Free Software Foundation. * However, the following notice accompanied the original version of this * file: * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent; import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.locks.LockSupport; /** * A reusable synchronization barrier, similar in functionality to * {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and * {@link java.util.concurrent.CountDownLatch CountDownLatch} * but supporting more flexible usage. * * <p> <b>Registration.</b> Unlike the case for other barriers, the * number of parties <em>registered</em> to synchronize on a phaser * may vary over time.  Tasks may be registered at any time (using * methods {@link #register}, {@link #bulkRegister}, or forms of * constructors establishing initial numbers of parties), and * optionally deregistered upon any arrival (using {@link * #arriveAndDeregister}).  As is the case with most basic * synchronization constructs, registration and deregistration affect * only internal counts; they do not establish any further internal * bookkeeping, so tasks cannot query whether they are registered. * (However, you can introduce such bookkeeping by subclassing this * class.) * * <p> <b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code * Phaser} may be repeatedly awaited.  Method {@link * #arriveAndAwaitAdvance} has effect analogous to {@link * java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each * generation of a phaser has an associated phase number. The phase * number starts at zero, and advances when all parties arrive at the * phaser, wrapping around to zero after reaching {@code * Integer.MAX_VALUE}. The use of phase numbers enables independent * control of actions upon arrival at a phaser and upon awaiting * others, via two kinds of methods that may be invoked by any * registered party: * * <ul> * *   <li> <b>Arrival.</b> Methods {@link #arrive} and *       {@link #arriveAndDeregister} record arrival.  These methods *       do not block, but return an associated <em>arrival phase *       number</em>; that is, the phase number of the phaser to which *       the arrival applied. When the final party for a given phase *       arrives, an optional action is performed and the phase *       advances.  These actions are performed by the party *       triggering a phase advance, and are arranged by overriding *       method {@link #onAdvance(int, int)}, which also controls *       termination. Overriding this method is similar to, but more *       flexible than, providing a barrier action to a {@code *       CyclicBarrier}. * *   <li> <b>Waiting.</b> Method {@link #awaitAdvance} requires an *       argument indicating an arrival phase number, and returns when *       the phaser advances to (or is already at) a different phase. *       Unlike similar constructions using {@code CyclicBarrier}, *       method {@code awaitAdvance} continues to wait even if the *       waiting thread is interrupted. Interruptible and timeout *       versions are also available, but exceptions encountered while *       tasks wait interruptibly or with timeout do not change the *       state of the phaser. If necessary, you can perform any *       associated recovery within handlers of those exceptions, *       often after invoking {@code forceTermination}.  Phasers may *       also be used by tasks executing in a {@link ForkJoinPool}, *       which will ensure sufficient parallelism to execute tasks *       when others are blocked waiting for a phase to advance. * * </ul> * * <p> <b>Termination.</b> A phaser may enter a <em>termination</em> * state, that may be checked using method {@link #isTerminated}. Upon * termination, all synchronization methods immediately return without * waiting for advance, as indicated by a negative return value. * Similarly, attempts to register upon termination have no effect. * Termination is triggered when an invocation of {@code onAdvance} * returns {@code true}. The default implementation returns {@code * true} if a deregistration has caused the number of registered * parties to become zero.  As illustrated below, when phasers control * actions with a fixed number of iterations, it is often convenient * to override this method to cause termination when the current phase * number reaches a threshold. Method {@link #forceTermination} is * also available to abruptly release waiting threads and allow them * to terminate. * * <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., * constructed in tree structures) to reduce contention. Phasers with * large numbers of parties that would otherwise experience heavy * synchronization contention costs may instead be set up so that * groups of sub-phasers share a common parent.  This may greatly * increase throughput even though it incurs greater per-operation * overhead. * * <p>In a tree of tiered phasers, registration and deregistration of * child phasers with their parent are managed automatically. * Whenever the number of registered parties of a child phaser becomes * non-zero (as established in the {@link #Phaser(Phaser,int)} * constructor, {@link #register}, or {@link #bulkRegister}), the * child phaser is registered with its parent.  Whenever the number of * registered parties becomes zero as the result of an invocation of * {@link #arriveAndDeregister}, the child phaser is deregistered * from its parent. * * <p><b>Monitoring.</b> While synchronization methods may be invoked * only by registered parties, the current state of a phaser may be * monitored by any caller.  At any given moment there are {@link * #getRegisteredParties} parties in total, of which {@link * #getArrivedParties} have arrived at the current phase ({@link * #getPhase}).  When the remaining ({@link #getUnarrivedParties}) * parties arrive, the phase advances.  The values returned by these * methods may reflect transient states and so are not in general * useful for synchronization control.  Method {@link #toString} * returns snapshots of these state queries in a form convenient for * informal monitoring. * * <p><b>Sample usages:</b> * * <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} * to control a one-shot action serving a variable number of parties. * The typical idiom is for the method setting this up to first * register, then start the actions, then deregister, as in: * *  <pre> {@code * void runTasks(List<Runnable> tasks) { *   final Phaser phaser = new Phaser(1); // "1" to register self *   // create and start threads *   for (final Runnable task : tasks) { *     phaser.register(); *     new Thread() { *       public void run() { *         phaser.arriveAndAwaitAdvance(); // await all creation *         task.run(); *       } *     }.start(); *   } * *   // allow threads to start and deregister self *   phaser.arriveAndDeregister(); * }}</pre> * * <p>One way to cause a set of threads to repeatedly perform actions * for a given number of iterations is to override {@code onAdvance}: * *  <pre> {@code * void startTasks(List<Runnable> tasks, final int iterations) { *   final Phaser phaser = new Phaser() { *     protected boolean onAdvance(int phase, int registeredParties) { *       return phase >= iterations || registeredParties == 0; *     } *   }; *   phaser.register(); *   for (final Runnable task : tasks) { *     phaser.register(); *     new Thread() { *       public void run() { *         do { *           task.run(); *           phaser.arriveAndAwaitAdvance(); *         } while (!phaser.isTerminated()); *       } *     }.start(); *   } *   phaser.arriveAndDeregister(); // deregister self, don't wait * }}</pre> * * If the main task must later await termination, it * may re-register and then execute a similar loop: *  <pre> {@code *   // ... *   phaser.register(); *   while (!phaser.isTerminated()) *     phaser.arriveAndAwaitAdvance();}</pre> * * <p>Related constructions may be used to await particular phase numbers * in contexts where you are sure that the phase will never wrap around * {@code Integer.MAX_VALUE}. For example: * *  <pre> {@code * void awaitPhase(Phaser phaser, int phase) { *   int p = phaser.register(); // assumes caller not already registered *   while (p < phase) { *     if (phaser.isTerminated()) *       // ... deal with unexpected termination *     else *       p = phaser.arriveAndAwaitAdvance(); *   } *   phaser.arriveAndDeregister(); * }}</pre> * * * <p>To create a set of {@code n} tasks using a tree of phasers, you * could use code of the following form, assuming a Task class with a * constructor accepting a {@code Phaser} that it registers with upon * construction. After invocation of {@code build(new Task[n], 0, n, * new Phaser())}, these tasks could then be started, for example by * submitting to a pool: * *  <pre> {@code * void build(Task[] tasks, int lo, int hi, Phaser ph) { *   if (hi - lo > TASKS_PER_PHASER) { *     for (int i = lo; i < hi; i += TASKS_PER_PHASER) { *       int j = Math.min(i + TASKS_PER_PHASER, hi); *       build(tasks, i, j, new Phaser(ph)); *     } *   } else { *     for (int i = lo; i < hi; ++i) *       tasks[i] = new Task(ph); *       // assumes new Task(ph) performs ph.register() *   } * }}</pre> * * The best value of {@code TASKS_PER_PHASER} depends mainly on * expected synchronization rates. A value as low as four may * be appropriate for extremely small per-phase task bodies (thus * high rates), or up to hundreds for extremely large ones. * * <p><b>Implementation notes</b>: This implementation restricts the * maximum number of parties to 65535. Attempts to register additional * parties result in {@code IllegalStateException}. However, you can and * should create tiered phasers to accommodate arbitrarily large sets * of participants. * * @since 1.7 * @author Doug Lea */public class Phaser {    /*     * This class implements an extension of X10 "clocks".  Thanks to     * Vijay Saraswat for the idea, and to Vivek Sarkar for     * enhancements to extend functionality.     */     /**     * Primary state representation, holding four bit-fields:     *     * unarrived  -- the number of parties yet to hit barrier (bits  0-15)     * parties    -- the number of parties to wait            (bits 16-31)     * phase      -- the generation of the barrier            (bits 32-62)     * terminated -- set if barrier is terminated             (bit  63 / sign)     *     * Except that a phaser with no registered parties is     * distinguished by the otherwise illegal state of having zero     * parties and one unarrived parties (encoded as EMPTY below).     *     * To efficiently maintain atomicity, these values are packed into     * a single (atomic) long. Good performance relies on keeping     * state decoding and encoding simple, and keeping race windows     * short.     *     * All state updates are performed via CAS except initial     * registration of a sub-phaser (i.e., one with a non-null     * parent).  In this (relatively rare) case, we use built-in     * synchronization to lock while first registering with its     * parent.     *     * The phase of a subphaser is allowed to lag that of its     * ancestors until it is actually accessed -- see method     * reconcileState.     */    private volatile long state;     private static final int  MAX_PARTIES     = 0xffff;    private static final int  MAX_PHASE       = Integer.MAX_VALUE;    private static final int  PARTIES_SHIFT   = 16;    private static final int  PHASE_SHIFT     = 32;    private static final int  UNARRIVED_MASK  = 0xffff;      // to mask ints    private static final long PARTIES_MASK    = 0xffff0000L; // to mask longs    private static final long TERMINATION_BIT = 1L << 63;     // some special values    private static final int  ONE_ARRIVAL     = 1;    private static final int  ONE_PARTY       = 1 << PARTIES_SHIFT;    private static final int  EMPTY           = 1;     // The following unpacking methods are usually manually inlined     private static int unarrivedOf(long s) {        int counts = (int)s;        return (counts == EMPTY) ? 0 : counts & UNARRIVED_MASK;    }     private static int partiesOf(long s) {        return (int)s >>> PARTIES_SHIFT;    }     private static int phaseOf(long s) {        return (int)(s >>> PHASE_SHIFT);    }     private static int arrivedOf(long s) {        int counts = (int)s;        return (counts == EMPTY) ? 0 :            (counts >>> PARTIES_SHIFT) - (counts & UNARRIVED_MASK);    }     /**     * The parent of this phaser, or null if none     */    private final Phaser parent;     /**     * The root of phaser tree. Equals this if not in a tree.     */    private final Phaser root;     /**     * Heads of Treiber stacks for waiting threads. To eliminate     * contention when releasing some threads while adding others, we     * use two of them, alternating across even and odd phases.     * Subphasers share queues with root to speed up releases.     */    private final AtomicReference<QNode> evenQ;    private final AtomicReference<QNode> oddQ;     private AtomicReference<QNode> queueFor(int phase) {        return ((phase & 1) == 0) ? evenQ : oddQ;    }     /**     * Returns message string for bounds exceptions on arrival.     */    private String badArrive(long s) {        return "Attempted arrival of unregistered party for " +            stateToString(s);    }     /**     * Returns message string for bounds exceptions on registration.     */    private String badRegister(long s) {        return "Attempt to register more than " +            MAX_PARTIES + " parties for " + stateToString(s);    }     /**     * Main implementation for methods arrive and arriveAndDeregister.     * Manually tuned to speed up and minimize race windows for the     * common case of just decrementing unarrived field.     *     * @param deregister false for arrive, true for arriveAndDeregister     */    private int doArrive(boolean deregister) {        int adj = deregister ? ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL;        final Phaser root = this.root;        for (;;) {            long s = (root == this) ? state : reconcileState();            int phase = (int)(s >>> PHASE_SHIFT);            int counts = (int)s;            int unarrived = (counts & UNARRIVED_MASK) - 1;            if (phase < 0)                return phase;            else if (counts == EMPTY || unarrived < 0) {                if (root == this || reconcileState() == s)                    throw new IllegalStateException(badArrive(s));            }            else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) {                if (unarrived == 0) {                    long n = s & PARTIES_MASK;  // base of next state                    int nextUnarrived = (int)n >>> PARTIES_SHIFT;                    if (root != this)                        return parent.doArrive(nextUnarrived == 0);                    if (onAdvance(phase, nextUnarrived))                        n |= TERMINATION_BIT;                    else if (nextUnarrived == 0)                        n |= EMPTY;                    else                        n |= nextUnarrived;                    n |= (long)((phase + 1) & MAX_PHASE) << PHASE_SHIFT;                    UNSAFE.compareAndSwapLong(this, stateOffset, s, n);                    releaseWaiters(phase);                }                return phase;            }        }    }     /**     * Implementation of register, bulkRegister     *     * @param registrations number to add to both parties and     * unarrived fields. Must be greater than zero.     */    private int doRegister(int registrations) {        // adjustment to state        long adj = ((long)registrations << PARTIES_SHIFT) | registrations;        final Phaser parent = this.parent;        int phase;        for (;;) {            long s = state;            int counts = (int)s;            int parties = counts >>> PARTIES_SHIFT;            int unarrived = counts & UNARRIVED_MASK;            if (registrations > MAX_PARTIES - parties)                throw new IllegalStateException(badRegister(s));            else if ((phase = (int)(s >>> PHASE_SHIFT)) < 0)                break;            else if (counts != EMPTY) {             // not 1st registration                if (parent == null || reconcileState() == s) {                    if (unarrived == 0)             // wait out advance                        root.internalAwaitAdvance(phase, null);                    else if (UNSAFE.compareAndSwapLong(this, stateOffset,                                                       s, s + adj))                        break;                }            }            else if (parent == null) {              // 1st root registration                long next = ((long)phase << PHASE_SHIFT) | adj;                if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next))                    break;            }            else {                synchronized (this) {               // 1st sub registration                    if (state == s) {               // recheck under lock                        parent.doRegister(1);                        do {                        // force current phase                            phase = (int)(root.state >>> PHASE_SHIFT);                            // assert phase < 0 || (int)state == EMPTY;                        } while (!UNSAFE.compareAndSwapLong                                 (this, stateOffset, state,                                  ((long)phase << PHASE_SHIFT) | adj));                        break;                    }                }            }        }        return phase;    }     /**     * Resolves lagged phase propagation from root if necessary.     * Reconciliation normally occurs when root has advanced but     * subphasers have not yet done so, in which case they must finish     * their own advance by setting unarrived to parties (or if     * parties is zero, resetting to unregistered EMPTY state).     * However, this method may also be called when "floating"     * subphasers with possibly some unarrived parties are merely     * catching up to current phase, in which case counts are     * unaffected.     *     * @return reconciled state     */    private long reconcileState() {        final Phaser root = this.root;        long s = state;        if (root != this) {            int phase, u, p;            // CAS root phase with current parties; possibly trip unarrived            while ((phase = (int)(root.state >>> PHASE_SHIFT)) !=                   (int)(s >>> PHASE_SHIFT) &&                   !UNSAFE.compareAndSwapLong                   (this, stateOffset, s,                    s = (((long)phase << PHASE_SHIFT) |                         (s & PARTIES_MASK) |                         ((p = (int)s >>> PARTIES_SHIFT) == 0 ? EMPTY :                          (u = (int)s & UNARRIVED_MASK) == 0 ? p : u))))                s = state;        }        return s;    }     /**     * Creates a new phaser with no initially registered parties, no     * parent, and initial phase number 0. Any thread using this     * phaser will need to first register for it.     */

Lines 1–500 of 1152.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t