src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
author dl
Tue, 10 Apr 2018 11:29:37 -0700
changeset 49563 79d2c9da2c26
parent 47216 71c04702a3d5
child 52957 c7c285b0b640
permissions -rw-r--r--
8200520: forkjoin tasks interrupted after shutdown Reviewed-by: martin, psandoz, chegar, dholmes
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
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
    38
import java.lang.Thread.UncaughtExceptionHandler;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    39
import java.lang.invoke.MethodHandles;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    40
import java.lang.invoke.VarHandle;
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
    41
import java.security.AccessController;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
    42
import java.security.AccessControlContext;
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
    43
import java.security.Permission;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
    44
import java.security.Permissions;
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
    45
import java.security.PrivilegedAction;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
    46
import java.security.ProtectionDomain;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    47
import java.util.ArrayList;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    48
import java.util.Collection;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    49
import java.util.Collections;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    50
import java.util.List;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    51
import java.util.function.Predicate;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
    52
import java.util.concurrent.locks.LockSupport;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    53
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    54
/**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    55
 * An {@link ExecutorService} for running {@link ForkJoinTask}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    56
 * A {@code ForkJoinPool} provides the entry point for submissions
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    57
 * from non-{@code ForkJoinTask} clients, as well as management and
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    58
 * monitoring operations.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    59
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    60
 * <p>A {@code ForkJoinPool} differs from other kinds of {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    61
 * ExecutorService} mainly by virtue of employing
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    62
 * <em>work-stealing</em>: all threads in the pool attempt to find and
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    63
 * execute tasks submitted to the pool and/or created by other active
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    64
 * tasks (eventually blocking waiting for work if none exist). This
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    65
 * enables efficient processing when most tasks spawn other subtasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    66
 * (as do most {@code ForkJoinTask}s), as well as when many small
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    67
 * tasks are submitted to the pool from external clients.  Especially
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    68
 * when setting <em>asyncMode</em> to true in constructors, {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    69
 * ForkJoinPool}s may also be appropriate for use with event-style
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
    70
 * tasks that are never joined. All worker threads are initialized
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
    71
 * with {@link Thread#isDaemon} set {@code true}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    72
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    73
 * <p>A static {@link #commonPool()} is available and appropriate for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    74
 * most applications. The common pool is used by any ForkJoinTask that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    75
 * is not explicitly submitted to a specified pool. Using the common
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    76
 * pool normally reduces resource usage (its threads are slowly
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    77
 * reclaimed during periods of non-use, and reinstated upon subsequent
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    78
 * use).
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    79
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    80
 * <p>For applications that require separate or custom pools, a {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    81
 * ForkJoinPool} may be constructed with a given target parallelism
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
    82
 * level; by default, equal to the number of available processors.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
    83
 * The pool attempts to maintain enough active (or available) threads
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
    84
 * by dynamically adding, suspending, or resuming internal worker
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
    85
 * threads, even if some tasks are stalled waiting to join others.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
    86
 * However, no such adjustments are guaranteed in the face of blocked
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
    87
 * I/O or other unmanaged synchronization. The nested {@link
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    88
 * ManagedBlocker} interface enables extension of the kinds of
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    89
 * synchronization accommodated. The default policies may be
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    90
 * overridden using a constructor with parameters corresponding to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
    91
 * those documented in class {@link ThreadPoolExecutor}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    92
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    93
 * <p>In addition to execution and lifecycle control methods, this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    94
 * class provides status check methods (for example
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    95
 * {@link #getStealCount}) that are intended to aid in developing,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    96
 * tuning, and monitoring fork/join applications. Also, method
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    97
 * {@link #toString} returns indications of pool state in a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    98
 * convenient form for informal monitoring.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    99
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   100
 * <p>As is the case with other ExecutorServices, there are three
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   101
 * main task execution methods summarized in the following table.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   102
 * These are designed to be used primarily by clients not already
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   103
 * engaged in fork/join computations in the current pool.  The main
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   104
 * forms of these methods accept instances of {@code ForkJoinTask},
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   105
 * but overloaded forms also allow mixed execution of plain {@code
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   106
 * Runnable}- or {@code Callable}- based activities as well.  However,
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   107
 * tasks that are already executing in a pool should normally instead
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   108
 * use the within-computation forms listed in the table unless using
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   109
 * async event-style tasks that are not usually joined, in which case
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   110
 * there is little difference among choice of methods.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   111
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
   112
 * <table class="plain">
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   113
 * <caption>Summary of task execution methods</caption>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   114
 *  <tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   115
 *    <td></td>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45184
diff changeset
   116
 *    <th scope="col"> Call from non-fork/join clients</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45184
diff changeset
   117
 *    <th scope="col"> Call from within fork/join computations</th>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   118
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   119
 *  <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45184
diff changeset
   120
 *    <th scope="row" style="text-align:left"> Arrange async execution</th>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   121
 *    <td> {@link #execute(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   122
 *    <td> {@link ForkJoinTask#fork}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   123
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   124
 *  <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45184
diff changeset
   125
 *    <th scope="row" style="text-align:left"> Await and obtain result</th>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   126
 *    <td> {@link #invoke(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   127
 *    <td> {@link ForkJoinTask#invoke}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   128
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   129
 *  <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45184
diff changeset
   130
 *    <th scope="row" style="text-align:left"> Arrange exec and obtain Future</th>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   131
 *    <td> {@link #submit(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   132
 *    <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   133
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   134
 * </table>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   135
 *
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   136
 * <p>The parameters used to construct the common pool may be controlled by
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   137
 * setting the following {@linkplain System#getProperty system properties}:
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   138
 * <ul>
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   139
 * <li>{@code java.util.concurrent.ForkJoinPool.common.parallelism}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   140
 * - the parallelism level, a non-negative integer
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   141
 * <li>{@code java.util.concurrent.ForkJoinPool.common.threadFactory}
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   142
 * - the class name of a {@link ForkJoinWorkerThreadFactory}.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   143
 * The {@linkplain ClassLoader#getSystemClassLoader() system class loader}
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   144
 * is used to load this class.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   145
 * <li>{@code java.util.concurrent.ForkJoinPool.common.exceptionHandler}
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   146
 * - the class name of a {@link UncaughtExceptionHandler}.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   147
 * The {@linkplain ClassLoader#getSystemClassLoader() system class loader}
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   148
 * is used to load this class.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   149
 * <li>{@code java.util.concurrent.ForkJoinPool.common.maximumSpares}
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   150
 * - the maximum number of allowed extra threads to maintain target
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   151
 * parallelism (default 256).
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   152
 * </ul>
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   153
 * If no thread factory is supplied via a system property, then the
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   154
 * common pool uses a factory that uses the system class loader as the
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   155
 * {@linkplain Thread#getContextClassLoader() thread context class loader}.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   156
 * In addition, if a {@link SecurityManager} is present, then
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   157
 * the common pool uses a factory supplying threads that have no
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   158
 * {@link Permissions} enabled.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   159
 *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   160
 * Upon any error in establishing these settings, default parameters
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   161
 * are used. It is possible to disable or limit the use of threads in
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   162
 * the common pool by setting the parallelism property to zero, and/or
19412
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
   163
 * using a factory that may return {@code null}. However doing so may
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
   164
 * cause unjoined tasks to never be executed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   165
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   166
 * <p><b>Implementation notes</b>: This implementation restricts the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   167
 * maximum number of running threads to 32767. Attempts to create
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   168
 * pools with greater than the maximum number result in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   169
 * {@code IllegalArgumentException}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   170
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   171
 * <p>This implementation rejects submitted tasks (that is, by throwing
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   172
 * {@link RejectedExecutionException}) only when the pool is shut down
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   173
 * or internal resources have been exhausted.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   174
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   175
 * @since 1.7
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   176
 * @author Doug Lea
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   177
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   178
public class ForkJoinPool extends AbstractExecutorService {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   179
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   180
    /*
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   181
     * Implementation Overview
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   182
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   183
     * This class and its nested classes provide the main
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   184
     * functionality and control for a set of worker threads:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   185
     * Submissions from non-FJ threads enter into submission queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   186
     * Workers take these tasks and typically split them into subtasks
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   187
     * that may be stolen by other workers. Work-stealing based on
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   188
     * randomized scans generally leads to better throughput than
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   189
     * "work dealing" in which producers assign tasks to idle threads,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   190
     * in part because threads that have finished other tasks before
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   191
     * the signalled thread wakes up (which can be a long time) can
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   192
     * take the task instead.  Preference rules give first priority to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   193
     * processing tasks from their own queues (LIFO or FIFO, depending
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   194
     * on mode), then to randomized FIFO steals of tasks in other
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   195
     * queues.  This framework began as vehicle for supporting
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   196
     * tree-structured parallelism using work-stealing.  Over time,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   197
     * its scalability advantages led to extensions and changes to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   198
     * better support more diverse usage contexts.  Because most
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   199
     * internal methods and nested classes are interrelated, their
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   200
     * main rationale and descriptions are presented here; individual
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   201
     * methods and nested classes contain only brief comments about
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   202
     * details.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   203
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   204
     * WorkQueues
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   205
     * ==========
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   206
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   207
     * Most operations occur within work-stealing queues (in nested
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   208
     * class WorkQueue).  These are special forms of Deques that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   209
     * support only three of the four possible end-operations -- push,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   210
     * pop, and poll (aka steal), under the further constraints that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   211
     * push and pop are called only from the owning thread (or, as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   212
     * extended here, under a lock), while poll may be called from
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   213
     * other threads.  (If you are unfamiliar with them, you probably
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   214
     * want to read Herlihy and Shavit's book "The Art of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   215
     * Multiprocessor programming", chapter 16 describing these in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   216
     * more detail before proceeding.)  The main work-stealing queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   217
     * design is roughly similar to those in the papers "Dynamic
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   218
     * Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   219
     * (http://research.sun.com/scalable/pubs/index.html) and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   220
     * "Idempotent work stealing" by Michael, Saraswat, and Vechev,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   221
     * PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186).
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   222
     * The main differences ultimately stem from GC requirements that
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   223
     * we null out taken slots as soon as we can, to maintain as small
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   224
     * a footprint as possible even in programs generating huge
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   225
     * numbers of tasks. To accomplish this, we shift the CAS
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   226
     * arbitrating pop vs poll (steal) from being on the indices
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   227
     * ("base" and "top") to the slots themselves.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   228
     *
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   229
     * Adding tasks then takes the form of a classic array push(task)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   230
     * in a circular buffer:
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   231
     *    q.array[q.top++ % length] = task;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   232
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   233
     * (The actual code needs to null-check and size-check the array,
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   234
     * uses masking, not mod, for indexing a power-of-two-sized array,
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   235
     * adds a release fence for publication, and possibly signals
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   236
     * waiting workers to start scanning -- see below.)  Both a
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   237
     * successful pop and poll mainly entail a CAS of a slot from
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   238
     * non-null to null.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   239
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   240
     * The pop operation (always performed by owner) is:
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   241
     *   if ((the task at top slot is not null) and
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   242
     *        (CAS slot to null))
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   243
     *           decrement top and return task;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   244
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   245
     * And the poll operation (usually by a stealer) is
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   246
     *    if ((the task at base slot is not null) and
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   247
     *        (CAS slot to null))
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   248
     *           increment base and return task;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   249
     *
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   250
     * There are several variants of each of these. Most uses occur
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   251
     * within operations that also interleave contention or emptiness
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   252
     * tracking or inspection of elements before extracting them, so
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   253
     * must interleave these with the above code. When performed by
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   254
     * owner, getAndSet is used instead of CAS (see for example method
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   255
     * nextLocalTask) which is usually more efficient, and possible
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   256
     * because the top index cannot independently change during the
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   257
     * operation.
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   258
     *
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   259
     * Memory ordering.  See "Correct and Efficient Work-Stealing for
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   260
     * Weak Memory Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   261
     * (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   262
     * analysis of memory ordering requirements in work-stealing
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   263
     * algorithms similar to (but different than) the one used here.
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   264
     * Extracting tasks in array slots via (fully fenced) CAS provides
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   265
     * primary synchronization. The base and top indices imprecisely
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   266
     * guide where to extract from. We do not usually require strict
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   267
     * orderings of array and index updates. Many index accesses use
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   268
     * plain mode, with ordering constrained by surrounding context
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   269
     * (usually with respect to element CASes or the two WorkQueue
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   270
     * volatile fields source and phase). When not otherwise already
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   271
     * constrained, reads of "base" by queue owners use acquire-mode,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   272
     * and some externally callable methods preface accesses with
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   273
     * acquire fences.  Additionally, to ensure that index update
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   274
     * writes are not coalesced or postponed in loops etc, "opaque"
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   275
     * mode is used in a few cases where timely writes are not
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   276
     * otherwise ensured. The "locked" versions of push- and pop-
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   277
     * based methods for shared queues differ from owned versions
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   278
     * because locking already forces some of the ordering.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   279
     *
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   280
     * Because indices and slot contents cannot always be consistent,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   281
     * a check that base == top indicates (momentary) emptiness, but
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   282
     * otherwise may err on the side of possibly making the queue
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   283
     * appear nonempty when a push, pop, or poll have not fully
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   284
     * committed, or making it appear empty when an update of top has
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   285
     * not yet been visibly written.  (Method isEmpty() checks the
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   286
     * case of a partially completed removal of the last element.)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   287
     * Because of this, the poll operation, considered individually,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   288
     * is not wait-free. One thief cannot successfully continue until
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   289
     * another in-progress one (or, if previously empty, a push)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   290
     * visibly completes.  This can stall threads when required to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   291
     * consume from a given queue (see method poll()).  However, in
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   292
     * the aggregate, we ensure at least probabilistic
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   293
     * non-blockingness.  If an attempted steal fails, a scanning
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   294
     * thief chooses a different random victim target to try next. So,
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   295
     * in order for one thief to progress, it suffices for any
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   296
     * in-progress poll or new push on any empty queue to complete.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   297
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   298
     * This approach also enables support of a user mode in which
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   299
     * local task processing is in FIFO, not LIFO order, simply by
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   300
     * using poll rather than pop.  This can be useful in
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   301
     * message-passing frameworks in which tasks are never joined.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   302
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   303
     * WorkQueues are also used in a similar way for tasks submitted
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   304
     * to the pool. We cannot mix these tasks in the same queues used
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   305
     * by workers. Instead, we randomly associate submission queues
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   306
     * with submitting threads, using a form of hashing.  The
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   307
     * ThreadLocalRandom probe value serves as a hash code for
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   308
     * choosing existing queues, and may be randomly repositioned upon
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   309
     * contention with other submitters.  In essence, submitters act
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   310
     * like workers except that they are restricted to executing local
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   311
     * tasks that they submitted.  Insertion of tasks in shared mode
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   312
     * requires a lock but we use only a simple spinlock (using field
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   313
     * phase), because submitters encountering a busy queue move to a
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   314
     * different position to use or create other queues -- they block
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   315
     * only when creating and registering new queues. Because it is
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   316
     * used only as a spinlock, unlocking requires only a "releasing"
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   317
     * store (using setRelease) unless otherwise signalling.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   318
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   319
     * Management
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   320
     * ==========
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   321
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   322
     * The main throughput advantages of work-stealing stem from
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   323
     * decentralized control -- workers mostly take tasks from
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   324
     * themselves or each other, at rates that can exceed a billion
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   325
     * per second.  The pool itself creates, activates (enables
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   326
     * scanning for and running tasks), deactivates, blocks, and
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   327
     * terminates threads, all with minimal central information.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   328
     * There are only a few properties that we can globally track or
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   329
     * maintain, so we pack them into a small number of variables,
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   330
     * often maintaining atomicity without blocking or locking.
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   331
     * Nearly all essentially atomic control state is held in a few
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   332
     * volatile variables that are by far most often read (not
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   333
     * written) as status and consistency checks. We pack as much
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   334
     * information into them as we can.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   335
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   336
     * Field "ctl" contains 64 bits holding information needed to
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   337
     * atomically decide to add, enqueue (on an event queue), and
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   338
     * dequeue and release workers.  To enable this packing, we
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   339
     * restrict maximum parallelism to (1<<15)-1 (which is far in
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   340
     * excess of normal operating range) to allow ids, counts, and
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   341
     * their negations (used for thresholding) to fit into 16bit
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   342
     * subfields.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   343
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   344
     * Field "mode" holds configuration parameters as well as lifetime
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   345
     * status, atomically and monotonically setting SHUTDOWN, STOP,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   346
     * and finally TERMINATED bits.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   347
     *
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   348
     * Field "workQueues" holds references to WorkQueues.  It is
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   349
     * updated (only during worker creation and termination) under
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   350
     * lock (using field workerNamePrefix as lock), but is otherwise
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   351
     * concurrently readable, and accessed directly. We also ensure
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   352
     * that uses of the array reference itself never become too stale
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   353
     * in case of resizing, by arranging that (re-)reads are separated
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   354
     * by at least one acquiring read access.  To simplify index-based
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   355
     * operations, the array size is always a power of two, and all
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   356
     * readers must tolerate null slots. Worker queues are at odd
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   357
     * indices. Shared (submission) queues are at even indices, up to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   358
     * a maximum of 64 slots, to limit growth even if the array needs
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   359
     * to expand to add more workers. Grouping them together in this
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   360
     * way simplifies and speeds up task scanning.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   361
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   362
     * All worker thread creation is on-demand, triggered by task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   363
     * submissions, replacement of terminated workers, and/or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   364
     * compensation for blocked workers. However, all other support
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   365
     * code is set up to work with other policies.  To ensure that we
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   366
     * do not hold on to worker references that would prevent GC, all
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   367
     * accesses to workQueues are via indices into the workQueues
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   368
     * array (which is one source of some of the messy code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   369
     * constructions here). In essence, the workQueues array serves as
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   370
     * a weak reference mechanism. Thus for example the stack top
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   371
     * subfield of ctl stores indices, not references.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   372
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   373
     * Queuing Idle Workers. Unlike HPC work-stealing frameworks, we
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   374
     * cannot let workers spin indefinitely scanning for tasks when
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   375
     * none can be found immediately, and we cannot start/resume
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   376
     * workers unless there appear to be tasks available.  On the
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   377
     * other hand, we must quickly prod them into action when new
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   378
     * tasks are submitted or generated. In many usages, ramp-up time
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   379
     * is the main limiting factor in overall performance, which is
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   380
     * compounded at program start-up by JIT compilation and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   381
     * allocation. So we streamline this as much as possible.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   382
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   383
     * The "ctl" field atomically maintains total worker and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   384
     * "released" worker counts, plus the head of the available worker
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   385
     * queue (actually stack, represented by the lower 32bit subfield
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   386
     * of ctl).  Released workers are those known to be scanning for
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   387
     * and/or running tasks. Unreleased ("available") workers are
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   388
     * recorded in the ctl stack. These workers are made available for
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   389
     * signalling by enqueuing in ctl (see method runWorker).  The
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   390
     * "queue" is a form of Treiber stack. This is ideal for
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   391
     * activating threads in most-recently used order, and improves
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   392
     * performance and locality, outweighing the disadvantages of
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   393
     * being prone to contention and inability to release a worker
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   394
     * unless it is topmost on stack.  To avoid missed signal problems
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   395
     * inherent in any wait/signal design, available workers rescan
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   396
     * for (and if found run) tasks after enqueuing.  Normally their
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   397
     * release status will be updated while doing so, but the released
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   398
     * worker ctl count may underestimate the number of active
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   399
     * threads. (However, it is still possible to determine quiescence
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   400
     * via a validation traversal -- see isQuiescent).  After an
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   401
     * unsuccessful rescan, available workers are blocked until
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   402
     * signalled (see signalWork).  The top stack state holds the
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   403
     * value of the "phase" field of the worker: its index and status,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   404
     * plus a version counter that, in addition to the count subfields
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   405
     * (also serving as version stamps) provide protection against
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   406
     * Treiber stack ABA effects.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   407
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   408
     * Creating workers. To create a worker, we pre-increment counts
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   409
     * (serving as a reservation), and attempt to construct a
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   410
     * ForkJoinWorkerThread via its factory. Upon construction, the
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   411
     * new thread invokes registerWorker, where it constructs a
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   412
     * WorkQueue and is assigned an index in the workQueues array
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   413
     * (expanding the array if necessary). The thread is then started.
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   414
     * Upon any exception across these steps, or null return from
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   415
     * factory, deregisterWorker adjusts counts and records
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   416
     * accordingly.  If a null return, the pool continues running with
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   417
     * fewer than the target number workers. If exceptional, the
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   418
     * exception is propagated, generally to some external caller.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   419
     * Worker index assignment avoids the bias in scanning that would
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   420
     * occur if entries were sequentially packed starting at the front
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   421
     * of the workQueues array. We treat the array as a simple
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   422
     * power-of-two hash table, expanding as needed. The seedIndex
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   423
     * increment ensures no collisions until a resize is needed or a
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   424
     * worker is deregistered and replaced, and thereafter keeps
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   425
     * probability of collision low. We cannot use
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   426
     * ThreadLocalRandom.getProbe() for similar purposes here because
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   427
     * the thread has not started yet, but do so for creating
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   428
     * submission queues for existing external threads (see
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   429
     * externalPush).
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   430
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   431
     * WorkQueue field "phase" is used by both workers and the pool to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   432
     * manage and track whether a worker is UNSIGNALLED (possibly
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   433
     * blocked waiting for a signal).  When a worker is enqueued its
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   434
     * phase field is set. Note that phase field updates lag queue CAS
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   435
     * releases so usage requires care -- seeing a negative phase does
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   436
     * not guarantee that the worker is available. When queued, the
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   437
     * lower 16 bits of scanState must hold its pool index. So we
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   438
     * place the index there upon initialization and otherwise keep it
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   439
     * there or restore it when necessary.
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   440
     *
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   441
     * The ctl field also serves as the basis for memory
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   442
     * synchronization surrounding activation. This uses a more
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   443
     * efficient version of a Dekker-like rule that task producers and
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   444
     * consumers sync with each other by both writing/CASing ctl (even
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   445
     * if to its current value).  This would be extremely costly. So
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   446
     * we relax it in several ways: (1) Producers only signal when
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   447
     * their queue is possibly empty at some point during a push
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   448
     * operation (which requires conservatively checking size zero or
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   449
     * one to cover races). (2) Other workers propagate this signal
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   450
     * when they find tasks in a queue with size greater than one. (3)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   451
     * Workers only enqueue after scanning (see below) and not finding
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   452
     * any tasks.  (4) Rather than CASing ctl to its current value in
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   453
     * the common case where no action is required, we reduce write
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   454
     * contention by equivalently prefacing signalWork when called by
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   455
     * an external task producer using a memory access with
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   456
     * full-volatile semantics or a "fullFence".
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   457
     *
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   458
     * Almost always, too many signals are issued, in part because a
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   459
     * task producer cannot tell if some existing worker is in the
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   460
     * midst of finishing one task (or already scanning) and ready to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   461
     * take another without being signalled. So the producer might
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   462
     * instead activate a different worker that does not find any
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   463
     * work, and then inactivates. This scarcely matters in
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   464
     * steady-state computations involving all workers, but can create
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   465
     * contention and bookkeeping bottlenecks during ramp-up,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   466
     * ramp-down, and small computations involving only a few workers.
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   467
     *
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   468
     * Scanning. Method scan (from runWorker) performs top-level
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   469
     * scanning for tasks. (Similar scans appear in helpQuiesce and
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   470
     * pollScan.)  Each scan traverses and tries to poll from each
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   471
     * queue starting at a random index. Scans are not performed in
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   472
     * ideal random permutation order, to reduce cacheline
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   473
     * contention. The pseudorandom generator need not have
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   474
     * high-quality statistical properties in the long term, but just
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   475
     * within computations; We use Marsaglia XorShifts (often via
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   476
     * ThreadLocalRandom.nextSecondarySeed), which are cheap and
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   477
     * suffice. Scanning also includes contention reduction: When
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   478
     * scanning workers fail to extract an apparently existing task,
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   479
     * they soon restart at a different pseudorandom index.  This form
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   480
     * of backoff improves throughput when many threads are trying to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   481
     * take tasks from few queues, which can be common in some usages.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   482
     * Scans do not otherwise explicitly take into account core
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   483
     * affinities, loads, cache localities, etc, However, they do
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   484
     * exploit temporal locality (which usually approximates these) by
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   485
     * preferring to re-poll from the same queue after a successful
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   486
     * poll before trying others (see method topLevelExec). However
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   487
     * this preference is bounded (see TOP_BOUND_SHIFT) as a safeguard
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   488
     * against infinitely unfair looping under unbounded user task
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   489
     * recursion, and also to reduce long-term contention when many
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   490
     * threads poll few queues holding many small tasks. The bound is
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   491
     * high enough to avoid much impact on locality and scheduling
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   492
     * overhead.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   493
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   494
     * Trimming workers. To release resources after periods of lack of
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   495
     * use, a worker starting to wait when the pool is quiescent will
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   496
     * time out and terminate (see method runWorker) if the pool has
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   497
     * remained quiescent for period given by field keepAlive.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   498
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   499
     * Shutdown and Termination. A call to shutdownNow invokes
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   500
     * tryTerminate to atomically set a runState bit. The calling
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   501
     * thread, as well as every other worker thereafter terminating,
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   502
     * helps terminate others by cancelling their unprocessed tasks,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   503
     * and waking them up, doing so repeatedly until stable. Calls to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   504
     * non-abrupt shutdown() preface this by checking whether
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   505
     * termination should commence by sweeping through queues (until
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   506
     * stable) to ensure lack of in-flight submissions and workers
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   507
     * about to process them before triggering the "STOP" phase of
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   508
     * termination.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   509
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   510
     * Joining Tasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   511
     * =============
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
     * Any of several actions may be taken when one worker is waiting
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   514
     * to join a task stolen (or always held) by another.  Because we
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   515
     * are multiplexing many tasks on to a pool of workers, we can't
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   516
     * always just let them block (as in Thread.join).  We also cannot
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   517
     * just reassign the joiner's run-time stack with another and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   518
     * replace it later, which would be a form of "continuation", that
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   519
     * even if possible is not necessarily a good idea since we may
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   520
     * need both an unblocked task and its continuation to progress.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   521
     * Instead we combine two tactics:
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   522
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   523
     *   Helping: Arranging for the joiner to execute some task that it
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   524
     *      would be running if the steal had not occurred.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   525
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   526
     *   Compensating: Unless there are already enough live threads,
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   527
     *      method tryCompensate() may create or re-activate a spare
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   528
     *      thread to compensate for blocked joiners until they unblock.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   529
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   530
     * A third form (implemented in tryRemoveAndExec) amounts to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   531
     * helping a hypothetical compensator: If we can readily tell that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   532
     * a possible action of a compensator is to steal and execute the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   533
     * task being joined, the joining thread can do so directly,
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   534
     * without the need for a compensation thread.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   535
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   536
     * The ManagedBlocker extension API can't use helping so relies
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   537
     * only on compensation in method awaitBlocker.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   538
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   539
     * The algorithm in awaitJoin entails a form of "linear helping".
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   540
     * Each worker records (in field source) the id of the queue from
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   541
     * which it last stole a task.  The scan in method awaitJoin uses
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   542
     * these markers to try to find a worker to help (i.e., steal back
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   543
     * a task from and execute it) that could hasten completion of the
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   544
     * actively joined task.  Thus, the joiner executes a task that
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   545
     * would be on its own local deque if the to-be-joined task had
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   546
     * not been stolen. This is a conservative variant of the approach
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   547
     * described in Wagner & Calder "Leapfrogging: a portable
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   548
     * technique for implementing efficient futures" SIGPLAN Notices,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   549
     * 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   550
     * mainly in that we only record queue ids, not full dependency
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   551
     * links.  This requires a linear scan of the workQueues array to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   552
     * locate stealers, but isolates cost to when it is needed, rather
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   553
     * than adding to per-task overhead. Searches can fail to locate
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   554
     * stealers GC stalls and the like delay recording sources.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   555
     * Further, even when accurately identified, stealers might not
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   556
     * ever produce a task that the joiner can in turn help with. So,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   557
     * compensation is tried upon failure to find tasks to run.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   558
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   559
     * Compensation does not by default aim to keep exactly the target
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   560
     * parallelism number of unblocked threads running at any given
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   561
     * time. Some previous versions of this class employed immediate
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   562
     * compensations for any blocked join. However, in practice, the
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   563
     * vast majority of blockages are transient byproducts of GC and
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   564
     * other JVM or OS activities that are made worse by replacement
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   565
     * when they cause longer-term oversubscription.  Rather than
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   566
     * impose arbitrary policies, we allow users to override the
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   567
     * default of only adding threads upon apparent starvation.  The
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   568
     * compensation mechanism may also be bounded.  Bounds for the
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   569
     * commonPool (see COMMON_MAX_SPARES) better enable JVMs to cope
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   570
     * with programming errors and abuse before running out of
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   571
     * resources to do so.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   572
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   573
     * Common Pool
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   574
     * ===========
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   575
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   576
     * The static common pool always exists after static
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   577
     * initialization.  Since it (or any other created pool) need
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   578
     * never be used, we minimize initial construction overhead and
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   579
     * footprint to the setup of about a dozen fields.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   580
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   581
     * When external threads submit to the common pool, they can
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   582
     * perform subtask processing (see externalHelpComplete and
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   583
     * related methods) upon joins.  This caller-helps policy makes it
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   584
     * sensible to set common pool parallelism level to one (or more)
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   585
     * less than the total number of available cores, or even zero for
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   586
     * pure caller-runs.  We do not need to record whether external
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   587
     * submissions are to the common pool -- if not, external help
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   588
     * methods return quickly. These submitters would otherwise be
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   589
     * blocked waiting for completion, so the extra effort (with
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   590
     * liberally sprinkled task status checks) in inapplicable cases
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   591
     * amounts to an odd form of limited spin-wait before blocking in
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   592
     * ForkJoinTask.join.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   593
     *
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   594
     * As a more appropriate default in managed environments, unless
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   595
     * overridden by system properties, we use workers of subclass
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   596
     * InnocuousForkJoinWorkerThread when there is a SecurityManager
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   597
     * present. These workers have no permissions set, do not belong
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   598
     * to any user-defined ThreadGroup, and erase all ThreadLocals
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   599
     * after executing any top-level task (see
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   600
     * WorkQueue.afterTopLevelExec).  The associated mechanics (mainly
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   601
     * in ForkJoinWorkerThread) may be JVM-dependent and must access
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   602
     * particular Thread class fields to achieve this effect.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
   603
     *
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   604
     * Memory placement
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   605
     * ================
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   606
     *
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   607
     * Performance can be very sensitive to placement of instances of
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   608
     * ForkJoinPool and WorkQueues and their queue arrays. To reduce
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   609
     * false-sharing impact, the @Contended annotation isolates
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   610
     * adjacent WorkQueue instances, as well as the ForkJoinPool.ctl
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   611
     * field. WorkQueue arrays are allocated (by their threads) with
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   612
     * larger initial sizes than most ever need, mostly to reduce
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   613
     * false sharing with current garbage collectors that use cardmark
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   614
     * tables.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   615
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   616
     * Style notes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   617
     * ===========
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   618
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   619
     * Memory ordering relies mainly on VarHandles.  This can be
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   620
     * awkward and ugly, but also reflects the need to control
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   621
     * outcomes across the unusual cases that arise in very racy code
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   622
     * with very few invariants. All fields are read into locals
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   623
     * before use, and null-checked if they are references.  Array
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   624
     * accesses using masked indices include checks (that are always
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   625
     * true) that the array length is non-zero to avoid compilers
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   626
     * inserting more expensive traps.  This is usually done in a
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   627
     * "C"-like style of listing declarations at the heads of methods
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   628
     * or blocks, and using inline assignments on first encounter.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   629
     * Nearly all explicit checks lead to bypass/return, not exception
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   630
     * throws, because they may legitimately arise due to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   631
     * cancellation/revocation during shutdown.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   632
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   633
     * There is a lot of representation-level coupling among classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   634
     * ForkJoinPool, ForkJoinWorkerThread, and ForkJoinTask.  The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   635
     * fields of WorkQueue maintain data structures managed by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   636
     * ForkJoinPool, so are directly accessed.  There is little point
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   637
     * trying to reduce this, since any associated future changes in
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   638
     * representations will need to be accompanied by algorithmic
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   639
     * changes anyway. Several methods intrinsically sprawl because
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   640
     * they must accumulate sets of consistent reads of fields held in
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   641
     * local variables. Some others are artificially broken up to
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   642
     * reduce producer/consumer imbalances due to dynamic compilation.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   643
     * There are also other coding oddities (including several
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   644
     * unnecessary-looking hoisted null checks) that help some methods
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   645
     * perform reasonably even when interpreted (not compiled).
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   646
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   647
     * The order of declarations in this file is (with a few exceptions):
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   648
     * (1) Static utility functions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   649
     * (2) Nested (static) classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   650
     * (3) Static fields
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   651
     * (4) Fields, along with constants used when unpacking some of them
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   652
     * (5) Internal control methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   653
     * (6) Callbacks and other support for ForkJoinTask methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   654
     * (7) Exported methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   655
     * (8) Static block initializing statics in minimally dependent order
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   656
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   657
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   658
    // Static utilities
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
     * If there is a security manager, makes sure caller has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   662
     * permission to modify threads.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   663
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   664
    private static void checkPermission() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   665
        SecurityManager security = System.getSecurityManager();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   666
        if (security != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   667
            security.checkPermission(modifyThreadPermission);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   668
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   669
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   670
    // Nested classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   671
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   672
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   673
     * Factory for creating new {@link ForkJoinWorkerThread}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   674
     * A {@code ForkJoinWorkerThreadFactory} must be defined and used
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   675
     * for {@code ForkJoinWorkerThread} subclasses that extend base
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   676
     * functionality or initialize threads with different contexts.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   677
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   678
    public static interface ForkJoinWorkerThreadFactory {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   679
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   680
         * Returns a new worker thread operating in the given pool.
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   681
         * Returning null or throwing an exception may result in tasks
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   682
         * never being executed.  If this method throws an exception,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   683
         * it is relayed to the caller of the method (for example
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   684
         * {@code execute}) causing attempted thread creation. If this
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   685
         * method returns null or throws an exception, it is not
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   686
         * retried until the next attempted creation (for example
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   687
         * another call to {@code execute}).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   688
         *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   689
         * @param pool the pool this thread works in
34341
423909a8c2a6 8143086: Document that ForkJoinWorkerThreadFactory.newThread can return null to reject request
dl
parents: 33674
diff changeset
   690
         * @return the new worker thread, or {@code null} if the request
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   691
         *         to create a thread is rejected
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   692
         * @throws NullPointerException if the pool is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   693
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   694
        public ForkJoinWorkerThread newThread(ForkJoinPool pool);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   695
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   696
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   697
    static AccessControlContext contextWithPermissions(Permission ... perms) {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   698
        Permissions permissions = new Permissions();
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   699
        for (Permission perm : perms)
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   700
            permissions.add(perm);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   701
        return new AccessControlContext(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   702
            new ProtectionDomain[] { new ProtectionDomain(null, permissions) });
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   703
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   704
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   705
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   706
     * Default ForkJoinWorkerThreadFactory implementation; creates a
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   707
     * new ForkJoinWorkerThread using the system class loader as the
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   708
     * thread context class loader.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   709
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   710
    private static final class DefaultForkJoinWorkerThreadFactory
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   711
        implements ForkJoinWorkerThreadFactory {
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   712
        private static final AccessControlContext ACC = contextWithPermissions(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   713
            new RuntimePermission("getClassLoader"),
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   714
            new RuntimePermission("setContextClassLoader"));
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   715
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   716
        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   717
            return AccessController.doPrivileged(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   718
                new PrivilegedAction<>() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   719
                    public ForkJoinWorkerThread run() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   720
                        return new ForkJoinWorkerThread(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   721
                            pool, ClassLoader.getSystemClassLoader()); }},
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
   722
                ACC);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   723
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   724
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   725
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   726
    // Constants shared across ForkJoinPool and WorkQueue
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   727
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   728
    // Bounds
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   729
    static final int SWIDTH       = 16;            // width of short
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   730
    static final int SMASK        = 0xffff;        // short bits == max index
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   731
    static final int MAX_CAP      = 0x7fff;        // max #workers - 1
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   732
    static final int SQMASK       = 0x007e;        // max 64 (even) slots
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   733
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   734
    // Masks and units for WorkQueue.phase and ctl sp subfield
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   735
    static final int UNSIGNALLED  = 1 << 31;       // must be negative
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   736
    static final int SS_SEQ       = 1 << 16;       // version count
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   737
    static final int QLOCK        = 1;             // must be 1
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   738
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   739
    // Mode bits and sentinels, some also used in WorkQueue id and.source fields
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   740
    static final int OWNED        = 1;             // queue has owner thread
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   741
    static final int FIFO         = 1 << 16;       // fifo queue or access mode
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   742
    static final int SHUTDOWN     = 1 << 18;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   743
    static final int TERMINATED   = 1 << 19;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   744
    static final int STOP         = 1 << 31;       // must be negative
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   745
    static final int QUIET        = 1 << 30;       // not scanning or working
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   746
    static final int DORMANT      = QUIET | UNSIGNALLED;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   747
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   748
    /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   749
     * Initial capacity of work-stealing queue array.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   750
     * Must be a power of two, at least 2.
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   751
     */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   752
    static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   753
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   754
    /**
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   755
     * Maximum capacity for queue arrays. Must be a power of two less
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   756
     * than or equal to 1 << (31 - width of array entry) to ensure
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   757
     * lack of wraparound of index calculations, but defined to a
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   758
     * value a bit less than this to help users trap runaway programs
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   759
     * before saturating systems.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   760
     */
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   761
    static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   762
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   763
    /**
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   764
     * The maximum number of top-level polls per worker before
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   765
     * checking other queues, expressed as a bit shift to, in effect,
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   766
     * multiply by pool size, and then use as random value mask, so
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   767
     * average bound is about poolSize*(1<<TOP_BOUND_SHIFT).  See
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   768
     * above for rationale.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   769
     */
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   770
    static final int TOP_BOUND_SHIFT = 10;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   771
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   772
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   773
     * Queues supporting work-stealing as well as external task
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   774
     * submission. See above for descriptions and algorithms.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   775
     */
