jdk/src/share/classes/java/util/concurrent/ForkJoinPool.java
author darcy
Wed, 13 Nov 2013 11:06:57 -0800
changeset 21667 e4c61eb98292
parent 19412 e7847bc5763b
child 22070 9a4f43c9b15a
permissions -rw-r--r--
8028300: Fix raw type lint warnings in java.util.concurrent Reviewed-by: chegar, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     1
/*
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     3
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
     9
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    14
 * accompanied this code).
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    15
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    23
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    24
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    25
/*
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    29
 * file:
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    30
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 8765
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    34
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    35
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    36
package java.util.concurrent;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    37
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
    38
import java.lang.Thread.UncaughtExceptionHandler;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    39
import java.util.ArrayList;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    40
import java.util.Arrays;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    41
import java.util.Collection;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    42
import java.util.Collections;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    43
import java.util.List;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    44
import java.util.concurrent.AbstractExecutorService;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    45
import java.util.concurrent.Callable;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    46
import java.util.concurrent.ExecutorService;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    47
import java.util.concurrent.Future;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    48
import java.util.concurrent.RejectedExecutionException;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    49
import java.util.concurrent.RunnableFuture;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
    50
import java.util.concurrent.ThreadLocalRandom;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    51
import java.util.concurrent.TimeUnit;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    52
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    53
/**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    54
 * An {@link ExecutorService} for running {@link ForkJoinTask}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    55
 * A {@code ForkJoinPool} provides the entry point for submissions
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    56
 * from non-{@code ForkJoinTask} clients, as well as management and
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    57
 * monitoring operations.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    58
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    59
 * <p>A {@code ForkJoinPool} differs from other kinds of {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    60
 * ExecutorService} mainly by virtue of employing
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    61
 * <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
    62
 * 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
    63
 * tasks (eventually blocking waiting for work if none exist). This
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    64
 * enables efficient processing when most tasks spawn other subtasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    65
 * (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
    66
 * tasks are submitted to the pool from external clients.  Especially
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    67
 * when setting <em>asyncMode</em> to true in constructors, {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    68
 * ForkJoinPool}s may also be appropriate for use with event-style
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    69
 * tasks that are never joined.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    70
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    71
 * <p>A static {@link #commonPool()} is available and appropriate for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    72
 * most applications. The common pool is used by any ForkJoinTask that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    73
 * is not explicitly submitted to a specified pool. Using the common
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    74
 * pool normally reduces resource usage (its threads are slowly
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    75
 * reclaimed during periods of non-use, and reinstated upon subsequent
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    76
 * use).
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    77
 *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    78
 * <p>For applications that require separate or custom pools, a {@code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    79
 * ForkJoinPool} may be constructed with a given target parallelism
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    80
 * level; by default, equal to the number of available processors. The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    81
 * pool attempts to maintain enough active (or available) threads by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    82
 * 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
    83
 * 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
    84
 * 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
    85
 * I/O or other unmanaged synchronization. The nested {@link
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    86
 * ManagedBlocker} interface enables extension of the kinds of
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    87
 * synchronization accommodated.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    88
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    89
 * <p>In addition to execution and lifecycle control methods, this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    90
 * class provides status check methods (for example
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    91
 * {@link #getStealCount}) that are intended to aid in developing,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    92
 * tuning, and monitoring fork/join applications. Also, method
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    93
 * {@link #toString} returns indications of pool state in a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    94
 * convenient form for informal monitoring.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    95
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    96
 * <p>As is the case with other ExecutorServices, there are three
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    97
 * main task execution methods summarized in the following table.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    98
 * These are designed to be used primarily by clients not already
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    99
 * engaged in fork/join computations in the current pool.  The main
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   100
 * forms of these methods accept instances of {@code ForkJoinTask},
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   101
 * but overloaded forms also allow mixed execution of plain {@code
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   102
 * Runnable}- or {@code Callable}- based activities as well.  However,
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   103
 * tasks that are already executing in a pool should normally instead
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   104
 * use the within-computation forms listed in the table unless using
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   105
 * 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
   106
 * there is little difference among choice of methods.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   107
 *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   108
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   109
 * <caption>Summary of task execution methods</caption>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   110
 *  <tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   111
 *    <td></td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   112
 *    <td ALIGN=CENTER> <b>Call from non-fork/join clients</b></td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   113
 *    <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   114
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   115
 *  <tr>
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   116
 *    <td> <b>Arrange async execution</b></td>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   117
 *    <td> {@link #execute(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   118
 *    <td> {@link ForkJoinTask#fork}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   119
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   120
 *  <tr>
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   121
 *    <td> <b>Await and obtain result</b></td>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   122
 *    <td> {@link #invoke(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   123
 *    <td> {@link ForkJoinTask#invoke}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   124
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   125
 *  <tr>
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   126
 *    <td> <b>Arrange exec and obtain Future</b></td>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   127
 *    <td> {@link #submit(ForkJoinTask)}</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   128
 *    <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   129
 *  </tr>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   130
 * </table>
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   131
 *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   132
 * <p>The common pool is by default constructed with default
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   133
 * parameters, but these may be controlled by setting three
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   134
 * {@linkplain System#getProperty system properties}:
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   135
 * <ul>
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   136
 * <li>{@code java.util.concurrent.ForkJoinPool.common.parallelism}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   137
 * - the parallelism level, a non-negative integer
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   138
 * <li>{@code java.util.concurrent.ForkJoinPool.common.threadFactory}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   139
 * - the class name of a {@link ForkJoinWorkerThreadFactory}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   140
 * <li>{@code java.util.concurrent.ForkJoinPool.common.exceptionHandler}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   141
 * - the class name of a {@link UncaughtExceptionHandler}
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   142
 * </ul>
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   143
 * The system class loader is used to load these classes.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   144
 * Upon any error in establishing these settings, default parameters
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   145
 * 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
   146
 * the common pool by setting the parallelism property to zero, and/or
19412
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
   147
 * using a factory that may return {@code null}. However doing so may
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
   148
 * cause unjoined tasks to never be executed.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   149
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   150
 * <p><b>Implementation notes</b>: This implementation restricts the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   151
 * maximum number of running threads to 32767. Attempts to create
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   152
 * pools with greater than the maximum number result in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   153
 * {@code IllegalArgumentException}.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   154
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   155
 * <p>This implementation rejects submitted tasks (that is, by throwing
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   156
 * {@link RejectedExecutionException}) only when the pool is shut down
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   157
 * or internal resources have been exhausted.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   158
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   159
 * @since 1.7
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   160
 * @author Doug Lea
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   161
 */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   162
