jdk/src/java.base/share/classes/java/util/concurrent/CyclicBarrier.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 42322 c3474fef4fe4
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    38
import java.util.concurrent.locks.Condition;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    39
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A synchronization aid that allows a set of threads to all wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * each other to reach a common barrier point.  CyclicBarriers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * useful in programs involving a fixed sized party of threads that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * must occasionally wait for each other. The barrier is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <em>cyclic</em> because it can be re-used after the waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * are released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    49
 * <p>A {@code CyclicBarrier} supports an optional {@link Runnable} command
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that is run once per barrier point, after the last thread in the party
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * arrives, but before any threads are released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This <em>barrier action</em> is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * for updating shared-state before any of the parties continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    55
 * <p><b>Sample usage:</b> Here is an example of using a barrier in a
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    56
 * parallel decomposition design:
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    57
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    58
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * class Solver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   final int N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   final float[][] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   class Worker implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     int myRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     Worker(int row) { myRow = row; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       while (!done()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *         processRow(myRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *           barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         } catch (BrokenBarrierException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   public Solver(float[][] matrix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     data = matrix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     N = matrix.length;
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
    85
 *     Runnable barrierAction = () -> mergeRows(...);
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    86
 *     barrier = new CyclicBarrier(N, barrierAction);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    88
 *     List<Thread> threads = new ArrayList<>(N);
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    89
 *     for (int i = 0; i < N; i++) {
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    90
 *       Thread thread = new Thread(new Worker(i));
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    91
 *       threads.add(thread);
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    92
 *       thread.start();
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    93
 *     }
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    94
 *
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    95
 *     // wait until done
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    96
 *     for (Thread thread : threads)
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    97
 *       thread.join();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    99
 * }}</pre>
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   100
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Here, each worker thread processes a row of the matrix then waits at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * barrier until all rows have been processed. When all rows are processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * the supplied {@link Runnable} barrier action is executed and merges the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * rows. If the merger
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   105
 * determines that a solution has been found then {@code done()} will return
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   106
 * {@code true} and each worker will terminate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p>If the barrier action does not rely on the parties being suspended when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * it is executed, then any of the threads in the party could execute that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * action when it is released. To facilitate this, each invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * {@link #await} returns the arrival index of that thread at the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * You can then choose which thread should execute the barrier action, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * example:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   114
 * <pre> {@code
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   115
 * if (barrier.await() == 0) {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   116
 *   // log the completion of this iteration
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   117
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   119
 * <p>The {@code CyclicBarrier} uses an all-or-none breakage model
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * for failed synchronization attempts: If a thread leaves a barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * point prematurely because of interruption, failure, or timeout, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * other threads waiting at that barrier point will also leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * abnormally via {@link BrokenBarrierException} (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * {@link InterruptedException} if they too were interrupted at about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the same time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>Memory consistency effects: Actions in a thread prior to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * {@code await()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * actions that are part of the barrier action, which in turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <i>happen-before</i> actions following a successful return from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * corresponding {@code await()} in other threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see CountDownLatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * @author Doug Lea
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
   137
 * @since 1.5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
public class CyclicBarrier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Each use of the barrier is represented as a generation instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The generation changes whenever the barrier is tripped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * is reset. There can be many generations associated with threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * using the barrier - due to the non-deterministic way the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * may be allocated to waiting threads - but only one of these
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   146
     * can be active at a time (the one to which {@code count} applies)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * and all the rest are either broken or tripped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * There need not be an active generation if there has been a break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * but no subsequent reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static class Generation {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   152
        Generation() {}                 // prevent access constructor creation
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42322
diff changeset
   153
        boolean broken;                 // initially false
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** The lock for guarding barrier entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /** Condition to wait on until tripped */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private final Condition trip = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /** The number of parties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private final int parties;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   162
    /** The command to run when tripped */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private final Runnable barrierCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /** The current generation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private Generation generation = new Generation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Number of parties still waiting. Counts down from parties to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * on each generation.  It is reset to parties on each new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * generation or when broken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Updates state on barrier trip and wakes up everyone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Called only while holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private void nextGeneration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // signal completion of last generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        trip.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // set up next generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        generation = new Generation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Sets current barrier generation as broken and wakes up everyone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Called only while holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private void breakBarrier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        generation.broken = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        trip.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Main barrier code, covering the various policies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private int dowait(boolean timed, long nanos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws InterruptedException, BrokenBarrierException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
               TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            final Generation g = generation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (g.broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new BrokenBarrierException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (Thread.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   215
            int index = --count;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   216
            if (index == 0) {  // tripped
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   217
                boolean ranAction = false;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   218
                try {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   219
                    final Runnable command = barrierCommand;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   220
                    if (command != null)
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   221
                        command.run();
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   222
                    ranAction = true;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   223
                    nextGeneration();
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   224
                    return 0;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   225
                } finally {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   226
                    if (!ranAction)
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   227
                        breakBarrier();
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   228
                }
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   229
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // loop until tripped, broken, interrupted, or timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    if (!timed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        trip.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    else if (nanos > 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        nanos = trip.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    if (g == generation && ! g.broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        // We're about to finish waiting even if we had not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        // been interrupted, so this interrupt is deemed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        // "belong" to subsequent execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (g.broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    throw new BrokenBarrierException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (g != generation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (timed && nanos <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    throw new TimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   267
     * Creates a new {@code CyclicBarrier} that will trip when the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * given number of parties (threads) are waiting upon it, and which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * will execute the given barrier action when the barrier is tripped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * performed by the last thread entering the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param parties the number of threads that must invoke {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *        before the barrier is tripped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param barrierAction the command to execute when the barrier is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *        tripped, or {@code null} if there is no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @throws IllegalArgumentException if {@code parties} is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public CyclicBarrier(int parties, Runnable barrierAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (parties <= 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        this.parties = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this.count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        this.barrierCommand = barrierAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   286
     * Creates a new {@code CyclicBarrier} that will trip when the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * given number of parties (threads) are waiting upon it, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * does not perform a predefined action when the barrier is tripped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param parties the number of threads that must invoke {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *        before the barrier is tripped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws IllegalArgumentException if {@code parties} is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public CyclicBarrier(int parties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        this(parties, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the number of parties required to trip this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return the number of parties required to trip this barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public int getParties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Waits until all {@linkplain #getParties parties} have invoked
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   309
     * {@code await} on this barrier.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>If the current thread is not the last to arrive then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * one of the following things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <li>The last thread arrives; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * one of the other waiting threads; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <li>Some other thread times out while waiting for barrier; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <li>Some other thread invokes {@link #reset} on this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * <p>If the barrier is {@link #reset} while any thread is waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * or if the barrier {@linkplain #isBroken is broken} when
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   334
     * {@code await} is invoked, or while any thread is waiting, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * {@link BrokenBarrierException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * then all other waiting threads will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * {@link BrokenBarrierException} and the barrier is placed in the broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>If the current thread is the last thread to arrive, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * non-null barrier action was supplied in the constructor, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * current thread runs the action before allowing the other threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * If an exception occurs during the barrier action then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * will be propagated in the current thread and the barrier is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * the broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return the arrival index of the current thread, where index
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   351
     *         {@code getParties() - 1} indicates the first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *         to arrive and zero indicates the last to arrive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *         while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @throws BrokenBarrierException if <em>another</em> thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         interrupted or timed out while the current thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         waiting, or the barrier was reset, or the barrier was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *         broken when {@code await} was called, or the barrier
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   359
     *         action (if present) failed due to an exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public int await() throws InterruptedException, BrokenBarrierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return dowait(false, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } catch (TimeoutException toe) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   365
            throw new Error(toe); // cannot happen
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Waits until all {@linkplain #getParties parties} have invoked
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   371
     * {@code await} on this barrier, or the specified waiting time elapses.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>If the current thread is not the last to arrive then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * one of the following things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <li>The last thread arrives; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <li>The specified timeout elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * one of the other waiting threads; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <li>Some other thread times out while waiting for barrier; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <li>Some other thread invokes {@link #reset} on this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p>If the specified waiting time elapses then {@link TimeoutException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * is thrown. If the time is less than or equal to zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * method will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p>If the barrier is {@link #reset} while any thread is waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * or if the barrier {@linkplain #isBroken is broken} when
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   401
     * {@code await} is invoked, or while any thread is waiting, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * {@link BrokenBarrierException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * waiting, then all other waiting threads will throw {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * BrokenBarrierException} and the barrier is placed in the broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>If the current thread is the last thread to arrive, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * non-null barrier action was supplied in the constructor, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * current thread runs the action before allowing the other threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * If an exception occurs during the barrier action then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * will be propagated in the current thread and the barrier is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * the broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param timeout the time to wait for the barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param unit the time unit of the timeout parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return the arrival index of the current thread, where index
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   420
     *         {@code getParties() - 1} indicates the first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         to arrive and zero indicates the last to arrive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *         while waiting
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   424
     * @throws TimeoutException if the specified timeout elapses.
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   425
     *         In this case the barrier will be broken.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @throws BrokenBarrierException if <em>another</em> thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *         interrupted or timed out while the current thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         waiting, or the barrier was reset, or the barrier was broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         when {@code await} was called, or the barrier action (if
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   430
     *         present) failed due to an exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public int await(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        throws InterruptedException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
               BrokenBarrierException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
               TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return dowait(true, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Queries if this barrier is in a broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return {@code true} if one or more parties broke out of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         barrier due to interruption or timeout since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         construction or the last reset, or a barrier action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *         failed due to an exception; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public boolean isBroken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return generation.broken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Resets the barrier to its initial state.  If any parties are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * currently waiting at the barrier, they will return with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * {@link BrokenBarrierException}. Note that resets <em>after</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * a breakage has occurred for other reasons can be complicated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * carry out; threads need to re-synchronize in some other way,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * and choose one to perform the reset.  It may be preferable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * instead create a new barrier for subsequent use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            breakBarrier();   // break the current generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            nextGeneration(); // start a new generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Returns the number of parties currently waiting at the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * This method is primarily useful for debugging and assertions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @return the number of parties currently blocked in {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public int getNumberWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return parties - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
}