src/java.base/share/classes/java/util/concurrent/CountedCompleter.java
author dl
Tue, 10 Apr 2018 11:37:18 -0700
changeset 49565 b5705ade8c8d
parent 48233 dd5157f363ab
permissions -rw-r--r--
8197531: Miscellaneous changes imported from jsr166 CVS 2018-04 Reviewed-by: martin, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     1
/*
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     3
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
     9
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    14
 * accompanied this code).
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    15
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    19
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    22
 * questions.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    23
 */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    24
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    25
/*
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    29
 * file:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    30
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    34
 */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    35
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    36
package java.util.concurrent;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
    39
import java.lang.invoke.VarHandle;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
    40
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    41
/**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    42
 * A {@link ForkJoinTask} with a completion action performed when
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    43
 * triggered and there are no remaining pending actions.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    44
 * CountedCompleters are in general more robust in the
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    45
 * presence of subtask stalls and blockage than are other forms of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    46
 * ForkJoinTasks, but are less intuitive to program.  Uses of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    47
 * CountedCompleter are similar to those of other completion based
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    48
 * components (such as {@link java.nio.channels.CompletionHandler})
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    49
 * except that multiple <em>pending</em> completions may be necessary
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    50
 * to trigger the completion action {@link #onCompletion(CountedCompleter)},
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    51
 * not just one.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    52
 * Unless initialized otherwise, the {@linkplain #getPendingCount pending
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    53
 * count} starts at zero, but may be (atomically) changed using
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    54
 * methods {@link #setPendingCount}, {@link #addToPendingCount}, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    55
 * {@link #compareAndSetPendingCount}. Upon invocation of {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    56
 * #tryComplete}, if the pending action count is nonzero, it is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    57
 * decremented; otherwise, the completion action is performed, and if
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    58
 * this completer itself has a completer, the process is continued
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    59
 * with its completer.  As is the case with related synchronization
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 40815
diff changeset
    60
 * components such as {@link Phaser} and {@link Semaphore}, these methods
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    61
 * affect only internal counts; they do not establish any further
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    62
 * internal bookkeeping. In particular, the identities of pending
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    63
 * tasks are not maintained. As illustrated below, you can create
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    64
 * subclasses that do record some or all pending tasks or their
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    65
 * results when needed.  As illustrated below, utility methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    66
 * supporting customization of completion traversals are also
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    67
 * provided. However, because CountedCompleters provide only basic
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    68
 * synchronization mechanisms, it may be useful to create further
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    69
 * abstract subclasses that maintain linkages, fields, and additional
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    70
 * support methods appropriate for a set of related usages.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    71
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    72
 * <p>A concrete CountedCompleter class must define method {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    73
 * #compute}, that should in most cases (as illustrated below), invoke
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    74
 * {@code tryComplete()} once before returning. The class may also
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    75
 * optionally override method {@link #onCompletion(CountedCompleter)}
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    76
 * to perform an action upon normal completion, and method
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    77
 * {@link #onExceptionalCompletion(Throwable, CountedCompleter)} to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    78
 * perform an action upon any exception.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    79
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    80
 * <p>CountedCompleters most often do not bear results, in which case
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    81
 * they are normally declared as {@code CountedCompleter<Void>}, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    82
 * will always return {@code null} as a result value.  In other cases,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    83
 * you should override method {@link #getRawResult} to provide a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    84
 * result from {@code join(), invoke()}, and related methods.  In
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    85
 * general, this method should return the value of a field (or a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    86
 * function of one or more fields) of the CountedCompleter object that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    87
 * holds the result upon completion. Method {@link #setRawResult} by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    88
 * default plays no role in CountedCompleters.  It is possible, but
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    89
 * rarely applicable, to override this method to maintain other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    90
 * objects or fields holding result data.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    91
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    92
 * <p>A CountedCompleter that does not itself have a completer (i.e.,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    93
 * one for which {@link #getCompleter} returns {@code null}) can be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    94
 * used as a regular ForkJoinTask with this added functionality.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    95
 * However, any completer that in turn has another completer serves
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    96
 * only as an internal helper for other computations, so its own task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    97
 * status (as reported in methods such as {@link ForkJoinTask#isDone})
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
    98
 * is arbitrary; this status changes only upon explicit invocations of
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    99
 * {@link #complete}, {@link ForkJoinTask#cancel},
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   100
 * {@link ForkJoinTask#completeExceptionally(Throwable)} or upon
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   101
 * exceptional completion of method {@code compute}. Upon any
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   102
 * exceptional completion, the exception may be relayed to a task's
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   103
 * completer (and its completer, and so on), if one exists and it has
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   104
 * not otherwise already completed. Similarly, cancelling an internal
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   105
 * CountedCompleter has only a local effect on that completer, so is
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   106
 * not often useful.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   107
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   108
 * <p><b>Sample Usages.</b>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   109
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   110
 * <p><b>Parallel recursive decomposition.</b> CountedCompleters may
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   111
 * be arranged in trees similar to those often used with {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   112
 * RecursiveAction}s, although the constructions involved in setting
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   113
 * them up typically vary. Here, the completer of each task is its
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   114
 * parent in the computation tree. Even though they entail a bit more
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   115
 * bookkeeping, CountedCompleters may be better choices when applying
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   116
 * a possibly time-consuming operation (that cannot be further
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   117
 * subdivided) to each element of an array or collection; especially
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   118
 * when the operation takes a significantly different amount of time
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   119
 * to complete for some elements than others, either because of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   120
 * intrinsic variation (for example I/O) or auxiliary effects such as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   121
 * garbage collection.  Because CountedCompleters provide their own
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   122
 * continuations, other tasks need not block waiting to perform them.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   123
 *
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   124
 * <p>For example, here is an initial version of a utility method that
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   125
 * uses divide-by-two recursive decomposition to divide work into
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   126
 * single pieces (leaf tasks). Even when work is split into individual
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   127
 * calls, tree-based techniques are usually preferable to directly
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   128
 * forking leaf tasks, because they reduce inter-thread communication
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   129
 * and improve load balancing. In the recursive case, the second of
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   130
 * each pair of subtasks to finish triggers completion of their parent
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   131
 * (because no result combination is performed, the default no-op
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   132
 * implementation of method {@code onCompletion} is not overridden).
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   133
 * The utility method sets up the root task and invokes it (here,
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   134
 * implicitly using the {@link ForkJoinPool#commonPool()}).  It is
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   135
 * straightforward and reliable (but not optimal) to always set the
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   136
 * pending count to the number of child tasks and call {@code
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   137
 * tryComplete()} immediately before returning.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   138
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   139
 * <pre> {@code
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   140
 * public static <E> void forEach(E[] array, Consumer<E> action) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   141
 *   class Task extends CountedCompleter<Void> {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   142
 *     final int lo, hi;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   143
 *     Task(Task parent, int lo, int hi) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   144
 *       super(parent); this.lo = lo; this.hi = hi;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   145
 *     }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   146
 *
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   147
 *     public void compute() {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   148
 *       if (hi - lo >= 2) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   149
 *         int mid = (lo + hi) >>> 1;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   150
 *         // must set pending count before fork
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   151
 *         setPendingCount(2);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   152
 *         new Task(this, mid, hi).fork(); // right child
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   153
 *         new Task(this, lo, mid).fork(); // left child
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   154
 *       }
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   155
 *       else if (hi > lo)
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   156
 *         action.accept(array[lo]);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   157
 *       tryComplete();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   158
 *     }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   159
 *   }
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   160
 *   new Task(null, 0, array.length).invoke();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   161
 * }}</pre>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   162
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   163
 * This design can be improved by noticing that in the recursive case,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   164
 * the task has nothing to do after forking its right task, so can
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   165
 * directly invoke its left task before returning. (This is an analog
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   166
 * of tail recursion removal.)  Also, when the last action in a task
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   167
 * is to fork or invoke a subtask (a "tail call"), the call to {@code
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   168
 * tryComplete()} can be optimized away, at the cost of making the
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   169
 * pending count look "off by one".
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   170
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   171
 * <pre> {@code
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   172
 *     public void compute() {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   173
 *       if (hi - lo >= 2) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   174
 *         int mid = (lo + hi) >>> 1;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   175
 *         setPendingCount(1); // looks off by one, but correct!
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   176
 *         new Task(this, mid, hi).fork(); // right child
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   177
 *         new Task(this, lo, mid).compute(); // direct invoke
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   178
 *       } else {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   179
 *         if (hi > lo)
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   180
 *           action.accept(array[lo]);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   181
 *         tryComplete();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   182
 *       }
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   183
 *     }}</pre>
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   184
 *
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   185
 * As a further optimization, notice that the left task need not even exist.
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   186
 * Instead of creating a new one, we can continue using the original task,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   187
 * and add a pending count for each fork.  Additionally, because no task
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   188
 * in this tree implements an {@link #onCompletion(CountedCompleter)} method,
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   189
 * {@code tryComplete} can be replaced with {@link #propagateCompletion}.
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   190
 *
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   191
 * <pre> {@code
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   192
 *     public void compute() {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   193
 *       int n = hi - lo;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   194
 *       for (; n >= 2; n /= 2) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   195
 *         addToPendingCount(1);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   196
 *         new Task(this, lo + n/2, lo + n).fork();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   197
 *       }
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   198
 *       if (n > 0)
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   199
 *         action.accept(array[lo]);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   200
 *       propagateCompletion();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   201
 *     }}</pre>
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   202
 *
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   203
 * When pending counts can be precomputed, they can be established in
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   204
 * the constructor:
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   205
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   206
 * <pre> {@code
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   207
 * public static <E> void forEach(E[] array, Consumer<E> action) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   208
 *   class Task extends CountedCompleter<Void> {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   209
 *     final int lo, hi;
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   210
 *     Task(Task parent, int lo, int hi) {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   211
 *       super(parent, 31 - Integer.numberOfLeadingZeros(hi - lo));
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   212
 *       this.lo = lo; this.hi = hi;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   213
 *     }
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   214
 *
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   215
 *     public void compute() {
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   216
 *       for (int n = hi - lo; n >= 2; n /= 2)
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   217
 *         new Task(this, lo + n/2, lo + n).fork();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   218
 *       action.accept(array[lo]);
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   219
 *       propagateCompletion();
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   220
 *     }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   221
 *   }
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   222
 *   if (array.length > 0)
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   223
 *     new Task(null, 0, array.length).invoke();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   224
 * }}</pre>
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   225
 *
40815
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   226
 * Additional optimizations of such classes might entail specializing
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   227
 * classes for leaf steps, subdividing by say, four, instead of two
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   228
 * per iteration, and using an adaptive threshold instead of always
196b358234d8 8164983: Improve CountedCompleter code samples; add corresponding tests
dl
parents: 40734
diff changeset
   229
 * subdividing down to single elements.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   230
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   231
 * <p><b>Searching.</b> A tree of CountedCompleters can search for a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   232
 * value or property in different parts of a data structure, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   233
 * report a result in an {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   234
 * java.util.concurrent.atomic.AtomicReference AtomicReference} as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   235
 * soon as one is found. The others can poll the result to avoid
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   236
 * unnecessary work. (You could additionally {@linkplain #cancel
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   237
 * cancel} other tasks, but it is usually simpler and more efficient
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   238
 * to just let them notice that the result is set and if so skip
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   239
 * further processing.)  Illustrating again with an array using full
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   240
 * partitioning (again, in practice, leaf tasks will almost always
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   241
 * process more than one element):
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   242
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   243
 * <pre> {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   244
 * class Searcher<E> extends CountedCompleter<E> {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   245
 *   final E[] array; final AtomicReference<E> result; final int lo, hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   246
 *   Searcher(CountedCompleter<?> p, E[] array, AtomicReference<E> result, int lo, int hi) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   247
 *     super(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   248
 *     this.array = array; this.result = result; this.lo = lo; this.hi = hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   249
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   250
 *   public E getRawResult() { return result.get(); }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   251
 *   public void compute() { // similar to ForEach version 3
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   252
 *     int l = lo, h = hi;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   253
 *     while (result.get() == null && h >= l) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   254
 *       if (h - l >= 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   255
 *         int mid = (l + h) >>> 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   256
 *         addToPendingCount(1);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   257
 *         new Searcher(this, array, result, mid, h).fork();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   258
 *         h = mid;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   259
 *       }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   260
 *       else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   261
 *         E x = array[l];
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   262
 *         if (matches(x) && result.compareAndSet(null, x))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   263
 *           quietlyCompleteRoot(); // root task is now joinable
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   264
 *         break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   265
 *       }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   266
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   267
 *     tryComplete(); // normally complete whether or not found
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   268
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   269
 *   boolean matches(E e) { ... } // return true if found
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   270
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   271
 *   public static <E> E search(E[] array) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   272
 *       return new Searcher<E>(null, array, new AtomicReference<E>(), 0, array.length).invoke();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   273
 *   }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   274
 * }}</pre>
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   275
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   276
 * In this example, as well as others in which tasks have no other
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   277
 * effects except to {@code compareAndSet} a common result, the
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   278
 * trailing unconditional invocation of {@code tryComplete} could be
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   279
 * made conditional ({@code if (result.get() == null) tryComplete();})
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   280
 * because no further bookkeeping is required to manage completions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   281
 * once the root task completes.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   282
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   283
 * <p><b>Recording subtasks.</b> CountedCompleter tasks that combine
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   284
 * results of multiple subtasks usually need to access these results
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   285
 * in method {@link #onCompletion(CountedCompleter)}. As illustrated in the following
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   286
 * class (that performs a simplified form of map-reduce where mappings
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   287
 * and reductions are all of type {@code E}), one way to do this in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   288
 * divide and conquer designs is to have each subtask record its
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   289
 * sibling, so that it can be accessed in method {@code onCompletion}.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   290
 * This technique applies to reductions in which the order of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   291
 * combining left and right results does not matter; ordered
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   292
 * reductions require explicit left/right designations.  Variants of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   293
 * other streamlinings seen in the above examples may also apply.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   294
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   295
 * <pre> {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   296
 * class MyMapper<E> { E apply(E v) {  ...  } }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   297
 * class MyReducer<E> { E apply(E x, E y) {  ...  } }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   298
 * class MapReducer<E> extends CountedCompleter<E> {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   299
 *   final E[] array; final MyMapper<E> mapper;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   300
 *   final MyReducer<E> reducer; final int lo, hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   301
 *   MapReducer<E> sibling;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   302
 *   E result;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   303
 *   MapReducer(CountedCompleter<?> p, E[] array, MyMapper<E> mapper,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   304
 *              MyReducer<E> reducer, int lo, int hi) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   305
 *     super(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   306
 *     this.array = array; this.mapper = mapper;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   307
 *     this.reducer = reducer; this.lo = lo; this.hi = hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   308
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   309
 *   public void compute() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   310
 *     if (hi - lo >= 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   311
 *       int mid = (lo + hi) >>> 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   312
 *       MapReducer<E> left = new MapReducer(this, array, mapper, reducer, lo, mid);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   313
 *       MapReducer<E> right = new MapReducer(this, array, mapper, reducer, mid, hi);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   314
 *       left.sibling = right;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   315
 *       right.sibling = left;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   316
 *       setPendingCount(1); // only right is pending
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   317
 *       right.fork();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   318
 *       left.compute();     // directly execute left
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   319
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   320
 *     else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   321
 *       if (hi > lo)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   322
 *           result = mapper.apply(array[lo]);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   323
 *       tryComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   324
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   325
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   326
 *   public void onCompletion(CountedCompleter<?> caller) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   327
 *     if (caller != this) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   328
 *       MapReducer<E> child = (MapReducer<E>)caller;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   329
 *       MapReducer<E> sib = child.sibling;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   330
 *       if (sib == null || sib.result == null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   331
 *         result = child.result;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   332
 *       else
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   333
 *         result = reducer.apply(child.result, sib.result);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   334
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   335
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   336
 *   public E getRawResult() { return result; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   337
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   338
 *   public static <E> E mapReduce(E[] array, MyMapper<E> mapper, MyReducer<E> reducer) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   339
 *     return new MapReducer<E>(null, array, mapper, reducer,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   340
 *                              0, array.length).invoke();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   341
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   342
 * }}</pre>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   343
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   344
 * Here, method {@code onCompletion} takes a form common to many
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   345
 * completion designs that combine results. This callback-style method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   346
 * is triggered once per task, in either of the two different contexts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   347
 * in which the pending count is, or becomes, zero: (1) by a task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   348
 * itself, if its pending count is zero upon invocation of {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   349
 * tryComplete}, or (2) by any of its subtasks when they complete and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   350
 * decrement the pending count to zero. The {@code caller} argument
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   351
 * distinguishes cases.  Most often, when the caller is {@code this},
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   352
 * no action is necessary. Otherwise the caller argument can be used
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   353
 * (usually via a cast) to supply a value (and/or links to other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   354
 * values) to be combined.  Assuming proper use of pending counts, the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   355
 * actions inside {@code onCompletion} occur (once) upon completion of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   356
 * a task and its subtasks. No additional synchronization is required
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   357
 * within this method to ensure thread safety of accesses to fields of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   358
 * this task or other completed tasks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   359
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   360
 * <p><b>Completion Traversals</b>. If using {@code onCompletion} to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   361
 * process completions is inapplicable or inconvenient, you can use
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   362
 * methods {@link #firstComplete} and {@link #nextComplete} to create
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   363
 * custom traversals.  For example, to define a MapReducer that only
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   364
 * splits out right-hand tasks in the form of the third ForEach
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   365
 * example, the completions must cooperatively reduce along
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   366
 * unexhausted subtask links, which can be done as follows:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   367
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   368
 * <pre> {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   369
 * class MapReducer<E> extends CountedCompleter<E> { // version 2
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   370
 *   final E[] array; final MyMapper<E> mapper;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   371
 *   final MyReducer<E> reducer; final int lo, hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   372
 *   MapReducer<E> forks, next; // record subtask forks in list
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   373
 *   E result;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   374
 *   MapReducer(CountedCompleter<?> p, E[] array, MyMapper<E> mapper,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   375
 *              MyReducer<E> reducer, int lo, int hi, MapReducer<E> next) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   376
 *     super(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   377
 *     this.array = array; this.mapper = mapper;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   378
 *     this.reducer = reducer; this.lo = lo; this.hi = hi;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   379
 *     this.next = next;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   380
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   381
 *   public void compute() {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   382
 *     int l = lo, h = hi;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   383
 *     while (h - l >= 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   384
 *       int mid = (l + h) >>> 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   385
 *       addToPendingCount(1);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   386
 *       (forks = new MapReducer(this, array, mapper, reducer, mid, h, forks)).fork();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   387
 *       h = mid;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   388
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   389
 *     if (h > l)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   390
 *       result = mapper.apply(array[l]);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   391
 *     // process completions by reducing along and advancing subtask links
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   392
 *     for (CountedCompleter<?> c = firstComplete(); c != null; c = c.nextComplete()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   393
 *       for (MapReducer t = (MapReducer)c, s = t.forks; s != null; s = t.forks = s.next)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   394
 *         t.result = reducer.apply(t.result, s.result);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   395
 *     }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   396
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   397
 *   public E getRawResult() { return result; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   398
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   399
 *   public static <E> E mapReduce(E[] array, MyMapper<E> mapper, MyReducer<E> reducer) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   400
 *     return new MapReducer<E>(null, array, mapper, reducer,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   401
 *                              0, array.length, null).invoke();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   402
 *   }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   403
 * }}</pre>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   404
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   405
 * <p><b>Triggers.</b> Some CountedCompleters are themselves never
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   406
 * forked, but instead serve as bits of plumbing in other designs;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   407
 * including those in which the completion of one or more async tasks
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   408
 * triggers another async task. For example:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   409
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   410
 * <pre> {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   411
 * class HeaderBuilder extends CountedCompleter<...> { ... }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   412
 * class BodyBuilder extends CountedCompleter<...> { ... }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   413
 * class PacketSender extends CountedCompleter<...> {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   414
 *   PacketSender(...) { super(null, 1); ... } // trigger on second completion
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   415
 *   public void compute() { } // never called
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   416
 *   public void onCompletion(CountedCompleter<?> caller) { sendPacket(); }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   417
 * }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   418
 * // sample use:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   419
 * PacketSender p = new PacketSender();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   420
 * new HeaderBuilder(p, ...).fork();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   421
 * new BodyBuilder(p, ...).fork();}</pre>
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   422
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   423
 * @since 1.8
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   424
 * @author Doug Lea
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   425
 */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   426