34369
b6df4cc80001 8140687: Move @Contended to the jdk.internal.vm.annotation package
chegar
parents: 33674
diff changeset
   776
    @jdk.internal.vm.annotation.Contended
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   777
    static final class WorkQueue {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   778
        volatile int source;       // source queue id, or sentinel
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   779
        int id;                    // pool index, mode, tag
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   780
        int base;                  // index of next slot for poll
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   781
        int top;                   // index of next slot for push
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   782
        volatile int phase;        // versioned, negative: queued, 1: locked
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   783
        int stackPred;             // pool stack (ctl) predecessor link
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   784
        int nsteals;               // number of steals
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   785
        ForkJoinTask<?>[] array;   // the queued tasks; power of 2 size
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   786
        final ForkJoinPool pool;   // the containing pool (may be null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   787
        final ForkJoinWorkerThread owner; // owning thread or null if shared
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   788
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   789
        WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   790
            this.pool = pool;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   791
            this.owner = owner;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   792
            // Place indices in the center of array (that is not yet allocated)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   793
            base = top = INITIAL_QUEUE_CAPACITY >>> 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   794
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   795
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   796
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   797
         * Tries to lock shared queue by CASing phase field.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   798
         */
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   799
        final boolean tryLockPhase() {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   800
            return PHASE.compareAndSet(this, 0, 1);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   801
        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   802
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   803
        final void releasePhaseLock() {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   804
            PHASE.setRelease(this, 0);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   805
        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   806
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   807
        /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   808
         * Returns an exportable index (used by ForkJoinWorkerThread).
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   809
         */
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   810
        final int getPoolIndex() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   811
            return (id & 0xffff) >>> 1; // ignore odd/even tag bit
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   812
        }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   813
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   814
        /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   815
         * Returns the approximate number of tasks in the queue.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   816
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   817
        final int queueSize() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   818
            int n = (int)BASE.getAcquire(this) - top;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   819
            return (n >= 0) ? 0 : -n; // ignore transient negative
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   820
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   821
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   822
        /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   823
         * Provides a more accurate estimate of whether this queue has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   824
         * any tasks than does queueSize, by checking whether a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   825
         * near-empty queue has at least one unclaimed task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   826
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   827
        final boolean isEmpty() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   828
            ForkJoinTask<?>[] a; int n, cap, b;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   829
            VarHandle.acquireFence(); // needed by external callers
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   830
            return ((n = (b = base) - top) >= 0 || // possibly one task
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   831
                    (n == -1 && ((a = array) == null ||
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   832
                                 (cap = a.length) == 0 ||
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   833
                                 a[(cap - 1) & b] == null)));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   834
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   835
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   836
        /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   837
         * Pushes a task. Call only by owner in unshared queues.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   838
         *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   839
         * @param task the task. Caller must ensure non-null.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   840
         * @throws RejectedExecutionException if array cannot be resized
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   841
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   842
        final void push(ForkJoinTask<?> task) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   843
            ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   844
            int s = top, d, cap, m;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   845
            ForkJoinPool p = pool;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   846
            if ((a = array) != null && (cap = a.length) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   847
                QA.setRelease(a, (m = cap - 1) & s, task);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   848
                top = s + 1;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   849
                if (((d = s - (int)BASE.getAcquire(this)) & ~1) == 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   850
                    p != null) {                 // size 0 or 1
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
   851
                    VarHandle.fullFence();
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   852
                    p.signalWork();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   853
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   854
                else if (d == m)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   855
                    growArray(false);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   856
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   857
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   858
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   859
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   860
         * Version of push for shared queues. Call only with phase lock held.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   861
         * @return true if should signal work
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   862
         */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   863
        final boolean lockedPush(ForkJoinTask<?> task) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   864
            ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   865
            boolean signal = false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   866
            int s = top, b = base, cap, d;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   867
            if ((a = array) != null && (cap = a.length) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   868
                a[(cap - 1) & s] = task;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   869
                top = s + 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   870
                if (b - s + cap - 1 == 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   871
                    growArray(true);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   872
                else {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   873
                    phase = 0; // full volatile unlock
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   874
                    if (((s - base) & ~1) == 0) // size 0 or 1
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   875
                        signal = true;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   876
                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   877
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   878
            return signal;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   879
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   880
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   881
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   882
         * Doubles the capacity of array. Call either by owner or with
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   883
         * lock held -- it is OK for base, but not top, to move while
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   884
         * resizings are in progress.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   885
         */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   886
        final void growArray(boolean locked) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   887
            ForkJoinTask<?>[] newA = null;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   888
            try {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   889
                ForkJoinTask<?>[] oldA; int oldSize, newSize;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   890
                if ((oldA = array) != null && (oldSize = oldA.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   891
                    (newSize = oldSize << 1) <= MAXIMUM_QUEUE_CAPACITY &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   892
                    newSize > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   893
                    try {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   894
                        newA = new ForkJoinTask<?>[newSize];
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   895
                    } catch (OutOfMemoryError ex) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   896
                    }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   897
                    if (newA != null) { // poll from old array, push to new
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   898
                        int oldMask = oldSize - 1, newMask = newSize - 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   899
                        for (int s = top - 1, k = oldMask; k >= 0; --k) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   900
                            ForkJoinTask<?> x = (ForkJoinTask<?>)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   901
                                QA.getAndSet(oldA, s & oldMask, null);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   902
                            if (x != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   903
                                newA[s-- & newMask] = x;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   904
                            else
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   905
                                break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   906
                        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   907
                        array = newA;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   908
                        VarHandle.releaseFence();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   909
                    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   910
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   911
            } finally {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   912
                if (locked)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   913
                    phase = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   914
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   915
            if (newA == null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   916
                throw new RejectedExecutionException("Queue capacity exceeded");
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   917
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   918
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   919
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   920
         * Takes next task, if one exists, in FIFO order.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   921
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   922
        final ForkJoinTask<?> poll() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   923
            int b, k, cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   924
            while ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   925
                   top - (b = base) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   926
                ForkJoinTask<?> t = (ForkJoinTask<?>)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   927
                    QA.getAcquire(a, k = (cap - 1) & b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   928
                if (base == b++) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   929
                    if (t == null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   930
                        Thread.yield(); // await index advance
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   931
                    else if (QA.compareAndSet(a, k, t, null)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   932
                        BASE.setOpaque(this, b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   933
                        return t;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   934
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   935
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   936
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   937
            return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   938
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   939
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   940
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   941
         * Takes next task, if one exists, in order specified by mode.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   942
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   943
        final ForkJoinTask<?> nextLocalTask() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   944
            ForkJoinTask<?> t = null;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   945
            int md = id, b, s, d, cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   946
            if ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   947
                (d = (s = top) - (b = base)) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   948
                if ((md & FIFO) == 0 || d == 1) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   949
                    if ((t = (ForkJoinTask<?>)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   950
                         QA.getAndSet(a, (cap - 1) & --s, null)) != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   951
                        TOP.setOpaque(this, s);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   952
                }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   953
                else if ((t = (ForkJoinTask<?>)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   954
                          QA.getAndSet(a, (cap - 1) & b++, null)) != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   955
                    BASE.setOpaque(this, b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   956
                }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   957
                else // on contention in FIFO mode, use regular poll
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   958
                    t = poll();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   959
            }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   960
            return t;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   961
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   962
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   963
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   964
         * Returns next task, if one exists, in order specified by mode.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   965
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   966
        final ForkJoinTask<?> peek() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   967
            int cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   968
            return ((a = array) != null && (cap = a.length) > 0) ?
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   969
                a[(cap - 1) & ((id & FIFO) != 0 ? base : top - 1)] : null;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   970
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   971
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   972
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   973
         * Pops the given task only if it is at the current top.
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   974
         */
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   975
        final boolean tryUnpush(ForkJoinTask<?> task) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   976
            boolean popped = false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   977
            int s, cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   978
            if ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   979
                (s = top) != base &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   980
                (popped = QA.compareAndSet(a, (cap - 1) & --s, task, null)))
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   981
                TOP.setOpaque(this, s);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   982
            return popped;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   983
        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   984
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   985
        /**
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   986
         * Shared version of tryUnpush.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   987
         */
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   988
        final boolean tryLockedUnpush(ForkJoinTask<?> task) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   989
            boolean popped = false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   990
            int s = top - 1, k, cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   991
            if ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   992
                a[k = (cap - 1) & s] == task && tryLockPhase()) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   993
                if (top == s + 1 && array == a &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   994
                    (popped = QA.compareAndSet(a, k, task, null)))
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
   995
                    top = s;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   996
                releasePhaseLock();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   997
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
   998
            return popped;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   999
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1000
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1001
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1002
         * Removes and cancels all known tasks, ignoring any exceptions.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1003
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1004
        final void cancelAll() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1005
            for (ForkJoinTask<?> t; (t = poll()) != null; )
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1006
                ForkJoinTask.cancelIgnoringExceptions(t);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1007
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1008
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1009
        // Specialized execution methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1010
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1011
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1012
         * Runs the given (stolen) task if nonnull, as well as
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1013
         * remaining local tasks and others available from the given
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1014
         * queue, up to bound n (to avoid infinite unfairness).
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1015
         */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1016
        final void topLevelExec(ForkJoinTask<?> t, WorkQueue q, int n) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1017
            if (t != null && q != null) { // hoist checks
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1018
                int nstolen = 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1019
                for (;;) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1020
                    t.doExec();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1021
                    if (n-- < 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1022
                        break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1023
                    else if ((t = nextLocalTask()) == null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1024
                        if ((t = q.poll()) == null)
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1025
                            break;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1026
                        else
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1027
                            ++nstolen;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1028
                    }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1029
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1030
                ForkJoinWorkerThread thread = owner;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1031
                nsteals += nstolen;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1032
                source = 0;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1033
                if (thread != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1034
                    thread.afterTopLevelExec();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1035
            }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1036
        }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1037
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1038
        /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1039
         * If present, removes task from queue and executes it.
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1040
         */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1041
        final void tryRemoveAndExec(ForkJoinTask<?> task) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1042
            ForkJoinTask<?>[] a; int s, cap;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1043
            if ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1044
                (s = top) - base > 0) { // traverse from top
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1045
                for (int m = cap - 1, ns = s - 1, i = ns; ; --i) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1046
                    int index = i & m;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1047
                    ForkJoinTask<?> t = (ForkJoinTask<?>)QA.get(a, index);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1048
                    if (t == null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1049
                        break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1050
                    else if (t == task) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1051
                        if (QA.compareAndSet(a, index, t, null)) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1052
                            top = ns;   // safely shift down
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1053
                            for (int j = i; j != ns; ++j) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1054
                                ForkJoinTask<?> f;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1055
                                int pindex = (j + 1) & m;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1056
                                f = (ForkJoinTask<?>)QA.get(a, pindex);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1057
                                QA.setVolatile(a, pindex, null);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1058
                                int jindex = j & m;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1059
                                QA.setRelease(a, jindex, f);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1060
                            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1061
                            VarHandle.releaseFence();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1062
                            t.doExec();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1063
                        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1064
                        break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1065
                    }
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1066
                }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1067
            }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1068
        }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1069
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1070
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1071
         * Tries to pop and run tasks within the target's computation
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1072
         * until done, not found, or limit exceeded.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1073
         *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1074
         * @param task root of CountedCompleter computation
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1075
         * @param limit max runs, or zero for no limit
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1076
         * @param shared true if must lock to extract task
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1077
         * @return task status on exit
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1078
         */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1079
        final int helpCC(CountedCompleter<?> task, int limit, boolean shared) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1080
            int status = 0;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1081
            if (task != null && (status = task.status) >= 0) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1082
                int s, k, cap; ForkJoinTask<?>[] a;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1083
                while ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1084
                       (s = top) - base > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1085
                    CountedCompleter<?> v = null;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1086
                    ForkJoinTask<?> o = a[k = (cap - 1) & (s - 1)];
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1087
                    if (o instanceof CountedCompleter) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1088
                        CountedCompleter<?> t = (CountedCompleter<?>)o;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1089
                        for (CountedCompleter<?> f = t;;) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1090
                            if (f != task) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1091
                                if ((f = f.completer) == null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1092
                                    break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1093
                            }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1094
                            else if (shared) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1095
                                if (tryLockPhase()) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1096
                                    if (top == s && array == a &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1097
                                        QA.compareAndSet(a, k, t, null)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1098
                                        top = s - 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1099
                                        v = t;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1100
                                    }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1101
                                    releasePhaseLock();
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1102
                                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1103
                                break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1104
                            }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1105
                            else {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1106
                                if (QA.compareAndSet(a, k, t, null)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1107
                                    top = s - 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1108
                                    v = t;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1109
                                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1110
                                break;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1111
                            }
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1112
                        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1113
                    }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1114
                    if (v != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1115
                        v.doExec();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1116
                    if ((status = task.status) < 0 || v == null ||
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1117
                        (limit != 0 && --limit == 0))
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1118
                        break;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1119
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1120
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1121
            return status;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1122
        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1123
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1124
        /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1125
         * Tries to poll and run AsynchronousCompletionTasks until
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1126
         * none found or blocker is released
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1127
         *
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1128
         * @param blocker the blocker
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1129
         */
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1130
        final void helpAsyncBlocker(ManagedBlocker blocker) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1131
            if (blocker != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1132
                int b, k, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1133
                while ((a = array) != null && (cap = a.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1134
                       top - (b = base) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1135
                    t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1136
                    if (blocker.isReleasable())
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1137
                        break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1138
                    else if (base == b++ && t != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1139
                        if (!(t instanceof CompletableFuture.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1140
                              AsynchronousCompletionTask))
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1141
                            break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1142
                        else if (QA.compareAndSet(a, k, t, null)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1143
                            BASE.setOpaque(this, b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1144
                            t.doExec();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1145
                        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1146
                    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1147
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1148
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1149
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1150
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1151
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1152
         * Returns true if owned and not known to be blocked.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1153
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1154
        final boolean isApparentlyUnblocked() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1155
            Thread wt; Thread.State s;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1156
            return ((wt = owner) != null &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1157
                    (s = wt.getState()) != Thread.State.BLOCKED &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1158
                    s != Thread.State.WAITING &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1159
                    s != Thread.State.TIMED_WAITING);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1160
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1161
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1162
        // VarHandle mechanics.
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1163
        static final VarHandle PHASE;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1164
        static final VarHandle BASE;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1165
        static final VarHandle TOP;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1166
        static {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1167
            try {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1168
                MethodHandles.Lookup l = MethodHandles.lookup();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1169
                PHASE = l.findVarHandle(WorkQueue.class, "phase", int.class);
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1170
                BASE = l.findVarHandle(WorkQueue.class, "base", int.class);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1171
                TOP = l.findVarHandle(WorkQueue.class, "top", int.class);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1172
            } catch (ReflectiveOperationException e) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1173
                throw new ExceptionInInitializerError(e);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1174
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1175
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1176
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1177
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1178
    // static fields (initialized in static initializer below)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1179
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1180
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1181
     * Creates a new ForkJoinWorkerThread. This factory is used unless
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1182
     * overridden in ForkJoinPool constructors.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1183
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1184
    public static final ForkJoinWorkerThreadFactory
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1185
        defaultForkJoinWorkerThreadFactory;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1186
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1187
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1188
     * Permission required for callers of methods that may start or
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1189
     * kill threads.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1190
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1191
    static final RuntimePermission modifyThreadPermission;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1192
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1193
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1194
     * Common (static) pool. Non-null for public use unless a static
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1195
     * construction exception, but internal usages null-check on use
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1196
     * to paranoically avoid potential initialization circularities
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1197
     * as well as to simplify generated code.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1198
     */
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1199
    static final ForkJoinPool common;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1200
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1201
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1202
     * Common pool parallelism. To allow simpler use and management
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1203
     * when common pool threads are disabled, we allow the underlying
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1204
     * common.parallelism field to be zero, but in that case still report
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1205
     * parallelism as 1 to reflect resulting caller-runs mechanics.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1206
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1207
    static final int COMMON_PARALLELISM;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1208
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1209
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1210
     * Limit on spare thread construction in tryCompensate.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1211
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1212
    private static final int COMMON_MAX_SPARES;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1213
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1214
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1215
     * Sequence number for creating workerNamePrefix.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1216
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1217
    private static int poolNumberSequence;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1218
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1219
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1220
     * Returns the next sequence number. We don't expect this to
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1221
     * ever contend, so use simple builtin sync.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1222
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1223
    private static final synchronized int nextPoolId() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1224
        return ++poolNumberSequence;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1225
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1226
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1227
    // static configuration constants
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1228
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1229
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1230
     * Default idle timeout value (in milliseconds) for the thread
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1231
     * triggering quiescence to park waiting for new work
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1232
     */
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  1233
    private static final long DEFAULT_KEEPALIVE = 60_000L;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1234
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1235
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1236
     * Undershoot tolerance for idle timeouts
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1237
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1238
    private static final long TIMEOUT_SLOP = 20L;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1239
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1240
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1241
     * The default value for COMMON_MAX_SPARES.  Overridable using the
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1242
     * "java.util.concurrent.ForkJoinPool.common.maximumSpares" system
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1243
     * property.  The default value is far in excess of normal
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1244
     * requirements, but also far short of MAX_CAP and typical OS
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1245
     * thread limits, so allows JVMs to catch misuse/abuse before
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1246
     * running out of resources needed to do so.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1247
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1248
    private static final int DEFAULT_COMMON_MAX_SPARES = 256;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1249
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1250
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1251
     * Increment for seed generators. See class ThreadLocal for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1252
     * explanation.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1253
     */
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  1254
    private static final int SEED_INCREMENT = 0x9e3779b9;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1255
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1256
    /*
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1257
     * Bits and masks for field ctl, packed with 4 16 bit subfields:
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1258
     * RC: Number of released (unqueued) workers minus target parallelism
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1259
     * TC: Number of total workers minus target parallelism
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1260
     * SS: version count and status of top waiting thread
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1261
     * ID: poolIndex of top of Treiber stack of waiters
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1262
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1263
     * When convenient, we can extract the lower 32 stack top bits
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1264
     * (including version bits) as sp=(int)ctl.  The offsets of counts
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1265
     * by the target parallelism and the positionings of fields makes
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1266
     * it possible to perform the most common checks via sign tests of
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1267
     * fields: When ac is negative, there are not enough unqueued
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1268
     * workers, when tc is negative, there are not enough total
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1269
     * workers.  When sp is non-zero, there are waiting workers.  To
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1270
     * deal with possibly negative fields, we use casts in and out of
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1271
     * "short" and/or signed shifts to maintain signedness.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1272
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1273
     * Because it occupies uppermost bits, we can add one release count
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1274
     * using getAndAddLong of RC_UNIT, rather than CAS, when returning
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1275
     * from a blocked join.  Other updates entail multiple subfields
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1276
     * and masking, requiring CAS.
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1277
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1278
     * The limits packed in field "bounds" are also offset by the
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1279
     * parallelism level to make them comparable to the ctl rc and tc
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1280
     * fields.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1281
     */
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1282
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1283
    // Lower and upper word masks
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1284
    private static final long SP_MASK    = 0xffffffffL;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1285
    private static final long UC_MASK    = ~SP_MASK;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1286
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1287
    // Release counts
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1288
    private static final int  RC_SHIFT   = 48;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1289
    private static final long RC_UNIT    = 0x0001L << RC_SHIFT;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1290
    private static final long RC_MASK    = 0xffffL << RC_SHIFT;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1291
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1292
    // Total counts
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1293
    private static final int  TC_SHIFT   = 32;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1294
    private static final long TC_UNIT    = 0x0001L << TC_SHIFT;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1295
    private static final long TC_MASK    = 0xffffL << TC_SHIFT;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1296
    private static final long ADD_WORKER = 0x0001L << (TC_SHIFT + 15); // sign
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1297
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1298
    // Instance fields
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1299
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1300
    volatile long stealCount;            // collects worker nsteals
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1301
    final long keepAlive;                // milliseconds before dropping if idle
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1302
    int indexSeed;                       // next worker index
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1303
    final int bounds;                    // min, max threads packed as shorts
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1304
    volatile int mode;                   // parallelism, runstate, queue mode
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1305
    WorkQueue[] workQueues;              // main registry
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1306
    final String workerNamePrefix;       // for worker thread string; sync lock
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1307
    final ForkJoinWorkerThreadFactory factory;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1308
    final UncaughtExceptionHandler ueh;  // per-worker UEH
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1309
    final Predicate<? super ForkJoinPool> saturate;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1310
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1311
    @jdk.internal.vm.annotation.Contended("fjpctl") // segregate
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1312
    volatile long ctl;                   // main pool control
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1313
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1314
    // Creating, registering and deregistering workers
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1315
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1316
    /**
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1317
     * Tries to construct and start one worker. Assumes that total
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1318
     * count has already been incremented as a reservation.  Invokes
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1319
     * deregisterWorker on any failure.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1320
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1321
     * @return true if successful
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1322
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1323
    private boolean createWorker() {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1324
        ForkJoinWorkerThreadFactory fac = factory;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1325
        Throwable ex = null;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1326
        ForkJoinWorkerThread wt = null;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1327
        try {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1328
            if (fac != null && (wt = fac.newThread(this)) != null) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1329
                wt.start();
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1330
                return true;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1331
            }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1332
        } catch (Throwable rex) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1333
            ex = rex;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1334
        }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1335
        deregisterWorker(wt, ex);
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1336
        return false;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1337
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1338
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1339
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1340
     * Tries to add one worker, incrementing ctl counts before doing
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1341
     * so, relying on createWorker to back out on failure.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1342
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1343
     * @param c incoming ctl value, with total count negative and no
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1344
     * idle workers.  On CAS failure, c is refreshed and retried if
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1345
     * this holds (otherwise, a new worker is not needed).
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1346
     */
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1347
    private void tryAddWorker(long c) {
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1348
        do {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1349
            long nc = ((RC_MASK & (c + RC_UNIT)) |
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1350
                       (TC_MASK & (c + TC_UNIT)));
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1351
            if (ctl == c && CTL.compareAndSet(this, c, nc)) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1352
                createWorker();
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1353
                break;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1354
            }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1355
        } while (((c = ctl) & ADD_WORKER) != 0L && (int)c == 0);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1356
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1357
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1358
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1359
     * Callback from ForkJoinWorkerThread constructor to establish and
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1360
     * record its WorkQueue.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1361
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1362
     * @param wt the worker thread
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1363
     * @return the worker's queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1364
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1365
    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1366
        UncaughtExceptionHandler handler;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1367
        wt.setDaemon(true);                             // configure thread
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1368
        if ((handler = ueh) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1369
            wt.setUncaughtExceptionHandler(handler);
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1370
        int tid = 0;                                    // for thread name
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1371
        int idbits = mode & FIFO;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1372
        String prefix = workerNamePrefix;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1373
        WorkQueue w = new WorkQueue(this, wt);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1374
        if (prefix != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1375
            synchronized (prefix) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1376
                WorkQueue[] ws = workQueues; int n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1377
                int s = indexSeed += SEED_INCREMENT;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1378
                idbits |= (s & ~(SMASK | FIFO | DORMANT));
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1379
                if (ws != null && (n = ws.length) > 1) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1380
                    int m = n - 1;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1381
                    tid = m & ((s << 1) | 1);           // odd-numbered indices
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1382
                    for (int probes = n >>> 1;;) {      // find empty slot
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1383
                        WorkQueue q;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1384
                        if ((q = ws[tid]) == null || q.phase == QUIET)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1385
                            break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1386
                        else if (--probes == 0) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1387
                            tid = n | 1;                // resize below
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1388
                            break;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1389
                        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1390
                        else
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1391
                            tid = (tid + 2) & m;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1392
                    }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1393
                    w.phase = w.id = tid | idbits;      // now publishable
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1394
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1395
                    if (tid < n)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1396
                        ws[tid] = w;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1397
                    else {                              // expand array
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1398
                        int an = n << 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1399
                        WorkQueue[] as = new WorkQueue[an];
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1400
                        as[tid] = w;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1401
                        int am = an - 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1402
                        for (int j = 0; j < n; ++j) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1403
                            WorkQueue v;                // copy external queue
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1404
                            if ((v = ws[j]) != null)    // position may change
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1405
                                as[v.id & am & SQMASK] = v;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1406
                            if (++j >= n)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1407
                                break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1408
                            as[j] = ws[j];              // copy worker
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1409
                        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1410
                        workQueues = as;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1411
                    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1412
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1413
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1414
            wt.setName(prefix.concat(Integer.toString(tid)));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1415
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1416
        return w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1417
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1418
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1419
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1420
     * Final callback from terminating worker, as well as upon failure
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1421
     * to construct or start a worker.  Removes record of worker from
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1422
     * array, and adjusts counts. If pool is shutting down, tries to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1423
     * complete termination.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1424
     *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1425
     * @param wt the worker thread, or null if construction failed
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1426
     * @param ex the exception causing failure, or null if none
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1427
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1428
    final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1429
        WorkQueue w = null;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1430
        int phase = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1431
        if (wt != null && (w = wt.workQueue) != null) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1432
            Object lock = workerNamePrefix;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1433
            int wid = w.id;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1434
            long ns = (long)w.nsteals & 0xffffffffL;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1435
            if (lock != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1436
                synchronized (lock) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1437
                    WorkQueue[] ws; int n, i;         // remove index from array
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1438
                    if ((ws = workQueues) != null && (n = ws.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1439
                        ws[i = wid & (n - 1)] == w)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1440
                        ws[i] = null;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1441
                    stealCount += ns;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1442
                }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1443
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1444
            phase = w.phase;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1445
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1446
        if (phase != QUIET) {                         // else pre-adjusted
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1447
            long c;                                   // decrement counts
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39723
diff changeset
  1448
            do {} while (!CTL.weakCompareAndSet
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1449
                         (this, c = ctl, ((RC_MASK & (c - RC_UNIT)) |
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1450
                                          (TC_MASK & (c - TC_UNIT)) |
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1451
                                          (SP_MASK & c))));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1452
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1453
        if (w != null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1454
            w.cancelAll();                            // cancel remaining tasks
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1455
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1456
        if (!tryTerminate(false, false) &&            // possibly replace worker
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1457
            w != null && w.array != null)             // avoid repeated failures
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1458
            signalWork();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1459
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1460
        if (ex == null)                               // help clean on way out
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1461
            ForkJoinTask.helpExpungeStaleExceptions();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1462
        else                                          // rethrow
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1463
            ForkJoinTask.rethrow(ex);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1464
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1465
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1466
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1467
     * Tries to create or release a worker if too few are running.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1468
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1469
    final void signalWork() {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1470
        for (;;) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1471
            long c; int sp; WorkQueue[] ws; int i; WorkQueue v;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1472
            if ((c = ctl) >= 0L)                      // enough workers
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1473
                break;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1474
            else if ((sp = (int)c) == 0) {            // no idle workers
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1475
                if ((c & ADD_WORKER) != 0L)           // too few workers
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1476
                    tryAddWorker(c);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1477
                break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1478
            }
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1479
            else if ((ws = workQueues) == null)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1480
                break;                                // unstarted/terminated
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1481
            else if (ws.length <= (i = sp & SMASK))
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1482
                break;                                // terminated
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1483
            else if ((v = ws[i]) == null)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1484
                break;                                // terminating
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1485
            else {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1486
                int np = sp & ~UNSIGNALLED;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1487
                int vp = v.phase;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1488
                long nc = (v.stackPred & SP_MASK) | (UC_MASK & (c + RC_UNIT));
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1489
                Thread vt = v.owner;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1490
                if (sp == vp && CTL.compareAndSet(this, c, nc)) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1491
                    v.phase = np;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1492
                    if (vt != null && v.source < 0)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1493
                        LockSupport.unpark(vt);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1494
                    break;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1495
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1496
            }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1497
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1498
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1499
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1500
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1501
     * Tries to decrement counts (sometimes implicitly) and possibly
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1502
     * arrange for a compensating worker in preparation for blocking:
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1503
     * If not all core workers yet exist, creates one, else if any are
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1504
     * unreleased (possibly including caller) releases one, else if
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1505
     * fewer than the minimum allowed number of workers running,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1506
     * checks to see that they are all active, and if so creates an
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1507
     * extra worker unless over maximum limit and policy is to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1508
     * saturate.  Most of these steps can fail due to interference, in
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1509
     * which case 0 is returned so caller will retry. A negative
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1510
     * return value indicates that the caller doesn't need to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1511
     * re-adjust counts when later unblocked.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1512
     *
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1513
     * @return 1: block then adjust, -1: block without adjust, 0 : retry
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1514
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1515
    private int tryCompensate(WorkQueue w) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1516
        int t, n, sp;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1517
        long c = ctl;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1518
        WorkQueue[] ws = workQueues;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1519
        if ((t = (short)(c >>> TC_SHIFT)) >= 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1520
            if (ws == null || (n = ws.length) <= 0 || w == null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1521
                return 0;                        // disabled
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1522
            else if ((sp = (int)c) != 0) {       // replace or release
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1523
                WorkQueue v = ws[sp & (n - 1)];
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1524
                int wp = w.phase;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1525
                long uc = UC_MASK & ((wp < 0) ? c + RC_UNIT : c);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1526
                int np = sp & ~UNSIGNALLED;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1527
                if (v != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1528
                    int vp = v.phase;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1529
                    Thread vt = v.owner;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1530
                    long nc = ((long)v.stackPred & SP_MASK) | uc;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1531
                    if (vp == sp && CTL.compareAndSet(this, c, nc)) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1532
                        v.phase = np;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1533
                        if (vt != null && v.source < 0)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1534
                            LockSupport.unpark(vt);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1535
                        return (wp < 0) ? -1 : 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1536
                    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1537
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1538
                return 0;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1539
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1540
            else if ((int)(c >> RC_SHIFT) -      // reduce parallelism
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1541
                     (short)(bounds & SMASK) > 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1542
                long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c));
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1543
                return CTL.compareAndSet(this, c, nc) ? 1 : 0;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1544
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1545
            else {                               // validate
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1546
                int md = mode, pc = md & SMASK, tc = pc + t, bc = 0;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1547
                boolean unstable = false;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1548
                for (int i = 1; i < n; i += 2) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1549
                    WorkQueue q; Thread wt; Thread.State ts;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1550
                    if ((q = ws[i]) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1551
                        if (q.source == 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1552
                            unstable = true;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1553
                            break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1554
                        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1555
                        else {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1556
                            --tc;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1557
                            if ((wt = q.owner) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1558
                                ((ts = wt.getState()) == Thread.State.BLOCKED ||
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1559
                                 ts == Thread.State.WAITING))
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1560
                                ++bc;            // worker is blocking
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1561
                        }
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1562
                    }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1563
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1564
                if (unstable || tc != 0 || ctl != c)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1565
                    return 0;                    // inconsistent
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1566
                else if (t + pc >= MAX_CAP || t >= (bounds >>> SWIDTH)) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1567
                    Predicate<? super ForkJoinPool> sat;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1568
                    if ((sat = saturate) != null && sat.test(this))
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1569
                        return -1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1570
                    else if (bc < pc) {          // lagging
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1571
                        Thread.yield();          // for retry spins
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1572
                        return 0;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1573
                    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1574
                    else
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1575
                        throw new RejectedExecutionException(
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1576
                            "Thread limit exceeded replacing blocked worker");
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1577
                }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1578
            }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1579
        }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1580
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1581
        long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); // expand pool
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1582
        return CTL.compareAndSet(this, c, nc) && createWorker() ? 1 : 0;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1583
    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1584
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1585
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1586
     * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1587
     * See above for explanation.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1588
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1589
    final void runWorker(WorkQueue w) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1590
        int r = (w.id ^ ThreadLocalRandom.nextSecondarySeed()) | FIFO; // rng
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1591
        w.array = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; // initialize
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1592
        for (;;) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1593
            int phase;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1594
            if (scan(w, r)) {                     // scan until apparently empty
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1595
                r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // move (xorshift)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1596
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1597
            else if ((phase = w.phase) >= 0) {    // enqueue, then rescan
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1598
                long np = (w.phase = (phase + SS_SEQ) | UNSIGNALLED) & SP_MASK;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1599
                long c, nc;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1600
                do {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1601
                    w.stackPred = (int)(c = ctl);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1602
                    nc = ((c - RC_UNIT) & UC_MASK) | np;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1603
                } while (!CTL.weakCompareAndSet(this, c, nc));
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1604
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1605
            else {                                // already queued
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1606
                int pred = w.stackPred;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1607
                Thread.interrupted();             // clear before park
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1608
                w.source = DORMANT;               // enable signal
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1609
                long c = ctl;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1610
                int md = mode, rc = (md & SMASK) + (int)(c >> RC_SHIFT);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1611
                if (md < 0)                       // terminating
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1612
                    break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1613
                else if (rc <= 0 && (md & SHUTDOWN) != 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1614
                         tryTerminate(false, false))
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1615
                    break;                        // quiescent shutdown
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1616
                else if (rc <= 0 && pred != 0 && phase == (int)c) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1617
                    long nc = (UC_MASK & (c - TC_UNIT)) | (SP_MASK & pred);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1618
                    long d = keepAlive + System.currentTimeMillis();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1619
                    LockSupport.parkUntil(this, d);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1620
                    if (ctl == c &&               // drop on timeout if all idle
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1621
                        d - System.currentTimeMillis() <= TIMEOUT_SLOP &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1622
                        CTL.compareAndSet(this, c, nc)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1623
                        w.phase = QUIET;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1624
                        break;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1625
                    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1626
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1627
                else if (w.phase < 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1628
                    LockSupport.park(this);       // OK if spuriously woken
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1629
                w.source = 0;                     // disable signal
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1630
            }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1631
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1632
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1633
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1634
    /**
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1635
     * Scans for and if found executes one or more top-level tasks from a queue.
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1636
     *
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1637
     * @return true if found an apparently non-empty queue, and
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1638
     * possibly ran task(s).
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1639
     */
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1640
    private boolean scan(WorkQueue w, int r) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1641
        WorkQueue[] ws; int n;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1642
        if ((ws = workQueues) != null && (n = ws.length) > 0 && w != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1643
            for (int m = n - 1, j = r & m;;) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1644
                WorkQueue q; int b;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1645
                if ((q = ws[j]) != null && q.top != (b = q.base)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1646
                    int qid = q.id;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1647
                    ForkJoinTask<?>[] a; int cap, k; ForkJoinTask<?> t;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1648
                    if ((a = q.array) != null && (cap = a.length) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1649
                        t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1650
                        if (q.base == b++ && t != null &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1651
                            QA.compareAndSet(a, k, t, null)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1652
                            q.base = b;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1653
                            w.source = qid;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1654
                            if (q.top - b > 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1655
                                signalWork();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1656
                            w.topLevelExec(t, q,  // random fairness bound
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1657
                                           r & ((n << TOP_BOUND_SHIFT) - 1));
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1658
                        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1659
                    }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1660
                    return true;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1661
                }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1662
                else if (--n > 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1663
                    j = (j + 1) & m;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1664
                else
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1665
                    break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1666
            }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1667
        }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1668
        return false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1669
    }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1670
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1671
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1672
     * Helps and/or blocks until the given task is done or timeout.
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1673
     * First tries locally helping, then scans other queues for a task
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1674
     * produced by one of w's stealers; compensating and blocking if
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1675
     * none are found (rescanning if tryCompensate fails).
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1676
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1677
     * @param w caller
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1678
     * @param task the task
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1679
     * @param deadline for timed waits, if nonzero
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1680
     * @return task status on exit
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1681
     */
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1682
    final int awaitJoin(WorkQueue w, ForkJoinTask<?> task, long deadline) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1683
        int s = 0;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1684
        int seed = ThreadLocalRandom.nextSecondarySeed();
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1685
        if (w != null && task != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1686
            (!(task instanceof CountedCompleter) ||
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1687
             (s = w.helpCC((CountedCompleter<?>)task, 0, false)) >= 0)) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1688
            w.tryRemoveAndExec(task);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1689
            int src = w.source, id = w.id;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1690
            int r = (seed >>> 16) | 1, step = (seed & ~1) | 2;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1691
            s = task.status;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1692
            while (s >= 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1693
                WorkQueue[] ws;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1694
                int n = (ws = workQueues) == null ? 0 : ws.length, m = n - 1;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1695
                while (n > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1696
                    WorkQueue q; int b;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1697
                    if ((q = ws[r & m]) != null && q.source == id &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1698
                        q.top != (b = q.base)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1699
                        ForkJoinTask<?>[] a; int cap, k;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1700
                        int qid = q.id;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1701
                        if ((a = q.array) != null && (cap = a.length) > 0) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1702
                            ForkJoinTask<?> t = (ForkJoinTask<?>)
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1703
                                QA.getAcquire(a, k = (cap - 1) & b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1704
                            if (q.source == id && q.base == b++ &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1705
                                t != null && QA.compareAndSet(a, k, t, null)) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1706
                                q.base = b;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1707
                                w.source = qid;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1708
                                t.doExec();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1709
                                w.source = src;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1710
                            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1711
                        }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1712
                        break;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1713
                    }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1714
                    else {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1715
                        r += step;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1716
                        --n;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1717
                    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1718
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1719
                if ((s = task.status) < 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1720
                    break;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1721
                else if (n == 0) { // empty scan
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1722
                    long ms, ns; int block;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1723
                    if (deadline == 0L)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1724
                        ms = 0L;                       // untimed
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1725
                    else if ((ns = deadline - System.nanoTime()) <= 0L)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1726
                        break;                         // timeout
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1727
                    else if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) <= 0L)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1728
                        ms = 1L;                       // avoid 0 for timed wait
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1729
                    if ((block = tryCompensate(w)) != 0) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1730
                        task.internalWait(ms);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1731
                        CTL.getAndAdd(this, (block > 0) ? RC_UNIT : 0L);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  1732
                    }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1733
                    s = task.status;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1734
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1735
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1736
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1737
        return s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1738
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1739
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1740
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1741
     * Runs tasks until {@code isQuiescent()}. Rather than blocking
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1742
     * when tasks cannot be found, rescans until all others cannot
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1743
     * find tasks either.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1744
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1745
    final void helpQuiescePool(WorkQueue w) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1746
        int prevSrc = w.source;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1747
        int seed = ThreadLocalRandom.nextSecondarySeed();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1748
        int r = seed >>> 16, step = r | 1;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1749
        for (int source = prevSrc, released = -1;;) { // -1 until known
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1750
            ForkJoinTask<?> localTask; WorkQueue[] ws;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1751
            while ((localTask = w.nextLocalTask()) != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1752
                localTask.doExec();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1753
            if (w.phase >= 0 && released == -1)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1754
                released = 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1755
            boolean quiet = true, empty = true;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1756
            int n = (ws = workQueues) == null ? 0 : ws.length;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1757
            for (int m = n - 1; n > 0; r += step, --n) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1758
                WorkQueue q; int b;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1759
                if ((q = ws[r & m]) != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1760
                    int qs = q.source;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1761
                    if (q.top != (b = q.base)) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1762
                        quiet = empty = false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1763
                        ForkJoinTask<?>[] a; int cap, k;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1764
                        int qid = q.id;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1765
                        if ((a = q.array) != null && (cap = a.length) > 0) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1766
                            if (released == 0) {    // increment
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1767
                                released = 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1768
                                CTL.getAndAdd(this, RC_UNIT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1769
                            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1770
                            ForkJoinTask<?> t = (ForkJoinTask<?>)
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1771
                                QA.getAcquire(a, k = (cap - 1) & b);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1772
                            if (q.base == b++ && t != null &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1773
                                QA.compareAndSet(a, k, t, null)) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1774
                                q.base = b;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1775
                                w.source = qid;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1776
                                t.doExec();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1777
                                w.source = source = prevSrc;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1778
                            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1779
                        }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1780
                        break;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1781
                    }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1782
                    else if ((qs & QUIET) == 0)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1783
                        quiet = false;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1784
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1785
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1786
            if (quiet) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1787
                if (released == 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1788
                    CTL.getAndAdd(this, RC_UNIT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1789
                w.source = prevSrc;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1790
                break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1791
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1792
            else if (empty) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1793
                if (source != QUIET)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1794
                    w.source = source = QUIET;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1795
                if (released == 1) {                 // decrement
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1796
                    released = 0;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1797
                    CTL.getAndAdd(this, RC_MASK & -RC_UNIT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1798
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1799
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1800
        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1801
    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1802
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1803
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1804
     * Scans for and returns a polled task, if available.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1805
     * Used only for untracked polls.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1806
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1807
     * @param submissionsOnly if true, only scan submission queues
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1808
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1809
    private ForkJoinTask<?> pollScan(boolean submissionsOnly) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1810
        WorkQueue[] ws; int n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1811
        rescan: while ((mode & STOP) == 0 && (ws = workQueues) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1812
                      (n = ws.length) > 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1813
            int m = n - 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1814
            int r = ThreadLocalRandom.nextSecondarySeed();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1815
            int h = r >>> 16;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1816
            int origin, step;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1817
            if (submissionsOnly) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1818
                origin = (r & ~1) & m;         // even indices and steps
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1819
                step = (h & ~1) | 2;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1820
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1821
            else {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1822
                origin = r & m;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1823
                step = h | 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1824
            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1825
            boolean nonempty = false;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1826
            for (int i = origin, oldSum = 0, checkSum = 0;;) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1827
                WorkQueue q;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1828
                if ((q = ws[i]) != null) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1829
                    int b; ForkJoinTask<?> t;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1830
                    if (q.top - (b = q.base) > 0) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1831
                        nonempty = true;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1832
                        if ((t = q.poll()) != null)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1833
                            return t;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1834
                    }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1835
                    else
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1836
                        checkSum += b + q.id;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1837
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1838
                if ((i = (i + step) & m) == origin) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1839
                    if (!nonempty && oldSum == (oldSum = checkSum))
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1840
                        break rescan;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1841
                    checkSum = 0;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1842
                    nonempty = false;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1843
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1844
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1845
        }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1846
        return null;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1847
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1848
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1849
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1850
     * Gets and removes a local or stolen task for the given worker.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1851
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1852
     * @return a task, if available
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1853
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1854
    final ForkJoinTask<?> nextTaskFor(WorkQueue w) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1855
        ForkJoinTask<?> t;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1856
        if (w == null || (t = w.nextLocalTask()) == null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1857
            t = pollScan(false);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1858
        return t;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1859
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1860
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1861
    // External operations
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1862
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1863
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1864
     * Adds the given task to a submission queue at submitter's
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1865
     * current queue, creating one if null or contended.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1866
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1867
     * @param task the task. Caller must ensure non-null.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1868
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1869
    final void externalPush(ForkJoinTask<?> task) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1870
        int r;                                // initialize caller's probe
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1871
        if ((r = ThreadLocalRandom.getProbe()) == 0) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1872
            ThreadLocalRandom.localInit();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1873
            r = ThreadLocalRandom.getProbe();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1874
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1875
        for (;;) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1876
            WorkQueue q;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1877
            int md = mode, n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1878
            WorkQueue[] ws = workQueues;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1879
            if ((md & SHUTDOWN) != 0 || ws == null || (n = ws.length) <= 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1880
                throw new RejectedExecutionException();
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1881
            else if ((q = ws[(n - 1) & r & SQMASK]) == null) { // add queue
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1882
                int qid = (r | QUIET) & ~(FIFO | OWNED);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1883
                Object lock = workerNamePrefix;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1884
                ForkJoinTask<?>[] qa =
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1885
                    new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1886
                q = new WorkQueue(this, null);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1887
                q.array = qa;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1888
                q.id = qid;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1889
                q.source = QUIET;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1890
                if (lock != null) {     // unless disabled, lock pool to install
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1891
                    synchronized (lock) {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1892
                        WorkQueue[] vs; int i, vn;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1893
                        if ((vs = workQueues) != null && (vn = vs.length) > 0 &&
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1894
                            vs[i = qid & (vn - 1) & SQMASK] == null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1895
                            vs[i] = q;  // else another thread already installed
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1896
                    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1897
                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1898
            }
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1899
            else if (!q.tryLockPhase()) // move if busy
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1900
                r = ThreadLocalRandom.advanceProbe(r);
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1901
            else {
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1902
                if (q.lockedPush(task))
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1903
                    signalWork();
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1904
                return;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1905
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1906
        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1907
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1908
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1909
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1910
     * Pushes a possibly-external submission.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1911
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1912
    private <T> ForkJoinTask<T> externalSubmit(ForkJoinTask<T> task) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1913
        Thread t; ForkJoinWorkerThread w; WorkQueue q;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1914
        if (task == null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1915
            throw new NullPointerException();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1916
        if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1917
            (w = (ForkJoinWorkerThread)t).pool == this &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1918
            (q = w.workQueue) != null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1919
            q.push(task);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1920
        else
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1921
            externalPush(task);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1922
        return task;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1923
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1924
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1925
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1926
     * Returns common pool queue for an external thread.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1927
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1928
    static WorkQueue commonSubmitterQueue() {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1929
        ForkJoinPool p = common;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1930
        int r = ThreadLocalRandom.getProbe();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1931
        WorkQueue[] ws; int n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1932
        return (p != null && (ws = p.workQueues) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1933
                (n = ws.length) > 0) ?
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1934
            ws[(n - 1) & r & SQMASK] : null;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1935
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1936
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1937
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1938
     * Performs tryUnpush for an external submitter.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1939
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1940
    final boolean tryExternalUnpush(ForkJoinTask<?> task) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1941
        int r = ThreadLocalRandom.getProbe();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1942
        WorkQueue[] ws; WorkQueue w; int n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1943
        return ((ws = workQueues) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1944
                (n = ws.length) > 0 &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1945
                (w = ws[(n - 1) & r & SQMASK]) != null &&
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1946
                w.tryLockedUnpush(task));
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1947
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1948
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1949
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1950
     * Performs helpComplete for an external submitter.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1951
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1952
    final int externalHelpComplete(CountedCompleter<?> task, int maxTasks) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1953
        int r = ThreadLocalRandom.getProbe();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1954
        WorkQueue[] ws; WorkQueue w; int n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1955
        return ((ws = workQueues) != null && (n = ws.length) > 0 &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1956
                (w = ws[(n - 1) & r & SQMASK]) != null) ?
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1957
            w.helpCC(task, maxTasks, true) : 0;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1958
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1959
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1960
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1961
     * Tries to steal and run tasks within the target's computation.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1962
     * The maxTasks argument supports external usages; internal calls
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1963
     * use zero, allowing unbounded steps (external calls trap
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1964
     * non-positive values).
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1965
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1966
     * @param w caller
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1967
     * @param maxTasks if non-zero, the maximum number of other tasks to run
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1968
     * @return task status on exit
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1969
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1970
    final int helpComplete(WorkQueue w, CountedCompleter<?> task,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  1971
                           int maxTasks) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  1972
        return (w == null) ? 0 : w.helpCC(task, maxTasks, false);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1973
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1974
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1975
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1976
     * Returns a cheap heuristic guide for task partitioning when
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1977
     * programmers, frameworks, tools, or languages have little or no
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  1978
     * idea about task granularity.  In essence, by offering this
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1979
     * method, we ask users only about tradeoffs in overhead vs
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1980
     * expected throughput and its variance, rather than how finely to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1981
     * partition tasks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1982
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1983
     * In a steady state strict (tree-structured) computation, each
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1984
     * thread makes available for stealing enough tasks for other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1985
     * threads to remain active. Inductively, if all threads play by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1986
     * the same rules, each thread should make available only a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1987
     * constant number of tasks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1988
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1989
     * The minimum useful constant is just 1. But using a value of 1
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1990
     * would require immediate replenishment upon each steal to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1991
     * maintain enough tasks, which is infeasible.  Further,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1992
     * partitionings/granularities of offered tasks should minimize
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1993
     * steal rates, which in general means that threads nearer the top
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1994
     * of computation tree should generate more than those nearer the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1995
     * bottom. In perfect steady state, each thread is at
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1996
     * approximately the same level of computation tree. However,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1997
     * producing extra tasks amortizes the uncertainty of progress and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1998
     * diffusion assumptions.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1999
     *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2000
     * So, users will want to use values larger (but not much larger)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2001
     * than 1 to both smooth over transient shortages and hedge
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2002
     * against uneven progress; as traded off against the cost of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2003
     * extra task overhead. We leave the user to pick a threshold
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2004
     * value to compare with the results of this call to guide
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2005
     * decisions, but recommend values such as 3.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2006
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2007
     * When all threads are active, it is on average OK to estimate
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2008
     * surplus strictly locally. In steady-state, if one thread is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2009
     * maintaining say 2 surplus tasks, then so are others. So we can
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2010
     * just use estimated queue length.  However, this strategy alone
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2011
     * leads to serious mis-estimates in some non-steady-state
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2012
     * conditions (ramp-up, ramp-down, other stalls). We can detect
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2013
     * many of these by further considering the number of "idle"
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2014
     * threads, that are known to have zero queued tasks, so
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2015
     * compensate by a factor of (#idle/#active) threads.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2016
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2017
    static int getSurplusQueuedTaskCount() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2018
        Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2019
        if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2020
            (pool = (wt = (ForkJoinWorkerThread)t).pool) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2021
            (q = wt.workQueue) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2022
            int p = pool.mode & SMASK;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2023
            int a = p + (int)(pool.ctl >> RC_SHIFT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2024
            int n = q.top - q.base;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2025
            return n - (a > (p >>>= 1) ? 0 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2026
                        a > (p >>>= 1) ? 1 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2027
                        a > (p >>>= 1) ? 2 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2028
                        a > (p >>>= 1) ? 4 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2029
                        8);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2030
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2031
        return 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2032
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2033
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2034
    // Termination
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2035
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2036
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2037
     * Possibly initiates and/or completes termination.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2038
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2039
     * @param now if true, unconditionally terminate, else only
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2040
     * if no work and no active workers
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2041
     * @param enable if true, terminate when next possible
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2042
     * @return true if terminating or terminated
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2043
     */
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2044
    private boolean tryTerminate(boolean now, boolean enable) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2045
        int md; // 3 phases: try to set SHUTDOWN, then STOP, then TERMINATED
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2046
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2047
        while (((md = mode) & SHUTDOWN) == 0) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2048
            if (!enable || this == common)        // cannot shutdown
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2049
                return false;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2050
            else
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2051
                MODE.compareAndSet(this, md, md | SHUTDOWN);
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2052
        }
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2053
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2054
        while (((md = mode) & STOP) == 0) {       // try to initiate termination
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2055
            if (!now) {                           // check if quiescent & empty
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2056
                for (long oldSum = 0L;;) {        // repeat until stable
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2057
                    boolean running = false;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2058
                    long checkSum = ctl;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2059
                    WorkQueue[] ws = workQueues;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2060
                    if ((md & SMASK) + (int)(checkSum >> RC_SHIFT) > 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2061
                        running = true;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2062
                    else if (ws != null) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2063
                        WorkQueue w;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2064
                        for (int i = 0; i < ws.length; ++i) {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2065
                            if ((w = ws[i]) != null) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2066
                                int s = w.source, p = w.phase;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2067
                                int d = w.id, b = w.base;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2068
                                if (b != w.top ||
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2069
                                    ((d & 1) == 1 && (s >= 0 || p >= 0))) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2070
                                    running = true;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2071
                                    break;     // working, scanning, or have work
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2072
                                }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2073
                                checkSum += (((long)s << 48) + ((long)p << 32) +
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2074
                                             ((long)b << 16) + (long)d);
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2075
                            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2076
                        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2077
                    }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2078
                    if (((md = mode) & STOP) != 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2079
                        break;                 // already triggered
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2080
                    else if (running)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2081
                        return false;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2082
                    else if (workQueues == ws && oldSum == (oldSum = checkSum))
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2083
                        break;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2084
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2085
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2086
            if ((md & STOP) == 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2087
                MODE.compareAndSet(this, md, md | STOP);
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2088
        }
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2089
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2090
        while (((md = mode) & TERMINATED) == 0) { // help terminate others
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2091
            for (long oldSum = 0L;;) {            // repeat until stable
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2092
                WorkQueue[] ws; WorkQueue w;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2093
                long checkSum = ctl;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2094
                if ((ws = workQueues) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2095
                    for (int i = 0; i < ws.length; ++i) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2096
                        if ((w = ws[i]) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2097
                            ForkJoinWorkerThread wt = w.owner;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2098
                            w.cancelAll();        // clear queues
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2099
                            if (wt != null) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2100
                                try {             // unblock join or park
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2101
                                    wt.interrupt();
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2102
                                } catch (Throwable ignore) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2103
                                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2104
                            }
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2105
                            checkSum += ((long)w.phase << 32) + w.base;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2106
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2107
                    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2108
                }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2109
                if (((md = mode) & TERMINATED) != 0 ||
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2110
                    (workQueues == ws && oldSum == (oldSum = checkSum)))
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2111
                    break;
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2112
            }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2113
            if ((md & TERMINATED) != 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2114
                break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2115
            else if ((md & SMASK) + (short)(ctl >>> TC_SHIFT) > 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2116
                break;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2117
            else if (MODE.compareAndSet(this, md, md | TERMINATED)) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2118
                synchronized (this) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2119
                    notifyAll();                  // for awaitTermination
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2120
                }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2121
                break;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2122
            }
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2123
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2124
        return true;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2125
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2126
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2127
    // Exported methods
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2128
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2129
    // Constructors
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2130
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2131
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2132
     * Creates a {@code ForkJoinPool} with parallelism equal to {@link
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2133
     * java.lang.Runtime#availableProcessors}, using defaults for all
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2134
     * other parameters (see {@link #ForkJoinPool(int,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2135
     * ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2136
     * int, int, int, Predicate, long, TimeUnit)}).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2137
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2138
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2139
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2140
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2141
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2142
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2143
    public ForkJoinPool() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2144
        this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()),
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2145
             defaultForkJoinWorkerThreadFactory, null, false,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2146
             0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2147
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2148
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2149
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2150
     * Creates a {@code ForkJoinPool} with the indicated parallelism
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2151
     * level, using defaults for all other parameters (see {@link
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2152
     * #ForkJoinPool(int, ForkJoinWorkerThreadFactory,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2153
     * UncaughtExceptionHandler, boolean, int, int, int, Predicate,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2154
     * long, TimeUnit)}).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2155
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2156
     * @param parallelism the parallelism level
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2157
     * @throws IllegalArgumentException if parallelism less than or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2158
     *         equal to zero, or greater than implementation limit
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2159
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2160
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2161
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2162
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2163
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2164
    public ForkJoinPool(int parallelism) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2165
        this(parallelism, defaultForkJoinWorkerThreadFactory, null, false,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2166
             0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2167
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2168
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2169
    /**
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2170
     * Creates a {@code ForkJoinPool} with the given parameters (using
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2171
     * defaults for others -- see {@link #ForkJoinPool(int,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2172
     * ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2173
     * int, int, int, Predicate, long, TimeUnit)}).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2174
     *
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2175
     * @param parallelism the parallelism level. For default value,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2176
     * use {@link java.lang.Runtime#availableProcessors}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2177
     * @param factory the factory for creating new threads. For default value,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2178
     * use {@link #defaultForkJoinWorkerThreadFactory}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2179
     * @param handler the handler for internal worker threads that
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2180
     * terminate due to unrecoverable errors encountered while executing
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2181
     * tasks. For default value, use {@code null}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2182
     * @param asyncMode if true,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2183
     * establishes local first-in-first-out scheduling mode for forked
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2184
     * tasks that are never joined. This mode may be more appropriate
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2185
     * than default locally stack-based mode in applications in which
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2186
     * worker threads only process event-style asynchronous tasks.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2187
     * For default value, use {@code false}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2188
     * @throws IllegalArgumentException if parallelism less than or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2189
     *         equal to zero, or greater than implementation limit
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2190
     * @throws NullPointerException if the factory is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2191
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2192
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2193
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2194
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2195
     */
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2196
    public ForkJoinPool(int parallelism,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2197
                        ForkJoinWorkerThreadFactory factory,
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2198
                        UncaughtExceptionHandler handler,
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2199
                        boolean asyncMode) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2200
        this(parallelism, factory, handler, asyncMode,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2201
             0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2202
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2203
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2204
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2205
     * Creates a {@code ForkJoinPool} with the given parameters.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2206
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2207
     * @param parallelism the parallelism level. For default value,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2208
     * use {@link java.lang.Runtime#availableProcessors}.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2209
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2210
     * @param factory the factory for creating new threads. For
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2211
     * default value, use {@link #defaultForkJoinWorkerThreadFactory}.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2212
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2213
     * @param handler the handler for internal worker threads that
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2214
     * terminate due to unrecoverable errors encountered while
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2215
     * executing tasks. For default value, use {@code null}.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2216
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2217
     * @param asyncMode if true, establishes local first-in-first-out
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2218
     * scheduling mode for forked tasks that are never joined. This
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2219
     * mode may be more appropriate than default locally stack-based
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2220
     * mode in applications in which worker threads only process
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2221
     * event-style asynchronous tasks.  For default value, use {@code
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2222
     * false}.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2223
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2224
     * @param corePoolSize the number of threads to keep in the pool
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2225
     * (unless timed out after an elapsed keep-alive). Normally (and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2226
     * by default) this is the same value as the parallelism level,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2227
     * but may be set to a larger value to reduce dynamic overhead if
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2228
     * tasks regularly block. Using a smaller value (for example
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2229
     * {@code 0}) has the same effect as the default.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2230
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2231
     * @param maximumPoolSize the maximum number of threads allowed.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2232
     * When the maximum is reached, attempts to replace blocked
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2233
     * threads fail.  (However, because creation and termination of
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2234
     * different threads may overlap, and may be managed by the given
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2235
     * thread factory, this value may be transiently exceeded.)  To
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2236
     * arrange the same value as is used by default for the common
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2237
     * pool, use {@code 256} plus the {@code parallelism} level. (By
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2238
     * default, the common pool allows a maximum of 256 spare
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2239
     * threads.)  Using a value (for example {@code
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2240
     * Integer.MAX_VALUE}) larger than the implementation's total
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2241
     * thread limit has the same effect as using this limit (which is
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2242
     * the default).
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2243
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2244
     * @param minimumRunnable the minimum allowed number of core
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2245
     * threads not blocked by a join or {@link ManagedBlocker}.  To
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2246
     * ensure progress, when too few unblocked threads exist and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2247
     * unexecuted tasks may exist, new threads are constructed, up to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2248
     * the given maximumPoolSize.  For the default value, use {@code
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2249
     * 1}, that ensures liveness.  A larger value might improve
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2250
     * throughput in the presence of blocked activities, but might
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2251
     * not, due to increased overhead.  A value of zero may be
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2252
     * acceptable when submitted tasks cannot have dependencies
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2253
     * requiring additional threads.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2254
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2255
     * @param saturate if non-null, a predicate invoked upon attempts
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2256
     * to create more than the maximum total allowed threads.  By
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2257
     * default, when a thread is about to block on a join or {@link
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2258
     * ManagedBlocker}, but cannot be replaced because the
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2259
     * maximumPoolSize would be exceeded, a {@link
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2260
     * RejectedExecutionException} is thrown.  But if this predicate
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2261
     * returns {@code true}, then no exception is thrown, so the pool
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2262
     * continues to operate with fewer than the target number of
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2263
     * runnable threads, which might not ensure progress.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2264
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2265
     * @param keepAliveTime the elapsed time since last use before
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2266
     * a thread is terminated (and then later replaced if needed).
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2267
     * For the default value, use {@code 60, TimeUnit.SECONDS}.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2268
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2269
     * @param unit the time unit for the {@code keepAliveTime} argument
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2270
     *
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2271
     * @throws IllegalArgumentException if parallelism is less than or
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2272
     *         equal to zero, or is greater than implementation limit,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2273
     *         or if maximumPoolSize is less than parallelism,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2274
     *         of if the keepAliveTime is less than or equal to zero.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2275
     * @throws NullPointerException if the factory is null
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2276
     * @throws SecurityException if a security manager exists and
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2277
     *         the caller is not permitted to modify threads
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2278
     *         because it does not hold {@link
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2279
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2280
     * @since 9
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2281
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2282
    public ForkJoinPool(int parallelism,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2283
                        ForkJoinWorkerThreadFactory factory,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2284
                        UncaughtExceptionHandler handler,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2285
                        boolean asyncMode,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2286
                        int corePoolSize,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2287
                        int maximumPoolSize,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2288
                        int minimumRunnable,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2289
                        Predicate<? super ForkJoinPool> saturate,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2290
                        long keepAliveTime,
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2291
                        TimeUnit unit) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2292
        // check, encode, pack parameters
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2293
        if (parallelism <= 0 || parallelism > MAX_CAP ||
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2294
            maximumPoolSize < parallelism || keepAliveTime <= 0L)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2295
            throw new IllegalArgumentException();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2296
        if (factory == null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2297
            throw new NullPointerException();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2298
        long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2299
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2300
        int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2301
        long c = ((((long)(-corep)       << TC_SHIFT) & TC_MASK) |
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2302
                  (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2303
        int m = parallelism | (asyncMode ? FIFO : 0);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2304
        int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2305
        int minAvail = Math.min(Math.max(minimumRunnable, 0), MAX_CAP);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2306
        int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2307
        int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2308
        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2309
        n = (n + 1) << 1; // power of two, including space for submission queues
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2310
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2311
        this.workerNamePrefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2312
        this.workQueues = new WorkQueue[n];
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2313
        this.factory = factory;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2314
        this.ueh = handler;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2315
        this.saturate = saturate;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2316
        this.keepAlive = ms;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2317
        this.bounds = b;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2318
        this.mode = m;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2319
        this.ctl = c;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2320
        checkPermission();
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2321
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2322
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43545
diff changeset
  2323
    private static Object newInstanceFromSystemProperty(String property)
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2324
        throws ReflectiveOperationException {
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2325
        String className = System.getProperty(property);
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2326
        return (className == null)
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2327
            ? null
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2328
            : ClassLoader.getSystemClassLoader().loadClass(className)
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2329
            .getConstructor().newInstance();
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2330
    }
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2331
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2332
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2333
     * Constructor for common pool using parameters possibly
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2334
     * overridden by system properties
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2335
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2336
    private ForkJoinPool(byte forCommonPoolOnly) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2337
        int parallelism = -1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2338
        ForkJoinWorkerThreadFactory fac = null;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2339
        UncaughtExceptionHandler handler = null;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2340
        try {  // ignore exceptions in accessing/parsing properties
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2341
            String pp = System.getProperty
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2342
                ("java.util.concurrent.ForkJoinPool.common.parallelism");
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2343
            if (pp != null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2344
                parallelism = Integer.parseInt(pp);
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2345
            fac = (ForkJoinWorkerThreadFactory) newInstanceFromSystemProperty(
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2346
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2347
            handler = (UncaughtExceptionHandler) newInstanceFromSystemProperty(
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2348
                "java.util.concurrent.ForkJoinPool.common.exceptionHandler");
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2349
        } catch (Exception ignore) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2350
        }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2351
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2352
        if (fac == null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2353
            if (System.getSecurityManager() == null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2354
                fac = defaultForkJoinWorkerThreadFactory;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2355
            else // use security-managed default
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2356
                fac = new InnocuousForkJoinWorkerThreadFactory();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2357
        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2358
        if (parallelism < 0 && // default 1 less than #cores
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2359
            (parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2360
            parallelism = 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2361
        if (parallelism > MAX_CAP)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2362
            parallelism = MAX_CAP;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2363
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2364
        long c = ((((long)(-parallelism) << TC_SHIFT) & TC_MASK) |
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2365
                  (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2366
        int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2367
        int n = (parallelism > 1) ? parallelism - 1 : 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2368
        n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2369
        n = (n + 1) << 1;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2370
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 40817
diff changeset
  2371
        this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2372
        this.workQueues = new WorkQueue[n];
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2373
        this.factory = fac;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2374
        this.ueh = handler;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2375
        this.saturate = null;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2376
        this.keepAlive = DEFAULT_KEEPALIVE;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2377
        this.bounds = b;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2378
        this.mode = parallelism;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2379
        this.ctl = c;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2380
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2381
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2382
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2383
     * Returns the common pool instance. This pool is statically
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2384
     * constructed; its run state is unaffected by attempts to {@link
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2385
     * #shutdown} or {@link #shutdownNow}. However this pool and any
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2386
     * ongoing processing are automatically terminated upon program
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2387
     * {@link System#exit}.  Any program that relies on asynchronous
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2388
     * task processing to complete before program termination should
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2389
     * invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence},
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2390
     * before exit.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2391
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2392
     * @return the common pool instance
15002
8ab8c2b2eee6 8005709: Add at since tags to new FJP getCommonPoolParallelism and commonPool
chegar
parents: 14914
diff changeset
  2393
     * @since 1.8
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2394
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2395
    public static ForkJoinPool commonPool() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2396
        // assert common != null : "static init error";
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2397
        return common;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2398
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2399
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2400
    // Execution methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2401
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2402
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2403
     * Performs the given task, returning its result upon completion.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2404
     * If the computation encounters an unchecked Exception or Error,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2405
     * it is rethrown as the outcome of this invocation.  Rethrown
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2406
     * exceptions behave in the same way as regular exceptions, but,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2407
     * when possible, contain stack traces (as displayed for example
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2408
     * using {@code ex.printStackTrace()}) of both the current thread
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2409
     * as well as the thread actually encountering the exception;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2410
     * minimally only the latter.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2411
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2412
     * @param task the task
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  2413
     * @param <T> the type of the task's result
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2414
     * @return the task's result
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2415
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2416
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2417
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2418
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2419
    public <T> T invoke(ForkJoinTask<T> task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2420
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2421
            throw new NullPointerException();
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2422
        externalSubmit(task);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2423
        return task.join();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2424
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2425
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2426
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2427
     * Arranges for (asynchronous) execution of the given task.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2428
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2429
     * @param task the task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2430
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2431
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2432
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2433
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2434
    public void execute(ForkJoinTask<?> task) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2435
        externalSubmit(task);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2436
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2437
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2438
    // AbstractExecutorService methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2439
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2440
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2441
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2442
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2443
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2444
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2445
    public void execute(Runnable task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2446
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2447
            throw new NullPointerException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2448
        ForkJoinTask<?> job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2449
        if (task instanceof ForkJoinTask<?>) // avoid re-wrap
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2450
            job = (ForkJoinTask<?>) task;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2451
        else
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2452
            job = new ForkJoinTask.RunnableExecuteAction(task);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2453
        externalSubmit(job);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2454
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2455
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2456
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2457
     * Submits a ForkJoinTask for execution.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2458
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2459
     * @param task the task to submit
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  2460
     * @param <T> the type of the task's result
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2461
     * @return the task
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2462
     * @throws NullPointerException if the task is null
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2463
     * @throws RejectedExecutionException if the task cannot be
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2464
     *         scheduled for execution
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2465
     */
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2466
    public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2467
        return externalSubmit(task);
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2468
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2469
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2470
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2471
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2472
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2473
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2474
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2475
    public <T> ForkJoinTask<T> submit(Callable<T> task) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2476
        return externalSubmit(new ForkJoinTask.AdaptedCallable<T>(task));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2477
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2478
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2479
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2480
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2481
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2482
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2483
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2484
    public <T> ForkJoinTask<T> submit(Runnable task, T result) {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2485
        return externalSubmit(new ForkJoinTask.AdaptedRunnable<T>(task, result));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2486
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2487
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2488
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2489
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2490
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2491
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2492
     */
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43545
diff changeset
  2493
    @SuppressWarnings("unchecked")
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2494
    public ForkJoinTask<?> submit(Runnable task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2495
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2496
            throw new NullPointerException();
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43545
diff changeset
  2497
        return externalSubmit((task instanceof ForkJoinTask<?>)
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43545
diff changeset
  2498
            ? (ForkJoinTask<Void>) task // avoid re-wrap
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 43545
diff changeset
  2499
            : new ForkJoinTask.AdaptedRunnableAction(task));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2500
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2501
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2502
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2503
     * @throws NullPointerException       {@inheritDoc}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2504
     * @throws RejectedExecutionException {@inheritDoc}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2505
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2506
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2507
        // In previous versions of this class, this method constructed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2508
        // a task to run ForkJoinTask.invokeAll, but now external
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2509
        // invocation of multiple tasks is at least as efficient.
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2510
        ArrayList<Future<T>> futures = new ArrayList<>(tasks.size());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2511
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2512
        try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2513
            for (Callable<T> t : tasks) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2514
                ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t);
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2515
                futures.add(f);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2516
                externalSubmit(f);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2517
            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2518
            for (int i = 0, size = futures.size(); i < size; i++)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2519
                ((ForkJoinTask<?>)futures.get(i)).quietlyJoin();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2520
            return futures;
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2521
        } catch (Throwable t) {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2522
            for (int i = 0, size = futures.size(); i < size; i++)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2523
                futures.get(i).cancel(false);
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2524
            throw t;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2525
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2526
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2527
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2528
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2529
     * Returns the factory used for constructing new workers.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2530
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2531
     * @return the factory used for constructing new workers
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2532
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2533
    public ForkJoinWorkerThreadFactory getFactory() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2534
        return factory;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2535
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2536
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2537
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2538
     * Returns the handler for internal worker threads that terminate
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2539
     * due to unrecoverable errors encountered while executing tasks.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2540
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2541
     * @return the handler, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2542
     */
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2543
    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2544
        return ueh;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2545
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2546
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2547
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2548
     * Returns the targeted parallelism level of this pool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2549
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2550
     * @return the targeted parallelism level of this pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2551
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2552
    public int getParallelism() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2553
        int par = mode & SMASK;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2554
        return (par > 0) ? par : 1;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2555
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2556
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2557
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2558
     * Returns the targeted parallelism level of the common pool.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2559
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2560
     * @return the targeted parallelism level of the common pool
15002
8ab8c2b2eee6 8005709: Add at since tags to new FJP getCommonPoolParallelism and commonPool
chegar
parents: 14914
diff changeset
  2561
     * @since 1.8
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2562
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2563
    public static int getCommonPoolParallelism() {
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  2564
        return COMMON_PARALLELISM;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2565
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2566
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2567
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2568
     * Returns the number of worker threads that have started but not
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2569
     * yet terminated.  The result returned by this method may differ
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2570
     * from {@link #getParallelism} when threads are created to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2571
     * maintain parallelism when others are cooperatively blocked.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2572
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2573
     * @return the number of worker threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2574
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2575
    public int getPoolSize() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2576
        return ((mode & SMASK) + (short)(ctl >>> TC_SHIFT));
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2577
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2578
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2579
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2580
     * Returns {@code true} if this pool uses local first-in-first-out
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2581
     * scheduling mode for forked tasks that are never joined.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2582
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2583
     * @return {@code true} if this pool uses async mode
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2584
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2585
    public boolean getAsyncMode() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2586
        return (mode & FIFO) != 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2587
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2588
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2589
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2590
     * Returns an estimate of the number of worker threads that are
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2591
     * not blocked waiting to join tasks or for other managed
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2592
     * synchronization. This method may overestimate the
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2593
     * number of running threads.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2594
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2595
     * @return the number of worker threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2596
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2597
    public int getRunningThreadCount() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2598
        WorkQueue[] ws; WorkQueue w;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2599
        VarHandle.acquireFence();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2600
        int rc = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2601
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2602
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2603
                if ((w = ws[i]) != null && w.isApparentlyUnblocked())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2604
                    ++rc;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2605
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2606
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2607
        return rc;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2608
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2609
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2610
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2611
     * Returns an estimate of the number of threads that are currently
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2612
     * stealing or executing tasks. This method may overestimate the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2613
     * number of active threads.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2614
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2615
     * @return the number of active threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2616
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2617
    public int getActiveThreadCount() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2618
        int r = (mode & SMASK) + (int)(ctl >> RC_SHIFT);
9278
f4672926fe4c 7037436: CR 7035020 fails to check shutdown
dl
parents: 9247
diff changeset
  2619
        return (r <= 0) ? 0 : r; // suppress momentarily negative values
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2620
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2621
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2622
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2623
     * Returns {@code true} if all worker threads are currently idle.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2624
     * An idle worker is one that cannot obtain a task to execute
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2625
     * because none are available to steal from other threads, and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2626
     * there are no pending submissions to the pool. This method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2627
     * conservative; it might not return {@code true} immediately upon
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2628
     * idleness of all threads, but will eventually become true if
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2629
     * threads remain inactive.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2630
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2631
     * @return {@code true} if all threads are currently idle
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2632
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2633
    public boolean isQuiescent() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2634
        for (;;) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2635
            long c = ctl;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2636
            int md = mode, pc = md & SMASK;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2637
            int tc = pc + (short)(c >>> TC_SHIFT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2638
            int rc = pc + (int)(c >> RC_SHIFT);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2639
            if ((md & (STOP | TERMINATED)) != 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2640
                return true;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2641
            else if (rc > 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2642
                return false;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2643
            else {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2644
                WorkQueue[] ws; WorkQueue v;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2645
                if ((ws = workQueues) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2646
                    for (int i = 1; i < ws.length; i += 2) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2647
                        if ((v = ws[i]) != null) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2648
                            if (v.source > 0)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2649
                                return false;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2650
                            --tc;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2651
                        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2652
                    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2653
                }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2654
                if (tc == 0 && ctl == c)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2655
                    return true;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2656
            }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2657
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2658
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2659
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2660
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2661
     * Returns an estimate of the total number of tasks stolen from
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2662
     * one thread's work queue by another. The reported value
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2663
     * underestimates the actual total number of steals when the pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2664
     * is not quiescent. This value may be useful for monitoring and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2665
     * tuning fork/join programs: in general, steal counts should be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2666
     * high enough to keep threads busy, but low enough to avoid
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2667
     * overhead and contention across threads.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2668
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2669
     * @return the number of steals
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2670
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2671
    public long getStealCount() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2672
        long count = stealCount;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2673
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2674
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2675
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2676
                if ((w = ws[i]) != null)
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2677
                    count += (long)w.nsteals & 0xffffffffL;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2678
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2679
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2680
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2681
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2682
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2683
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2684
     * Returns an estimate of the total number of tasks currently held
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2685
     * in queues by worker threads (but not including tasks submitted
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2686
     * to the pool that have not begun executing). This value is only
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2687
     * an approximation, obtained by iterating across all threads in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2688
     * the pool. This method may be useful for tuning task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2689
     * granularities.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2690
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2691
     * @return the number of queued tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2692
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2693
    public long getQueuedTaskCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2694
        WorkQueue[] ws; WorkQueue w;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2695
        VarHandle.acquireFence();
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2696
        int count = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2697
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2698
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2699
                if ((w = ws[i]) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2700
                    count += w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2701
            }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2702
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2703
        return count;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2704
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2705
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2706
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2707
     * Returns an estimate of the number of tasks submitted to this
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2708
     * pool that have not yet begun executing.  This method may take
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2709
     * time proportional to the number of submissions.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2710
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2711
     * @return the number of queued submissions
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2712
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2713
    public int getQueuedSubmissionCount() {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2714
        WorkQueue[] ws; WorkQueue w;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2715
        VarHandle.acquireFence();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2716
        int count = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2717
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2718
            for (int i = 0; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2719
                if ((w = ws[i]) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2720
                    count += w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2721
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2722
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2723
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2724
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2725
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2726
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2727
     * Returns {@code true} if there are any tasks submitted to this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2728
     * pool that have not yet begun executing.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2729
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2730
     * @return {@code true} if there are any queued submissions
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2731
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2732
    public boolean hasQueuedSubmissions() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2733
        WorkQueue[] ws; WorkQueue w;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2734
        VarHandle.acquireFence();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2735
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2736
            for (int i = 0; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2737
                if ((w = ws[i]) != null && !w.isEmpty())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2738
                    return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2739
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2740
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2741
        return false;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2742
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2743
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2744
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2745
     * Removes and returns the next unexecuted submission if one is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2746
     * available.  This method may be useful in extensions to this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2747
     * class that re-assign work in systems with multiple pools.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2748
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2749
     * @return the next submission, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2750
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2751
    protected ForkJoinTask<?> pollSubmission() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2752
        return pollScan(true);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2753
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2754
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2755
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2756
     * Removes all available unexecuted submitted and forked tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2757
     * from scheduling queues and adds them to the given collection,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2758
     * without altering their execution status. These may include
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2759
     * artificially generated or wrapped tasks. This method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2760
     * designed to be invoked only when the pool is known to be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2761
     * quiescent. Invocations at other times may not remove all
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2762
     * tasks. A failure encountered while attempting to add elements
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2763
     * to collection {@code c} may result in elements being in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2764
     * neither, either or both collections when the associated
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2765
     * exception is thrown.  The behavior of this operation is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2766
     * undefined if the specified collection is modified while the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2767
     * operation is in progress.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2768
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2769
     * @param c the collection to transfer elements into
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2770
     * @return the number of elements transferred
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2771
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2772
    protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2773
        WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2774
        VarHandle.acquireFence();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2775
        int count = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2776
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2777
            for (int i = 0; i < ws.length; ++i) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2778
                if ((w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2779
                    while ((t = w.poll()) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2780
                        c.add(t);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2781
                        ++count;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2782
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2783
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2784
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2785
        }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2786
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2787
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2788
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2789
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2790
     * Returns a string identifying this pool, as well as its state,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2791
     * including indications of run state, parallelism level, and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2792
     * worker and task counts.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2793
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2794
     * @return a string identifying this pool, as well as its state
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2795
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2796
    public String toString() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2797
        // Use a single pass through workQueues to collect counts
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2798
        int md = mode; // read volatile fields first
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2799
        long c = ctl;
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  2800
        long st = stealCount;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2801
        long qt = 0L, qs = 0L; int rc = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2802
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2803
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2804
            for (int i = 0; i < ws.length; ++i) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2805
                if ((w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2806
                    int size = w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2807
                    if ((i & 1) == 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2808
                        qs += size;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2809
                    else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2810
                        qt += size;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2811
                        st += (long)w.nsteals & 0xffffffffL;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2812
                        if (w.isApparentlyUnblocked())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2813
                            ++rc;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2814
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2815
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2816
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2817
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2818
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2819
        int pc = (md & SMASK);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2820
        int tc = pc + (short)(c >>> TC_SHIFT);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2821
        int ac = pc + (int)(c >> RC_SHIFT);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2822
        if (ac < 0) // ignore transient negative
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2823
            ac = 0;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2824
        String level = ((md & TERMINATED) != 0 ? "Terminated" :
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2825
                        (md & STOP)       != 0 ? "Terminating" :
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2826
                        (md & SHUTDOWN)   != 0 ? "Shutting down" :
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  2827
                        "Running");
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2828
        return super.toString() +
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2829
            "[" + level +
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2830
            ", parallelism = " + pc +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2831
            ", size = " + tc +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2832
            ", active = " + ac +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2833
            ", running = " + rc +
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2834
            ", steals = " + st +
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2835
            ", tasks = " + qt +
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2836
            ", submissions = " + qs +
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2837
            "]";
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2838
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2839
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2840
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2841
     * Possibly initiates an orderly shutdown in which previously
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2842
     * submitted tasks are executed, but no new tasks will be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2843
     * accepted. Invocation has no effect on execution state if this
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2844
     * is the {@link #commonPool()}, and no additional effect if
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2845
     * already shut down.  Tasks that are in the process of being
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2846
     * submitted concurrently during the course of this method may or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2847
     * may not be rejected.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2848
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2849
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2850
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2851
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2852
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2853
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2854
    public void shutdown() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2855
        checkPermission();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2856
        tryTerminate(false, true);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2857
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2858
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2859
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2860
     * Possibly attempts to cancel and/or stop all tasks, and reject
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2861
     * all subsequently submitted tasks.  Invocation has no effect on
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2862
     * execution state if this is the {@link #commonPool()}, and no
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2863
     * additional effect if already shut down. Otherwise, tasks that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2864
     * are in the process of being submitted or executed concurrently
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2865
     * during the course of this method may or may not be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2866
     * rejected. This method cancels both existing and unexecuted
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2867
     * tasks, in order to permit termination in the presence of task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2868
     * dependencies. So the method always returns an empty list
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2869
     * (unlike the case for some other Executors).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2870
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2871
     * @return an empty list
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2872
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2873
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2874
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2875
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2876
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2877
    public List<Runnable> shutdownNow() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2878
        checkPermission();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2879
        tryTerminate(true, true);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2880
        return Collections.emptyList();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2881
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2882
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2883
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2884
     * Returns {@code true} if all tasks have completed following shut down.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2885
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2886
     * @return {@code true} if all tasks have completed following shut down
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2887
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2888
    public boolean isTerminated() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2889
        return (mode & TERMINATED) != 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2890
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2891
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2892
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2893
     * Returns {@code true} if the process of termination has
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2894
     * commenced but not yet completed.  This method may be useful for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2895
     * debugging. A return of {@code true} reported a sufficient
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2896
     * period after shutdown may indicate that submitted tasks have
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2897
     * ignored or suppressed interruption, or are waiting for I/O,
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2898
     * causing this executor not to properly terminate. (See the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2899
     * advisory notes for class {@link ForkJoinTask} stating that
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2900
     * tasks should not normally entail blocking operations.  But if
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2901
     * they do, they must abort them on interrupt.)
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2902
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2903
     * @return {@code true} if terminating but not yet terminated
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2904
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2905
    public boolean isTerminating() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2906
        int md = mode;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2907
        return (md & STOP) != 0 && (md & TERMINATED) == 0;
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2908
    }
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2909
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2910
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2911
     * Returns {@code true} if this pool has been shut down.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2912
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2913
     * @return {@code true} if this pool has been shut down
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2914
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2915
    public boolean isShutdown() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2916
        return (mode & SHUTDOWN) != 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2917
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2918
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2919
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2920
     * Blocks until all tasks have completed execution after a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2921
     * shutdown request, or the timeout occurs, or the current thread
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2922
     * is interrupted, whichever happens first. Because the {@link
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2923
     * #commonPool()} never terminates until program shutdown, when
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2924
     * applied to the common pool, this method is equivalent to {@link
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2925
     * #awaitQuiescence(long, TimeUnit)} but always returns {@code false}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2926
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2927
     * @param timeout the maximum time to wait
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2928
     * @param unit the time unit of the timeout argument
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2929
     * @return {@code true} if this executor terminated and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2930
     *         {@code false} if the timeout elapsed before termination
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2931
     * @throws InterruptedException if interrupted while waiting
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2932
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2933
    public boolean awaitTermination(long timeout, TimeUnit unit)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2934
        throws InterruptedException {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2935
        if (Thread.interrupted())
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2936
            throw new InterruptedException();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2937
        if (this == common) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2938
            awaitQuiescence(timeout, unit);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2939
            return false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2940
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2941
        long nanos = unit.toNanos(timeout);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2942
        if (isTerminated())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2943
            return true;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2944
        if (nanos <= 0L)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2945
            return false;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2946
        long deadline = System.nanoTime() + nanos;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2947
        synchronized (this) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2948
            for (;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2949
                if (isTerminated())
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2950
                    return true;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2951
                if (nanos <= 0L)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2952
                    return false;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2953
                long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2954
                wait(millis > 0L ? millis : 1L);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2955
                nanos = deadline - System.nanoTime();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2956
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2957
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2958
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2959
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2960
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2961
     * If called by a ForkJoinTask operating in this pool, equivalent
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2962
     * in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise,
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2963
     * waits and/or attempts to assist performing tasks until this
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2964
     * pool {@link #isQuiescent} or the indicated timeout elapses.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2965
     *
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2966
     * @param timeout the maximum time to wait
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2967
     * @param unit the time unit of the timeout argument
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2968
     * @return {@code true} if quiescent; {@code false} if the
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2969
     * timeout elapsed.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2970
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2971
    public boolean awaitQuiescence(long timeout, TimeUnit unit) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2972
        long nanos = unit.toNanos(timeout);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2973
        ForkJoinWorkerThread wt;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2974
        Thread thread = Thread.currentThread();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2975
        if ((thread instanceof ForkJoinWorkerThread) &&
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2976
            (wt = (ForkJoinWorkerThread)thread).pool == this) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2977
            helpQuiescePool(wt.workQueue);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2978
            return true;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2979
        }
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2980
        else {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2981
            for (long startTime = System.nanoTime();;) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2982
                ForkJoinTask<?> t;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2983
                if ((t = pollScan(false)) != null)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2984
                    t.doExec();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2985
                else if (isQuiescent())
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2986
                    return true;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2987
                else if ((System.nanoTime() - startTime) > nanos)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2988
                    return false;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2989
                else
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  2990
                    Thread.yield(); // cannot block
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2991
            }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2992
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2993
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2994
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2995
    /**
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2996
     * Waits and/or attempts to assist performing tasks indefinitely
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2997
     * until the {@link #commonPool()} {@link #isQuiescent}.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2998
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2999
    static void quiesceCommonPool() {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3000
        common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3001
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3002
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3003
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3004
     * Interface for extending managed parallelism for tasks running
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3005
     * in {@link ForkJoinPool}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3006
     *
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3007
     * <p>A {@code ManagedBlocker} provides two methods.  Method
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3008
     * {@link #isReleasable} must return {@code true} if blocking is
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3009
     * not necessary. Method {@link #block} blocks the current thread
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3010
     * if necessary (perhaps internally invoking {@code isReleasable}
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3011
     * before actually blocking). These actions are performed by any
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3012
     * thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3013
     * The unusual methods in this API accommodate synchronizers that
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3014
     * may, but don't usually, block for long periods. Similarly, they
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3015
     * allow more efficient internal handling of cases in which
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3016
     * additional workers may be, but usually are not, needed to
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3017
     * ensure sufficient parallelism.  Toward this end,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3018
     * implementations of method {@code isReleasable} must be amenable
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3019
     * to repeated invocation.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3020
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3021
     * <p>For example, here is a ManagedBlocker based on a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3022
     * ReentrantLock:
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3023
     * <pre> {@code
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3024
     * class ManagedLocker implements ManagedBlocker {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3025
     *   final ReentrantLock lock;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3026
     *   boolean hasLock = false;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3027
     *   ManagedLocker(ReentrantLock lock) { this.lock = lock; }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3028
     *   public boolean block() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3029
     *     if (!hasLock)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3030
     *       lock.lock();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3031
     *     return true;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3032
     *   }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3033
     *   public boolean isReleasable() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3034
     *     return hasLock || (hasLock = lock.tryLock());
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3035
     *   }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3036
     * }}</pre>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3037
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3038
     * <p>Here is a class that possibly blocks waiting for an
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3039
     * item on a given queue:
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3040
     * <pre> {@code
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3041
     * class QueueTaker<E> implements ManagedBlocker {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3042
     *   final BlockingQueue<E> queue;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3043
     *   volatile E item = null;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3044
     *   QueueTaker(BlockingQueue<E> q) { this.queue = q; }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3045
     *   public boolean block() throws InterruptedException {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3046
     *     if (item == null)
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3047
     *       item = queue.take();
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3048
     *     return true;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3049
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3050
     *   public boolean isReleasable() {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3051
     *     return item != null || (item = queue.poll()) != null;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3052
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3053
     *   public E getItem() { // call after pool.managedBlock completes
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3054
     *     return item;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3055
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3056
     * }}</pre>
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3057
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3058
    public static interface ManagedBlocker {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3059
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3060
         * Possibly blocks the current thread, for example waiting for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3061
         * a lock or condition.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3062
         *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3063
         * @return {@code true} if no additional blocking is necessary
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3064
         * (i.e., if isReleasable would return true)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3065
         * @throws InterruptedException if interrupted while waiting
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3066
         * (the method is not required to do so, but is allowed to)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3067
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3068
        boolean block() throws InterruptedException;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3069
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3070
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3071
         * Returns {@code true} if blocking is unnecessary.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3072
         * @return {@code true} if blocking is unnecessary
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3073
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3074
        boolean isReleasable();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3075
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3076
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3077
    /**
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3078
     * Runs the given possibly blocking task.  When {@linkplain
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3079
     * ForkJoinTask#inForkJoinPool() running in a ForkJoinPool}, this
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3080
     * method possibly arranges for a spare thread to be activated if
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3081
     * necessary to ensure sufficient parallelism while the current
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3082
     * thread is blocked in {@link ManagedBlocker#block blocker.block()}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3083
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3084
     * <p>This method repeatedly calls {@code blocker.isReleasable()} and
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3085
     * {@code blocker.block()} until either method returns {@code true}.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3086
     * Every call to {@code blocker.block()} is preceded by a call to
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3087
     * {@code blocker.isReleasable()} that returned {@code false}.
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3088
     *
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3089
     * <p>If not running in a ForkJoinPool, this method is
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3090
     * behaviorally equivalent to
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3091
     * <pre> {@code
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3092
     * while (!blocker.isReleasable())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3093
     *   if (blocker.block())
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3094
     *     break;}</pre>
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3095
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3096
     * If running in a ForkJoinPool, the pool may first be expanded to
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3097
     * ensure sufficient parallelism available during the call to
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3098
     * {@code blocker.block()}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3099
     *
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3100
     * @param blocker the blocker task
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3101
     * @throws InterruptedException if {@code blocker.block()} did so
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3102
     */
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3103
    public static void managedBlock(ManagedBlocker blocker)
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3104
        throws InterruptedException {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3105
        if (blocker == null) throw new NullPointerException();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3106
        ForkJoinPool p;
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3107
        ForkJoinWorkerThread wt;
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3108
        WorkQueue w;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3109
        Thread t = Thread.currentThread();
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
  3110
        if ((t instanceof ForkJoinWorkerThread) &&
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3111
            (p = (wt = (ForkJoinWorkerThread)t).pool) != null &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3112
            (w = wt.workQueue) != null) {
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3113
            int block;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3114
            while (!blocker.isReleasable()) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3115
                if ((block = p.tryCompensate(w)) != 0) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3116
                    try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3117
                        do {} while (!blocker.isReleasable() &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3118
                                     !blocker.block());
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3119
                    } finally {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3120
                        CTL.getAndAdd(p, (block > 0) ? RC_UNIT : 0L);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3121
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3122
                    break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3123
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3124
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3125
        }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3126
        else {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3127
            do {} while (!blocker.isReleasable() &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3128
                         !blocker.block());
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3129
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3130
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3131
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3132
    /**
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3133
     * If the given executor is a ForkJoinPool, poll and execute
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3134
     * AsynchronousCompletionTasks from worker's queue until none are
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3135
     * available or blocker is released.
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3136
     */
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3137
    static void helpAsyncBlocker(Executor e, ManagedBlocker blocker) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3138
        if (e instanceof ForkJoinPool) {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3139
            WorkQueue w; ForkJoinWorkerThread wt; WorkQueue[] ws; int r, n;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3140
            ForkJoinPool p = (ForkJoinPool)e;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3141
            Thread thread = Thread.currentThread();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3142
            if (thread instanceof ForkJoinWorkerThread &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3143
                (wt = (ForkJoinWorkerThread)thread).pool == p)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3144
                w = wt.workQueue;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3145
            else if ((r = ThreadLocalRandom.getProbe()) != 0 &&
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3146
                     (ws = p.workQueues) != null && (n = ws.length) > 0)
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3147
                w = ws[(n - 1) & r & SQMASK];
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3148
            else
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3149
                w = null;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3150
            if (w != null)
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3151
                w.helpAsyncBlocker(blocker);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3152
        }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3153
    }
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3154
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3155
    // AbstractExecutorService overrides.  These rely on undocumented
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3156
    // fact that ForkJoinTask.adapt returns ForkJoinTasks that also
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3157
    // implement RunnableFuture.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3158
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3159
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3160
        return new ForkJoinTask.AdaptedRunnable<T>(runnable, value);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3161
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3162
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3163
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3164
        return new ForkJoinTask.AdaptedCallable<T>(callable);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3165
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3166
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3167
    // VarHandle mechanics
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3168
    private static final VarHandle CTL;
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3169
    private static final VarHandle MODE;
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3170
    static final VarHandle QA;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3171
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3172
    static {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3173
        try {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3174
            MethodHandles.Lookup l = MethodHandles.lookup();
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3175
            CTL = l.findVarHandle(ForkJoinPool.class, "ctl", long.class);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3176
            MODE = l.findVarHandle(ForkJoinPool.class, "mode", int.class);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3177
            QA = MethodHandles.arrayElementVarHandle(ForkJoinTask[].class);
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3178
        } catch (ReflectiveOperationException e) {
49563
79d2c9da2c26 8200520: forkjoin tasks interrupted after shutdown
dl
parents: 47216
diff changeset
  3179
            throw new ExceptionInInitializerError(e);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3180
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3181
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3182
        // Reduce the risk of rare disastrous classloading in first call to
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3183
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3184
        Class<?> ensureLoaded = LockSupport.class;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3185
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3186
        int commonMaxSpares = DEFAULT_COMMON_MAX_SPARES;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3187
        try {
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3188
            String p = System.getProperty
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3189
                ("java.util.concurrent.ForkJoinPool.common.maximumSpares");
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3190
            if (p != null)
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3191
                commonMaxSpares = Integer.parseInt(p);
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3192
        } catch (Exception ignore) {}
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3193
        COMMON_MAX_SPARES = commonMaxSpares;
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3194
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3195
        defaultForkJoinWorkerThreadFactory =
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3196
            new DefaultForkJoinWorkerThreadFactory();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3197
        modifyThreadPermission = new RuntimePermission("modifyThread");
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3198
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
  3199
        common = AccessController.doPrivileged(new PrivilegedAction<>() {
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
  3200
            public ForkJoinPool run() {
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 41131
diff changeset
  3201
                return new ForkJoinPool((byte)0); }});
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3202
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 37782
diff changeset
  3203
        COMMON_PARALLELISM = Math.max(common.mode & SMASK, 1);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3204
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3205
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3206
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3207
     * Factory for innocuous worker threads.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3208
     */
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 30681
diff changeset
  3209
    private static final class InnocuousForkJoinWorkerThreadFactory
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3210
        implements ForkJoinWorkerThreadFactory {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3211
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3212
        /**
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3213
         * An ACC to restrict permissions for the factory itself.
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3214
         * The constructed workers have no permissions set.
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3215
         */
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3216
        private static final AccessControlContext ACC = contextWithPermissions(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3217
            modifyThreadPermission,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3218
            new RuntimePermission("enableContextClassLoaderOverride"),
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3219
            new RuntimePermission("modifyThreadGroup"),
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3220
            new RuntimePermission("getClassLoader"),
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3221
            new RuntimePermission("setContextClassLoader"));
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3222
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3223
        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3224
            return AccessController.doPrivileged(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3225
                new PrivilegedAction<>() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3226
                    public ForkJoinWorkerThread run() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3227
                        return new ForkJoinWorkerThread.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3228
                            InnocuousForkJoinWorkerThread(pool); }},
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 42322
diff changeset
  3229
                ACC);
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3230
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 21667
diff changeset
  3231
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3232
}