jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinWorkerThread.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 39723 9aa34e4a0469
child 43219 566b551b7820
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    38
import java.security.AccessControlContext;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    39
import java.security.ProtectionDomain;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    40
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    41
/**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    42
 * A thread managed by a {@link ForkJoinPool}, which executes
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    43
 * {@link ForkJoinTask}s.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    44
 * This class is subclassable solely for the sake of adding
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    45
 * functionality -- there are no overridable methods dealing with
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    46
 * scheduling or execution.  However, you can override initialization
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    47
 * and termination methods surrounding the main task processing loop.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    48
 * If you do create such a subclass, you will also need to supply a
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    49
 * custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    50
 * {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    51
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    52
 * @since 1.7
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    53
 * @author Doug Lea
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    54
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    55
public class ForkJoinWorkerThread extends Thread {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    56
    /*
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    57
     * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    58
     * ForkJoinTasks. For explanation, see the internal documentation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    59
     * of class ForkJoinPool.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    60
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    61
     * This class just maintains links to its pool and WorkQueue.  The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    62
     * pool field is set immediately upon construction, but the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    63
     * workQueue field is not set until a call to registerWorker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    64
     * completes. This leads to a visibility race, that is tolerated
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    65
     * by requiring that the workQueue field is only accessed by the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    66
     * owning thread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    67
     *
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    68
     * Support for (non-public) subclass InnocuousForkJoinWorkerThread
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    69
     * requires that we break quite a lot of encapsulation (via helper
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    70
     * methods in ThreadLocalRandom) both here and in the subclass to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    71
     * access and set Thread fields.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    72
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    73
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    74
    final ForkJoinPool pool;                // the pool this thread works in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    75
    final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    76
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    77
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    78
     * Creates a ForkJoinWorkerThread operating in the given pool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    79
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    80
     * @param pool the pool this thread works in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    81
     * @throws NullPointerException if pool is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    82
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    83
    protected ForkJoinWorkerThread(ForkJoinPool pool) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    84
        // Use a placeholder until a useful name can be set in registerWorker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    85
        super("aForkJoinWorkerThread");
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    86
        this.pool = pool;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    87
        this.workQueue = pool.registerWorker(this);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    88
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    89
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    90
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
    91
     * Version for InnocuousForkJoinWorkerThread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    92
     */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    93
    ForkJoinWorkerThread(ForkJoinPool pool, ThreadGroup threadGroup,
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    94
                         AccessControlContext acc) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    95
        super(threadGroup, null, "aForkJoinWorkerThread");
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    96
        ThreadLocalRandom.setInheritedAccessControlContext(this, acc);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    97
        ThreadLocalRandom.eraseThreadLocals(this); // clear before registering
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    98
        this.pool = pool;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    99
        this.workQueue = pool.registerWorker(this);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   100
    }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   101
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   102
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   103
     * Returns the pool hosting this thread.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   104
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   105
     * @return the pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   106
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   107
    public ForkJoinPool getPool() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   108
        return pool;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   109
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   111
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   112
     * Returns the unique index number of this thread in its pool.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   113
     * The returned value ranges from zero to the maximum number of
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   114
     * threads (minus one) that may exist in the pool, and does not
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   115
     * change during the lifetime of the thread.  This method may be
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   116
     * useful for applications that track status or collect results
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   117
     * per-worker-thread rather than per-task.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   118
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   119
     * @return the index number
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   120
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   121
    public int getPoolIndex() {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   122
        return workQueue.getPoolIndex();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   123
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   124
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   125
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   126
     * Initializes internal state after construction but before
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   127
     * processing any tasks. If you override this method, you must
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   128
     * invoke {@code super.onStart()} at the beginning of the method.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   129
     * Initialization requires care: Most fields must have legal
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   130
     * default values, to ensure that attempted accesses from other
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   131
     * threads work correctly even before this thread starts
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   132
     * processing tasks.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   133
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   134
    protected void onStart() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   135
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   136
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   137
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   138
     * Performs cleanup associated with termination of this worker
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   139
     * thread.  If you override this method, you must invoke
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   140
     * {@code super.onTermination} at the end of the overridden method.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   141
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   142
     * @param exception the exception causing this thread to abort due
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   143
     * to an unrecoverable error, or {@code null} if completed normally
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   144
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   145
    protected void onTermination(Throwable exception) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   146
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   147
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   148
    /**
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   149
     * This method is required to be public, but should never be
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   150
     * called explicitly. It performs the main run loop to execute
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
   151
     * {@link ForkJoinTask}s.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   152
     */
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   153
    public void run() {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   154
        if (workQueue.array == null) { // only run once
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   155
            Throwable exception = null;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   156
            try {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   157
                onStart();
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   158
                pool.runWorker(workQueue);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   159
            } catch (Throwable ex) {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   160
                exception = ex;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   161
            } finally {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   162
                try {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   163
                    onTermination(exception);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   164
                } catch (Throwable ex) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   165
                    if (exception == null)
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   166
                        exception = ex;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   167
                } finally {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   168
                    pool.deregisterWorker(this, exception);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   169
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   170
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   171
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   172
    }
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   173
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   174
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   175
     * Non-public hook method for InnocuousForkJoinWorkerThread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   176
     */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   177
    void afterTopLevelExec() {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   178
    }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   179
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   180
    /**
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   181
     * A worker thread that has no permissions, is not a member of any
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   182
     * user-defined ThreadGroup, and erases all ThreadLocals after
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   183
     * running each top-level task.
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   184
     */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   185
    static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   186
        /** The ThreadGroup for all InnocuousForkJoinWorkerThreads */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   187
        private static final ThreadGroup innocuousThreadGroup =
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
   188
            ThreadLocalRandom.createThreadGroup("InnocuousForkJoinWorkerThreadGroup");
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   189
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   190
        /** An AccessControlContext supporting no privileges */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   191
        private static final AccessControlContext INNOCUOUS_ACC =
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   192
            new AccessControlContext(
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   193
                new ProtectionDomain[] {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   194
                    new ProtectionDomain(null, null)
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   195
                });
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   196
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   197
        InnocuousForkJoinWorkerThread(ForkJoinPool pool) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   198
            super(pool, innocuousThreadGroup, INNOCUOUS_ACC);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   199
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   200
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   201
        @Override // to erase ThreadLocals
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   202
        void afterTopLevelExec() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
   203
            ThreadLocalRandom.eraseThreadLocals(this);
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   204
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   205
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   206
        @Override // to always report system loader
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   207
        public ClassLoader getContextClassLoader() {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   208
            return ClassLoader.getSystemClassLoader();
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   209
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   210
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   211
        @Override // to silently fail
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   212
        public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   213
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   214
        @Override // paranoically
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   215
        public void setContextClassLoader(ClassLoader cl) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   216
            throw new SecurityException("setContextClassLoader");
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   217
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   218
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   219
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   220
}