src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java
author darcy
Wed, 16 Oct 2019 16:55:52 -0700
changeset 58657 6252605fb005
parent 52730 345266000aba
permissions -rw-r--r--
8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     1
/*
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     3
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     9
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    14
 * accompanied this code).
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    15
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    23
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    24
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    25
/*
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    29
 * file:
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    30
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 8765
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    34
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    35
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    36
package java.util.concurrent;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    37
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    38
import java.io.Serializable;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
    39
import java.lang.invoke.MethodHandles;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
    40
import java.lang.invoke.VarHandle;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
    41
import java.lang.ref.ReferenceQueue;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
    42
import java.lang.ref.WeakReference;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
    43
import java.lang.reflect.Constructor;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    44
import java.util.Collection;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    45
import java.util.List;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    46
import java.util.RandomAccess;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
    47
import java.util.concurrent.locks.ReentrantLock;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    48
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    49
/**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    50
 * Abstract base class for tasks that run within a {@link ForkJoinPool}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    51
 * A {@code ForkJoinTask} is a thread-like entity that is much
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    52
 * lighter weight than a normal thread.  Huge numbers of tasks and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    53
 * subtasks may be hosted by a small number of actual threads in a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    54
 * ForkJoinPool, at the price of some usage limitations.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    55
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    56
 * <p>A "main" {@code ForkJoinTask} begins execution when it is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    57
 * explicitly submitted to a {@link ForkJoinPool}, or, if not already
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    58
 * engaged in a ForkJoin computation, commenced in the {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    59
 * ForkJoinPool#commonPool()} via {@link #fork}, {@link #invoke}, or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    60
 * related methods.  Once started, it will usually in turn start other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    61
 * subtasks.  As indicated by the name of this class, many programs
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    62
 * using {@code ForkJoinTask} employ only methods {@link #fork} and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    63
 * {@link #join}, or derivatives such as {@link
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
    64
 * #invokeAll(ForkJoinTask...) invokeAll}.  However, this class also
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
    65
 * provides a number of other methods that can come into play in
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    66
 * advanced usages, as well as extension mechanics that allow support
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    67
 * of new forms of fork/join processing.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    68
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    69
 * <p>A {@code ForkJoinTask} is a lightweight form of {@link Future}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    70
 * The efficiency of {@code ForkJoinTask}s stems from a set of
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    71
 * restrictions (that are only partially statically enforceable)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    72
 * reflecting their main use as computational tasks calculating pure
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    73
 * functions or operating on purely isolated objects.  The primary
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    74
 * coordination mechanisms are {@link #fork}, that arranges
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    75
 * asynchronous execution, and {@link #join}, that doesn't proceed
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    76
 * until the task's result has been computed.  Computations should
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    77
 * ideally avoid {@code synchronized} methods or blocks, and should
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    78
 * minimize other blocking synchronization apart from joining other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    79
 * tasks or using synchronizers such as Phasers that are advertised to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    80
 * cooperate with fork/join scheduling. Subdividable tasks should also
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    81
 * not perform blocking I/O, and should ideally access variables that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    82
 * are completely independent of those accessed by other running
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    83
 * tasks. These guidelines are loosely enforced by not permitting
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    84
 * checked exceptions such as {@code IOExceptions} to be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    85
 * thrown. However, computations may still encounter unchecked
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    86
 * exceptions, that are rethrown to callers attempting to join
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    87
 * them. These exceptions may additionally include {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    88
 * RejectedExecutionException} stemming from internal resource
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    89
 * exhaustion, such as failure to allocate internal task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    90
 * queues. Rethrown exceptions behave in the same way as regular
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    91
 * exceptions, but, when possible, contain stack traces (as displayed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    92
 * for example using {@code ex.printStackTrace()}) of both the thread
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    93
 * that initiated the computation as well as the thread actually
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    94
 * encountering the exception; minimally only the latter.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    95
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    96
 * <p>It is possible to define and use ForkJoinTasks that may block,
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
    97
 * but doing so requires three further considerations: (1) Completion
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    98
 * of few if any <em>other</em> tasks should be dependent on a task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    99
 * that blocks on external synchronization or I/O. Event-style async
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   100
 * tasks that are never joined (for example, those subclassing {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   101
 * CountedCompleter}) often fall into this category.  (2) To minimize
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   102
 * resource impact, tasks should be small; ideally performing only the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   103
 * (possibly) blocking action. (3) Unless the {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   104
 * ForkJoinPool.ManagedBlocker} API is used, or the number of possibly
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   105
 * blocked tasks is known to be less than the pool's {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   106
 * ForkJoinPool#getParallelism} level, the pool cannot guarantee that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   107
 * enough threads will be available to ensure progress or good
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   108
 * performance.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   109
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   110
 * <p>The primary method for awaiting completion and extracting
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   111
 * results of a task is {@link #join}, but there are several variants:
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   112
 * The {@link Future#get} methods support interruptible and/or timed
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   113
 * waits for completion and report results using {@code Future}
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   114
 * conventions. Method {@link #invoke} is semantically
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   115
 * equivalent to {@code fork(); join()} but always attempts to begin
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   116
 * execution in the current thread. The "<em>quiet</em>" forms of
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   117
 * these methods do not extract results or report exceptions. These
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   118
 * may be useful when a set of tasks are being executed, and you need
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   119
 * to delay processing of results or exceptions until all complete.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   120
 * Method {@code invokeAll} (available in multiple versions)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   121
 * performs the most common form of parallel invocation: forking a set
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   122
 * of tasks and joining them all.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   123
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   124
 * <p>In the most typical usages, a fork-join pair act like a call
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   125
 * (fork) and return (join) from a parallel recursive function. As is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   126
 * the case with other forms of recursive calls, returns (joins)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   127
 * should be performed innermost-first. For example, {@code a.fork();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   128
 * b.fork(); b.join(); a.join();} is likely to be substantially more
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   129
 * efficient than joining {@code a} before {@code b}.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   130
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   131
 * <p>The execution status of tasks may be queried at several levels
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   132
 * of detail: {@link #isDone} is true if a task completed in any way
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   133
 * (including the case where a task was cancelled without executing);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   134
 * {@link #isCompletedNormally} is true if a task completed without
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   135
 * cancellation or encountering an exception; {@link #isCancelled} is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   136
 * true if the task was cancelled (in which case {@link #getException}
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   137
 * returns a {@link CancellationException}); and
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   138
 * {@link #isCompletedAbnormally} is true if a task was either
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   139
 * cancelled or encountered an exception, in which case {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   140
 * #getException} will return either the encountered exception or
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   141
 * {@link CancellationException}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   142
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   143
 * <p>The ForkJoinTask class is not usually directly subclassed.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   144
 * Instead, you subclass one of the abstract classes that support a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   145
 * particular style of fork/join processing, typically {@link
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   146
 * RecursiveAction} for most computations that do not return results,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   147
 * {@link RecursiveTask} for those that do, and {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   148
 * CountedCompleter} for those in which completed actions trigger
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   149
 * other actions.  Normally, a concrete ForkJoinTask subclass declares
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   150
 * fields comprising its parameters, established in a constructor, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   151
 * then defines a {@code compute} method that somehow uses the control
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   152
 * methods supplied by this base class.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   153
 *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   154
 * <p>Method {@link #join} and its variants are appropriate for use
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   155
 * only when completion dependencies are acyclic; that is, the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   156
 * parallel computation can be described as a directed acyclic graph
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   157
 * (DAG). Otherwise, executions may encounter a form of deadlock as
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   158
 * tasks cyclically wait for each other.  However, this framework
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   159
 * supports other methods and techniques (for example the use of
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   160
 * {@link Phaser}, {@link #helpQuiesce}, and {@link #complete}) that
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   161
 * may be of use in constructing custom subclasses for problems that
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   162
 * are not statically structured as DAGs. To support such usages, a
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   163
 * ForkJoinTask may be atomically <em>tagged</em> with a {@code short}
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   164
 * value using {@link #setForkJoinTaskTag} or {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   165
 * #compareAndSetForkJoinTaskTag} and checked using {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   166
 * #getForkJoinTaskTag}. The ForkJoinTask implementation does not use
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   167
 * these {@code protected} methods or tags for any purpose, but they
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   168
 * may be of use in the construction of specialized subclasses.  For
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   169
 * example, parallel graph traversals can use the supplied methods to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   170
 * avoid revisiting nodes/tasks that have already been processed.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   171
 * (Method names for tagging are bulky in part to encourage definition
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   172
 * of methods that reflect their usage patterns.)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   173
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   174
 * <p>Most base support methods are {@code final}, to prevent
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   175
 * overriding of implementations that are intrinsically tied to the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   176
 * underlying lightweight task scheduling framework.  Developers
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   177
 * creating new basic styles of fork/join processing should minimally
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   178
 * implement {@code protected} methods {@link #exec}, {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   179
 * #setRawResult}, and {@link #getRawResult}, while also introducing
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   180
 * an abstract computational method that can be implemented in its
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   181
 * subclasses, possibly relying on other {@code protected} methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   182
 * provided by this class.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   183
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   184
 * <p>ForkJoinTasks should perform relatively small amounts of
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   185
 * computation. Large tasks should be split into smaller subtasks,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   186
 * usually via recursive decomposition. As a very rough rule of thumb,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   187
 * a task should perform more than 100 and less than 10000 basic
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   188
 * computational steps, and should avoid indefinite looping. If tasks
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   189
 * are too big, then parallelism cannot improve throughput. If too
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   190
 * small, then memory and internal task maintenance overhead may
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   191
 * overwhelm processing.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   192
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   193
 * <p>This class provides {@code adapt} methods for {@link Runnable}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   194
 * and {@link Callable}, that may be of use when mixing execution of
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   195
 * {@code ForkJoinTasks} with other kinds of tasks. When all tasks are
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   196
 * of this form, consider using a pool constructed in <em>asyncMode</em>.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   197
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   198
 * <p>ForkJoinTasks are {@code Serializable}, which enables them to be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   199
 * used in extensions such as remote execution frameworks. It is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   200
 * sensible to serialize tasks only before or after, but not during,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   201
 * execution. Serialization is not relied on during execution itself.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   202
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   203
 * @since 1.7
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   204
 * @author Doug Lea
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   205
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   206
public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   207
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   208
    /*
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   209
     * See the internal documentation of class ForkJoinPool for a
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   210
     * general implementation overview.  ForkJoinTasks are mainly
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   211
     * responsible for maintaining their "status" field amidst relays
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   212
     * to methods in ForkJoinWorkerThread and ForkJoinPool.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   213
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   214
     * The methods of this class are more-or-less layered into
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   215
     * (1) basic status maintenance
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   216
     * (2) execution and awaiting completion
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   217
     * (3) user-level methods that additionally report results.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   218
     * This is sometimes hard to see because this file orders exported
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   219
     * methods in a way that flows well in javadocs.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   220
     */
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   221
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   222
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   223
     * The status field holds run control status bits packed into a
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   224
     * single int to ensure atomicity.  Status is initially zero, and
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   225
     * takes on nonnegative values until completed, upon which it
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   226
     * holds (sign bit) DONE, possibly with ABNORMAL (cancelled or
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   227
     * exceptional) and THROWN (in which case an exception has been
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   228
     * stored). Tasks with dependent blocked waiting joiners have the
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   229
     * SIGNAL bit set.  Completion of a task with SIGNAL set awakens
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   230
     * any waiters via notifyAll. (Waiters also help signal others
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   231
     * upon completion.)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   232
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   233
     * These control bits occupy only (some of) the upper half (16
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   234
     * bits) of status field. The lower bits are used for user-defined
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   235
     * tags.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   236
     */
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   237
    volatile int status; // accessed directly by pool and workers
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   238
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   239
    private static final int DONE     = 1 << 31; // must be negative
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   240
    private static final int ABNORMAL = 1 << 18; // set atomically with DONE
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   241
    private static final int THROWN   = 1 << 17; // set atomically with ABNORMAL
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   242
    private static final int SIGNAL   = 1 << 16; // true if joiner waiting
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   243
    private static final int SMASK    = 0xffff;  // short bits for tags
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   244
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   245
    static boolean isExceptionalStatus(int s) {  // needed by subclasses
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   246
        return (s & THROWN) != 0;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   247
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   248
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   249
    /**
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   250
     * Sets DONE status and wakes up threads waiting to join this task.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   251
     *
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   252
     * @return status on exit
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   253
     */
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   254
    private int setDone() {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   255
        int s;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   256
        if (((s = (int)STATUS.getAndBitwiseOr(this, DONE)) & SIGNAL) != 0)
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   257
            synchronized (this) { notifyAll(); }
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   258
        return s | DONE;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   259
    }
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   260
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   261
    /**
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   262
     * Marks cancelled or exceptional completion unless already done.
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   263
     *
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   264
     * @param completion must be DONE | ABNORMAL, ORed with THROWN if exceptional
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   265
     * @return status on exit
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   266
     */
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   267
    private int abnormalCompletion(int completion) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   268
        for (int s, ns;;) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   269
            if ((s = status) < 0)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   270
                return s;
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   271
            else if (STATUS.weakCompareAndSet(this, s, ns = s | completion)) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   272
                if ((s & SIGNAL) != 0)
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   273
                    synchronized (this) { notifyAll(); }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   274
                return ns;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   275
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   276
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   277
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   278
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   279
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   280
     * Primary execution method for stolen tasks. Unless done, calls
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   281
     * exec and records status if completed, but doesn't wait for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   282
     * completion otherwise.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   283
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   284
     * @return status on exit from this method
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   285
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   286
    final int doExec() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   287
        int s; boolean completed;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   288
        if ((s = status) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   289
            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   290
                completed = exec();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   291
            } catch (Throwable rex) {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   292
                completed = false;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   293
                s = setExceptionalCompletion(rex);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   294
            }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   295
            if (completed)
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   296
                s = setDone();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   297
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   298
        return s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   299
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   300
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   301
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   302
     * If not done, sets SIGNAL status and performs Object.wait(timeout).
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   303
     * This task may or may not be done on exit. Ignores interrupts.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   304
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   305
     * @param timeout using Object.wait conventions.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   306
     */
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   307
    final void internalWait(long timeout) {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   308
        if ((int)STATUS.getAndBitwiseOr(this, SIGNAL) >= 0) {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   309
            synchronized (this) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   310
                if (status >= 0)
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   311
                    try { wait(timeout); } catch (InterruptedException ie) { }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   312
                else
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   313
                    notifyAll();
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   314
            }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   315
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   316
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   317
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   318
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   319
     * Blocks a non-worker-thread until completion.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   320
     * @return status upon completion
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   321
     */
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   322
    private int externalAwaitDone() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   323
        int s = tryExternalHelp();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   324
        if (s >= 0 && (s = (int)STATUS.getAndBitwiseOr(this, SIGNAL)) >= 0) {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   325
            boolean interrupted = false;
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   326
            synchronized (this) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   327
                for (;;) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   328
                    if ((s = status) >= 0) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   329
                        try {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   330
                            wait(0L);
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   331
                        } catch (InterruptedException ie) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   332
                            interrupted = true;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   333
                        }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   334
                    }
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   335
                    else {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   336
                        notifyAll();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   337
                        break;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   338
                    }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   339
                }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   340
            }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   341
            if (interrupted)
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   342
                Thread.currentThread().interrupt();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   343
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   344
        return s;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   345
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   346
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   347
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   348
     * Blocks a non-worker-thread until completion or interruption.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   349
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   350
    private int externalInterruptibleAwaitDone() throws InterruptedException {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   351
        int s = tryExternalHelp();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   352
        if (s >= 0 && (s = (int)STATUS.getAndBitwiseOr(this, SIGNAL)) >= 0) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   353
            synchronized (this) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   354
                for (;;) {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   355
                    if ((s = status) >= 0)
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   356
                        wait(0L);
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   357
                    else {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   358
                        notifyAll();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   359
                        break;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   360
                    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   361
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   362
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   363
        }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   364
        else if (Thread.interrupted())
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   365
            throw new InterruptedException();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   366
        return s;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   367
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   368
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   369
    /**
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   370
     * Tries to help with tasks allowed for external callers.
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   371
     *
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   372
     * @return current status
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   373
     */
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   374
    private int tryExternalHelp() {
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   375
        int s;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   376
        return ((s = status) < 0 ? s:
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   377
                (this instanceof CountedCompleter) ?
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   378
                ForkJoinPool.common.externalHelpComplete(
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   379
                    (CountedCompleter<?>)this, 0) :
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   380
                ForkJoinPool.common.tryExternalUnpush(this) ?
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   381
                doExec() : 0);
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   382
    }
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   383
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   384
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   385
     * Implementation for join, get, quietlyJoin. Directly handles
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   386
     * only cases of already-completed, external wait, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   387
     * unfork+exec.  Others are relayed to ForkJoinPool.awaitJoin.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   388
     *
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   389
     * @return status upon completion
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   390
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   391
    private int doJoin() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   392
        int s; Thread t; ForkJoinWorkerThread wt; ForkJoinPool.WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   393
        return (s = status) < 0 ? s :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   394
            ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   395
            (w = (wt = (ForkJoinWorkerThread)t).workQueue).
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   396
            tryUnpush(this) && (s = doExec()) < 0 ? s :
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   397
            wt.pool.awaitJoin(w, this, 0L) :
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   398
            externalAwaitDone();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   399
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   400
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   401
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   402
     * Implementation for invoke, quietlyInvoke.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   403
     *
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   404
     * @return status upon completion
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   405
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   406
    private int doInvoke() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   407
        int s; Thread t; ForkJoinWorkerThread wt;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   408
        return (s = doExec()) < 0 ? s :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   409
            ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   410
            (wt = (ForkJoinWorkerThread)t).pool.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   411
            awaitJoin(wt.workQueue, this, 0L) :
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   412
            externalAwaitDone();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   413
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   414
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   415
    // Exception table support
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   416
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   417
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   418
     * Hash table of exceptions thrown by tasks, to enable reporting
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   419
     * by callers. Because exceptions are rare, we don't directly keep
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   420
     * them with task objects, but instead use a weak ref table.  Note
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   421
     * that cancellation exceptions don't appear in the table, but are
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   422
     * instead recorded as status values.
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   423
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   424
     * The exception table has a fixed capacity.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   425
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   426
    private static final ExceptionNode[] exceptionTable
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   427
        = new ExceptionNode[32];
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   428
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   429
    /** Lock protecting access to exceptionTable. */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   430
    private static final ReentrantLock exceptionTableLock
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   431
        = new ReentrantLock();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   432
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   433
    /** Reference queue of stale exceptionally completed tasks. */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   434
    private static final ReferenceQueue<ForkJoinTask<?>> exceptionTableRefQueue
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 39723
diff changeset
   435
        = new ReferenceQueue<>();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   436
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   437
    /**
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   438
     * Key-value nodes for exception table.  The chained hash table
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   439
     * uses identity comparisons, full locking, and weak references
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   440
     * for keys. The table has a fixed capacity because it only
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   441
     * maintains task exceptions long enough for joiners to access
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   442
     * them, so should never become very large for sustained
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   443
     * periods. However, since we do not know when the last joiner
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   444
     * completes, we must use weak references and expunge them. We do
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   445
     * so on each operation (hence full locking). Also, some thread in
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   446
     * any ForkJoinPool will call helpExpungeStaleExceptions when its
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   447
     * pool becomes isQuiescent.
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   448
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   449
    static final class ExceptionNode extends WeakReference<ForkJoinTask<?>> {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   450
        final Throwable ex;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   451
        ExceptionNode next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   452
        final long thrower;  // use id not ref to avoid weak cycles
20882
5d8c39217c1b 8004138: ForkJoinTask leaks exceptions
mduigou
parents: 19048
diff changeset
   453
        final int hashCode;  // store task hashCode before weak ref disappears
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   454
        ExceptionNode(ForkJoinTask<?> task, Throwable ex, ExceptionNode next,
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   455
                      ReferenceQueue<ForkJoinTask<?>> exceptionTableRefQueue) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   456
            super(task, exceptionTableRefQueue);
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   457
            this.ex = ex;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   458
            this.next = next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   459
            this.thrower = Thread.currentThread().getId();
20882
5d8c39217c1b 8004138: ForkJoinTask leaks exceptions
mduigou
parents: 19048
diff changeset
   460
            this.hashCode = System.identityHashCode(task);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   461
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   462
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   463
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   464
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   465
     * Records exception and sets status.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   466
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   467
     * @return status on exit
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   468
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   469
    final int recordExceptionalCompletion(Throwable ex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   470
        int s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   471
        if ((s = status) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   472
            int h = System.identityHashCode(this);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   473
            final ReentrantLock lock = exceptionTableLock;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   474
            lock.lock();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   475
            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   476
                expungeStaleExceptions();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   477
                ExceptionNode[] t = exceptionTable;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   478
                int i = h & (t.length - 1);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   479
                for (ExceptionNode e = t[i]; ; e = e.next) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   480
                    if (e == null) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   481
                        t[i] = new ExceptionNode(this, ex, t[i],
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   482
                                                 exceptionTableRefQueue);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   483
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   484
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   485
                    if (e.get() == this) // already present
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   486
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   487
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   488
            } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   489
                lock.unlock();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   490
            }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   491
            s = abnormalCompletion(DONE | ABNORMAL | THROWN);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   492
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   493
        return s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   494
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   495
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   496
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
   497
     * Records exception and possibly propagates.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   498
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   499
     * @return status on exit
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   500
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   501
    private int setExceptionalCompletion(Throwable ex) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   502
        int s = recordExceptionalCompletion(ex);
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   503
        if ((s & THROWN) != 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   504
            internalPropagateException(ex);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   505
        return s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   506
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   507
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   508
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   509
     * Hook for exception propagation support for tasks with completers.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   510
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   511
    void internalPropagateException(Throwable ex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   512
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   513
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   514
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   515
     * Cancels, ignoring any exceptions thrown by cancel. Used during
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   516
     * worker and pool shutdown. Cancel is spec'ed not to throw any
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   517
     * exceptions, but if it does anyway, we have no recourse during
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   518
     * shutdown, so guard against this case.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   519
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   520
    static final void cancelIgnoringExceptions(ForkJoinTask<?> t) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   521
        if (t != null && t.status >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   522
            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   523
                t.cancel(false);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   524
            } catch (Throwable ignore) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   525
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   526
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   527
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   528
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   529
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
   530
     * Removes exception node and clears status.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   531
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   532
    private void clearExceptionalCompletion() {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   533
        int h = System.identityHashCode(this);
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   534
        final ReentrantLock lock = exceptionTableLock;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   535
        lock.lock();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   536
        try {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   537
            ExceptionNode[] t = exceptionTable;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   538
            int i = h & (t.length - 1);
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   539
            ExceptionNode e = t[i];
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   540
            ExceptionNode pred = null;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   541
            while (e != null) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   542
                ExceptionNode next = e.next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   543
                if (e.get() == this) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   544
                    if (pred == null)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   545
                        t[i] = next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   546
                    else
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   547
                        pred.next = next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   548
                    break;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   549
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   550
                pred = e;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   551
                e = next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   552
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   553
            expungeStaleExceptions();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   554
            status = 0;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   555
        } finally {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   556
            lock.unlock();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   557
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   558
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   559
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   560
    /**
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   561
     * Returns a rethrowable exception for this task, if available.
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   562
     * To provide accurate stack traces, if the exception was not
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   563
     * thrown by the current thread, we try to create a new exception
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   564
     * of the same type as the one thrown, but with the recorded
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   565
     * exception as its cause. If there is no such constructor, we
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   566
     * instead try to use a no-arg constructor, followed by initCause,
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   567
     * to the same effect. If none of these apply, or any fail due to
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   568
     * other exceptions, we return the recorded exception, which is
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   569
     * still correct, although it may contain a misleading stack
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   570
     * trace.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   571
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   572
     * @return the exception, or null if none
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   573
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   574
    private Throwable getThrowableException() {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   575
        int h = System.identityHashCode(this);
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   576
        ExceptionNode e;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   577
        final ReentrantLock lock = exceptionTableLock;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   578
        lock.lock();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   579
        try {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   580
            expungeStaleExceptions();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   581
            ExceptionNode[] t = exceptionTable;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   582
            e = t[h & (t.length - 1)];
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   583
            while (e != null && e.get() != this)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   584
                e = e.next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   585
        } finally {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   586
            lock.unlock();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   587
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   588
        Throwable ex;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   589
        if (e == null || (ex = e.ex) == null)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   590
            return null;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   591
        if (e.thrower != Thread.currentThread().getId()) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   592
            try {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   593
                Constructor<?> noArgCtor = null;
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   594
                // public ctors only
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   595
                for (Constructor<?> c : ex.getClass().getConstructors()) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   596
                    Class<?>[] ps = c.getParameterTypes();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   597
                    if (ps.length == 0)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   598
                        noArgCtor = c;
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   599
                    else if (ps.length == 1 && ps[0] == Throwable.class)
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   600
                        return (Throwable)c.newInstance(ex);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   601
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   602
                if (noArgCtor != null) {
35981
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   603
                    Throwable wx = (Throwable)noArgCtor.newInstance();
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   604
                    wx.initCause(ex);
e3e89c0bb3d9 8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents: 35302
diff changeset
   605
                    return wx;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   606
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   607
            } catch (Exception ignore) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   608
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   609
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   610
        return ex;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   611
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   612
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   613
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   614
     * Polls stale refs and removes them. Call only while holding lock.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   615
     */
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   616
    private static void expungeStaleExceptions() {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   617
        for (Object x; (x = exceptionTableRefQueue.poll()) != null;) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   618
            if (x instanceof ExceptionNode) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   619
                ExceptionNode[] t = exceptionTable;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
   620
                int i = ((ExceptionNode)x).hashCode & (t.length - 1);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   621
                ExceptionNode e = t[i];
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   622
                ExceptionNode pred = null;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   623
                while (e != null) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   624
                    ExceptionNode next = e.next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   625
                    if (e == x) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   626
                        if (pred == null)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   627
                            t[i] = next;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   628
                        else
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   629
                            pred.next = next;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   630
                        break;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   631
                    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   632
                    pred = e;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   633
                    e = next;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   634
                }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   635
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   636
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   637
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   638
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   639
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   640
     * If lock is available, polls stale refs and removes them.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   641
     * Called from ForkJoinPool when pools become quiescent.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   642
     */
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   643
    static final void helpExpungeStaleExceptions() {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   644
        final ReentrantLock lock = exceptionTableLock;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   645
        if (lock.tryLock()) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   646
            try {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   647
                expungeStaleExceptions();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   648
            } finally {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   649
                lock.unlock();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   650
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   651
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   652
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   653
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   654
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   655
     * A version of "sneaky throw" to relay exceptions.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   656
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   657
    static void rethrow(Throwable ex) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   658
        ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   659
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   660
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   661
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   662
     * The sneaky part of sneaky throw, relying on generics
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   663
     * limitations to evade compiler complaints about rethrowing
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   664
     * unchecked exceptions.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   665
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   666
    @SuppressWarnings("unchecked") static <T extends Throwable>
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   667
    void uncheckedThrow(Throwable t) throws T {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   668
        if (t != null)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   669
            throw (T)t; // rely on vacuous cast
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   670
        else
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   671
            throw new Error("Unknown Exception");
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   672
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   673
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   674
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   675
     * Throws exception, if any, associated with the given status.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   676
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   677
    private void reportException(int s) {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   678
        rethrow((s & THROWN) != 0 ? getThrowableException() :
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   679
                new CancellationException());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   680
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   681
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   682
    // public methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   683
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   684
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   685
     * Arranges to asynchronously execute this task in the pool the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   686
     * current task is running in, if applicable, or using the {@link
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   687
     * ForkJoinPool#commonPool()} if not {@link #inForkJoinPool}.  While
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   688
     * it is not necessarily enforced, it is a usage error to fork a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   689
     * task more than once unless it has completed and been
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   690
     * reinitialized.  Subsequent modifications to the state of this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   691
     * task or any data it operates on are not necessarily
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   692
     * consistently observable by any thread other than the one
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   693
     * executing it unless preceded by a call to {@link #join} or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   694
     * related methods, or a call to {@link #isDone} returning {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   695
     * true}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   696
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   697
     * @return {@code this}, to simplify usage
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   698
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   699
    public final ForkJoinTask<V> fork() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   700
        Thread t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   701
        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   702
            ((ForkJoinWorkerThread)t).workQueue.push(this);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   703
        else
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
   704
            ForkJoinPool.common.externalPush(this);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   705
        return this;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   706
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   707
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   708
    /**
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   709
     * Returns the result of the computation when it
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   710
     * {@linkplain #isDone is done}.
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   711
     * This method differs from {@link #get()} in that abnormal
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   712
     * completion results in {@code RuntimeException} or {@code Error},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   713
     * not {@code ExecutionException}, and that interrupts of the
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   714
     * calling thread do <em>not</em> cause the method to abruptly
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 42322
diff changeset
   715
     * return by throwing {@code InterruptedException}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   716
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   717
     * @return the computed result
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   718
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   719
    public final V join() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   720
        int s;
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   721
        if (((s = doJoin()) & ABNORMAL) != 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   722
            reportException(s);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   723
        return getRawResult();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   724
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   725
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   726
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   727
     * Commences performing this task, awaits its completion if
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   728
     * necessary, and returns its result, or throws an (unchecked)
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   729
     * {@code RuntimeException} or {@code Error} if the underlying
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   730
     * computation did so.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   731
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   732
     * @return the computed result
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   733
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   734
    public final V invoke() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   735
        int s;
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   736
        if (((s = doInvoke()) & ABNORMAL) != 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   737
            reportException(s);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   738
        return getRawResult();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   739
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   740
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   741
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   742
     * Forks the given tasks, returning when {@code isDone} holds for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   743
     * each task or an (unchecked) exception is encountered, in which
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   744
     * case the exception is rethrown. If more than one task
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   745
     * encounters an exception, then this method throws any one of
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   746
     * these exceptions. If any task encounters an exception, the
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   747
     * other may be cancelled. However, the execution status of
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   748
     * individual tasks is not guaranteed upon exceptional return. The
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   749
     * status of each task may be obtained using {@link
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   750
     * #getException()} and related methods to check if they have been
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   751
     * cancelled, completed normally or exceptionally, or left
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   752
     * unprocessed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   753
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   754
     * @param t1 the first task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   755
     * @param t2 the second task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   756
     * @throws NullPointerException if any task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   757
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   758
    public static void invokeAll(ForkJoinTask<?> t1, ForkJoinTask<?> t2) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   759
        int s1, s2;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   760
        t2.fork();
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   761
        if (((s1 = t1.doInvoke()) & ABNORMAL) != 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   762
            t1.reportException(s1);
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   763
        if (((s2 = t2.doJoin()) & ABNORMAL) != 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   764
            t2.reportException(s2);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   765
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   766
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   767
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   768
     * Forks the given tasks, returning when {@code isDone} holds for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   769
     * each task or an (unchecked) exception is encountered, in which
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   770
     * case the exception is rethrown. If more than one task
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   771
     * encounters an exception, then this method throws any one of
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   772
     * these exceptions. If any task encounters an exception, others
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   773
     * may be cancelled. However, the execution status of individual
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   774
     * tasks is not guaranteed upon exceptional return. The status of
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   775
     * each task may be obtained using {@link #getException()} and
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   776
     * related methods to check if they have been cancelled, completed
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   777
     * normally or exceptionally, or left unprocessed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   778
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   779
     * @param tasks the tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   780
     * @throws NullPointerException if any task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   781
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   782
    public static void invokeAll(ForkJoinTask<?>... tasks) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   783
        Throwable ex = null;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   784
        int last = tasks.length - 1;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   785
        for (int i = last; i >= 0; --i) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   786
            ForkJoinTask<?> t = tasks[i];
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   787
            if (t == null) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   788
                if (ex == null)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   789
                    ex = new NullPointerException();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   790
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   791
            else if (i != 0)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   792
                t.fork();
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   793
            else if ((t.doInvoke() & ABNORMAL) != 0 && ex == null)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   794
                ex = t.getException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   795
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   796
        for (int i = 1; i <= last; ++i) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   797
            ForkJoinTask<?> t = tasks[i];
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   798
            if (t != null) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   799
                if (ex != null)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   800
                    t.cancel(false);
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   801
                else if ((t.doJoin() & ABNORMAL) != 0)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   802
                    ex = t.getException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   803
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   804
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   805
        if (ex != null)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   806
            rethrow(ex);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   807
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   808
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   809
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   810
     * Forks all tasks in the specified collection, returning when
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   811
     * {@code isDone} holds for each task or an (unchecked) exception
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   812
     * is encountered, in which case the exception is rethrown. If
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   813
     * more than one task encounters an exception, then this method
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   814
     * throws any one of these exceptions. If any task encounters an
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   815
     * exception, others may be cancelled. However, the execution
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   816
     * status of individual tasks is not guaranteed upon exceptional
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   817
     * return. The status of each task may be obtained using {@link
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   818
     * #getException()} and related methods to check if they have been
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   819
     * cancelled, completed normally or exceptionally, or left
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   820
     * unprocessed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   821
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   822
     * @param tasks the collection of tasks
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
   823
     * @param <T> the type of the values returned from the tasks
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   824
     * @return the tasks argument, to simplify usage
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   825
     * @throws NullPointerException if tasks or any element are null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   826
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   827
    public static <T extends ForkJoinTask<?>> Collection<T> invokeAll(Collection<T> tasks) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   828
        if (!(tasks instanceof RandomAccess) || !(tasks instanceof List<?>)) {
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49565
diff changeset
   829
            invokeAll(tasks.toArray(new ForkJoinTask<?>[0]));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   830
            return tasks;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   831
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   832
        @SuppressWarnings("unchecked")
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   833
        List<? extends ForkJoinTask<?>> ts =
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   834
            (List<? extends ForkJoinTask<?>>) tasks;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   835
        Throwable ex = null;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   836
        int last = ts.size() - 1;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   837
        for (int i = last; i >= 0; --i) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   838
            ForkJoinTask<?> t = ts.get(i);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   839
            if (t == null) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   840
                if (ex == null)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   841
                    ex = new NullPointerException();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   842
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   843
            else if (i != 0)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   844
                t.fork();
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   845
            else if ((t.doInvoke() & ABNORMAL) != 0 && ex == null)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   846
                ex = t.getException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   847
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   848
        for (int i = 1; i <= last; ++i) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   849
            ForkJoinTask<?> t = ts.get(i);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   850
            if (t != null) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   851
                if (ex != null)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   852
                    t.cancel(false);
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   853
                else if ((t.doJoin() & ABNORMAL) != 0)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   854
                    ex = t.getException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   855
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   856
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   857
        if (ex != null)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   858
            rethrow(ex);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   859
        return tasks;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   860
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   861
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   862
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   863
     * Attempts to cancel execution of this task. This attempt will
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   864
     * fail if the task has already completed or could not be
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   865
     * cancelled for some other reason. If successful, and this task
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   866
     * has not started when {@code cancel} is called, execution of
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   867
     * this task is suppressed. After this method returns
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   868
     * successfully, unless there is an intervening call to {@link
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   869
     * #reinitialize}, subsequent calls to {@link #isCancelled},
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   870
     * {@link #isDone}, and {@code cancel} will return {@code true}
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   871
     * and calls to {@link #join} and related methods will result in
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   872
     * {@code CancellationException}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   873
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   874
     * <p>This method may be overridden in subclasses, but if so, must
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   875
     * still ensure that these properties hold. In particular, the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   876
     * {@code cancel} method itself must not throw exceptions.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   877
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   878
     * <p>This method is designed to be invoked by <em>other</em>
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   879
     * tasks. To terminate the current task, you can just return or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   880
     * throw an unchecked exception from its computation method, or
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
   881
     * invoke {@link #completeExceptionally(Throwable)}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   882
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   883
     * @param mayInterruptIfRunning this value has no effect in the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   884
     * default implementation because interrupts are not used to
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   885
     * control cancellation.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   886
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   887
     * @return {@code true} if this task is now cancelled
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   888
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   889
    public boolean cancel(boolean mayInterruptIfRunning) {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   890
        int s = abnormalCompletion(DONE | ABNORMAL);
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   891
        return (s & (ABNORMAL | THROWN)) == ABNORMAL;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   892
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   893
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   894
    public final boolean isDone() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   895
        return status < 0;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   896
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   897
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   898
    public final boolean isCancelled() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   899
        return (status & (ABNORMAL | THROWN)) == ABNORMAL;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   900
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   901
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   902
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   903
     * Returns {@code true} if this task threw an exception or was cancelled.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   904
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   905
     * @return {@code true} if this task threw an exception or was cancelled
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   906
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   907
    public final boolean isCompletedAbnormally() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   908
        return (status & ABNORMAL) != 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   909
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   910
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   911
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   912
     * Returns {@code true} if this task completed without throwing an
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   913
     * exception and was not cancelled.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   914
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   915
     * @return {@code true} if this task completed without throwing an
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   916
     * exception and was not cancelled
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   917
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   918
    public final boolean isCompletedNormally() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   919
        return (status & (DONE | ABNORMAL)) == DONE;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   920
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   921
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   922
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   923
     * Returns the exception thrown by the base computation, or a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   924
     * {@code CancellationException} if cancelled, or {@code null} if
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   925
     * none or if the method has not yet completed.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   926
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   927
     * @return the exception, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   928
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   929
    public final Throwable getException() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   930
        int s = status;
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   931
        return ((s & ABNORMAL) == 0 ? null :
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   932
                (s & THROWN)   == 0 ? new CancellationException() :
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   933
                getThrowableException());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   934
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   935
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   936
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   937
     * Completes this task abnormally, and if not already aborted or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   938
     * cancelled, causes it to throw the given exception upon
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   939
     * {@code join} and related operations. This method may be used
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   940
     * to induce exceptions in asynchronous tasks, or to force
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   941
     * completion of tasks that would not otherwise complete.  Its use
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   942
     * in other situations is discouraged.  This method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   943
     * overridable, but overridden versions must invoke {@code super}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   944
     * implementation to maintain guarantees.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   945
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   946
     * @param ex the exception to throw. If this exception is not a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   947
     * {@code RuntimeException} or {@code Error}, the actual exception
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   948
     * thrown will be a {@code RuntimeException} with cause {@code ex}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   949
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   950
    public void completeExceptionally(Throwable ex) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   951
        setExceptionalCompletion((ex instanceof RuntimeException) ||
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   952
                                 (ex instanceof Error) ? ex :
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   953
                                 new RuntimeException(ex));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   954
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   955
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   956
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   957
     * Completes this task, and if not already aborted or cancelled,
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   958
     * returning the given value as the result of subsequent
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   959
     * invocations of {@code join} and related operations. This method
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   960
     * may be used to provide results for asynchronous tasks, or to
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   961
     * provide alternative handling for tasks that would not otherwise
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   962
     * complete normally. Its use in other situations is
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   963
     * discouraged. This method is overridable, but overridden
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   964
     * versions must invoke {@code super} implementation to maintain
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   965
     * guarantees.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   966
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   967
     * @param value the result value for this task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   968
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   969
    public void complete(V value) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   970
        try {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   971
            setRawResult(value);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   972
        } catch (Throwable rex) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   973
            setExceptionalCompletion(rex);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   974
            return;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   975
        }
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   976
        setDone();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   977
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   978
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   979
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   980
     * Completes this task normally without setting a value. The most
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   981
     * recent value established by {@link #setRawResult} (or {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   982
     * null} by default) will be returned as the result of subsequent
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   983
     * invocations of {@code join} and related operations.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   984
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   985
     * @since 1.8
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   986
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   987
    public final void quietlyComplete() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
   988
        setDone();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   989
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   990
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   991
    /**
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   992
     * Waits if necessary for the computation to complete, and then
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   993
     * retrieves its result.
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   994
     *
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   995
     * @return the computed result
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   996
     * @throws CancellationException if the computation was cancelled
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   997
     * @throws ExecutionException if the computation threw an
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   998
     * exception
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
   999
     * @throws InterruptedException if the current thread is not a
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1000
     * member of a ForkJoinPool and was interrupted while waiting
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1001
     */
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1002
    public final V get() throws InterruptedException, ExecutionException {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1003
        int s = (Thread.currentThread() instanceof ForkJoinWorkerThread) ?
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1004
            doJoin() : externalInterruptibleAwaitDone();
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1005
        if ((s & THROWN) != 0)
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1006
            throw new ExecutionException(getThrowableException());
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1007
        else if ((s & ABNORMAL) != 0)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1008
            throw new CancellationException();
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1009
        else
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1010
            return getRawResult();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1011
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1012
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1013
    /**
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1014
     * Waits if necessary for at most the given time for the computation
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1015
     * to complete, and then retrieves its result, if available.
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1016
     *
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1017
     * @param timeout the maximum time to wait
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1018
     * @param unit the time unit of the timeout argument
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1019
     * @return the computed result
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1020
     * @throws CancellationException if the computation was cancelled
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1021
     * @throws ExecutionException if the computation threw an
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1022
     * exception
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1023
     * @throws InterruptedException if the current thread is not a
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1024
     * member of a ForkJoinPool and was interrupted while waiting
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1025
     * @throws TimeoutException if the wait timed out
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  1026
     */
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1027
    public final V get(long timeout, TimeUnit unit)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1028
        throws InterruptedException, ExecutionException, TimeoutException {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1029
        int s;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1030
        long nanos = unit.toNanos(timeout);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1031
        if (Thread.interrupted())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1032
            throw new InterruptedException();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1033
        if ((s = status) >= 0 && nanos > 0L) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1034
            long d = System.nanoTime() + nanos;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1035
            long deadline = (d == 0L) ? 1L : d; // avoid 0
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1036
            Thread t = Thread.currentThread();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1037
            if (t instanceof ForkJoinWorkerThread) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1038
                ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1039
                s = wt.pool.awaitJoin(wt.workQueue, this, deadline);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1040
            }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1041
            else if ((s = ((this instanceof CountedCompleter) ?
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1042
                           ForkJoinPool.common.externalHelpComplete(
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1043
                               (CountedCompleter<?>)this, 0) :
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1044
                           ForkJoinPool.common.tryExternalUnpush(this) ?
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1045
                           doExec() : 0)) >= 0) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1046
                long ns, ms; // measure in nanosecs, but wait in millisecs
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1047
                while ((s = status) >= 0 &&
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1048
                       (ns = deadline - System.nanoTime()) > 0L) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1049
                    if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) > 0L &&
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1050
                        (s = (int)STATUS.getAndBitwiseOr(this, SIGNAL)) >= 0) {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1051
                        synchronized (this) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1052
                            if (status >= 0)
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1053
                                wait(ms); // OK to throw InterruptedException
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1054
                            else
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1055
                                notifyAll();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1056
                        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1057
                    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1058
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1059
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1060
        }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1061
        if (s >= 0)
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1062
            throw new TimeoutException();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1063
        else if ((s & THROWN) != 0)
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1064
            throw new ExecutionException(getThrowableException());
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1065
        else if ((s & ABNORMAL) != 0)
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1066
            throw new CancellationException();
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1067
        else
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1068
            return getRawResult();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1069
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1070
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1071
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1072
     * Joins this task, without returning its result or throwing its
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1073
     * exception. This method may be useful when processing
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1074
     * collections of tasks when some have been cancelled or otherwise
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1075
     * known to have aborted.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1076
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1077
    public final void quietlyJoin() {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1078
        doJoin();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1079
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1080
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1081
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1082
     * Commences performing this task and awaits its completion if
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1083
     * necessary, without returning its result or throwing its
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1084
     * exception.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1085
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1086
    public final void quietlyInvoke() {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1087
        doInvoke();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1088
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1089
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1090
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1091
     * Possibly executes tasks until the pool hosting the current task
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1092
     * {@linkplain ForkJoinPool#isQuiescent is quiescent}.  This
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1093
     * method may be of use in designs in which many tasks are forked,
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1094
     * but none are explicitly joined, instead executing them until
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1095
     * all are processed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1096
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1097
    public static void helpQuiesce() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1098
        Thread t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1099
        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1100
            ForkJoinWorkerThread wt = (ForkJoinWorkerThread)t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1101
            wt.pool.helpQuiescePool(wt.workQueue);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1102
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1103
        else
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1104
            ForkJoinPool.quiesceCommonPool();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1105
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1106
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1107
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1108
     * Resets the internal bookkeeping state of this task, allowing a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1109
     * subsequent {@code fork}. This method allows repeated reuse of
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1110
     * this task, but only if reuse occurs when this task has either
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1111
     * never been forked, or has been forked, then completed and all
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1112
     * outstanding joins of this task have also completed. Effects
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1113
     * under any other usage conditions are not guaranteed.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1114
     * This method may be useful when executing
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1115
     * pre-constructed trees of subtasks in loops.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1116
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1117
     * <p>Upon completion of this method, {@code isDone()} reports
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1118
     * {@code false}, and {@code getException()} reports {@code
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1119
     * null}. However, the value returned by {@code getRawResult} is
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1120
     * unaffected. To clear this value, you can invoke {@code
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1121
     * setRawResult(null)}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1122
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1123
    public void reinitialize() {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1124
        if ((status & THROWN) != 0)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1125
            clearExceptionalCompletion();
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1126
        else
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1127
            status = 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1128
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1129
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1130
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1131
     * Returns the pool hosting the current thread, or {@code null}
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1132
     * if the current thread is executing outside of any ForkJoinPool.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1133
     *
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1134
     * <p>This method returns {@code null} if and only if {@link
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1135
     * #inForkJoinPool} returns {@code false}.
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1136
     *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1137
     * @return the pool, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1138
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1139
    public static ForkJoinPool getPool() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1140
        Thread t = Thread.currentThread();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1141
        return (t instanceof ForkJoinWorkerThread) ?
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1142
            ((ForkJoinWorkerThread) t).pool : null;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1143
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1144
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1145
    /**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1146
     * Returns {@code true} if the current thread is a {@link
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1147
     * ForkJoinWorkerThread} executing as a ForkJoinPool computation.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1148
     *
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1149
     * @return {@code true} if the current thread is a {@link
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1150
     * ForkJoinWorkerThread} executing as a ForkJoinPool computation,
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1151
     * or {@code false} otherwise
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1152
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1153
    public static boolean inForkJoinPool() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1154
        return Thread.currentThread() instanceof ForkJoinWorkerThread;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1155
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1156
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1157
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1158
     * Tries to unschedule this task for execution. This method will
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1159
     * typically (but is not guaranteed to) succeed if this task is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1160
     * the most recently forked task by the current thread, and has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1161
     * not commenced executing in another thread.  This method may be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1162
     * useful when arranging alternative local processing of tasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1163
     * that could have been, but were not, stolen.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1164
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1165
     * @return {@code true} if unforked
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1166
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1167
    public boolean tryUnfork() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1168
        Thread t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1169
        return (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1170
                ((ForkJoinWorkerThread)t).workQueue.tryUnpush(this) :
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1171
                ForkJoinPool.common.tryExternalUnpush(this));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1172
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1173
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1174
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1175
     * Returns an estimate of the number of tasks that have been
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1176
     * forked by the current worker thread but not yet executed. This
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1177
     * value may be useful for heuristic decisions about whether to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1178
     * fork other tasks.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1179
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1180
     * @return the number of tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1181
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1182
    public static int getQueuedTaskCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1183
        Thread t; ForkJoinPool.WorkQueue q;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1184
        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1185
            q = ((ForkJoinWorkerThread)t).workQueue;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1186
        else
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1187
            q = ForkJoinPool.commonSubmitterQueue();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1188
        return (q == null) ? 0 : q.queueSize();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1189
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1190
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1191
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1192
     * Returns an estimate of how many more locally queued tasks are
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1193
     * held by the current worker thread than there are other worker
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1194
     * threads that might steal them, or zero if this thread is not
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1195
     * operating in a ForkJoinPool. This value may be useful for
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1196
     * heuristic decisions about whether to fork other tasks. In many
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1197
     * usages of ForkJoinTasks, at steady state, each worker should
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1198
     * aim to maintain a small constant surplus (for example, 3) of
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1199
     * tasks, and to process computations locally if this threshold is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1200
     * exceeded.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1201
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1202
     * @return the surplus number of tasks, which may be negative
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1203
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1204
    public static int getSurplusQueuedTaskCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1205
        return ForkJoinPool.getSurplusQueuedTaskCount();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1206
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1207
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1208
    // Extension methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1209
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1210
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1211
     * Returns the result that would be returned by {@link #join}, even
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1212
     * if this task completed abnormally, or {@code null} if this task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1213
     * is not known to have been completed.  This method is designed
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1214
     * to aid debugging, as well as to support extensions. Its use in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1215
     * any other context is discouraged.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1216
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1217
     * @return the result, or {@code null} if not completed
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1218
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1219
    public abstract V getRawResult();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1220
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1221
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1222
     * Forces the given value to be returned as a result.  This method
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1223
     * is designed to support extensions, and should not in general be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1224
     * called otherwise.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1225
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1226
     * @param value the value
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1227
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1228
    protected abstract void setRawResult(V value);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1229
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1230
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1231
     * Immediately performs the base action of this task and returns
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1232
     * true if, upon return from this method, this task is guaranteed
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1233
     * to have completed. This method may return false otherwise, to
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1234
     * indicate that this task is not necessarily complete (or is not
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1235
     * known to be complete), for example in asynchronous actions that
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1236
     * require explicit invocations of completion methods. This method
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1237
     * may also throw an (unchecked) exception to indicate abnormal
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1238
     * exit. This method is designed to support extensions, and should
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 50764
diff changeset
  1239
     * not in general be called otherwise.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1240
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1241
     * @return {@code true} if this task is known to have completed normally
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1242
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1243
    protected abstract boolean exec();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1244
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1245
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1246
     * Returns, but does not unschedule or execute, a task queued by
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1247
     * the current thread but not yet executed, if one is immediately
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1248
     * available. There is no guarantee that this task will actually
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1249
     * be polled or executed next. Conversely, this method may return
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1250
     * null even if a task exists but cannot be accessed without
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1251
     * contention with other threads.  This method is designed
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1252
     * primarily to support extensions, and is unlikely to be useful
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1253
     * otherwise.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1254
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1255
     * @return the next task, or {@code null} if none are available
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1256
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1257
    protected static ForkJoinTask<?> peekNextLocalTask() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1258
        Thread t; ForkJoinPool.WorkQueue q;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1259
        if ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1260
            q = ((ForkJoinWorkerThread)t).workQueue;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1261
        else
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1262
            q = ForkJoinPool.commonSubmitterQueue();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1263
        return (q == null) ? null : q.peek();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1264
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1265
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1266
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1267
     * Unschedules and returns, without executing, the next task
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1268
     * queued by the current thread but not yet executed, if the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1269
     * current thread is operating in a ForkJoinPool.  This method is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1270
     * designed primarily to support extensions, and is unlikely to be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1271
     * useful otherwise.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1272
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1273
     * @return the next task, or {@code null} if none are available
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1274
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1275
    protected static ForkJoinTask<?> pollNextLocalTask() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1276
        Thread t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1277
        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1278
            ((ForkJoinWorkerThread)t).workQueue.nextLocalTask() :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1279
            null;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1280
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1281
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1282
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1283
     * If the current thread is operating in a ForkJoinPool,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1284
     * unschedules and returns, without executing, the next task
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1285
     * queued by the current thread but not yet executed, if one is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1286
     * available, or if not available, a task that was forked by some
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1287
     * other thread, if available. Availability may be transient, so a
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1288
     * {@code null} result does not necessarily imply quiescence of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1289
     * the pool this task is operating in.  This method is designed
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1290
     * primarily to support extensions, and is unlikely to be useful
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1291
     * otherwise.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1292
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1293
     * @return a task, or {@code null} if none are available
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1294
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1295
    protected static ForkJoinTask<?> pollTask() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1296
        Thread t; ForkJoinWorkerThread wt;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1297
        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1298
            (wt = (ForkJoinWorkerThread)t).pool.nextTaskFor(wt.workQueue) :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1299
            null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1300
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1301
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1302
    /**
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1303
     * If the current thread is operating in a ForkJoinPool,
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1304
     * unschedules and returns, without executing, a task externally
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1305
     * submitted to the pool, if one is available. Availability may be
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1306
     * transient, so a {@code null} result does not necessarily imply
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1307
     * quiescence of the pool.  This method is designed primarily to
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1308
     * support extensions, and is unlikely to be useful otherwise.
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1309
     *
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1310
     * @return a task, or {@code null} if none are available
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  1311
     * @since 9
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1312
     */
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1313
    protected static ForkJoinTask<?> pollSubmission() {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1314
        Thread t;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1315
        return ((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) ?
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1316
            ((ForkJoinWorkerThread)t).pool.pollSubmission() : null;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1317
    }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1318
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1319
    // tag operations
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1320
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1321
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1322
     * Returns the tag for this task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1323
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1324
     * @return the tag for this task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1325
     * @since 1.8
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1326
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1327
    public final short getForkJoinTaskTag() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1328
        return (short)status;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1329
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1330
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1331
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1332
     * Atomically sets the tag value for this task and returns the old value.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1333
     *
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1334
     * @param newValue the new tag value
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1335
     * @return the previous value of the tag
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1336
     * @since 1.8
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1337
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1338
    public final short setForkJoinTaskTag(short newValue) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1339
        for (int s;;) {
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1340
            if (STATUS.weakCompareAndSet(this, s = status,
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1341
                                         (s & ~SMASK) | (newValue & SMASK)))
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1342
                return (short)s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1343
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1344
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1345
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1346
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1347
     * Atomically conditionally sets the tag value for this task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1348
     * Among other applications, tags can be used as visit markers
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1349
     * in tasks operating on graphs, as in methods that check: {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1350
     * if (task.compareAndSetForkJoinTaskTag((short)0, (short)1))}
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1351
     * before processing, otherwise exiting because the node has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1352
     * already been visited.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1353
     *
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1354
     * @param expect the expected tag value
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1355
     * @param update the new tag value
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1356
     * @return {@code true} if successful; i.e., the current value was
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1357
     * equal to {@code expect} and was changed to {@code update}.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1358
     * @since 1.8
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1359
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1360
    public final boolean compareAndSetForkJoinTaskTag(short expect, short update) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1361
        for (int s;;) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1362
            if ((short)(s = status) != expect)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1363
                return false;
48233
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1364
            if (STATUS.weakCompareAndSet(this, s,
dd5157f363ab 8192944: Miscellaneous changes imported from jsr166 CVS 2017-12-08
dl
parents: 47306
diff changeset
  1365
                                         (s & ~SMASK) | (update & SMASK)))
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1366
                return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1367
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1368
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1369
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1370
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1371
     * Adapter for Runnables. This implements RunnableFuture
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1372
     * to be compliant with AbstractExecutorService constraints
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1373
     * when used in ForkJoinPool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1374
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1375
    static final class AdaptedRunnable<T> extends ForkJoinTask<T>
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1376
        implements RunnableFuture<T> {
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1377
        @SuppressWarnings("serial") // Conditionally serializable
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1378
        final Runnable runnable;
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1379
        @SuppressWarnings("serial") // Conditionally serializable
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1380
        T result;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1381
        AdaptedRunnable(Runnable runnable, T result) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1382
            if (runnable == null) throw new NullPointerException();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1383
            this.runnable = runnable;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1384
            this.result = result; // OK to set this even before completion
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1385
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1386
        public final T getRawResult() { return result; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1387
        public final void setRawResult(T v) { result = v; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1388
        public final boolean exec() { runnable.run(); return true; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1389
        public final void run() { invoke(); }
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1390
        public String toString() {
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1391
            return super.toString() + "[Wrapped task = " + runnable + "]";
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1392
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1393
        private static final long serialVersionUID = 5232453952276885070L;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1394
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1395
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1396
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1397
     * Adapter for Runnables without results.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1398
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1399
    static final class AdaptedRunnableAction extends ForkJoinTask<Void>
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1400
        implements RunnableFuture<Void> {
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1401
        @SuppressWarnings("serial") // Conditionally serializable
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1402
        final Runnable runnable;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1403
        AdaptedRunnableAction(Runnable runnable) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1404
            if (runnable == null) throw new NullPointerException();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1405
            this.runnable = runnable;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1406
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1407
        public final Void getRawResult() { return null; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1408
        public final void setRawResult(Void v) { }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1409
        public final boolean exec() { runnable.run(); return true; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1410
        public final void run() { invoke(); }
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1411
        public String toString() {
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1412
            return super.toString() + "[Wrapped task = " + runnable + "]";
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1413
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1414
        private static final long serialVersionUID = 5232453952276885070L;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1415
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1416
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1417
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1418
     * Adapter for Runnables in which failure forces worker exception.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1419
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1420
    static final class RunnableExecuteAction extends ForkJoinTask<Void> {
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1421
        @SuppressWarnings("serial") // Conditionally serializable
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1422
        final Runnable runnable;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1423
        RunnableExecuteAction(Runnable runnable) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1424
            if (runnable == null) throw new NullPointerException();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1425
            this.runnable = runnable;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1426
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1427
        public final Void getRawResult() { return null; }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1428
        public final void setRawResult(Void v) { }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1429
        public final boolean exec() { runnable.run(); return true; }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1430
        void internalPropagateException(Throwable ex) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1431
            rethrow(ex); // rethrow outside exec() catches.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1432
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1433
        private static final long serialVersionUID = 5232453952276885070L;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1434
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1435
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 14914
diff changeset
  1436
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1437
     * Adapter for Callables.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1438
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1439
    static final class AdaptedCallable<T> extends ForkJoinTask<T>
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1440
        implements RunnableFuture<T> {
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1441
        @SuppressWarnings("serial") // Conditionally serializable
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1442
        final Callable<? extends T> callable;
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 52730
diff changeset
  1443
        @SuppressWarnings("serial") // Conditionally serializable
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1444
        T result;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1445
        AdaptedCallable(Callable<? extends T> callable) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1446
            if (callable == null) throw new NullPointerException();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1447
            this.callable = callable;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1448
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1449
        public final T getRawResult() { return result; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1450
        public final void setRawResult(T v) { result = v; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1451
        public final boolean exec() {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1452
            try {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1453
                result = callable.call();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1454
                return true;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1455
            } catch (RuntimeException rex) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1456
                throw rex;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1457
            } catch (Exception ex) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1458
                throw new RuntimeException(ex);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1459
            }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1460
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1461
        public final void run() { invoke(); }
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1462
        public String toString() {
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1463
            return super.toString() + "[Wrapped task = " + callable + "]";
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  1464
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1465
        private static final long serialVersionUID = 2838392045355241008L;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1466
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1467
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1468
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1469
     * Returns a new {@code ForkJoinTask} that performs the {@code run}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1470
     * method of the given {@code Runnable} as its action, and returns
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1471
     * a null result upon {@link #join}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1472
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1473
     * @param runnable the runnable action
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1474
     * @return the task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1475
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1476
    public static ForkJoinTask<?> adapt(Runnable runnable) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1477
        return new AdaptedRunnableAction(runnable);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1478
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1479
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1480
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1481
     * Returns a new {@code ForkJoinTask} that performs the {@code run}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1482
     * method of the given {@code Runnable} as its action, and returns
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1483
     * the given result upon {@link #join}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1484
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1485
     * @param runnable the runnable action
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1486
     * @param result the result upon completion
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1487
     * @param <T> the type of the result
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1488
     * @return the task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1489
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1490
    public static <T> ForkJoinTask<T> adapt(Runnable runnable, T result) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1491
        return new AdaptedRunnable<T>(runnable, result);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1492
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1493
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1494
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1495
     * Returns a new {@code ForkJoinTask} that performs the {@code call}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1496
     * method of the given {@code Callable} as its action, and returns
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1497
     * its result upon {@link #join}, translating any checked exceptions
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1498
     * encountered into {@code RuntimeException}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1499
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1500
     * @param callable the callable action
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1501
     * @param <T> the type of the callable's result
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1502
     * @return the task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1503
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1504
    public static <T> ForkJoinTask<T> adapt(Callable<? extends T> callable) {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1505
        return new AdaptedCallable<T>(callable);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1506
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1507
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1508
    // Serialization support
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1509
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1510
    private static final long serialVersionUID = -7721805057305804111L;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1511
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1512
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1513
     * Saves this task to a stream (that is, serializes it).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1514
     *
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1515
     * @param s the stream
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1516
     * @throws java.io.IOException if an I/O error occurs
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1517
     * @serialData the current run status and the exception thrown
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1518
     * during execution, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1519
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1520
    private void writeObject(java.io.ObjectOutputStream s)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1521
        throws java.io.IOException {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1522
        s.defaultWriteObject();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1523
        s.writeObject(getException());
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1524
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1525
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1526
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1527
     * Reconstitutes this task from a stream (that is, deserializes it).
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1528
     * @param s the stream
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1529
     * @throws ClassNotFoundException if the class of a serialized object
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1530
     *         could not be found
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  1531
     * @throws java.io.IOException if an I/O error occurs
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1532
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1533
    private void readObject(java.io.ObjectInputStream s)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1534
        throws java.io.IOException, ClassNotFoundException {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1535
        s.defaultReadObject();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1536
        Object ex = s.readObject();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1537
        if (ex != null)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1538
            setExceptionalCompletion((Throwable)ex);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1539
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1540
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
  1541
    // VarHandle mechanics
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
  1542
    private static final VarHandle STATUS;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1543
    static {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1544
        try {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
  1545
            MethodHandles.Lookup l = MethodHandles.lookup();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 35981
diff changeset
  1546
            STATUS = l.findVarHandle(ForkJoinTask.class, "status", int.class);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
  1547
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 48233
diff changeset
  1548
            throw new ExceptionInInitializerError(e);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1549
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1550
    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1551
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1552
}