@sun.misc.Contended
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   163
public class ForkJoinPool extends AbstractExecutorService {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   164
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   165
    /*
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   166
     * Implementation Overview
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   167
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   168
     * This class and its nested classes provide the main
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   169
     * functionality and control for a set of worker threads:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   170
     * Submissions from non-FJ threads enter into submission queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   171
     * Workers take these tasks and typically split them into subtasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   172
     * that may be stolen by other workers.  Preference rules give
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   173
     * first priority to processing tasks from their own queues (LIFO
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   174
     * or FIFO, depending on mode), then to randomized FIFO steals of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   175
     * tasks in other queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   176
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   177
     * WorkQueues
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   178
     * ==========
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   179
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   180
     * Most operations occur within work-stealing queues (in nested
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   181
     * class WorkQueue).  These are special forms of Deques that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   182
     * support only three of the four possible end-operations -- push,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   183
     * pop, and poll (aka steal), under the further constraints that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   184
     * 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
   185
     * extended here, under a lock), while poll may be called from
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   186
     * other threads.  (If you are unfamiliar with them, you probably
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   187
     * want to read Herlihy and Shavit's book "The Art of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   188
     * Multiprocessor programming", chapter 16 describing these in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   189
     * more detail before proceeding.)  The main work-stealing queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   190
     * design is roughly similar to those in the papers "Dynamic
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   191
     * Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   192
     * (http://research.sun.com/scalable/pubs/index.html) and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   193
     * "Idempotent work stealing" by Michael, Saraswat, and Vechev,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   194
     * PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186).
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   195
     * See also "Correct and Efficient Work-Stealing for Weak Memory
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   196
     * Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   197
     * (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   198
     * analysis of memory ordering (atomic, volatile etc) issues.  The
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   199
     * main differences ultimately stem from GC requirements that we
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   200
     * null out taken slots as soon as we can, to maintain as small a
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   201
     * footprint as possible even in programs generating huge numbers
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   202
     * of tasks. To accomplish this, we shift the CAS arbitrating pop
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   203
     * vs poll (steal) from being on the indices ("base" and "top") to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   204
     * the slots themselves.  So, both a successful pop and poll
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   205
     * mainly entail a CAS of a slot from non-null to null.  Because
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   206
     * we rely on CASes of references, we do not need tag bits on base
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   207
     * or top.  They are simple ints as used in any circular
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   208
     * array-based queue (see for example ArrayDeque).  Updates to the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   209
     * indices must still be ordered in a way that guarantees that top
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   210
     * == base means the queue is empty, but otherwise may err on the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   211
     * side of possibly making the queue appear nonempty when a push,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   212
     * pop, or poll have not fully committed. Note that this means
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   213
     * that the poll operation, considered individually, is not
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   214
     * wait-free. One thief cannot successfully continue until another
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   215
     * in-progress one (or, if previously empty, a push) completes.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   216
     * However, in the aggregate, we ensure at least probabilistic
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   217
     * non-blockingness.  If an attempted steal fails, a thief always
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   218
     * chooses a different random victim target to try next. So, in
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   219
     * order for one thief to progress, it suffices for any
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   220
     * in-progress poll or new push on any empty queue to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   221
     * complete. (This is why we normally use method pollAt and its
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   222
     * variants that try once at the apparent base index, else
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   223
     * consider alternative actions, rather than method poll.)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   224
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   225
     * This approach also enables support of a user mode in which local
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   226
     * task processing is in FIFO, not LIFO order, simply by using
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   227
     * poll rather than pop.  This can be useful in message-passing
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   228
     * frameworks in which tasks are never joined.  However neither
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   229
     * mode considers affinities, loads, cache localities, etc, so
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   230
     * rarely provide the best possible performance on a given
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   231
     * machine, but portably provide good throughput by averaging over
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   232
     * these factors.  (Further, even if we did try to use such
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   233
     * information, we do not usually have a basis for exploiting it.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   234
     * For example, some sets of tasks profit from cache affinities,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   235
     * but others are harmed by cache pollution effects.)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   236
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   237
     * WorkQueues are also used in a similar way for tasks submitted
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   238
     * to the pool. We cannot mix these tasks in the same queues used
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   239
     * for work-stealing (this would contaminate lifo/fifo
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   240
     * processing). Instead, we randomly associate submission queues
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   241
     * with submitting threads, using a form of hashing.  The
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   242
     * ThreadLocalRandom probe value serves as a hash code for
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   243
     * choosing existing queues, and may be randomly repositioned upon
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   244
     * contention with other submitters.  In essence, submitters act
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   245
     * like workers except that they are restricted to executing local
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   246
     * tasks that they submitted (or in the case of CountedCompleters,
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   247
     * others with the same root task).  However, because most
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   248
     * shared/external queue operations are more expensive than
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   249
     * internal, and because, at steady state, external submitters
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   250
     * will compete for CPU with workers, ForkJoinTask.join and
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   251
     * related methods disable them from repeatedly helping to process
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   252
     * tasks if all workers are active.  Insertion of tasks in shared
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   253
     * mode requires a lock (mainly to protect in the case of
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   254
     * resizing) but we use only a simple spinlock (using bits in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   255
     * field qlock), because submitters encountering a busy queue move
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   256
     * on to try or create other queues -- they block only when
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   257
     * creating and registering new queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   258
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   259
     * Management
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   260
     * ==========
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   261
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   262
     * The main throughput advantages of work-stealing stem from
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   263
     * decentralized control -- workers mostly take tasks from
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   264
     * themselves or each other. We cannot negate this in the
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   265
     * implementation of other management responsibilities. The main
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   266
     * tactic for avoiding bottlenecks is packing nearly all
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   267
     * essentially atomic control state into two volatile variables
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   268
     * that are by far most often read (not written) as status and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   269
     * consistency checks.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   270
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   271
     * Field "ctl" contains 64 bits holding all the information needed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   272
     * to atomically decide to add, inactivate, enqueue (on an event
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   273
     * queue), dequeue, and/or re-activate workers.  To enable this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   274
     * packing, we restrict maximum parallelism to (1<<15)-1 (which is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   275
     * far in excess of normal operating range) to allow ids, counts,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   276
     * and their negations (used for thresholding) to fit into 16bit
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   277
     * fields.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   278
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   279
     * Field "plock" is a form of sequence lock with a saturating
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   280
     * shutdown bit (similarly for per-queue "qlocks"), mainly
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   281
     * protecting updates to the workQueues array, as well as to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   282
     * enable shutdown.  When used as a lock, it is normally only very
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   283
     * briefly held, so is nearly always available after at most a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   284
     * brief spin, but we use a monitor-based backup strategy to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   285
     * block when needed.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   286
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   287
     * Recording WorkQueues.  WorkQueues are recorded in the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   288
     * "workQueues" array that is created upon first use and expanded
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   289
     * if necessary.  Updates to the array while recording new workers
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   290
     * and unrecording terminated ones are protected from each other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   291
     * by a lock but the array is otherwise concurrently readable, and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   292
     * accessed directly.  To simplify index-based operations, the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   293
     * array size is always a power of two, and all readers must
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   294
     * tolerate null slots. Worker queues are at odd indices. Shared
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   295
     * (submission) queues are at even indices, up to a maximum of 64
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   296
     * slots, to limit growth even if array needs to expand to add
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   297
     * more workers. Grouping them together in this way simplifies and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   298
     * speeds up task scanning.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   299
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   300
     * All worker thread creation is on-demand, triggered by task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   301
     * submissions, replacement of terminated workers, and/or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   302
     * compensation for blocked workers. However, all other support
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   303
     * code is set up to work with other policies.  To ensure that we
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   304
     * do not hold on to worker references that would prevent GC, ALL
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   305
     * accesses to workQueues are via indices into the workQueues
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   306
     * array (which is one source of some of the messy code
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   307
     * constructions here). In essence, the workQueues array serves as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   308
     * a weak reference mechanism. Thus for example the wait queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   309
     * field of ctl stores indices, not references.  Access to the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   310
     * workQueues in associated methods (for example signalWork) must
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   311
     * both index-check and null-check the IDs. All such accesses
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   312
     * ignore bad IDs by returning out early from what they are doing,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   313
     * since this can only be associated with termination, in which
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   314
     * case it is OK to give up.  All uses of the workQueues array
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   315
     * also check that it is non-null (even if previously
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   316
     * non-null). This allows nulling during termination, which is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   317
     * currently not necessary, but remains an option for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   318
     * resource-revocation-based shutdown schemes. It also helps
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   319
     * reduce JIT issuance of uncommon-trap code, which tends to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   320
     * unnecessarily complicate control flow in some methods.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   321
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   322
     * Event Queuing. Unlike HPC work-stealing frameworks, we cannot
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   323
     * let workers spin indefinitely scanning for tasks when none can
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   324
     * be found immediately, and we cannot start/resume workers unless
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   325
     * there appear to be tasks available.  On the other hand, we must
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   326
     * quickly prod them into action when new tasks are submitted or
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   327
     * generated. In many usages, ramp-up time to activate workers is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   328
     * the main limiting factor in overall performance (this is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   329
     * compounded at program start-up by JIT compilation and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   330
     * allocation). So we try to streamline this as much as possible.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   331
     * We park/unpark workers after placing in an event wait queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   332
     * when they cannot find work. This "queue" is actually a simple
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   333
     * Treiber stack, headed by the "id" field of ctl, plus a 15bit
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   334
     * counter value (that reflects the number of times a worker has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   335
     * been inactivated) to avoid ABA effects (we need only as many
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   336
     * version numbers as worker threads). Successors are held in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   337
     * field WorkQueue.nextWait.  Queuing deals with several intrinsic
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   338
     * races, mainly that a task-producing thread can miss seeing (and
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   339
     * signalling) another thread that gave up looking for work but
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   340
     * has not yet entered the wait queue. We solve this by requiring
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   341
     * a full sweep of all workers (via repeated calls to method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   342
     * scan()) both before and after a newly waiting worker is added
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   343
     * to the wait queue.  Because enqueued workers may actually be
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   344
     * rescanning rather than waiting, we set and clear the "parker"
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   345
     * field of WorkQueues to reduce unnecessary calls to unpark.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   346
     * (This requires a secondary recheck to avoid missed signals.)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   347
     * Note the unusual conventions about Thread.interrupts
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   348
     * surrounding parking and other blocking: Because interrupts are
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   349
     * used solely to alert threads to check termination, which is
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   350
     * checked anyway upon blocking, we clear status (using
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   351
     * Thread.interrupted) before any call to park, so that park does
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   352
     * not immediately return due to status being set via some other
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   353
     * unrelated call to interrupt in user code.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   354
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   355
     * Signalling.  We create or wake up workers only when there
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   356
     * appears to be at least one task they might be able to find and
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   357
     * execute.  When a submission is added or another worker adds a
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   358
     * task to a queue that has fewer than two tasks, they signal
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   359
     * waiting workers (or trigger creation of new ones if fewer than
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   360
     * the given parallelism level -- signalWork).  These primary
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   361
     * signals are buttressed by others whenever other threads remove
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   362
     * a task from a queue and notice that there are other tasks there
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   363
     * as well.  So in general, pools will be over-signalled. On most
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   364
     * platforms, signalling (unpark) overhead time is noticeably
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   365
     * long, and the time between signalling a thread and it actually
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   366
     * making progress can be very noticeably long, so it is worth
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   367
     * offloading these delays from critical paths as much as
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   368
     * possible. Additionally, workers spin-down gradually, by staying
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   369
     * alive so long as they see the ctl state changing.  Similar
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   370
     * stability-sensing techniques are also used before blocking in
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   371
     * awaitJoin and helpComplete.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   372
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   373
     * Trimming workers. To release resources after periods of lack of
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   374
     * use, a worker starting to wait when the pool is quiescent will
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   375
     * time out and terminate if the pool has remained quiescent for a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   376
     * given period -- a short period if there are more threads than
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   377
     * parallelism, longer as the number of threads decreases. This
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   378
     * will slowly propagate, eventually terminating all workers after
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   379
     * periods of non-use.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   380
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   381
     * Shutdown and Termination. A call to shutdownNow atomically sets
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   382
     * a plock bit and then (non-atomically) sets each worker's
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   383
     * qlock status, cancels all unprocessed tasks, and wakes up
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   384
     * all waiting workers.  Detecting whether termination should
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   385
     * commence after a non-abrupt shutdown() call requires more work
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   386
     * and bookkeeping. We need consensus about quiescence (i.e., that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   387
     * there is no more work). The active count provides a primary
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   388
     * indication but non-abrupt shutdown still requires a rechecking
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   389
     * scan for any workers that are inactive but not queued.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   390
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   391
     * Joining Tasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   392
     * =============
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   393
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   394
     * 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
   395
     * 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
   396
     * are multiplexing many tasks on to a pool of workers, we can't
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   397
     * just let them block (as in Thread.join).  We also cannot just
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   398
     * reassign the joiner's run-time stack with another and replace
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   399
     * it later, which would be a form of "continuation", that even if
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   400
     * possible is not necessarily a good idea since we sometimes need
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   401
     * both an unblocked task and its continuation to progress.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   402
     * Instead we combine two tactics:
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   403
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   404
     *   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
   405
     *      would be running if the steal had not occurred.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   406
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   407
     *   Compensating: Unless there are already enough live threads,
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   408
     *      method tryCompensate() may create or re-activate a spare
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   409
     *      thread to compensate for blocked joiners until they unblock.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   410
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   411
     * A third form (implemented in tryRemoveAndExec) amounts to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   412
     * helping a hypothetical compensator: If we can readily tell that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   413
     * 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
   414
     * task being joined, the joining thread can do so directly,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   415
     * without the need for a compensation thread (although at the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   416
     * expense of larger run-time stacks, but the tradeoff is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   417
     * typically worthwhile).
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   418
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   419
     * The ManagedBlocker extension API can't use helping so relies
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   420
     * only on compensation in method awaitBlocker.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   421
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   422
     * The algorithm in tryHelpStealer entails a form of "linear"
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   423
     * helping: Each worker records (in field currentSteal) the most
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   424
     * recent task it stole from some other worker. Plus, it records
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   425
     * (in field currentJoin) the task it is currently actively
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   426
     * joining. Method tryHelpStealer uses these markers to try to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   427
     * find a worker to help (i.e., steal back a task from and execute
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   428
     * it) that could hasten completion of the actively joined task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   429
     * In essence, the joiner executes a task that would be on its own
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   430
     * local deque had the to-be-joined task not been stolen. This may
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   431
     * be seen as a conservative variant of the approach in Wagner &
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   432
     * Calder "Leapfrogging: a portable technique for implementing
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   433
     * efficient futures" SIGPLAN Notices, 1993
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   434
     * (http://portal.acm.org/citation.cfm?id=155354). It differs in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   435
     * that: (1) We only maintain dependency links across workers upon
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   436
     * steals, rather than use per-task bookkeeping.  This sometimes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   437
     * requires a linear scan of workQueues array to locate stealers,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   438
     * but often doesn't because stealers leave hints (that may become
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   439
     * stale/wrong) of where to locate them.  It is only a hint
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   440
     * because a worker might have had multiple steals and the hint
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   441
     * records only one of them (usually the most current).  Hinting
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   442
     * isolates cost to when it is needed, rather than adding to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   443
     * per-task overhead.  (2) It is "shallow", ignoring nesting and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   444
     * potentially cyclic mutual steals.  (3) It is intentionally
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   445
     * racy: field currentJoin is updated only while actively joining,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   446
     * which means that we miss links in the chain during long-lived
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   447
     * tasks, GC stalls etc (which is OK since blocking in such cases
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   448
     * is usually a good idea).  (4) We bound the number of attempts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   449
     * to find work (see MAX_HELP) and fall back to suspending the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   450
     * worker and if necessary replacing it with another.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   451
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   452
     * Helping actions for CountedCompleters are much simpler: Method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   453
     * helpComplete can take and execute any task with the same root
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   454
     * as the task being waited on. However, this still entails some
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   455
     * traversal of completer chains, so is less efficient than using
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   456
     * CountedCompleters without explicit joins.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   457
     *
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   458
     * It is impossible to keep exactly the target parallelism number
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   459
     * of threads running at any given time.  Determining the
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   460
     * existence of conservatively safe helping targets, the
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   461
     * availability of already-created spares, and the apparent need
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   462
     * to create new spares are all racy, so we rely on multiple
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   463
     * retries of each.  Compensation in the apparent absence of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   464
     * helping opportunities is challenging to control on JVMs, where
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   465
     * GC and other activities can stall progress of tasks that in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   466
     * turn stall out many other dependent tasks, without us being
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   467
     * able to determine whether they will ever require compensation.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   468
     * Even though work-stealing otherwise encounters little
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   469
     * degradation in the presence of more threads than cores,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   470
     * aggressively adding new threads in such cases entails risk of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   471
     * unwanted positive feedback control loops in which more threads
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   472
     * cause more dependent stalls (as well as delayed progress of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   473
     * unblocked threads to the point that we know they are available)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   474
     * leading to more situations requiring more threads, and so
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   475
     * on. This aspect of control can be seen as an (analytically
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   476
     * intractable) game with an opponent that may choose the worst
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   477
     * (for us) active thread to stall at any time.  We take several
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   478
     * precautions to bound losses (and thus bound gains), mainly in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   479
     * methods tryCompensate and awaitJoin.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   480
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   481
     * Common Pool
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   482
     * ===========
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   483
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   484
     * The static common pool always exists after static
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   485
     * initialization.  Since it (or any other created pool) need
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   486
     * never be used, we minimize initial construction overhead and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   487
     * footprint to the setup of about a dozen fields, with no nested
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   488
     * allocation. Most bootstrapping occurs within method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   489
     * fullExternalPush during the first submission to the pool.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   490
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   491
     * When external threads submit to the common pool, they can
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   492
     * perform subtask processing (see externalHelpJoin and related
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   493
     * methods).  This caller-helps policy makes it sensible to set
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   494
     * common pool parallelism level to one (or more) less than the
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   495
     * total number of available cores, or even zero for pure
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   496
     * caller-runs.  We do not need to record whether external
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   497
     * submissions are to the common pool -- if not, externalHelpJoin
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   498
     * returns quickly (at the most helping to signal some common pool
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   499
     * workers). These submitters would otherwise be blocked waiting
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   500
     * for completion, so the extra effort (with liberally sprinkled
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   501
     * task status checks) in inapplicable cases amounts to an odd
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   502
     * form of limited spin-wait before blocking in ForkJoinTask.join.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   503
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   504
     * Style notes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   505
     * ===========
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   506
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   507
     * There is a lot of representation-level coupling among classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   508
     * ForkJoinPool, ForkJoinWorkerThread, and ForkJoinTask.  The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   509
     * fields of WorkQueue maintain data structures managed by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   510
     * ForkJoinPool, so are directly accessed.  There is little point
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   511
     * trying to reduce this, since any associated future changes in
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   512
     * representations will need to be accompanied by algorithmic
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   513
     * changes anyway. Several methods intrinsically sprawl because
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   514
     * they must accumulate sets of consistent reads of volatiles held
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   515
     * in local variables.  Methods signalWork() and scan() are the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   516
     * main bottlenecks, so are especially heavily
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   517
     * micro-optimized/mangled.  There are lots of inline assignments
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   518
     * (of form "while ((local = field) != 0)") which are usually the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   519
     * simplest way to ensure the required read orderings (which are
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   520
     * sometimes critical). This leads to a "C"-like style of listing
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   521
     * declarations of these locals at the heads of methods or blocks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   522
     * There are several occurrences of the unusual "do {} while
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   523
     * (!cas...)"  which is the simplest way to force an update of a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   524
     * CAS'ed variable. There are also other coding oddities (including
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   525
     * several unnecessary-looking hoisted null checks) that help
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   526
     * some methods perform reasonably even when interpreted (not
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   527
     * compiled).
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   528
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   529
     * The order of declarations in this file is:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   530
     * (1) Static utility functions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   531
     * (2) Nested (static) classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   532
     * (3) Static fields
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   533
     * (4) Fields, along with constants used when unpacking some of them
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   534
     * (5) Internal control methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   535
     * (6) Callbacks and other support for ForkJoinTask methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   536
     * (7) Exported methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   537
     * (8) Static block initializing statics in minimally dependent order
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   538
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   539
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   540
    // Static utilities
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   541
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   542
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   543
     * If there is a security manager, makes sure caller has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   544
     * permission to modify threads.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   545
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   546
    private static void checkPermission() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   547
        SecurityManager security = System.getSecurityManager();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   548
        if (security != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   549
            security.checkPermission(modifyThreadPermission);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   550
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   551
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   552
    // Nested classes
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   553
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   554
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   555
     * Factory for creating new {@link ForkJoinWorkerThread}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   556
     * A {@code ForkJoinWorkerThreadFactory} must be defined and used
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   557
     * for {@code ForkJoinWorkerThread} subclasses that extend base
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   558
     * functionality or initialize threads with different contexts.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   559
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   560
    public static interface ForkJoinWorkerThreadFactory {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   561
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   562
         * Returns a new worker thread operating in the given pool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   563
         *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   564
         * @param pool the pool this thread works in
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
   565
         * @return the new worker thread
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   566
         * @throws NullPointerException if the pool is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   567
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   568
        public ForkJoinWorkerThread newThread(ForkJoinPool pool);
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   569
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   570
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   571
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   572
     * Default ForkJoinWorkerThreadFactory implementation; creates a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   573
     * new ForkJoinWorkerThread.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   574
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   575
    static final class DefaultForkJoinWorkerThreadFactory
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   576
        implements ForkJoinWorkerThreadFactory {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   577
        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   578
            return new ForkJoinWorkerThread(pool);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   579
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   580
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   581
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   582
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   583
     * Class for artificial tasks that are used to replace the target
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   584
     * of local joins if they are removed from an interior queue slot
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   585
     * in WorkQueue.tryRemoveAndExec. We don't need the proxy to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   586
     * actually do anything beyond having a unique identity.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   587
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   588
    static final class EmptyTask extends ForkJoinTask<Void> {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   589
        private static final long serialVersionUID = -7721805057305804111L;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   590
        EmptyTask() { status = ForkJoinTask.NORMAL; } // force done
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   591
        public final Void getRawResult() { return null; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   592
        public final void setRawResult(Void x) {}
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   593
        public final boolean exec() { return true; }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   594
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   595
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   596
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   597
     * Queues supporting work-stealing as well as external task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   598
     * submission. See above for main rationale and algorithms.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   599
     * Implementation relies heavily on "Unsafe" intrinsics
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   600
     * and selective use of "volatile":
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   601
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   602
     * Field "base" is the index (mod array.length) of the least valid
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   603
     * queue slot, which is always the next position to steal (poll)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   604
     * from if nonempty. Reads and writes require volatile orderings
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   605
     * but not CAS, because updates are only performed after slot
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   606
     * CASes.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   607
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   608
     * Field "top" is the index (mod array.length) of the next queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   609
     * slot to push to or pop from. It is written only by owner thread
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   610
     * for push, or under lock for external/shared push, and accessed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   611
     * by other threads only after reading (volatile) base.  Both top
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   612
     * and base are allowed to wrap around on overflow, but (top -
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   613
     * base) (or more commonly -(base - top) to force volatile read of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   614
     * base before top) still estimates size. The lock ("qlock") is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   615
     * forced to -1 on termination, causing all further lock attempts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   616
     * to fail. (Note: we don't need CAS for termination state because
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   617
     * upon pool shutdown, all shared-queues will stop being used
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   618
     * anyway.)  Nearly all lock bodies are set up so that exceptions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   619
     * within lock bodies are "impossible" (modulo JVM errors that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   620
     * would cause failure anyway.)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   621
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   622
     * The array slots are read and written using the emulation of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   623
     * volatiles/atomics provided by Unsafe. Insertions must in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   624
     * general use putOrderedObject as a form of releasing store to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   625
     * ensure that all writes to the task object are ordered before
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   626
     * its publication in the queue.  All removals entail a CAS to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   627
     * null.  The array is always a power of two. To ensure safety of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   628
     * Unsafe array operations, all accesses perform explicit null
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   629
     * checks and implicit bounds checks via power-of-two masking.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   630
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   631
     * In addition to basic queuing support, this class contains
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   632
     * fields described elsewhere to control execution. It turns out
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   633
     * to work better memory-layout-wise to include them in this class
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   634
     * rather than a separate class.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   635
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   636
     * Performance on most platforms is very sensitive to placement of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   637
     * instances of both WorkQueues and their arrays -- we absolutely
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   638
     * do not want multiple WorkQueue instances or multiple queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   639
     * arrays sharing cache lines. (It would be best for queue objects
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   640
     * and their arrays to share, but there is nothing available to
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   641
     * help arrange that). The @Contended annotation alerts JVMs to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   642
     * try to keep instances apart.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   643
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   644
    @sun.misc.Contended
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   645
    static final class WorkQueue {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   646
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   647
         * Capacity of work-stealing queue array upon initialization.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   648
         * Must be a power of two; at least 4, but should be larger to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   649
         * reduce or eliminate cacheline sharing among queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   650
         * Currently, it is much larger, as a partial workaround for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   651
         * the fact that JVMs often place arrays in locations that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   652
         * share GC bookkeeping (especially cardmarks) such that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   653
         * per-write accesses encounter serious memory contention.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   654
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   655
        static final int INITIAL_QUEUE_CAPACITY = 1 << 13;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   656
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   657
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   658
         * Maximum size for queue arrays. Must be a power of two less
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   659
         * than or equal to 1 << (31 - width of array entry) to ensure
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   660
         * lack of wraparound of index calculations, but defined to a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   661
         * value a bit less than this to help users trap runaway
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   662
         * programs before saturating systems.
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
        static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   665
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   666
        volatile int eventCount;   // encoded inactivation count; < 0 if inactive
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   667
        int nextWait;              // encoded record of next event waiter
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   668
        int nsteals;               // number of steals
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   669
        int hint;                  // steal index hint
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   670
        short poolIndex;           // index of this queue in pool
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   671
        final short mode;          // 0: lifo, > 0: fifo, < 0: shared
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   672
        volatile int qlock;        // 1: locked, -1: terminate; else 0
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   673
        volatile int base;         // index of next slot for poll
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   674
        int top;                   // index of next slot for push
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   675
        ForkJoinTask<?>[] array;   // the elements (initially unallocated)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   676
        final ForkJoinPool pool;   // the containing pool (may be null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   677
        final ForkJoinWorkerThread owner; // owning thread or null if shared
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   678
        volatile Thread parker;    // == owner during call to park; else null
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   679
        volatile ForkJoinTask<?> currentJoin;  // task being joined in awaitJoin
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   680
        ForkJoinTask<?> currentSteal; // current non-local task being executed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   681
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   682
        WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   683
                  int seed) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   684
            this.pool = pool;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   685
            this.owner = owner;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   686
            this.mode = (short)mode;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   687
            this.hint = seed; // store initial seed for runWorker
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   688
            // 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
   689
            base = top = INITIAL_QUEUE_CAPACITY >>> 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   690
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   691
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   692
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   693
         * Returns the approximate number of tasks in the queue.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   694
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   695
        final int queueSize() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   696
            int n = base - top;       // non-owner callers must read base first
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   697
            return (n >= 0) ? 0 : -n; // ignore transient negative
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   698
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   699
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   700
        /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   701
         * Provides a more accurate estimate of whether this queue has
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   702
         * any tasks than does queueSize, by checking whether a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   703
         * near-empty queue has at least one unclaimed task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   704
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   705
        final boolean isEmpty() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   706
            ForkJoinTask<?>[] a; int m, s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   707
            int n = base - (s = top);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   708
            return (n >= 0 ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   709
                    (n == -1 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   710
                     ((a = array) == null ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   711
                      (m = a.length - 1) < 0 ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   712
                      U.getObject
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   713
                      (a, (long)((m & (s - 1)) << ASHIFT) + ABASE) == null)));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   714
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   715
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   716
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   717
         * Pushes a task. Call only by owner in unshared queues.  (The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   718
         * shared-queue version is embedded in method externalPush.)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   719
         *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   720
         * @param task the task. Caller must ensure non-null.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   721
         * @throws RejectedExecutionException if array cannot be resized
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   722
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   723
        final void push(ForkJoinTask<?> task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   724
            ForkJoinTask<?>[] a; ForkJoinPool p;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   725
            int s = top, n;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   726
            if ((a = array) != null) {    // ignore if queue removed
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   727
                int m = a.length - 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   728
                U.putOrderedObject(a, ((m & s) << ASHIFT) + ABASE, task);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   729
                if ((n = (top = s + 1) - base) <= 2)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   730
                    (p = pool).signalWork(p.workQueues, this);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   731
                else if (n >= m)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   732
                    growArray();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   733
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   734
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   735
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   736
        /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   737
         * Initializes or doubles the capacity of array. Call either
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   738
         * by owner or with lock held -- it is OK for base, but not
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   739
         * top, to move while resizings are in progress.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   740
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   741
        final ForkJoinTask<?>[] growArray() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   742
            ForkJoinTask<?>[] oldA = array;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   743
            int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   744
            if (size > MAXIMUM_QUEUE_CAPACITY)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   745
                throw new RejectedExecutionException("Queue capacity exceeded");
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   746
            int oldMask, t, b;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   747
            ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   748
            if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   749
                (t = top) - (b = base) > 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   750
                int mask = size - 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   751
                do {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   752
                    ForkJoinTask<?> x;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   753
                    int oldj = ((b & oldMask) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   754
                    int j    = ((b &    mask) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   755
                    x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   756
                    if (x != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   757
                        U.compareAndSwapObject(oldA, oldj, x, null))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   758
                        U.putObjectVolatile(a, j, x);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   759
                } while (++b != t);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   760
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   761
            return a;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   762
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   763
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   764
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   765
         * Takes next task, if one exists, in LIFO order.  Call only
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   766
         * by owner in unshared queues.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   767
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   768
        final ForkJoinTask<?> pop() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   769
            ForkJoinTask<?>[] a; ForkJoinTask<?> t; int m;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   770
            if ((a = array) != null && (m = a.length - 1) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   771
                for (int s; (s = top - 1) - base >= 0;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   772
                    long j = ((m & s) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   773
                    if ((t = (ForkJoinTask<?>)U.getObject(a, j)) == null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   774
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   775
                    if (U.compareAndSwapObject(a, j, t, null)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   776
                        top = s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   777
                        return t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   778
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   779
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   780
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   781
            return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   782
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   783
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   784
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   785
         * Takes a task in FIFO order if b is base of queue and a task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   786
         * can be claimed without contention. Specialized versions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   787
         * appear in ForkJoinPool methods scan and tryHelpStealer.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   788
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   789
        final ForkJoinTask<?> pollAt(int b) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   790
            ForkJoinTask<?> t; ForkJoinTask<?>[] a;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   791
            if ((a = array) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   792
                int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   793
                if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null &&
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   794
                    base == b && U.compareAndSwapObject(a, j, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   795
                    U.putOrderedInt(this, QBASE, b + 1);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   796
                    return t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   797
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   798
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   799
            return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   800
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   801
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   802
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   803
         * Takes next task, if one exists, in FIFO order.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   804
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   805
        final ForkJoinTask<?> poll() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   806
            ForkJoinTask<?>[] a; int b; ForkJoinTask<?> t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   807
            while ((b = base) - top < 0 && (a = array) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   808
                int j = (((a.length - 1) & b) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   809
                t = (ForkJoinTask<?>)U.getObjectVolatile(a, j);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   810
                if (t != null) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   811
                    if (U.compareAndSwapObject(a, j, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   812
                        U.putOrderedInt(this, QBASE, b + 1);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   813
                        return t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   814
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   815
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   816
                else if (base == b) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   817
                    if (b + 1 == top)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   818
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   819
                    Thread.yield(); // wait for lagging update (very rare)
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
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   822
            return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   823
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   824
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   825
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   826
         * Takes next task, if one exists, in order specified by mode.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   827
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   828
        final ForkJoinTask<?> nextLocalTask() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   829
            return mode == 0 ? pop() : poll();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   830
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   831
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   832
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   833
         * Returns next task, if one exists, in order specified by mode.
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
        final ForkJoinTask<?> peek() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   836
            ForkJoinTask<?>[] a = array; int m;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   837
            if (a == null || (m = a.length - 1) < 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   838
                return null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   839
            int i = mode == 0 ? top - 1 : base;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   840
            int j = ((i & m) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   841
            return (ForkJoinTask<?>)U.getObjectVolatile(a, j);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   842
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   843
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   844
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   845
         * Pops the given task only if it is at the current top.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   846
         * (A shared version is available only via FJP.tryExternalUnpush)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   847
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   848
        final boolean tryUnpush(ForkJoinTask<?> t) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   849
            ForkJoinTask<?>[] a; int s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   850
            if ((a = array) != null && (s = top) != base &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   851
                U.compareAndSwapObject
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   852
                (a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   853
                top = s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   854
                return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   855
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   856
            return false;
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
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   859
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   860
         * Removes and cancels all known tasks, ignoring any exceptions.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   861
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   862
        final void cancelAll() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   863
            ForkJoinTask.cancelIgnoringExceptions(currentJoin);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   864
            ForkJoinTask.cancelIgnoringExceptions(currentSteal);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   865
            for (ForkJoinTask<?> t; (t = poll()) != null; )
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   866
                ForkJoinTask.cancelIgnoringExceptions(t);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   867
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   868
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   869
        // Specialized execution methods
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   870
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   871
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   872
         * Polls and runs tasks until empty.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   873
         */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   874
        final void pollAndExecAll() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   875
            for (ForkJoinTask<?> t; (t = poll()) != null;)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   876
                t.doExec();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   877
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   878
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   879
        /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   880
         * Executes a top-level task and any local tasks remaining
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   881
         * after execution.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   882
         */
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   883
        final void runTask(ForkJoinTask<?> task) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   884
            if ((currentSteal = task) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   885
                task.doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   886
                ForkJoinTask<?>[] a = array;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   887
                int md = mode;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   888
                ++nsteals;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   889
                currentSteal = null;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   890
                if (md != 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   891
                    pollAndExecAll();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   892
                else if (a != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   893
                    int s, m = a.length - 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   894
                    ForkJoinTask<?> t;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   895
                    while ((s = top - 1) - base >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   896
                           (t = (ForkJoinTask<?>)U.getAndSetObject
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   897
                            (a, ((m & s) << ASHIFT) + ABASE, null)) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   898
                        top = s;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   899
                        t.doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   900
                    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   901
                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   902
            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   903
        }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   904
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   905
        /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   906
         * If present, removes from queue and executes the given task,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   907
         * or any other cancelled task. Returns (true) on any CAS
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   908
         * or consistency check failure so caller can retry.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   909
         *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
   910
         * @return false if no progress can be made, else true
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   911
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   912
        final boolean tryRemoveAndExec(ForkJoinTask<?> task) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   913
            boolean stat;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   914
            ForkJoinTask<?>[] a; int m, s, b, n;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   915
            if (task != null && (a = array) != null && (m = a.length - 1) >= 0 &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   916
                (n = (s = top) - (b = base)) > 0) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   917
                boolean removed = false, empty = true;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   918
                stat = true;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   919
                for (ForkJoinTask<?> t;;) {           // traverse from s to b
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   920
                    long j = ((--s & m) << ASHIFT) + ABASE;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   921
                    t = (ForkJoinTask<?>)U.getObject(a, j);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   922
                    if (t == null)                    // inconsistent length
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   923
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   924
                    else if (t == task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   925
                        if (s + 1 == top) {           // pop
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   926
                            if (!U.compareAndSwapObject(a, j, task, null))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   927
                                break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   928
                            top = s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   929
                            removed = true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   930
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   931
                        else if (base == b)           // replace with proxy
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   932
                            removed = U.compareAndSwapObject(a, j, task,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   933
                                                             new EmptyTask());
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   934
                        break;
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
                    else if (t.status >= 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   937
                        empty = false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   938
                    else if (s + 1 == top) {          // pop and throw away
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   939
                        if (U.compareAndSwapObject(a, j, t, null))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   940
                            top = s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   941
                        break;
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
                    if (--n == 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   944
                        if (!empty && base == b)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   945
                            stat = false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   946
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   947
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   948
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   949
                if (removed)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   950
                    task.doExec();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   951
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   952
            else
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   953
                stat = false;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   954
            return stat;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   955
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   956
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   957
        /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   958
         * Tries to poll for and execute the given task or any other
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   959
         * task in its CountedCompleter computation.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   960
         */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   961
        final boolean pollAndExecCC(CountedCompleter<?> root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   962
            ForkJoinTask<?>[] a; int b; Object o; CountedCompleter<?> t, r;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   963
            if ((b = base) - top < 0 && (a = array) != null) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   964
                long j = (((a.length - 1) & b) << ASHIFT) + ABASE;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   965
                if ((o = U.getObjectVolatile(a, j)) == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   966
                    return true; // retry
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   967
                if (o instanceof CountedCompleter) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   968
                    for (t = (CountedCompleter<?>)o, r = t;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   969
                        if (r == root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   970
                            if (base == b &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   971
                                U.compareAndSwapObject(a, j, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   972
                                U.putOrderedInt(this, QBASE, b + 1);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   973
                                t.doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   974
                            }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   975
                            return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   976
                        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   977
                        else if ((r = r.completer) == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   978
                            break; // not part of root computation
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   979
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   980
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   981
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   982
            return false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   983
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   984
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   985
        /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   986
         * Tries to pop and execute the given task or any other task
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   987
         * in its CountedCompleter computation.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   988
         */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   989
        final boolean externalPopAndExecCC(CountedCompleter<?> root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   990
            ForkJoinTask<?>[] a; int s; Object o; CountedCompleter<?> t, r;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   991
            if (base - (s = top) < 0 && (a = array) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   992
                long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   993
                if ((o = U.getObject(a, j)) instanceof CountedCompleter) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   994
                    for (t = (CountedCompleter<?>)o, r = t;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   995
                        if (r == root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   996
                            if (U.compareAndSwapInt(this, QLOCK, 0, 1)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   997
                                if (top == s && array == a &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   998
                                    U.compareAndSwapObject(a, j, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
   999
                                    top = s - 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1000
                                    qlock = 0;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1001
                                    t.doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1002
                                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1003
                                else
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1004
                                    qlock = 0;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1005
                            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1006
                            return true;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1007
                        }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1008
                        else if ((r = r.completer) == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1009
                            break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1010
                    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1011
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1012
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1013
            return false;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1014
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1015
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1016
        /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1017
         * Internal version
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1018
         */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1019
        final boolean internalPopAndExecCC(CountedCompleter<?> root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1020
            ForkJoinTask<?>[] a; int s; Object o; CountedCompleter<?> t, r;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1021
            if (base - (s = top) < 0 && (a = array) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1022
                long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1023
                if ((o = U.getObject(a, j)) instanceof CountedCompleter) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1024
                    for (t = (CountedCompleter<?>)o, r = t;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1025
                        if (r == root) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1026
                            if (U.compareAndSwapObject(a, j, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1027
                                top = s - 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1028
                                t.doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1029
                            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1030
                            return true;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1031
                        }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1032
                        else if ((r = r.completer) == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1033
                            break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1034
                    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1035
                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1036
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1037
            return false;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1038
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1039
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1040
        /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1041
         * Returns true if owned and not known to be blocked.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1042
         */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1043
        final boolean isApparentlyUnblocked() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1044
            Thread wt; Thread.State s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1045
            return (eventCount >= 0 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1046
                    (wt = owner) != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1047
                    (s = wt.getState()) != Thread.State.BLOCKED &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1048
                    s != Thread.State.WAITING &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1049
                    s != Thread.State.TIMED_WAITING);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1050
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1051
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1052
        // Unsafe mechanics
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1053
        private static final sun.misc.Unsafe U;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1054
        private static final long QBASE;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1055
        private static final long QLOCK;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1056
        private static final int ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1057
        private static final int ASHIFT;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1058
        static {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1059
            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1060
                U = sun.misc.Unsafe.getUnsafe();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1061
                Class<?> k = WorkQueue.class;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1062
                Class<?> ak = ForkJoinTask[].class;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1063
                QBASE = U.objectFieldOffset
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1064
                    (k.getDeclaredField("base"));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1065
                QLOCK = U.objectFieldOffset
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1066
                    (k.getDeclaredField("qlock"));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1067
                ABASE = U.arrayBaseOffset(ak);
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1068
                int scale = U.arrayIndexScale(ak);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1069
                if ((scale & (scale - 1)) != 0)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1070
                    throw new Error("data type scale not a power of two");
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1071
                ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1072
            } catch (Exception e) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1073
                throw new Error(e);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1074
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1075
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1076
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1077
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1078
    // static fields (initialized in static initializer below)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1079
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1080
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1081
     * Creates a new ForkJoinWorkerThread. This factory is used unless
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1082
     * overridden in ForkJoinPool constructors.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1083
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1084
    public static final ForkJoinWorkerThreadFactory
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1085
        defaultForkJoinWorkerThreadFactory;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1086
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1087
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1088
     * Permission required for callers of methods that may start or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1089
     * kill threads.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1090
     */
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1091
    private static final RuntimePermission modifyThreadPermission;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1092
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1093
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1094
     * Common (static) pool. Non-null for public use unless a static
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1095
     * construction exception, but internal usages null-check on use
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1096
     * to paranoically avoid potential initialization circularities
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1097
     * as well as to simplify generated code.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1098
     */
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1099
    static final ForkJoinPool common;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1100
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1101
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1102
     * Common pool parallelism. To allow simpler use and management
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1103
     * 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
  1104
     * 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
  1105
     * parallelism as 1 to reflect resulting caller-runs mechanics.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1106
     */
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1107
    static final int commonParallelism;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1108
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1109
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1110
     * Sequence number for creating workerNamePrefix.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1111
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1112
    private static int poolNumberSequence;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1113
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1114
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1115
     * 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
  1116
     * ever contend, so use simple builtin sync.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1117
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1118
    private static final synchronized int nextPoolId() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1119
        return ++poolNumberSequence;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1120
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1121
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1122
    // static constants
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1123
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1124
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1125
     * Initial timeout value (in nanoseconds) for the thread
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1126
     * triggering quiescence to park waiting for new work. On timeout,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1127
     * the thread will instead try to shrink the number of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1128
     * workers. The value should be large enough to avoid overly
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1129
     * aggressive shrinkage during most transient stalls (long GCs
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1130
     * etc).
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1131
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1132
    private static final long IDLE_TIMEOUT      = 2000L * 1000L * 1000L; // 2sec
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1133
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1134
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1135
     * Timeout value when there are more threads than parallelism level
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1136
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1137
    private static final long FAST_IDLE_TIMEOUT =  200L * 1000L * 1000L;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1138
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1139
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1140
     * Tolerance for idle timeouts, to cope with timer undershoots
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1141
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1142
    private static final long TIMEOUT_SLOP = 2000000L;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1143
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1144
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1145
     * The maximum stolen->joining link depth allowed in method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1146
     * tryHelpStealer.  Must be a power of two.  Depths for legitimate
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1147
     * chains are unbounded, but we use a fixed constant to avoid
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1148
     * (otherwise unchecked) cycles and to bound staleness of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1149
     * traversal parameters at the expense of sometimes blocking when
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1150
     * we could be helping.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1151
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1152
    private static final int MAX_HELP = 64;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1153
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1154
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1155
     * Increment for seed generators. See class ThreadLocal for
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1156
     * explanation.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1157
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1158
    private static final int SEED_INCREMENT = 0x61c88647;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1159
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1160
    /*
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1161
     * Bits and masks for control variables
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1162
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1163
     * Field ctl is a long packed with:
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1164
     * AC: Number of active running workers minus target parallelism (16 bits)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1165
     * TC: Number of total workers minus target parallelism (16 bits)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1166
     * ST: true if pool is terminating (1 bit)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1167
     * EC: the wait count of top waiting thread (15 bits)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1168
     * ID: poolIndex of top of Treiber stack of waiters (16 bits)
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1169
     *
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1170
     * When convenient, we can extract the upper 32 bits of counts and
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1171
     * the lower 32 bits of queue state, u = (int)(ctl >>> 32) and e =
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1172
     * (int)ctl.  The ec field is never accessed alone, but always
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1173
     * together with id and st. The offsets of counts by the target
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1174
     * parallelism and the positionings of fields makes it possible to
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1175
     * perform the most common checks via sign tests of fields: When
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1176
     * ac is negative, there are not enough active workers, when tc is
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1177
     * negative, there are not enough total workers, and when e is
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1178
     * negative, the pool is terminating.  To deal with these possibly
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1179
     * negative fields, we use casts in and out of "short" and/or
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1180
     * signed shifts to maintain signedness.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1181
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1182
     * When a thread is queued (inactivated), its eventCount field is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1183
     * set negative, which is the only way to tell if a worker is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1184
     * prevented from executing tasks, even though it must continue to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1185
     * scan for them to avoid queuing races. Note however that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1186
     * eventCount updates lag releases so usage requires care.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1187
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1188
     * Field plock is an int packed with:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1189
     * SHUTDOWN: true if shutdown is enabled (1 bit)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1190
     * SEQ:  a sequence lock, with PL_LOCK bit set if locked (30 bits)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1191
     * SIGNAL: set when threads may be waiting on the lock (1 bit)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1192
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1193
     * The sequence number enables simple consistency checks:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1194
     * Staleness of read-only operations on the workQueues array can
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1195
     * be checked by comparing plock before vs after the reads.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1196
     */
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1197
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1198
    // bit positions/shifts for fields
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1199
    private static final int  AC_SHIFT   = 48;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1200
    private static final int  TC_SHIFT   = 32;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1201
    private static final int  ST_SHIFT   = 31;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1202
    private static final int  EC_SHIFT   = 16;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1203
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1204
    // bounds
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1205
    private static final int  SMASK      = 0xffff;  // short bits
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1206
    private static final int  MAX_CAP    = 0x7fff;  // max #workers - 1
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1207
    private static final int  EVENMASK   = 0xfffe;  // even short bits
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1208
    private static final int  SQMASK     = 0x007e;  // max 64 (even) slots
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1209
    private static final int  SHORT_SIGN = 1 << 15;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1210
    private static final int  INT_SIGN   = 1 << 31;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1211
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1212
    // masks
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1213
    private static final long STOP_BIT   = 0x0001L << ST_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1214
    private static final long AC_MASK    = ((long)SMASK) << AC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1215
    private static final long TC_MASK    = ((long)SMASK) << TC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1216
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1217
    // units for incrementing and decrementing
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1218
    private static final long TC_UNIT    = 1L << TC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1219
    private static final long AC_UNIT    = 1L << AC_SHIFT;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1220
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1221
    // masks and units for dealing with u = (int)(ctl >>> 32)
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1222
    private static final int  UAC_SHIFT  = AC_SHIFT - 32;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1223
    private static final int  UTC_SHIFT  = TC_SHIFT - 32;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1224
    private static final int  UAC_MASK   = SMASK << UAC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1225
    private static final int  UTC_MASK   = SMASK << UTC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1226
    private static final int  UAC_UNIT   = 1 << UAC_SHIFT;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1227
    private static final int  UTC_UNIT   = 1 << UTC_SHIFT;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1228
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1229
    // masks and units for dealing with e = (int)ctl
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1230
    private static final int E_MASK      = 0x7fffffff; // no STOP_BIT
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1231
    private static final int E_SEQ       = 1 << EC_SHIFT;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1232
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1233
    // plock bits
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1234
    private static final int SHUTDOWN    = 1 << 31;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1235
    private static final int PL_LOCK     = 2;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1236
    private static final int PL_SIGNAL   = 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1237
    private static final int PL_SPINS    = 1 << 8;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1238
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1239
    // access mode for WorkQueue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1240
    static final int LIFO_QUEUE          =  0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1241
    static final int FIFO_QUEUE          =  1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1242
    static final int SHARED_QUEUE        = -1;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1243
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1244
    // Instance fields
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1245
    volatile long stealCount;                  // collects worker counts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1246
    volatile long ctl;                         // main pool control
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1247
    volatile int plock;                        // shutdown status and seqLock
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1248
    volatile int indexSeed;                    // worker/submitter index seed
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1249
    final short parallelism;                   // parallelism level
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1250
    final short mode;                          // LIFO/FIFO
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1251
    WorkQueue[] workQueues;                    // main registry
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1252
    final ForkJoinWorkerThreadFactory factory;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1253
    final UncaughtExceptionHandler ueh;        // per-worker UEH
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1254
    final String workerNamePrefix;             // to create worker name string
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
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1257
     * Acquires the plock lock to protect worker array and related
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1258
     * updates. This method is called only if an initial CAS on plock
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1259
     * fails. This acts as a spinlock for normal cases, but falls back
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1260
     * to builtin monitor to block when (rarely) needed. This would be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1261
     * a terrible idea for a highly contended lock, but works fine as
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1262
     * a more conservative alternative to a pure spinlock.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1263
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1264
    private int acquirePlock() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1265
        int spins = PL_SPINS, ps, nps;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1266
        for (;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1267
            if (((ps = plock) & PL_LOCK) == 0 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1268
                U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1269
                return nps;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1270
            else if (spins >= 0) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1271
                if (ThreadLocalRandom.nextSecondarySeed() >= 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1272
                    --spins;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1273
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1274
            else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1275
                synchronized (this) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1276
                    if ((plock & PL_SIGNAL) != 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1277
                        try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1278
                            wait();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1279
                        } catch (InterruptedException ie) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1280
                            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1281
                                Thread.currentThread().interrupt();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1282
                            } catch (SecurityException ignore) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1283
                            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1284
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1285
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1286
                    else
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1287
                        notifyAll();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1288
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1289
            }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1290
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1291
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1292
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1293
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1294
     * Unlocks and signals any thread waiting for plock. Called only
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1295
     * when CAS of seq value for unlock fails.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1296
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1297
    private void releasePlock(int ps) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1298
        plock = ps;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1299
        synchronized (this) { notifyAll(); }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1300
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1301
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1302
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1303
     * Tries to create and start one worker if fewer than target
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1304
     * parallelism level exist. Adjusts counts etc on failure.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1305
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1306
    private void tryAddWorker() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1307
        long c; int u, e;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1308
        while ((u = (int)((c = ctl) >>> 32)) < 0 &&
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1309
               (u & SHORT_SIGN) != 0 && (e = (int)c) >= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1310
            long nc = ((long)(((u + UTC_UNIT) & UTC_MASK) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1311
                              ((u + UAC_UNIT) & UAC_MASK)) << 32) | (long)e;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1312
            if (U.compareAndSwapLong(this, CTL, c, nc)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1313
                ForkJoinWorkerThreadFactory fac;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1314
                Throwable ex = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1315
                ForkJoinWorkerThread wt = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1316
                try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1317
                    if ((fac = factory) != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1318
                        (wt = fac.newThread(this)) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1319
                        wt.start();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1320
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1321
                    }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1322
                } catch (Throwable rex) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1323
                    ex = rex;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1324
                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1325
                deregisterWorker(wt, ex);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1326
                break;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1327
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1328
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1329
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1330
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1331
    //  Registering and deregistering workers
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1332
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1333
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1334
     * Callback from ForkJoinWorkerThread to establish and record its
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1335
     * WorkQueue. To avoid scanning bias due to packing entries in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1336
     * front of the workQueues array, we treat the array as a simple
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1337
     * power-of-two hash table using per-thread seed as hash,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1338
     * expanding as needed.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1339
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1340
     * @param wt the worker thread
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1341
     * @return the worker's queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1342
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1343
    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1344
        UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1345
        wt.setDaemon(true);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1346
        if ((handler = ueh) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1347
            wt.setUncaughtExceptionHandler(handler);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1348
        do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1349
                                          s += SEED_INCREMENT) ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1350
                     s == 0); // skip 0
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1351
        WorkQueue w = new WorkQueue(this, wt, mode, s);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1352
        if (((ps = plock) & PL_LOCK) != 0 ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1353
            !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1354
            ps = acquirePlock();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1355
        int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1356
        try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1357
            if ((ws = workQueues) != null) {    // skip if shutting down
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1358
                int n = ws.length, m = n - 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1359
                int r = (s << 1) | 1;           // use odd-numbered indices
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1360
                if (ws[r &= m] != null) {       // collision
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1361
                    int probes = 0;             // step by approx half size
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1362
                    int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1363
                    while (ws[r = (r + step) & m] != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1364
                        if (++probes >= n) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1365
                            workQueues = ws = Arrays.copyOf(ws, n <<= 1);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1366
                            m = n - 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1367
                            probes = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1368
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1369
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1370
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1371
                w.poolIndex = (short)r;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1372
                w.eventCount = r; // volatile write orders
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1373
                ws[r] = w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1374
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1375
        } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1376
            if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1377
                releasePlock(nps);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1378
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1379
        wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex >>> 1)));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1380
        return w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1381
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1382
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1383
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1384
     * Final callback from terminating worker, as well as upon failure
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1385
     * to construct or start a worker.  Removes record of worker from
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1386
     * array, and adjusts counts. If pool is shutting down, tries to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1387
     * complete termination.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1388
     *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1389
     * @param wt the worker thread, or null if construction failed
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1390
     * @param ex the exception causing failure, or null if none
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1391
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1392
    final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1393
        WorkQueue w = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1394
        if (wt != null && (w = wt.workQueue) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1395
            int ps;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1396
            w.qlock = -1;                // ensure set
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1397
            U.getAndAddLong(this, STEALCOUNT, w.nsteals); // collect steals
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1398
            if (((ps = plock) & PL_LOCK) != 0 ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1399
                !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1400
                ps = acquirePlock();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1401
            int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1402
            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1403
                int idx = w.poolIndex;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1404
                WorkQueue[] ws = workQueues;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1405
                if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1406
                    ws[idx] = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1407
            } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1408
                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1409
                    releasePlock(nps);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1410
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1411
        }
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
        long c;                          // adjust ctl counts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1414
        do {} while (!U.compareAndSwapLong
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1415
                     (this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1416
                                           ((c - TC_UNIT) & TC_MASK) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1417
                                           (c & ~(AC_MASK|TC_MASK)))));
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
        if (!tryTerminate(false, false) && w != null && w.array != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1420
            w.cancelAll();               // cancel remaining tasks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1421
            WorkQueue[] ws; WorkQueue v; Thread p; int u, i, e;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1422
            while ((u = (int)((c = ctl) >>> 32)) < 0 && (e = (int)c) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1423
                if (e > 0) {             // activate or create replacement
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1424
                    if ((ws = workQueues) == null ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1425
                        (i = e & SMASK) >= ws.length ||
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1426
                        (v = ws[i]) == null)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1427
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1428
                    long nc = (((long)(v.nextWait & E_MASK)) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1429
                               ((long)(u + UAC_UNIT) << 32));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1430
                    if (v.eventCount != (e | INT_SIGN))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1431
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1432
                    if (U.compareAndSwapLong(this, CTL, c, nc)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1433
                        v.eventCount = (e + E_SEQ) & E_MASK;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1434
                        if ((p = v.parker) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1435
                            U.unpark(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1436
                        break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1437
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1438
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1439
                else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1440
                    if ((short)u < 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1441
                        tryAddWorker();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1442
                    break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1443
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1444
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1445
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1446
        if (ex == null)                     // help clean refs on way out
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1447
            ForkJoinTask.helpExpungeStaleExceptions();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1448
        else                                // rethrow
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1449
            ForkJoinTask.rethrow(ex);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1450
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1451
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1452
    // Submissions
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1453
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1454
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1455
     * Unless shutting down, adds the given task to a submission queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1456
     * at submitter's current queue index (modulo submission
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1457
     * range). Only the most common path is directly handled in this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1458
     * method. All others are relayed to fullExternalPush.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1459
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1460
     * @param task the task. Caller must ensure non-null.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1461
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1462
    final void externalPush(ForkJoinTask<?> task) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1463
        WorkQueue q; int m, s, n, am; ForkJoinTask<?>[] a;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1464
        int r = ThreadLocalRandom.getProbe();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1465
        int ps = plock;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1466
        WorkQueue[] ws = workQueues;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1467
        if (ps > 0 && ws != null && (m = (ws.length - 1)) >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1468
            (q = ws[m & r & SQMASK]) != null && r != 0 &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1469
            U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1470
            if ((a = q.array) != null &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1471
                (am = a.length - 1) > (n = (s = q.top) - q.base)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1472
                int j = ((am & s) << ASHIFT) + ABASE;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1473
                U.putOrderedObject(a, j, task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1474
                q.top = s + 1;                     // push on to deque
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1475
                q.qlock = 0;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1476
                if (n <= 1)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1477
                    signalWork(ws, q);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1478
                return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1479
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1480
            q.qlock = 0;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1481
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1482
        fullExternalPush(task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1483
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1484
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1485
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1486
     * Full version of externalPush. This method is called, among
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1487
     * other times, upon the first submission of the first task to the
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1488
     * pool, so must perform secondary initialization.  It also
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1489
     * detects first submission by an external thread by looking up
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1490
     * its ThreadLocal, and creates a new shared queue if the one at
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1491
     * index if empty or contended. The plock lock body must be
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1492
     * exception-free (so no try/finally) so we optimistically
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1493
     * allocate new queues outside the lock and throw them away if
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1494
     * (very rarely) not needed.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1495
     *
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1496
     * Secondary initialization occurs when plock is zero, to create
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1497
     * workQueue array and set plock to a valid value.  This lock body
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1498
     * must also be exception-free. Because the plock seq value can
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1499
     * eventually wrap around zero, this method harmlessly fails to
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1500
     * reinitialize if workQueues exists, while still advancing plock.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1501
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1502
    private void fullExternalPush(ForkJoinTask<?> task) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1503
        int r;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1504
        if ((r = ThreadLocalRandom.getProbe()) == 0) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1505
            ThreadLocalRandom.localInit();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1506
            r = ThreadLocalRandom.getProbe();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1507
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1508
        for (;;) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1509
            WorkQueue[] ws; WorkQueue q; int ps, m, k;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1510
            boolean move = false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1511
            if ((ps = plock) < 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1512
                throw new RejectedExecutionException();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1513
            else if (ps == 0 || (ws = workQueues) == null ||
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1514
                     (m = ws.length - 1) < 0) { // initialize workQueues
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1515
                int p = parallelism;            // find power of two table size
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1516
                int n = (p > 1) ? p - 1 : 1;    // ensure at least 2 slots
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1517
                n |= n >>> 1; n |= n >>> 2;  n |= n >>> 4;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1518
                n |= n >>> 8; n |= n >>> 16; n = (n + 1) << 1;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1519
                WorkQueue[] nws = ((ws = workQueues) == null || ws.length == 0 ?
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1520
                                   new WorkQueue[n] : null);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1521
                if (((ps = plock) & PL_LOCK) != 0 ||
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1522
                    !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1523
                    ps = acquirePlock();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1524
                if (((ws = workQueues) == null || ws.length == 0) && nws != null)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1525
                    workQueues = nws;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1526
                int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1527
                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1528
                    releasePlock(nps);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1529
            }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1530
            else if ((q = ws[k = r & m & SQMASK]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1531
                if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1532
                    ForkJoinTask<?>[] a = q.array;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1533
                    int s = q.top;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1534
                    boolean submitted = false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1535
                    try {                      // locked version of push
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1536
                        if ((a != null && a.length > s + 1 - q.base) ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1537
                            (a = q.growArray()) != null) {   // must presize
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1538
                            int j = (((a.length - 1) & s) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1539
                            U.putOrderedObject(a, j, task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1540
                            q.top = s + 1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1541
                            submitted = true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1542
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1543
                    } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1544
                        q.qlock = 0;  // unlock
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1545
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1546
                    if (submitted) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1547
                        signalWork(ws, q);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1548
                        return;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1549
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1550
                }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1551
                move = true; // move on failure
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1552
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1553
            else if (((ps = plock) & PL_LOCK) == 0) { // create new queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1554
                q = new WorkQueue(this, null, SHARED_QUEUE, r);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1555
                q.poolIndex = (short)k;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1556
                if (((ps = plock) & PL_LOCK) != 0 ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1557
                    !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1558
                    ps = acquirePlock();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1559
                if ((ws = workQueues) != null && k < ws.length && ws[k] == null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1560
                    ws[k] = q;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1561
                int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1562
                if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1563
                    releasePlock(nps);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1564
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1565
            else
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1566
                move = true; // move if busy
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1567
            if (move)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  1568
                r = ThreadLocalRandom.advanceProbe(r);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1569
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1570
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1571
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1572
    // Maintaining ctl counts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1573
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1574
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1575
     * Increments active count; mainly called upon return from blocking.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1576
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1577
    final void incrementActiveCount() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1578
        long c;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1579
        do {} while (!U.compareAndSwapLong
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1580
                     (this, CTL, c = ctl, ((c & ~AC_MASK) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1581
                                           ((c & AC_MASK) + AC_UNIT))));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1582
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1583
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1584
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1585
     * Tries to create or activate a worker if too few are active.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1586
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1587
     * @param ws the worker array to use to find signallees
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1588
     * @param q if non-null, the queue holding tasks to be processed
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1589
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1590
    final void signalWork(WorkQueue[] ws, WorkQueue q) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1591
        for (;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1592
            long c; int e, u, i; WorkQueue w; Thread p;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1593
            if ((u = (int)((c = ctl) >>> 32)) >= 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1594
                break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1595
            if ((e = (int)c) <= 0) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1596
                if ((short)u < 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1597
                    tryAddWorker();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1598
                break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1599
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1600
            if (ws == null || ws.length <= (i = e & SMASK) ||
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1601
                (w = ws[i]) == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1602
                break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1603
            long nc = (((long)(w.nextWait & E_MASK)) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1604
                       ((long)(u + UAC_UNIT)) << 32);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1605
            int ne = (e + E_SEQ) & E_MASK;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1606
            if (w.eventCount == (e | INT_SIGN) &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1607
                U.compareAndSwapLong(this, CTL, c, nc)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1608
                w.eventCount = ne;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1609
                if ((p = w.parker) != null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1610
                    U.unpark(p);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1611
                break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1612
            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1613
            if (q != null && q.base >= q.top)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1614
                break;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1615
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1616
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1617
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1618
    // Scanning for tasks
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1619
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1620
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1621
     * Top-level runloop for workers, called by ForkJoinWorkerThread.run.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1622
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1623
    final void runWorker(WorkQueue w) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1624
        w.growArray(); // allocate queue
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1625
        for (int r = w.hint; scan(w, r) == 0; ) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1626
            r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1627
        }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1628
    }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1629
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  1630
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1631
     * Scans for and, if found, runs one task, else possibly
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1632
     * inactivates the worker. This method operates on single reads of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1633
     * volatile state and is designed to be re-invoked continuously,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1634
     * in part because it returns upon detecting inconsistencies,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1635
     * contention, or state changes that indicate possible success on
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1636
     * re-invocation.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1637
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1638
     * The scan searches for tasks across queues starting at a random
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1639
     * index, checking each at least twice.  The scan terminates upon
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1640
     * either finding a non-empty queue, or completing the sweep. If
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1641
     * the worker is not inactivated, it takes and runs a task from
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1642
     * this queue. Otherwise, if not activated, it tries to activate
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1643
     * itself or some other worker by signalling. On failure to find a
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1644
     * task, returns (for retry) if pool state may have changed during
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1645
     * an empty scan, or tries to inactivate if active, else possibly
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1646
     * blocks or terminates via method awaitWork.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1647
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1648
     * @param w the worker (via its WorkQueue)
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1649
     * @param r a random seed
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1650
     * @return worker qlock status if would have waited, else 0
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  1651
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1652
    private final int scan(WorkQueue w, int r) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1653
        WorkQueue[] ws; int m;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1654
        long c = ctl;                            // for consistency check
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1655
        if ((ws = workQueues) != null && (m = ws.length - 1) >= 0 && w != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1656
            for (int j = m + m + 1, ec = w.eventCount;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1657
                WorkQueue q; int b, e; ForkJoinTask<?>[] a; ForkJoinTask<?> t;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1658
                if ((q = ws[(r - j) & m]) != null &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1659
                    (b = q.base) - q.top < 0 && (a = q.array) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1660
                    long i = (((a.length - 1) & b) << ASHIFT) + ABASE;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1661
                    if ((t = ((ForkJoinTask<?>)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1662
                              U.getObjectVolatile(a, i))) != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1663
                        if (ec < 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1664
                            helpRelease(c, ws, w, q, b);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1665
                        else if (q.base == b &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1666
                                 U.compareAndSwapObject(a, i, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1667
                            U.putOrderedInt(q, QBASE, b + 1);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1668
                            if ((b + 1) - q.top < 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1669
                                signalWork(ws, q);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1670
                            w.runTask(t);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1671
                        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1672
                    }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1673
                    break;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1674
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1675
                else if (--j < 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1676
                    if ((ec | (e = (int)c)) < 0) // inactive or terminating
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1677
                        return awaitWork(w, c, ec);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1678
                    else if (ctl == c) {         // try to inactivate and enqueue
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1679
                        long nc = (long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK));
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1680
                        w.nextWait = e;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1681
                        w.eventCount = ec | INT_SIGN;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1682
                        if (!U.compareAndSwapLong(this, CTL, c, nc))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1683
                            w.eventCount = ec;   // back out
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1684
                    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1685
                    break;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1686
                }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1687
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1688
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1689
        return 0;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1690
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1691
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1692
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1693
     * A continuation of scan(), possibly blocking or terminating
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1694
     * worker w. Returns without blocking if pool state has apparently
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1695
     * changed since last invocation.  Also, if inactivating w has
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1696
     * caused the pool to become quiescent, checks for pool
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1697
     * termination, and, so long as this is not the only worker, waits
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1698
     * for event for up to a given duration.  On timeout, if ctl has
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1699
     * not changed, terminates the worker, which will in turn wake up
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1700
     * another worker to possibly repeat this process.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1701
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1702
     * @param w the calling worker
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1703
     * @param c the ctl value on entry to scan
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1704
     * @param ec the worker's eventCount on entry to scan
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1705
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1706
    private final int awaitWork(WorkQueue w, long c, int ec) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1707
        int stat, ns; long parkTime, deadline;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1708
        if ((stat = w.qlock) >= 0 && w.eventCount == ec && ctl == c &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1709
            !Thread.interrupted()) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1710
            int e = (int)c;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1711
            int u = (int)(c >>> 32);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1712
            int d = (u >> UAC_SHIFT) + parallelism; // active count
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1713
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1714
            if (e < 0 || (d <= 0 && tryTerminate(false, false)))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1715
                stat = w.qlock = -1;          // pool is terminating
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1716
            else if ((ns = w.nsteals) != 0) { // collect steals and retry
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1717
                w.nsteals = 0;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1718
                U.getAndAddLong(this, STEALCOUNT, (long)ns);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1719
            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1720
            else {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1721
                long pc = ((d > 0 || ec != (e | INT_SIGN)) ? 0L :
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1722
                           ((long)(w.nextWait & E_MASK)) | // ctl to restore
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1723
                           ((long)(u + UAC_UNIT)) << 32);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1724
                if (pc != 0L) {               // timed wait if last waiter
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1725
                    int dc = -(short)(c >>> TC_SHIFT);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1726
                    parkTime = (dc < 0 ? FAST_IDLE_TIMEOUT:
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1727
                                (dc + 1) * IDLE_TIMEOUT);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1728
                    deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1729
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1730
                else
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1731
                    parkTime = deadline = 0L;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1732
                if (w.eventCount == ec && ctl == c) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1733
                    Thread wt = Thread.currentThread();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1734
                    U.putObject(wt, PARKBLOCKER, this);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1735
                    w.parker = wt;            // emulate LockSupport.park
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1736
                    if (w.eventCount == ec && ctl == c)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1737
                        U.park(false, parkTime);  // must recheck before park
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1738
                    w.parker = null;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1739
                    U.putObject(wt, PARKBLOCKER, null);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1740
                    if (parkTime != 0L && ctl == c &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1741
                        deadline - System.nanoTime() <= 0L &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1742
                        U.compareAndSwapLong(this, CTL, c, pc))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1743
                        stat = w.qlock = -1;  // shrink pool
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1744
                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1745
            }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1746
        }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1747
        return stat;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1748
    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1749
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1750
    /**
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1751
     * Possibly releases (signals) a worker. Called only from scan()
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1752
     * when a worker with apparently inactive status finds a non-empty
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1753
     * queue. This requires revalidating all of the associated state
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1754
     * from caller.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1755
     */
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1756
    private final void helpRelease(long c, WorkQueue[] ws, WorkQueue w,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1757
                                   WorkQueue q, int b) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1758
        WorkQueue v; int e, i; Thread p;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1759
        if (w != null && w.eventCount < 0 && (e = (int)c) > 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1760
            ws != null && ws.length > (i = e & SMASK) &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1761
            (v = ws[i]) != null && ctl == c) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1762
            long nc = (((long)(v.nextWait & E_MASK)) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1763
                       ((long)((int)(c >>> 32) + UAC_UNIT)) << 32);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1764
            int ne = (e + E_SEQ) & E_MASK;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1765
            if (q != null && q.base == b && w.eventCount < 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1766
                v.eventCount == (e | INT_SIGN) &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1767
                U.compareAndSwapLong(this, CTL, c, nc)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1768
                v.eventCount = ne;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1769
                if ((p = v.parker) != null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1770
                    U.unpark(p);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  1771
            }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1772
        }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1773
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1774
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1775
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1776
     * Tries to locate and execute tasks for a stealer of the given
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1777
     * task, or in turn one of its stealers, Traces currentSteal ->
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1778
     * currentJoin links looking for a thread working on a descendant
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1779
     * of the given task and with a non-empty queue to steal back and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1780
     * execute tasks from. The first call to this method upon a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1781
     * waiting join will often entail scanning/search, (which is OK
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1782
     * because the joiner has nothing better to do), but this method
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1783
     * leaves hints in workers to speed up subsequent calls. The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1784
     * implementation is very branchy to cope with potential
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1785
     * inconsistencies or loops encountering chains that are stale,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1786
     * unknown, or so long that they are likely cyclic.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1787
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1788
     * @param joiner the joining worker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1789
     * @param task the task to join
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1790
     * @return 0 if no progress can be made, negative if task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1791
     * known complete, else positive
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1792
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1793
    private int tryHelpStealer(WorkQueue joiner, ForkJoinTask<?> task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1794
        int stat = 0, steps = 0;                    // bound to avoid cycles
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1795
        if (task != null && joiner != null &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1796
            joiner.base - joiner.top >= 0) {        // hoist checks
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1797
            restart: for (;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1798
                ForkJoinTask<?> subtask = task;     // current target
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1799
                for (WorkQueue j = joiner, v;;) {   // v is stealer of subtask
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1800
                    WorkQueue[] ws; int m, s, h;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1801
                    if ((s = task.status) < 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1802
                        stat = s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1803
                        break restart;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1804
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1805
                    if ((ws = workQueues) == null || (m = ws.length - 1) <= 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1806
                        break restart;              // shutting down
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1807
                    if ((v = ws[h = (j.hint | 1) & m]) == null ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1808
                        v.currentSteal != subtask) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1809
                        for (int origin = h;;) {    // find stealer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1810
                            if (((h = (h + 2) & m) & 15) == 1 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1811
                                (subtask.status < 0 || j.currentJoin != subtask))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1812
                                continue restart;   // occasional staleness check
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1813
                            if ((v = ws[h]) != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1814
                                v.currentSteal == subtask) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1815
                                j.hint = h;        // save hint
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1816
                                break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1817
                            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1818
                            if (h == origin)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1819
                                break restart;      // cannot find stealer
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1820
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1821
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1822
                    for (;;) { // help stealer or descend to its stealer
21667
e4c61eb98292 8028300: Fix raw type lint warnings in java.util.concurrent
darcy
parents: 19412
diff changeset
  1823
                        ForkJoinTask<?>[] a; int b;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1824
                        if (subtask.status < 0)     // surround probes with
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1825
                            continue restart;       //   consistency checks
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1826
                        if ((b = v.base) - v.top < 0 && (a = v.array) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1827
                            int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1828
                            ForkJoinTask<?> t =
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1829
                                (ForkJoinTask<?>)U.getObjectVolatile(a, i);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1830
                            if (subtask.status < 0 || j.currentJoin != subtask ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1831
                                v.currentSteal != subtask)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1832
                                continue restart;   // stale
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1833
                            stat = 1;               // apparent progress
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1834
                            if (v.base == b) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1835
                                if (t == null)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1836
                                    break restart;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1837
                                if (U.compareAndSwapObject(a, i, t, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1838
                                    U.putOrderedInt(v, QBASE, b + 1);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1839
                                    ForkJoinTask<?> ps = joiner.currentSteal;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1840
                                    int jt = joiner.top;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1841
                                    do {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1842
                                        joiner.currentSteal = t;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1843
                                        t.doExec(); // clear local tasks too
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1844
                                    } while (task.status >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1845
                                             joiner.top != jt &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1846
                                             (t = joiner.pop()) != null);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1847
                                    joiner.currentSteal = ps;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1848
                                    break restart;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1849
                                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1850
                            }
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
                        else {                      // empty -- try to descend
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1853
                            ForkJoinTask<?> next = v.currentJoin;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1854
                            if (subtask.status < 0 || j.currentJoin != subtask ||
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1855
                                v.currentSteal != subtask)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1856
                                continue restart;   // stale
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1857
                            else if (next == null || ++steps == MAX_HELP)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1858
                                break restart;      // dead-end or maybe cyclic
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1859
                            else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1860
                                subtask = next;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1861
                                j = v;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1862
                                break;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1863
                            }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1864
                        }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1865
                    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  1866
                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1867
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1868
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1869
        return stat;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1870
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1871
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1872
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1873
     * Analog of tryHelpStealer for CountedCompleters. Tries to steal
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1874
     * and run tasks within the target's computation.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1875
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1876
     * @param task the task to join
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1877
     * @param maxTasks the maximum number of other tasks to run
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1878
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1879
    final int helpComplete(WorkQueue joiner, CountedCompleter<?> task,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1880
                           int maxTasks) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1881
        WorkQueue[] ws; int m;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1882
        int s = 0;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1883
        if ((ws = workQueues) != null && (m = ws.length - 1) >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1884
            joiner != null && task != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1885
            int j = joiner.poolIndex;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1886
            int scans = m + m + 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1887
            long c = 0L;              // for stability check
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1888
            for (int k = scans; ; j += 2) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1889
                WorkQueue q;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1890
                if ((s = task.status) < 0)
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1891
                    break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1892
                else if (joiner.internalPopAndExecCC(task)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1893
                    if (--maxTasks <= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1894
                        s = task.status;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1895
                        break;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1896
                    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1897
                    k = scans;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1898
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1899
                else if ((s = task.status) < 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1900
                    break;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1901
                else if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1902
                    if (--maxTasks <= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1903
                        s = task.status;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1904
                        break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1905
                    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1906
                    k = scans;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1907
                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1908
                else if (--k < 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1909
                    if (c == (c = ctl))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1910
                        break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1911
                    k = scans;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1912
                }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1913
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1914
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1915
        return s;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1916
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1917
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1918
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1919
     * Tries to decrement active count (sometimes implicitly) and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1920
     * possibly release or create a compensating worker in preparation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1921
     * for blocking. Fails on contention or termination. Otherwise,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1922
     * adds a new thread if no idle workers are available and pool
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1923
     * may become starved.
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1924
     *
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1925
     * @param c the assumed ctl value
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1926
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1927
    final boolean tryCompensate(long c) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1928
        WorkQueue[] ws = workQueues;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1929
        int pc = parallelism, e = (int)c, m, tc;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1930
        if (ws != null && (m = ws.length - 1) >= 0 && e >= 0 && ctl == c) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1931
            WorkQueue w = ws[e & m];
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1932
            if (e != 0 && w != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1933
                Thread p;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1934
                long nc = ((long)(w.nextWait & E_MASK) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1935
                           (c & (AC_MASK|TC_MASK)));
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1936
                int ne = (e + E_SEQ) & E_MASK;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1937
                if (w.eventCount == (e | INT_SIGN) &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1938
                    U.compareAndSwapLong(this, CTL, c, nc)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1939
                    w.eventCount = ne;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1940
                    if ((p = w.parker) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1941
                        U.unpark(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1942
                    return true;   // replace with idle worker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1943
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1944
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1945
            else if ((tc = (short)(c >>> TC_SHIFT)) >= 0 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1946
                     (int)(c >> AC_SHIFT) + pc > 1) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1947
                long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1948
                if (U.compareAndSwapLong(this, CTL, c, nc))
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1949
                    return true;   // no compensation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1950
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1951
            else if (tc + pc < MAX_CAP) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1952
                long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1953
                if (U.compareAndSwapLong(this, CTL, c, nc)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1954
                    ForkJoinWorkerThreadFactory fac;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1955
                    Throwable ex = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1956
                    ForkJoinWorkerThread wt = null;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1957
                    try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1958
                        if ((fac = factory) != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1959
                            (wt = fac.newThread(this)) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1960
                            wt.start();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1961
                            return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1962
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1963
                    } catch (Throwable rex) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1964
                        ex = rex;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1965
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1966
                    deregisterWorker(wt, ex); // clean up and return false
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1967
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1968
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1969
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1970
        return false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1971
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1972
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
     * Helps and/or blocks until the given task is done.
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
     * @param joiner the joining worker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1977
     * @param task the task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1978
     * @return task status on exit
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1979
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1980
    final int awaitJoin(WorkQueue joiner, ForkJoinTask<?> task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1981
        int s = 0;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1982
        if (task != null && (s = task.status) >= 0 && joiner != null) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1983
            ForkJoinTask<?> prevJoin = joiner.currentJoin;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1984
            joiner.currentJoin = task;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1985
            do {} while (joiner.tryRemoveAndExec(task) && // process local tasks
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1986
                         (s = task.status) >= 0);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1987
            if (s >= 0 && (task instanceof CountedCompleter))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1988
                s = helpComplete(joiner, (CountedCompleter<?>)task, Integer.MAX_VALUE);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1989
            long cc = 0;        // for stability checks
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1990
            while (s >= 0 && (s = task.status) >= 0) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1991
                if ((s = tryHelpStealer(joiner, task)) == 0 &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1992
                    (s = task.status) >= 0) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1993
                    if (!tryCompensate(cc))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1994
                        cc = ctl;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  1995
                    else {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1996
                        if (task.trySetSignal() && (s = task.status) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1997
                            synchronized (task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1998
                                if (task.status >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  1999
                                    try {                // see ForkJoinTask
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2000
                                        task.wait();     //  for explanation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2001
                                    } catch (InterruptedException ie) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2002
                                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2003
                                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2004
                                else
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2005
                                    task.notifyAll();
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
                        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2008
                        long c; // reactivate
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2009
                        do {} while (!U.compareAndSwapLong
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2010
                                     (this, CTL, c = ctl,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2011
                                      ((c & ~AC_MASK) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2012
                                       ((c & AC_MASK) + AC_UNIT))));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2013
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2014
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2015
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2016
            joiner.currentJoin = prevJoin;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2017
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2018
        return s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2019
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2020
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2021
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2022
     * Stripped-down variant of awaitJoin used by timed joins. Tries
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2023
     * to help join only while there is continuous progress. (Caller
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2024
     * will then enter a timed wait.)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2025
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2026
     * @param joiner the joining worker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2027
     * @param task the task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2028
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2029
    final void helpJoinOnce(WorkQueue joiner, ForkJoinTask<?> task) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2030
        int s;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2031
        if (joiner != null && task != null && (s = task.status) >= 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2032
            ForkJoinTask<?> prevJoin = joiner.currentJoin;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2033
            joiner.currentJoin = task;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2034
            do {} while (joiner.tryRemoveAndExec(task) && // process local tasks
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2035
                         (s = task.status) >= 0);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2036
            if (s >= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2037
                if (task instanceof CountedCompleter)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2038
                    helpComplete(joiner, (CountedCompleter<?>)task, Integer.MAX_VALUE);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2039
                do {} while (task.status >= 0 &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2040
                             tryHelpStealer(joiner, task) > 0);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2041
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2042
            joiner.currentJoin = prevJoin;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2043
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2044
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2045
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2046
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2047
     * Returns a (probably) non-empty steal queue, if one is found
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2048
     * during a scan, else null.  This method must be retried by
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2049
     * caller if, by the time it tries to use the queue, it is empty.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2050
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2051
    private WorkQueue findNonEmptyStealQueue() {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2052
        int r = ThreadLocalRandom.nextSecondarySeed();
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2053
        for (;;) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2054
            int ps = plock, m; WorkQueue[] ws; WorkQueue q;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2055
            if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2056
                for (int j = (m + 1) << 2; j >= 0; --j) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2057
                    if ((q = ws[(((r - j) << 1) | 1) & m]) != null &&
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2058
                        q.base - q.top < 0)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2059
                        return q;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2060
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2061
            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2062
            if (plock == ps)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2063
                return null;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2064
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2065
    }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2066
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2067
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2068
     * Runs tasks until {@code isQuiescent()}. We piggyback on
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2069
     * active count ctl maintenance, but rather than blocking
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2070
     * when tasks cannot be found, we rescan until all others cannot
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2071
     * find tasks either.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2072
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2073
    final void helpQuiescePool(WorkQueue w) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2074
        ForkJoinTask<?> ps = w.currentSteal;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2075
        for (boolean active = true;;) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2076
            long c; WorkQueue q; ForkJoinTask<?> t; int b;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2077
            while ((t = w.nextLocalTask()) != null)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2078
                t.doExec();
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2079
            if ((q = findNonEmptyStealQueue()) != null) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2080
                if (!active) {      // re-establish active count
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2081
                    active = true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2082
                    do {} while (!U.compareAndSwapLong
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2083
                                 (this, CTL, c = ctl,
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2084
                                  ((c & ~AC_MASK) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2085
                                   ((c & AC_MASK) + AC_UNIT))));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2086
                }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2087
                if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2088
                    (w.currentSteal = t).doExec();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2089
                    w.currentSteal = ps;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2090
                }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2091
            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2092
            else if (active) {       // decrement active count without queuing
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2093
                long nc = ((c = ctl) & ~AC_MASK) | ((c & AC_MASK) - AC_UNIT);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2094
                if ((int)(nc >> AC_SHIFT) + parallelism == 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2095
                    break;          // bypass decrement-then-increment
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2096
                if (U.compareAndSwapLong(this, CTL, c, nc))
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2097
                    active = false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2098
            }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2099
            else if ((int)((c = ctl) >> AC_SHIFT) + parallelism <= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2100
                     U.compareAndSwapLong
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2101
                     (this, CTL, c, ((c & ~AC_MASK) |
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2102
                                     ((c & AC_MASK) + AC_UNIT))))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2103
                break;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2104
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2105
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2106
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2107
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2108
     * 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
  2109
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2110
     * @return a task, if available
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2111
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2112
    final ForkJoinTask<?> nextTaskFor(WorkQueue w) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2113
        for (ForkJoinTask<?> t;;) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2114
            WorkQueue q; int b;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2115
            if ((t = w.nextLocalTask()) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2116
                return t;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2117
            if ((q = findNonEmptyStealQueue()) == null)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2118
                return null;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2119
            if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2120
                return t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2121
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2122
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2123
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2124
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2125
     * Returns a cheap heuristic guide for task partitioning when
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2126
     * programmers, frameworks, tools, or languages have little or no
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2127
     * idea about task granularity.  In essence by offering this
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2128
     * method, we ask users only about tradeoffs in overhead vs
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2129
     * expected throughput and its variance, rather than how finely to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2130
     * partition tasks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2131
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2132
     * In a steady state strict (tree-structured) computation, each
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2133
     * thread makes available for stealing enough tasks for other
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2134
     * threads to remain active. Inductively, if all threads play by
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2135
     * the same rules, each thread should make available only a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2136
     * constant number of tasks.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2137
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2138
     * 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
  2139
     * would require immediate replenishment upon each steal to
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2140
     * maintain enough tasks, which is infeasible.  Further,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2141
     * partitionings/granularities of offered tasks should minimize
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2142
     * steal rates, which in general means that threads nearer the top
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2143
     * of computation tree should generate more than those nearer the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2144
     * bottom. In perfect steady state, each thread is at
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2145
     * approximately the same level of computation tree. However,
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2146
     * producing extra tasks amortizes the uncertainty of progress and
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2147
     * diffusion assumptions.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2148
     *
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2149
     * 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
  2150
     * than 1 to both smooth over transient shortages and hedge
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2151
     * against uneven progress; as traded off against the cost of
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2152
     * extra task overhead. We leave the user to pick a threshold
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2153
     * value to compare with the results of this call to guide
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2154
     * decisions, but recommend values such as 3.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2155
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2156
     * 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
  2157
     * surplus strictly locally. In steady-state, if one thread is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2158
     * 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
  2159
     * just use estimated queue length.  However, this strategy alone
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2160
     * leads to serious mis-estimates in some non-steady-state
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2161
     * conditions (ramp-up, ramp-down, other stalls). We can detect
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2162
     * many of these by further considering the number of "idle"
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2163
     * threads, that are known to have zero queued tasks, so
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2164
     * compensate by a factor of (#idle/#active) threads.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2165
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2166
     * Note: The approximation of #busy workers as #active workers is
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2167
     * not very good under current signalling scheme, and should be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2168
     * improved.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2169
     */
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2170
    static int getSurplusQueuedTaskCount() {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2171
        Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2172
        if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2173
            int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).parallelism;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2174
            int n = (q = wt.workQueue).top - q.base;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2175
            int a = (int)(pool.ctl >> AC_SHIFT) + p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2176
            return n - (a > (p >>>= 1) ? 0 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2177
                        a > (p >>>= 1) ? 1 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2178
                        a > (p >>>= 1) ? 2 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2179
                        a > (p >>>= 1) ? 4 :
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2180
                        8);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2181
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2182
        return 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2183
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2184
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2185
    //  Termination
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2186
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2187
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2188
     * Possibly initiates and/or completes termination.  The caller
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2189
     * triggering termination runs three passes through workQueues:
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2190
     * (0) Setting termination status, followed by wakeups of queued
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2191
     * workers; (1) cancelling all tasks; (2) interrupting lagging
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2192
     * threads (likely in external tasks, but possibly also blocked in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2193
     * joins).  Each pass repeats previous steps because of potential
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2194
     * lagging thread creation.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2195
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2196
     * @param now if true, unconditionally terminate, else only
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2197
     * if no work and no active workers
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2198
     * @param enable if true, enable shutdown when next possible
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2199
     * @return true if now terminating or terminated
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2200
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2201
    private boolean tryTerminate(boolean now, boolean enable) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2202
        int ps;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2203
        if (this == common)                        // cannot shut down
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2204
            return false;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2205
        if ((ps = plock) >= 0) {                   // enable by setting plock
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2206
            if (!enable)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2207
                return false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2208
            if ((ps & PL_LOCK) != 0 ||
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2209
                !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2210
                ps = acquirePlock();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2211
            int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2212
            if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2213
                releasePlock(nps);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2214
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2215
        for (long c;;) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2216
            if (((c = ctl) & STOP_BIT) != 0) {     // already terminating
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2217
                if ((short)(c >>> TC_SHIFT) + parallelism <= 0) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2218
                    synchronized (this) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2219
                        notifyAll();               // signal when 0 workers
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2220
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2221
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2222
                return true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2223
            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2224
            if (!now) {                            // check if idle & no tasks
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2225
                WorkQueue[] ws; WorkQueue w;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2226
                if ((int)(c >> AC_SHIFT) + parallelism > 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2227
                    return false;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2228
                if ((ws = workQueues) != null) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2229
                    for (int i = 0; i < ws.length; ++i) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2230
                        if ((w = ws[i]) != null &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2231
                            (!w.isEmpty() ||
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2232
                             ((i & 1) != 0 && w.eventCount >= 0))) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2233
                            signalWork(ws, w);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2234
                            return false;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2235
                        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2236
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2237
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2238
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2239
            if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2240
                for (int pass = 0; pass < 3; ++pass) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2241
                    WorkQueue[] ws; WorkQueue w; Thread wt;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2242
                    if ((ws = workQueues) != null) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2243
                        int n = ws.length;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2244
                        for (int i = 0; i < n; ++i) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2245
                            if ((w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2246
                                w.qlock = -1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2247
                                if (pass > 0) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2248
                                    w.cancelAll();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2249
                                    if (pass > 1 && (wt = w.owner) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2250
                                        if (!wt.isInterrupted()) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2251
                                            try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2252
                                                wt.interrupt();
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2253
                                            } catch (Throwable ignore) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2254
                                            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2255
                                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2256
                                        U.unpark(wt);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2257
                                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2258
                                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2259
                            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2260
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2261
                        // Wake up workers parked on event queue
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2262
                        int i, e; long cc; Thread p;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2263
                        while ((e = (int)(cc = ctl) & E_MASK) != 0 &&
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2264
                               (i = e & SMASK) < n && i >= 0 &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2265
                               (w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2266
                            long nc = ((long)(w.nextWait & E_MASK) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2267
                                       ((cc + AC_UNIT) & AC_MASK) |
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2268
                                       (cc & (TC_MASK|STOP_BIT)));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2269
                            if (w.eventCount == (e | INT_SIGN) &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2270
                                U.compareAndSwapLong(this, CTL, cc, nc)) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2271
                                w.eventCount = (e + E_SEQ) & E_MASK;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2272
                                w.qlock = -1;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2273
                                if ((p = w.parker) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2274
                                    U.unpark(p);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2275
                            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2276
                        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2277
                    }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2278
                }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2279
            }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2280
        }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2281
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2282
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2283
    // external operations on common pool
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2284
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2285
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2286
     * Returns common pool queue for a thread that has submitted at
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2287
     * least one task.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2288
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2289
    static WorkQueue commonSubmitterQueue() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2290
        ForkJoinPool p; WorkQueue[] ws; int m, z;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2291
        return ((z = ThreadLocalRandom.getProbe()) != 0 &&
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2292
                (p = common) != null &&
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2293
                (ws = p.workQueues) != null &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2294
                (m = ws.length - 1) >= 0) ?
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2295
            ws[m & z & SQMASK] : null;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2296
    }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2297
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2298
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2299
     * Tries to pop the given task from submitter's queue in common pool.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2300
     */
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2301
    final boolean tryExternalUnpush(ForkJoinTask<?> task) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2302
        WorkQueue joiner; ForkJoinTask<?>[] a; int m, s;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2303
        WorkQueue[] ws = workQueues;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2304
        int z = ThreadLocalRandom.getProbe();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2305
        boolean popped = false;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2306
        if (ws != null && (m = ws.length - 1) >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2307
            (joiner = ws[z & m & SQMASK]) != null &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2308
            joiner.base != (s = joiner.top) &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2309
            (a = joiner.array) != null) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2310
            long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2311
            if (U.getObject(a, j) == task &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2312
                U.compareAndSwapInt(joiner, QLOCK, 0, 1)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2313
                if (joiner.top == s && joiner.array == a &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2314
                    U.compareAndSwapObject(a, j, task, null)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2315
                    joiner.top = s - 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2316
                    popped = true;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2317
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2318
                joiner.qlock = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2319
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2320
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2321
        return popped;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2322
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2323
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2324
    final int externalHelpComplete(CountedCompleter<?> task, int maxTasks) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2325
        WorkQueue joiner; int m;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2326
        WorkQueue[] ws = workQueues;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2327
        int j = ThreadLocalRandom.getProbe();
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2328
        int s = 0;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2329
        if (ws != null && (m = ws.length - 1) >= 0 &&
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2330
            (joiner = ws[j & m & SQMASK]) != null && task != null) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2331
            int scans = m + m + 1;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2332
            long c = 0L;             // for stability check
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2333
            j |= 1;                  // poll odd queues
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2334
            for (int k = scans; ; j += 2) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2335
                WorkQueue q;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2336
                if ((s = task.status) < 0)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2337
                    break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2338
                else if (joiner.externalPopAndExecCC(task)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2339
                    if (--maxTasks <= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2340
                        s = task.status;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2341
                        break;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2342
                    }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2343
                    k = scans;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2344
                }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2345
                else if ((s = task.status) < 0)
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2346
                    break;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2347
                else if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2348
                    if (--maxTasks <= 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2349
                        s = task.status;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2350
                        break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2351
                    }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2352
                    k = scans;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2353
                }
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2354
                else if (--k < 0) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2355
                    if (c == (c = ctl))
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2356
                        break;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2357
                    k = scans;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2358
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2359
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2360
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2361
        return s;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2362
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2363
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2364
    // Exported methods
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2365
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2366
    // Constructors
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2367
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2368
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2369
     * Creates a {@code ForkJoinPool} with parallelism equal to {@link
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2370
     * java.lang.Runtime#availableProcessors}, using the {@linkplain
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2371
     * #defaultForkJoinWorkerThreadFactory default thread factory},
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2372
     * no UncaughtExceptionHandler, and non-async LIFO processing mode.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2373
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2374
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2375
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2376
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2377
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2378
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2379
    public ForkJoinPool() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2380
        this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()),
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2381
             defaultForkJoinWorkerThreadFactory, null, false);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2382
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2383
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2384
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2385
     * Creates a {@code ForkJoinPool} with the indicated parallelism
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2386
     * level, the {@linkplain
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2387
     * #defaultForkJoinWorkerThreadFactory default thread factory},
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2388
     * no UncaughtExceptionHandler, and non-async LIFO processing mode.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2389
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2390
     * @param parallelism the parallelism level
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2391
     * @throws IllegalArgumentException if parallelism less than or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2392
     *         equal to zero, or greater than implementation limit
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2393
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2394
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2395
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2396
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2397
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2398
    public ForkJoinPool(int parallelism) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2399
        this(parallelism, defaultForkJoinWorkerThreadFactory, null, false);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2400
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2401
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2402
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2403
     * Creates a {@code ForkJoinPool} with the given parameters.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2404
     *
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2405
     * @param parallelism the parallelism level. For default value,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2406
     * use {@link java.lang.Runtime#availableProcessors}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2407
     * @param factory the factory for creating new threads. For default value,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2408
     * use {@link #defaultForkJoinWorkerThreadFactory}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2409
     * @param handler the handler for internal worker threads that
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2410
     * terminate due to unrecoverable errors encountered while executing
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2411
     * tasks. For default value, use {@code null}.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2412
     * @param asyncMode if true,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2413
     * establishes local first-in-first-out scheduling mode for forked
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2414
     * tasks that are never joined. This mode may be more appropriate
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2415
     * than default locally stack-based mode in applications in which
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2416
     * worker threads only process event-style asynchronous tasks.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2417
     * For default value, use {@code false}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2418
     * @throws IllegalArgumentException if parallelism less than or
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2419
     *         equal to zero, or greater than implementation limit
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2420
     * @throws NullPointerException if the factory is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2421
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2422
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2423
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2424
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2425
     */
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2426
    public ForkJoinPool(int parallelism,
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2427
                        ForkJoinWorkerThreadFactory factory,
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2428
                        UncaughtExceptionHandler handler,
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2429
                        boolean asyncMode) {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2430
        this(checkParallelism(parallelism),
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2431
             checkFactory(factory),
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2432
             handler,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2433
             (asyncMode ? FIFO_QUEUE : LIFO_QUEUE),
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2434
             "ForkJoinPool-" + nextPoolId() + "-worker-");
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2435
        checkPermission();
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2436
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2437
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2438
    private static int checkParallelism(int parallelism) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2439
        if (parallelism <= 0 || parallelism > MAX_CAP)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2440
            throw new IllegalArgumentException();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2441
        return parallelism;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2442
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2443
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2444
    private static ForkJoinWorkerThreadFactory checkFactory
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2445
        (ForkJoinWorkerThreadFactory factory) {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2446
        if (factory == null)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2447
            throw new NullPointerException();
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2448
        return factory;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2449
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2450
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2451
    /**
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2452
     * Creates a {@code ForkJoinPool} with the given parameters, without
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2453
     * any security checks or parameter validation.  Invoked directly by
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2454
     * makeCommonPool.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2455
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2456
    private ForkJoinPool(int parallelism,
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2457
                         ForkJoinWorkerThreadFactory factory,
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2458
                         UncaughtExceptionHandler handler,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2459
                         int mode,
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2460
                         String workerNamePrefix) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2461
        this.workerNamePrefix = workerNamePrefix;
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2462
        this.factory = factory;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2463
        this.ueh = handler;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2464
        this.mode = (short)mode;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2465
        this.parallelism = (short)parallelism;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2466
        long np = (long)(-parallelism); // offset ctl counts
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2467
        this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2468
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2469
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2470
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2471
     * Returns the common pool instance. This pool is statically
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2472
     * constructed; its run state is unaffected by attempts to {@link
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2473
     * #shutdown} or {@link #shutdownNow}. However this pool and any
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2474
     * ongoing processing are automatically terminated upon program
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2475
     * {@link System#exit}.  Any program that relies on asynchronous
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2476
     * task processing to complete before program termination should
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2477
     * invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence},
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2478
     * before exit.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2479
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2480
     * @return the common pool instance
15002
8ab8c2b2eee6 8005709: Add at since tags to new FJP getCommonPoolParallelism and commonPool
chegar
parents: 14914
diff changeset
  2481
     * @since 1.8
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2482
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2483
    public static ForkJoinPool commonPool() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2484
        // assert common != null : "static init error";
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2485
        return common;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2486
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2487
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2488
    // Execution methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2489
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2490
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2491
     * Performs the given task, returning its result upon completion.
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2492
     * If the computation encounters an unchecked Exception or Error,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2493
     * it is rethrown as the outcome of this invocation.  Rethrown
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2494
     * exceptions behave in the same way as regular exceptions, but,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2495
     * when possible, contain stack traces (as displayed for example
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2496
     * using {@code ex.printStackTrace()}) of both the current thread
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2497
     * as well as the thread actually encountering the exception;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2498
     * minimally only the latter.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2499
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2500
     * @param task the task
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  2501
     * @param <T> the type of the task's result
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2502
     * @return the task's result
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2503
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2504
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2505
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2506
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2507
    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
  2508
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2509
            throw new NullPointerException();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2510
        externalPush(task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2511
        return task.join();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2512
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2513
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2514
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2515
     * Arranges for (asynchronous) execution of the given task.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2516
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2517
     * @param task the task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2518
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2519
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2520
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2521
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2522
    public void execute(ForkJoinTask<?> task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2523
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2524
            throw new NullPointerException();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2525
        externalPush(task);
4110
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
    // AbstractExecutorService methods
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2529
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2530
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2531
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2532
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2533
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2534
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2535
    public void execute(Runnable task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2536
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2537
            throw new NullPointerException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2538
        ForkJoinTask<?> job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2539
        if (task instanceof ForkJoinTask<?>) // avoid re-wrap
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2540
            job = (ForkJoinTask<?>) task;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2541
        else
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2542
            job = new ForkJoinTask.RunnableExecuteAction(task);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2543
        externalPush(job);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2544
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2545
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2546
    /**
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2547
     * Submits a ForkJoinTask for execution.
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2548
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2549
     * @param task the task to submit
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
  2550
     * @param <T> the type of the task's result
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2551
     * @return the task
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2552
     * @throws NullPointerException if the task is null
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2553
     * @throws RejectedExecutionException if the task cannot be
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2554
     *         scheduled for execution
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2555
     */
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2556
    public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2557
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2558
            throw new NullPointerException();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2559
        externalPush(task);
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2560
        return task;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2561
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2562
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2563
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2564
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2565
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2566
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2567
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2568
    public <T> ForkJoinTask<T> submit(Callable<T> task) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2569
        ForkJoinTask<T> job = new ForkJoinTask.AdaptedCallable<T>(task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2570
        externalPush(job);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2571
        return job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2572
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2573
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2574
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2575
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2576
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2577
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2578
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2579
    public <T> ForkJoinTask<T> submit(Runnable task, T result) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2580
        ForkJoinTask<T> job = new ForkJoinTask.AdaptedRunnable<T>(task, result);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2581
        externalPush(job);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2582
        return job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2583
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2584
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2585
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2586
     * @throws NullPointerException if the task is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2587
     * @throws RejectedExecutionException if the task cannot be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2588
     *         scheduled for execution
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2589
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2590
    public ForkJoinTask<?> submit(Runnable task) {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2591
        if (task == null)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
  2592
            throw new NullPointerException();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2593
        ForkJoinTask<?> job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2594
        if (task instanceof ForkJoinTask<?>) // avoid re-wrap
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2595
            job = (ForkJoinTask<?>) task;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2596
        else
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2597
            job = new ForkJoinTask.AdaptedRunnableAction(task);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2598
        externalPush(job);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2599
        return job;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2600
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2601
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2602
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2603
     * @throws NullPointerException       {@inheritDoc}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2604
     * @throws RejectedExecutionException {@inheritDoc}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2605
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2606
    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
  2607
        // In previous versions of this class, this method constructed
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2608
        // a task to run ForkJoinTask.invokeAll, but now external
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2609
        // invocation of multiple tasks is at least as efficient.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2610
        ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2611
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2612
        boolean done = false;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2613
        try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2614
            for (Callable<T> t : tasks) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2615
                ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t);
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2616
                futures.add(f);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2617
                externalPush(f);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2618
            }
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2619
            for (int i = 0, size = futures.size(); i < size; i++)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2620
                ((ForkJoinTask<?>)futures.get(i)).quietlyJoin();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2621
            done = true;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2622
            return futures;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2623
        } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2624
            if (!done)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2625
                for (int i = 0, size = futures.size(); i < size; i++)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2626
                    futures.get(i).cancel(false);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2627
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2628
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2629
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2630
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2631
     * Returns the factory used for constructing new workers.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2632
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2633
     * @return the factory used for constructing new workers
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2634
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2635
    public ForkJoinWorkerThreadFactory getFactory() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2636
        return factory;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2637
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2638
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2639
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2640
     * Returns the handler for internal worker threads that terminate
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2641
     * due to unrecoverable errors encountered while executing tasks.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2642
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2643
     * @return the handler, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2644
     */
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2645
    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2646
        return ueh;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2647
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2648
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2649
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2650
     * Returns the targeted parallelism level of this pool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2651
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2652
     * @return the targeted parallelism level of this pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2653
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2654
    public int getParallelism() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2655
        int par;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2656
        return ((par = parallelism) > 0) ? par : 1;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2657
    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2658
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2659
    /**
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2660
     * Returns the targeted parallelism level of the common pool.
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2661
     *
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2662
     * @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
  2663
     * @since 1.8
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2664
     */
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2665
    public static int getCommonPoolParallelism() {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2666
        return commonParallelism;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2667
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2668
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2669
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2670
     * Returns the number of worker threads that have started but not
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2671
     * yet terminated.  The result returned by this method may differ
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2672
     * from {@link #getParallelism} when threads are created to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2673
     * maintain parallelism when others are cooperatively blocked.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2674
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2675
     * @return the number of worker threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2676
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2677
    public int getPoolSize() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2678
        return parallelism + (short)(ctl >>> TC_SHIFT);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2679
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2680
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2681
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2682
     * Returns {@code true} if this pool uses local first-in-first-out
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2683
     * scheduling mode for forked tasks that are never joined.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2684
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2685
     * @return {@code true} if this pool uses async mode
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2686
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2687
    public boolean getAsyncMode() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2688
        return mode == FIFO_QUEUE;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2689
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2690
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2691
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2692
     * Returns an estimate of the number of worker threads that are
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2693
     * not blocked waiting to join tasks or for other managed
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2694
     * synchronization. This method may overestimate the
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2695
     * number of running threads.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2696
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2697
     * @return the number of worker threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2698
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2699
    public int getRunningThreadCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2700
        int rc = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2701
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2702
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2703
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2704
                if ((w = ws[i]) != null && w.isApparentlyUnblocked())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2705
                    ++rc;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2706
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2707
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2708
        return rc;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2709
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2710
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2711
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2712
     * Returns an estimate of the number of threads that are currently
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2713
     * stealing or executing tasks. This method may overestimate the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2714
     * number of active threads.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2715
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2716
     * @return the number of active threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2717
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2718
    public int getActiveThreadCount() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2719
        int r = parallelism + (int)(ctl >> AC_SHIFT);
9278
f4672926fe4c 7037436: CR 7035020 fails to check shutdown
dl
parents: 9247
diff changeset
  2720
        return (r <= 0) ? 0 : r; // suppress momentarily negative values
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2721
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2722
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2723
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2724
     * Returns {@code true} if all worker threads are currently idle.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2725
     * An idle worker is one that cannot obtain a task to execute
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2726
     * because none are available to steal from other threads, and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2727
     * there are no pending submissions to the pool. This method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2728
     * conservative; it might not return {@code true} immediately upon
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2729
     * idleness of all threads, but will eventually become true if
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2730
     * threads remain inactive.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2731
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2732
     * @return {@code true} if all threads are currently idle
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2733
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2734
    public boolean isQuiescent() {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2735
        return parallelism + (int)(ctl >> AC_SHIFT) <= 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2736
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2737
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2738
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2739
     * Returns an estimate of the total number of tasks stolen from
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2740
     * one thread's work queue by another. The reported value
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2741
     * underestimates the actual total number of steals when the pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2742
     * is not quiescent. This value may be useful for monitoring and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2743
     * tuning fork/join programs: in general, steal counts should be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2744
     * high enough to keep threads busy, but low enough to avoid
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2745
     * overhead and contention across threads.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2746
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2747
     * @return the number of steals
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2748
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2749
    public long getStealCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2750
        long count = stealCount;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2751
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2752
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2753
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2754
                if ((w = ws[i]) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2755
                    count += w.nsteals;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2756
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2757
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2758
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2759
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2760
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2761
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2762
     * Returns an estimate of the total number of tasks currently held
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2763
     * in queues by worker threads (but not including tasks submitted
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2764
     * to the pool that have not begun executing). This value is only
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2765
     * an approximation, obtained by iterating across all threads in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2766
     * the pool. This method may be useful for tuning task
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2767
     * granularities.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2768
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2769
     * @return the number of queued tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2770
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2771
    public long getQueuedTaskCount() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2772
        long count = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2773
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2774
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2775
            for (int i = 1; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2776
                if ((w = ws[i]) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2777
                    count += w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2778
            }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2779
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2780
        return count;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2781
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2782
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2783
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2784
     * 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
  2785
     * pool that have not yet begun executing.  This method may take
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2786
     * time proportional to the number of submissions.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2787
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2788
     * @return the number of queued submissions
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2789
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2790
    public int getQueuedSubmissionCount() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2791
        int count = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2792
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2793
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2794
            for (int i = 0; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2795
                if ((w = ws[i]) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2796
                    count += w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2797
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2798
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2799
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2800
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2801
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2802
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2803
     * Returns {@code true} if there are any tasks submitted to this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2804
     * pool that have not yet begun executing.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2805
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2806
     * @return {@code true} if there are any queued submissions
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2807
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2808
    public boolean hasQueuedSubmissions() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2809
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2810
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2811
            for (int i = 0; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2812
                if ((w = ws[i]) != null && !w.isEmpty())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2813
                    return true;
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
        return false;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2817
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2818
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2819
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2820
     * Removes and returns the next unexecuted submission if one is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2821
     * available.  This method may be useful in extensions to this
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2822
     * class that re-assign work in systems with multiple pools.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2823
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2824
     * @return the next submission, or {@code null} if none
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2825
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2826
    protected ForkJoinTask<?> pollSubmission() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2827
        WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2828
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2829
            for (int i = 0; i < ws.length; i += 2) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2830
                if ((w = ws[i]) != null && (t = w.poll()) != null)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2831
                    return t;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2832
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2833
        }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2834
        return null;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2835
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2836
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2837
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2838
     * Removes all available unexecuted submitted and forked tasks
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2839
     * from scheduling queues and adds them to the given collection,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2840
     * without altering their execution status. These may include
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2841
     * artificially generated or wrapped tasks. This method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2842
     * designed to be invoked only when the pool is known to be
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2843
     * quiescent. Invocations at other times may not remove all
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2844
     * tasks. A failure encountered while attempting to add elements
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2845
     * to collection {@code c} may result in elements being in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2846
     * neither, either or both collections when the associated
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2847
     * exception is thrown.  The behavior of this operation is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2848
     * undefined if the specified collection is modified while the
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2849
     * operation is in progress.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2850
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2851
     * @param c the collection to transfer elements into
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2852
     * @return the number of elements transferred
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2853
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2854
    protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2855
        int count = 0;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2856
        WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2857
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2858
            for (int i = 0; i < ws.length; ++i) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2859
                if ((w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2860
                    while ((t = w.poll()) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2861
                        c.add(t);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2862
                        ++count;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2863
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2864
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2865
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2866
        }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2867
        return count;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2868
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2869
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2870
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2871
     * Returns a string identifying this pool, as well as its state,
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2872
     * including indications of run state, parallelism level, and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2873
     * worker and task counts.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2874
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2875
     * @return a string identifying this pool, as well as its state
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2876
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2877
    public String toString() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2878
        // Use a single pass through workQueues to collect counts
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2879
        long qt = 0L, qs = 0L; int rc = 0;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2880
        long st = stealCount;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2881
        long c = ctl;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2882
        WorkQueue[] ws; WorkQueue w;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2883
        if ((ws = workQueues) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2884
            for (int i = 0; i < ws.length; ++i) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2885
                if ((w = ws[i]) != null) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2886
                    int size = w.queueSize();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2887
                    if ((i & 1) == 0)
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2888
                        qs += size;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2889
                    else {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2890
                        qt += size;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2891
                        st += w.nsteals;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2892
                        if (w.isApparentlyUnblocked())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2893
                            ++rc;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2894
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2895
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2896
            }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2897
        }
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2898
        int pc = parallelism;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2899
        int tc = pc + (short)(c >>> TC_SHIFT);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2900
        int ac = pc + (int)(c >> AC_SHIFT);
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2901
        if (ac < 0) // ignore transient negative
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2902
            ac = 0;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2903
        String level;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2904
        if ((c & STOP_BIT) != 0)
9278
f4672926fe4c 7037436: CR 7035020 fails to check shutdown
dl
parents: 9247
diff changeset
  2905
            level = (tc == 0) ? "Terminated" : "Terminating";
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2906
        else
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2907
            level = plock < 0 ? "Shutting down" : "Running";
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2908
        return super.toString() +
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2909
            "[" + level +
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2910
            ", parallelism = " + pc +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2911
            ", size = " + tc +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2912
            ", active = " + ac +
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  2913
            ", running = " + rc +
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2914
            ", steals = " + st +
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2915
            ", tasks = " + qt +
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2916
            ", submissions = " + qs +
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
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2920
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2921
     * Possibly initiates an orderly shutdown in which previously
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2922
     * submitted tasks are executed, but no new tasks will be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2923
     * 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
  2924
     * is the {@link #commonPool()}, and no additional effect if
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2925
     * already shut down.  Tasks that are in the process of being
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2926
     * submitted concurrently during the course of this method may or
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2927
     * may not be rejected.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2928
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2929
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2930
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2931
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2932
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2933
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2934
    public void shutdown() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2935
        checkPermission();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2936
        tryTerminate(false, true);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2937
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2938
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2939
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2940
     * Possibly attempts to cancel and/or stop all tasks, and reject
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2941
     * all subsequently submitted tasks.  Invocation has no effect on
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  2942
     * execution state if this is the {@link #commonPool()}, and no
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2943
     * additional effect if already shut down. Otherwise, tasks that
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2944
     * are in the process of being submitted or executed concurrently
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2945
     * during the course of this method may or may not be
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2946
     * rejected. This method cancels both existing and unexecuted
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2947
     * tasks, in order to permit termination in the presence of task
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2948
     * dependencies. So the method always returns an empty list
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2949
     * (unlike the case for some other Executors).
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2950
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2951
     * @return an empty list
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2952
     * @throws SecurityException if a security manager exists and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2953
     *         the caller is not permitted to modify threads
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2954
     *         because it does not hold {@link
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2955
     *         java.lang.RuntimePermission}{@code ("modifyThread")}
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2956
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2957
    public List<Runnable> shutdownNow() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2958
        checkPermission();
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2959
        tryTerminate(true, true);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2960
        return Collections.emptyList();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2961
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2962
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2963
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2964
     * Returns {@code true} if all tasks have completed following shut down.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2965
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2966
     * @return {@code true} if all tasks have completed following shut down
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2967
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2968
    public boolean isTerminated() {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2969
        long c = ctl;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2970
        return ((c & STOP_BIT) != 0L &&
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2971
                (short)(c >>> TC_SHIFT) + parallelism <= 0);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2972
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2973
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2974
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2975
     * Returns {@code true} if the process of termination has
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2976
     * commenced but not yet completed.  This method may be useful for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2977
     * debugging. A return of {@code true} reported a sufficient
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2978
     * period after shutdown may indicate that submitted tasks have
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2979
     * 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
  2980
     * 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
  2981
     * 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
  2982
     * 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
  2983
     * they do, they must abort them on interrupt.)
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2984
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2985
     * @return {@code true} if terminating but not yet terminated
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2986
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2987
    public boolean isTerminating() {
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2988
        long c = ctl;
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  2989
        return ((c & STOP_BIT) != 0L &&
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  2990
                (short)(c >>> TC_SHIFT) + parallelism > 0);
6674
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2991
    }
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2992
2b22e69fdb75 6986050: Small clarifications and fixes for ForkJoin
dl
parents: 6543
diff changeset
  2993
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2994
     * Returns {@code true} if this pool has been shut down.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2995
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2996
     * @return {@code true} if this pool has been shut down
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2997
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  2998
    public boolean isShutdown() {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  2999
        return plock < 0;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3000
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3001
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3002
    /**
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3003
     * Blocks until all tasks have completed execution after a
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3004
     * 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
  3005
     * is interrupted, whichever happens first. Because the {@link
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3006
     * #commonPool()} never terminates until program shutdown, when
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3007
     * 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
  3008
     * #awaitQuiescence(long, TimeUnit)} but always returns {@code false}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3009
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3010
     * @param timeout the maximum time to wait
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3011
     * @param unit the time unit of the timeout argument
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3012
     * @return {@code true} if this executor terminated and
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3013
     *         {@code false} if the timeout elapsed before termination
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3014
     * @throws InterruptedException if interrupted while waiting
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3015
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3016
    public boolean awaitTermination(long timeout, TimeUnit unit)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3017
        throws InterruptedException {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3018
        if (Thread.interrupted())
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3019
            throw new InterruptedException();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3020
        if (this == common) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3021
            awaitQuiescence(timeout, unit);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3022
            return false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3023
        }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3024
        long nanos = unit.toNanos(timeout);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3025
        if (isTerminated())
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3026
            return true;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3027
        if (nanos <= 0L)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3028
            return false;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3029
        long deadline = System.nanoTime() + nanos;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3030
        synchronized (this) {
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3031
            for (;;) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3032
                if (isTerminated())
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3033
                    return true;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3034
                if (nanos <= 0L)
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3035
                    return false;
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3036
                long millis = TimeUnit.NANOSECONDS.toMillis(nanos);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3037
                wait(millis > 0L ? millis : 1L);
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3038
                nanos = deadline - System.nanoTime();
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3039
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3040
        }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3041
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3042
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3043
    /**
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3044
     * If called by a ForkJoinTask operating in this pool, equivalent
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3045
     * in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise,
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3046
     * waits and/or attempts to assist performing tasks until this
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3047
     * pool {@link #isQuiescent} or the indicated timeout elapses.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3048
     *
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3049
     * @param timeout the maximum time to wait
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3050
     * @param unit the time unit of the timeout argument
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3051
     * @return {@code true} if quiescent; {@code false} if the
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3052
     * timeout elapsed.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3053
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3054
    public boolean awaitQuiescence(long timeout, TimeUnit unit) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3055
        long nanos = unit.toNanos(timeout);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3056
        ForkJoinWorkerThread wt;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3057
        Thread thread = Thread.currentThread();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3058
        if ((thread instanceof ForkJoinWorkerThread) &&
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3059
            (wt = (ForkJoinWorkerThread)thread).pool == this) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3060
            helpQuiescePool(wt.workQueue);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3061
            return true;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3062
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3063
        long startTime = System.nanoTime();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3064
        WorkQueue[] ws;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3065
        int r = 0, m;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3066
        boolean found = true;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3067
        while (!isQuiescent() && (ws = workQueues) != null &&
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3068
               (m = ws.length - 1) >= 0) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3069
            if (!found) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3070
                if ((System.nanoTime() - startTime) > nanos)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3071
                    return false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3072
                Thread.yield(); // cannot block
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3073
            }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3074
            found = false;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3075
            for (int j = (m + 1) << 2; j >= 0; --j) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3076
                ForkJoinTask<?> t; WorkQueue q; int b;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3077
                if ((q = ws[r++ & m]) != null && (b = q.base) - q.top < 0) {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3078
                    found = true;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3079
                    if ((t = q.pollAt(b)) != null)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3080
                        t.doExec();
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3081
                    break;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3082
                }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3083
            }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3084
        }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3085
        return true;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3086
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3087
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3088
    /**
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3089
     * Waits and/or attempts to assist performing tasks indefinitely
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3090
     * until the {@link #commonPool()} {@link #isQuiescent}.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3091
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3092
    static void quiesceCommonPool() {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3093
        common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3094
    }
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3095
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3096
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3097
     * Interface for extending managed parallelism for tasks running
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3098
     * in {@link ForkJoinPool}s.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3099
     *
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3100
     * <p>A {@code ManagedBlocker} provides two methods.  Method
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3101
     * {@code isReleasable} must return {@code true} if blocking is
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3102
     * not necessary. Method {@code block} blocks the current thread
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3103
     * if necessary (perhaps internally invoking {@code isReleasable}
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3104
     * before actually blocking). These actions are performed by any
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3105
     * thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3106
     * The unusual methods in this API accommodate synchronizers that
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3107
     * 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
  3108
     * allow more efficient internal handling of cases in which
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3109
     * additional workers may be, but usually are not, needed to
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3110
     * ensure sufficient parallelism.  Toward this end,
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3111
     * implementations of method {@code isReleasable} must be amenable
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3112
     * to repeated invocation.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3113
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3114
     * <p>For example, here is a ManagedBlocker based on a
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3115
     * ReentrantLock:
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3116
     *  <pre> {@code
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3117
     * class ManagedLocker implements ManagedBlocker {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3118
     *   final ReentrantLock lock;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3119
     *   boolean hasLock = false;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3120
     *   ManagedLocker(ReentrantLock lock) { this.lock = lock; }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3121
     *   public boolean block() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3122
     *     if (!hasLock)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3123
     *       lock.lock();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3124
     *     return true;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3125
     *   }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3126
     *   public boolean isReleasable() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3127
     *     return hasLock || (hasLock = lock.tryLock());
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3128
     *   }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3129
     * }}</pre>
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3130
     *
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3131
     * <p>Here is a class that possibly blocks waiting for an
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3132
     * item on a given queue:
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3133
     *  <pre> {@code
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3134
     * class QueueTaker<E> implements ManagedBlocker {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3135
     *   final BlockingQueue<E> queue;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3136
     *   volatile E item = null;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3137
     *   QueueTaker(BlockingQueue<E> q) { this.queue = q; }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3138
     *   public boolean block() throws InterruptedException {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3139
     *     if (item == null)
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3140
     *       item = queue.take();
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3141
     *     return true;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3142
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3143
     *   public boolean isReleasable() {
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3144
     *     return item != null || (item = queue.poll()) != null;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3145
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3146
     *   public E getItem() { // call after pool.managedBlock completes
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3147
     *     return item;
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3148
     *   }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3149
     * }}</pre>
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3150
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3151
    public static interface ManagedBlocker {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3152
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3153
         * Possibly blocks the current thread, for example waiting for
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3154
         * a lock or condition.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3155
         *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3156
         * @return {@code true} if no additional blocking is necessary
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3157
         * (i.e., if isReleasable would return true)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3158
         * @throws InterruptedException if interrupted while waiting
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3159
         * (the method is not required to do so, but is allowed to)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3160
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3161
        boolean block() throws InterruptedException;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3162
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3163
        /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3164
         * Returns {@code true} if blocking is unnecessary.
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3165
         * @return {@code true} if blocking is unnecessary
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3166
         */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3167
        boolean isReleasable();
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3168
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3169
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3170
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3171
     * Blocks in accord with the given blocker.  If the current thread
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3172
     * is a {@link ForkJoinWorkerThread}, this method possibly
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3173
     * arranges for a spare thread to be activated if necessary to
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3174
     * ensure sufficient parallelism while the current thread is blocked.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3175
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3176
     * <p>If the caller is not a {@link ForkJoinTask}, this method is
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3177
     * behaviorally equivalent to
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3178
     *  <pre> {@code
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3179
     * while (!blocker.isReleasable())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3180
     *   if (blocker.block())
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3181
     *     return;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3182
     * }</pre>
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3183
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3184
     * If the caller is a {@code ForkJoinTask}, then the pool may
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3185
     * first be expanded to ensure parallelism, and later adjusted.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3186
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3187
     * @param blocker the blocker
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3188
     * @throws InterruptedException if blocker.block did so
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3189
     */
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3190
    public static void managedBlock(ManagedBlocker blocker)
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3191
        throws InterruptedException {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3192
        Thread t = Thread.currentThread();
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3193
        if (t instanceof ForkJoinWorkerThread) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3194
            ForkJoinPool p = ((ForkJoinWorkerThread)t).pool;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3195
            while (!blocker.isReleasable()) {
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3196
                if (p.tryCompensate(p.ctl)) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3197
                    try {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3198
                        do {} while (!blocker.isReleasable() &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3199
                                     !blocker.block());
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3200
                    } finally {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3201
                        p.incrementActiveCount();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3202
                    }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3203
                    break;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3204
                }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3205
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3206
        }
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3207
        else {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3208
            do {} while (!blocker.isReleasable() &&
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3209
                         !blocker.block());
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
  3210
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3211
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3212
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3213
    // AbstractExecutorService overrides.  These rely on undocumented
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3214
    // fact that ForkJoinTask.adapt returns ForkJoinTasks that also
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3215
    // implement RunnableFuture.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3216
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3217
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3218
        return new ForkJoinTask.AdaptedRunnable<T>(runnable, value);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3219
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3220
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3221
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3222
        return new ForkJoinTask.AdaptedCallable<T>(callable);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3223
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3224
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3225
    // Unsafe mechanics
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3226
    private static final sun.misc.Unsafe U;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3227
    private static final long CTL;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3228
    private static final long PARKBLOCKER;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3229
    private static final int ABASE;
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3230
    private static final int ASHIFT;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3231
    private static final long STEALCOUNT;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3232
    private static final long PLOCK;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3233
    private static final long INDEXSEED;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3234
    private static final long QBASE;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3235
    private static final long QLOCK;
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3236
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3237
    static {
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3238
        // initialize field offsets for CAS etc
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3239
        try {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3240
            U = sun.misc.Unsafe.getUnsafe();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9278
diff changeset
  3241
            Class<?> k = ForkJoinPool.class;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3242
            CTL = U.objectFieldOffset
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3243
                (k.getDeclaredField("ctl"));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3244
            STEALCOUNT = U.objectFieldOffset
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3245
                (k.getDeclaredField("stealCount"));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3246
            PLOCK = U.objectFieldOffset
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3247
                (k.getDeclaredField("plock"));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3248
            INDEXSEED = U.objectFieldOffset
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3249
                (k.getDeclaredField("indexSeed"));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3250
            Class<?> tk = Thread.class;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3251
            PARKBLOCKER = U.objectFieldOffset
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3252
                (tk.getDeclaredField("parkBlocker"));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3253
            Class<?> wk = WorkQueue.class;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3254
            QBASE = U.objectFieldOffset
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3255
                (wk.getDeclaredField("base"));
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3256
            QLOCK = U.objectFieldOffset
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3257
                (wk.getDeclaredField("qlock"));
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3258
            Class<?> ak = ForkJoinTask[].class;
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3259
            ABASE = U.arrayBaseOffset(ak);
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3260
            int scale = U.arrayIndexScale(ak);
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3261
            if ((scale & (scale - 1)) != 0)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3262
                throw new Error("data type scale not a power of two");
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3263
            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3264
        } catch (Exception e) {
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3265
            throw new Error(e);
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3266
        }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3267
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3268
        defaultForkJoinWorkerThreadFactory =
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3269
            new DefaultForkJoinWorkerThreadFactory();
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3270
        modifyThreadPermission = new RuntimePermission("modifyThread");
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3271
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3272
        common = java.security.AccessController.doPrivileged
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3273
            (new java.security.PrivilegedAction<ForkJoinPool>() {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3274
                public ForkJoinPool run() { return makeCommonPool(); }});
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3275
        int par = common.parallelism; // report 1 even if threads disabled
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3276
        commonParallelism = par > 0 ? par : 1;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3277
    }
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3278
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3279
    /**
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3280
     * Creates and returns the common pool, respecting user settings
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3281
     * specified via system properties.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3282
     */
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3283
    private static ForkJoinPool makeCommonPool() {
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3284
        int parallelism = -1;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3285
        ForkJoinWorkerThreadFactory factory
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3286
            = defaultForkJoinWorkerThreadFactory;
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3287
        UncaughtExceptionHandler handler = null;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3288
        try {  // ignore exceptions in accessing/parsing properties
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3289
            String pp = System.getProperty
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3290
                ("java.util.concurrent.ForkJoinPool.common.parallelism");
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3291
            String fp = System.getProperty
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3292
                ("java.util.concurrent.ForkJoinPool.common.threadFactory");
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3293
            String hp = System.getProperty
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3294
                ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3295
            if (pp != null)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3296
                parallelism = Integer.parseInt(pp);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3297
            if (fp != null)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3298
                factory = ((ForkJoinWorkerThreadFactory)ClassLoader.
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3299
                           getSystemClassLoader().loadClass(fp).newInstance());
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3300
            if (hp != null)
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3301
                handler = ((UncaughtExceptionHandler)ClassLoader.
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3302
                           getSystemClassLoader().loadClass(hp).newInstance());
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3303
        } catch (Exception ignore) {
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3304
        }
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
  3305
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3306
        if (parallelism < 0 && // default 1 less than #cores
19412
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
  3307
            (parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0)
e7847bc5763b 8023103: FJ parallelism zero
dl
parents: 19048
diff changeset
  3308
            parallelism = 1;
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3309
        if (parallelism > MAX_CAP)
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3310
            parallelism = MAX_CAP;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 16049
diff changeset
  3311
        return new ForkJoinPool(parallelism, factory, handler, LIFO_QUEUE,
16049
92a3a919d4dc 8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents: 15002
diff changeset
  3312
                                "ForkJoinPool.commonPool-worker-");
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
  3313
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3314
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
  3315
}