public abstract class CountedCompleter<T> extends ForkJoinTask<T> {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   427
    private static final long serialVersionUID = 5232453752276485070L;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   428
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   429
    /** This task's completer, or null if none */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   430
    final CountedCompleter<?> completer;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   431
    /** The number of pending tasks until completion */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   432
    volatile int pending;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   433
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   434
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   435
     * Creates a new CountedCompleter with the given completer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   436
     * and initial pending count.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   437
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   438
     * @param completer this task's completer, or {@code null} if none
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   439
     * @param initialPendingCount the initial pending count
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   440
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   441
    protected CountedCompleter(CountedCompleter<?> completer,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   442
                               int initialPendingCount) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   443
        this.completer = completer;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   444
        this.pending = initialPendingCount;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   445
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   446
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   447
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   448
     * Creates a new CountedCompleter with the given completer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   449
     * and an initial pending count of zero.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   450
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   451
     * @param completer this task's completer, or {@code null} if none
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   452
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   453
    protected CountedCompleter(CountedCompleter<?> completer) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   454
        this.completer = completer;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   455
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   456
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   457
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   458
     * Creates a new CountedCompleter with no completer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   459
     * and an initial pending count of zero.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   460
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   461
    protected CountedCompleter() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   462
        this.completer = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   463
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   464
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   465
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   466
     * The main computation performed by this task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   467
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   468
    public abstract void compute();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   469
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   470
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   471
     * Performs an action when method {@link #tryComplete} is invoked
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   472
     * and the pending count is zero, or when the unconditional
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   473
     * method {@link #complete} is invoked.  By default, this method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   474
     * does nothing. You can distinguish cases by checking the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   475
     * identity of the given caller argument. If not equal to {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   476
     * this}, then it is typically a subtask that may contain results
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   477
     * (and/or links to other results) to combine.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   478
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   479
     * @param caller the task invoking this method (which may
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   480
     * be this task itself)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   481
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   482
    public void onCompletion(CountedCompleter<?> caller) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   483
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   484
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   485
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   486
     * Performs an action when method {@link
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   487
     * #completeExceptionally(Throwable)} is invoked or method {@link
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   488
     * #compute} throws an exception, and this task has not already
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   489
     * otherwise completed normally. On entry to this method, this task
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   490
     * {@link ForkJoinTask#isCompletedAbnormally}.  The return value
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   491
     * of this method controls further propagation: If {@code true}
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   492
     * and this task has a completer that has not completed, then that
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   493
     * completer is also completed exceptionally, with the same
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   494
     * exception as this completer.  The default implementation of
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   495
     * this method does nothing except return {@code true}.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   496
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   497
     * @param ex the exception
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   498
     * @param caller the task invoking this method (which may
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   499
     * be this task itself)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   500
     * @return {@code true} if this exception should be propagated to this
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   501
     * task's completer, if one exists
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   502
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   503
    public boolean onExceptionalCompletion(Throwable ex, CountedCompleter<?> caller) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   504
        return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   505
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   506
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   507
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   508
     * Returns the completer established in this task's constructor,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   509
     * or {@code null} if none.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   510
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   511
     * @return the completer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   512
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   513
    public final CountedCompleter<?> getCompleter() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   514
        return completer;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   515
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   516
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   517
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   518
     * Returns the current pending count.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   519
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   520
     * @return the current pending count
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   521
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   522
    public final int getPendingCount() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   523
        return pending;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   524
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   525
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   526
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   527
     * Sets the pending count to the given value.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   528
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   529
     * @param count the count
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   530
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   531
    public final void setPendingCount(int count) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   532
        pending = count;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   533
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   534
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   535
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   536
     * Adds (atomically) the given value to the pending count.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   537
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   538
     * @param delta the value to add
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   539
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   540
    public final void addToPendingCount(int delta) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   541
        PENDING.getAndAdd(this, delta);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   542
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   543
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   544
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   545
     * Sets (atomically) the pending count to the given count only if
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   546
     * it currently holds the given expected value.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   547
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   548
     * @param expected the expected value
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   549
     * @param count the new value
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   550
     * @return {@code true} if successful
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   551
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   552
    public final boolean compareAndSetPendingCount(int expected, int count) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   553
        return PENDING.compareAndSet(this, expected, count);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   554
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   555
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   556
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   557
     * If the pending count is nonzero, (atomically) decrements it.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   558
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   559
     * @return the initial (undecremented) pending count holding on entry
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   560
     * to this method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   561
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   562
    public final int decrementPendingCountUnlessZero() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   563
        int c;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   564
        do {} while ((c = pending) != 0 &&
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   565
                     !PENDING.weakCompareAndSet(this, c, c - 1));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   566
        return c;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   567
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   568
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   569
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   570
     * Returns the root of the current computation; i.e., this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   571
     * task if it has no completer, else its completer's root.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   572
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   573
     * @return the root of the current computation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   574
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   575
    public final CountedCompleter<?> getRoot() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   576
        CountedCompleter<?> a = this, p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   577
        while ((p = a.completer) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   578
            a = p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   579
        return a;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   580
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   581
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   582
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   583
     * If the pending count is nonzero, decrements the count;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   584
     * otherwise invokes {@link #onCompletion(CountedCompleter)}
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   585
     * and then similarly tries to complete this task's completer,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   586
     * if one exists, else marks this task as complete.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   587
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   588
    public final void tryComplete() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   589
        CountedCompleter<?> a = this, s = a;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   590
        for (int c;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   591
            if ((c = a.pending) == 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   592
                a.onCompletion(s);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   593
                if ((a = (s = a).completer) == null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   594
                    s.quietlyComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   595
                    return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   596
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   597
            }
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   598
            else if (PENDING.weakCompareAndSet(a, c, c - 1))
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   599
                return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   600
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   601
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   602
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   603
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   604
     * Equivalent to {@link #tryComplete} but does not invoke {@link
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   605
     * #onCompletion(CountedCompleter)} along the completion path:
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   606
     * If the pending count is nonzero, decrements the count;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   607
     * otherwise, similarly tries to complete this task's completer, if
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   608
     * one exists, else marks this task as complete. This method may be
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   609
     * useful in cases where {@code onCompletion} should not, or need
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   610
     * not, be invoked for each completer in a computation.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   611
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   612
    public final void propagateCompletion() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 34348
diff changeset
   613
        CountedCompleter<?> a = this, s;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   614
        for (int c;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   615
            if ((c = a.pending) == 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   616
                if ((a = (s = a).completer) == null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   617
                    s.quietlyComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   618
                    return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   619
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   620
            }
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   621
            else if (PENDING.weakCompareAndSet(a, c, c - 1))
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   622
                return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   623
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   624
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   625
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   626
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   627
     * Regardless of pending count, invokes
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   628
     * {@link #onCompletion(CountedCompleter)}, marks this task as
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   629
     * complete and further triggers {@link #tryComplete} on this
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   630
     * task's completer, if one exists.  The given rawResult is
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   631
     * used as an argument to {@link #setRawResult} before invoking
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   632
     * {@link #onCompletion(CountedCompleter)} or marking this task
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   633
     * as complete; its value is meaningful only for classes
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   634
     * overriding {@code setRawResult}.  This method does not modify
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   635
     * the pending count.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   636
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   637
     * <p>This method may be useful when forcing completion as soon as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   638
     * any one (versus all) of several subtask results are obtained.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   639
     * However, in the common (and recommended) case in which {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   640
     * setRawResult} is not overridden, this effect can be obtained
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   641
     * more simply using {@link #quietlyCompleteRoot()}.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   642
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   643
     * @param rawResult the raw result
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   644
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   645
    public void complete(T rawResult) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   646
        CountedCompleter<?> p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   647
        setRawResult(rawResult);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   648
        onCompletion(this);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   649
        quietlyComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   650
        if ((p = completer) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   651
            p.tryComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   652
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   653
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   654
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   655
     * If this task's pending count is zero, returns this task;
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   656
     * otherwise decrements its pending count and returns {@code null}.
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   657
     * This method is designed to be used with {@link #nextComplete} in
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 33674
diff changeset
   658
     * completion traversal loops.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   659
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   660
     * @return this task, if pending count was zero, else {@code null}
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   661
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   662
    public final CountedCompleter<?> firstComplete() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   663
        for (int c;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   664
            if ((c = pending) == 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   665
                return this;
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39725
diff changeset
   666
            else if (PENDING.weakCompareAndSet(this, c, c - 1))
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   667
                return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   668
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   669
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   670
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   671
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   672
     * If this task does not have a completer, invokes {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   673
     * ForkJoinTask#quietlyComplete} and returns {@code null}.  Or, if
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   674
     * the completer's pending count is non-zero, decrements that
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   675
     * pending count and returns {@code null}.  Otherwise, returns the
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   676
     * completer.  This method can be used as part of a completion
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   677
     * traversal loop for homogeneous task hierarchies:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   678
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   679
     * <pre> {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   680
     * for (CountedCompleter<?> c = firstComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   681
     *      c != null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   682
     *      c = c.nextComplete()) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   683
     *   // ... process c ...
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   684
     * }}</pre>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   685
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   686
     * @return the completer, or {@code null} if none
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   687
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   688
    public final CountedCompleter<?> nextComplete() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   689
        CountedCompleter<?> p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   690
        if ((p = completer) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   691
            return p.firstComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   692
        else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   693
            quietlyComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   694
            return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   695
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   696
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   697
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   698
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   699
     * Equivalent to {@code getRoot().quietlyComplete()}.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   700
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   701
    public final void quietlyCompleteRoot() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   702
        for (CountedCompleter<?> a = this, p;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   703
            if ((p = a.completer) == null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   704
                a.quietlyComplete();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   705
                return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   706
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   707
            a = p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   708
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   709
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   710
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   711
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   712
     * If this task has not completed, attempts to process at most the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   713
     * given number of other unprocessed tasks for which this task is
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   714
     * on the completion path, if any are known to exist.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   715
     *
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   716
     * @param maxTasks the maximum number of tasks to process.  If
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   717
     *                 less than or equal to zero, then no tasks are
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   718
     *                 processed.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   719
     */
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   720
    public final void helpComplete(int maxTasks) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   721
        Thread t; ForkJoinWorkerThread wt;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   722
        if (maxTasks > 0 && status >= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   723
            if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   724
                (wt = (ForkJoinWorkerThread)t).pool.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   725
                    helpComplete(wt.workQueue, this, maxTasks);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   726
            else
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   727
                ForkJoinPool.common.externalHelpComplete(this, maxTasks);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   728
        }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   729
    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   730
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   731
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   732
     * Supports ForkJoinTask exception propagation.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   733
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   734
    void internalPropagateException(Throwable ex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   735
        CountedCompleter<?> a = this, s = a;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   736
        while (a.onExceptionalCompletion(ex, s) &&
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   737
               (a = (s = a).completer) != null && a.status >= 0 &&
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47216
diff changeset
   738
               isExceptionalStatus(a.recordExceptionalCompletion(ex)))
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   739
            ;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   740
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   741
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   742
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   743
     * Implements execution conventions for CountedCompleters.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   744
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   745
    protected final boolean exec() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   746
        compute();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   747
        return false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   748
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   749
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   750
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   751
     * Returns the result of the computation.  By default,
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   752
     * returns {@code null}, which is appropriate for {@code Void}
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   753
     * actions, but in other cases should be overridden, almost
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   754
     * always to return a field or function of a field that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   755
     * holds the result upon completion.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   756
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   757
     * @return the result of the computation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   758
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   759
    public T getRawResult() { return null; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   760
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   761
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   762
     * A method that result-bearing CountedCompleters may optionally
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   763
     * use to help maintain result data.  By default, does nothing.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   764
     * Overrides are not recommended. However, if this method is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   765
     * overridden to update existing objects or fields, then it must
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   766
     * in general be defined to be thread-safe.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   767
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   768
    protected void setRawResult(T t) { }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   769
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   770
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   771
    private static final VarHandle PENDING;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   772
    static {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   773
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   774
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   775
            PENDING = l.findVarHandle(CountedCompleter.class, "pending", int.class);
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   776
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   777
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 48233
diff changeset
   778
            throw new ExceptionInInitializerError(e);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   779
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   780
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
diff changeset
   781
}