jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinWorkerThread.java
author dl
Fri, 03 Mar 2017 10:45:38 -0800
changeset 44039 058585425bb7
parent 43545 fdecd1d14d53
child 44589 64d9270bd24c
permissions -rw-r--r--
8173909: Miscellaneous changes imported from jsr166 CVS 2017-03 Reviewed-by: martin, 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;
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    39
import java.security.AccessController;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    40
import java.security.PrivilegedAction;
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    41
import java.security.ProtectionDomain;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    42
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    43
/**
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    44
 * 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
    45
 * {@link ForkJoinTask}s.
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 6674
diff changeset
    46
 * 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
    47
 * 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
    48
 * 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
    49
 * 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
    50
 * 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
    51
 * custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
    52
 * {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    53
 *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    54
 * @since 1.7
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    55
 * @author Doug Lea
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    56
 */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    57
public class ForkJoinWorkerThread extends Thread {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    58
    /*
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
    59
     * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    60
     * ForkJoinTasks. For explanation, see the internal documentation
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    61
     * of class ForkJoinPool.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    62
     *
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    63
     * This class just maintains links to its pool and WorkQueue.  The
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    64
     * pool field is set immediately upon construction, but the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    65
     * workQueue field is not set until a call to registerWorker
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    66
     * completes. This leads to a visibility race, that is tolerated
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    67
     * by requiring that the workQueue field is only accessed by the
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    68
     * owning thread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    69
     *
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
    70
     * Support for (non-public) subclass InnocuousForkJoinWorkerThread
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    71
     * requires that we break quite a lot of encapsulation (via helper
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    72
     * methods in ThreadLocalRandom) both here and in the subclass to
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
    73
     * access and set Thread fields.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    74
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    75
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    76
    final ForkJoinPool pool;                // the pool this thread works in
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    77
    final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    78
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    79
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    80
     * Creates a ForkJoinWorkerThread operating in the given pool.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    81
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    82
     * @param pool the pool this thread works in
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    83
     * @throws NullPointerException if pool is null
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    84
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    85
    protected ForkJoinWorkerThread(ForkJoinPool pool) {
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    86
        // 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
    87
        super("aForkJoinWorkerThread");
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    88
        this.pool = pool;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
    89
        this.workQueue = pool.registerWorker(this);
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    90
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    91
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
    92
    /**
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    93
     * Version for use by the default pool.  Supports setting the
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    94
     * context class loader.  This is a separate constructor to avoid
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    95
     * affecting the protected constructor.
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    96
     */
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    97
    ForkJoinWorkerThread(ForkJoinPool pool, ClassLoader ccl) {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    98
        super("aForkJoinWorkerThread");
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
    99
        super.setContextClassLoader(ccl);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   100
        this.pool = pool;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   101
        this.workQueue = pool.registerWorker(this);
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   102
    }
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   103
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   104
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   105
     * Version for InnocuousForkJoinWorkerThread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   106
     */
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   107
    ForkJoinWorkerThread(ForkJoinPool pool,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   108
                         ClassLoader ccl,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   109
                         ThreadGroup threadGroup,
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   110
                         AccessControlContext acc) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   111
        super(threadGroup, null, "aForkJoinWorkerThread");
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   112
        super.setContextClassLoader(ccl);
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
   113
        ThreadLocalRandom.setInheritedAccessControlContext(this, acc);
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
   114
        ThreadLocalRandom.eraseThreadLocals(this); // clear before registering
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   115
        this.pool = pool;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   116
        this.workQueue = pool.registerWorker(this);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   117
    }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   118
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   119
    /**
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   120
     * Returns the pool hosting this thread.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   121
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   122
     * @return the pool
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   123
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   124
    public ForkJoinPool getPool() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   125
        return pool;
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   126
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   127
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   128
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14914
diff changeset
   129
     * 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
   130
     * 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
   131
     * 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
   132
     * 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
   133
     * 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
   134
     * per-worker-thread rather than per-task.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   135
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   136
     * @return the index number
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   137
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   138
    public int getPoolIndex() {
26448
5853628b0e63 8056248: Improve ForkJoin thread throttling
dl
parents: 25859
diff changeset
   139
        return workQueue.getPoolIndex();
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   140
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   141
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   142
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   143
     * Initializes internal state after construction but before
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   144
     * 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
   145
     * invoke {@code super.onStart()} at the beginning of the method.
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   146
     * Initialization requires care: Most fields must have legal
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   147
     * default values, to ensure that attempted accesses from other
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   148
     * threads work correctly even before this thread starts
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   149
     * processing tasks.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   150
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   151
    protected void onStart() {
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   152
    }
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   153
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   154
    /**
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   155
     * Performs cleanup associated with termination of this worker
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   156
     * thread.  If you override this method, you must invoke
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   157
     * {@code super.onTermination} at the end of the overridden method.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   158
     *
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   159
     * @param exception the exception causing this thread to abort due
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   160
     * to an unrecoverable error, or {@code null} if completed normally
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   161
     */
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   162
    protected void onTermination(Throwable exception) {
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   163
    }
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   164
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   165
    /**
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   166
     * This method is required to be public, but should never be
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   167
     * 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
   168
     * {@link ForkJoinTask}s.
6543
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   169
     */
c06e5f2c6bb1 6978087: jsr166y Updates
dl
parents: 5506
diff changeset
   170
    public void run() {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   171
        if (workQueue.array == null) { // only run once
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   172
            Throwable exception = null;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   173
            try {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   174
                onStart();
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   175
                pool.runWorker(workQueue);
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   176
            } catch (Throwable ex) {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   177
                exception = ex;
14914
69bfd88e3ea1 8002356: Add ForkJoin common pool and CountedCompleter
dl
parents: 11279
diff changeset
   178
            } finally {
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   179
                try {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   180
                    onTermination(exception);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   181
                } catch (Throwable ex) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   182
                    if (exception == null)
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   183
                        exception = ex;
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   184
                } finally {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   185
                    pool.deregisterWorker(this, exception);
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   186
                }
8765
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   187
            }
dfc2a131d08a 7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents: 7976
diff changeset
   188
        }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   189
    }
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   190
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   191
    /**
32988
da3715f8eec3 8134852: Integrate fork/join with API enhancements
dl
parents: 26448
diff changeset
   192
     * Non-public hook method for InnocuousForkJoinWorkerThread.
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   193
     */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   194
    void afterTopLevelExec() {
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
    /**
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   198
     * A worker thread that has no permissions, is not a member of any
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   199
     * user-defined ThreadGroup, uses the system class loader as
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   200
     * thread context class loader, and erases all ThreadLocals after
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   201
     * running each top-level task.
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   202
     */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   203
    static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   204
        /** The ThreadGroup for all InnocuousForkJoinWorkerThreads */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   205
        private static final ThreadGroup innocuousThreadGroup =
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   206
            java.security.AccessController.doPrivileged(
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   207
                new java.security.PrivilegedAction<>() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   208
                    public ThreadGroup run() {
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   209
                        ThreadGroup group = Thread.currentThread().getThreadGroup();
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   210
                        for (ThreadGroup p; (p = group.getParent()) != null; )
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   211
                            group = p;
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   212
                        return new ThreadGroup(group, "InnocuousForkJoinWorkerThreadGroup");
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   213
                    }});
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   214
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   215
        /** An AccessControlContext supporting no privileges */
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   216
        private static final AccessControlContext INNOCUOUS_ACC =
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   217
            new AccessControlContext(
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   218
                new ProtectionDomain[] {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   219
                    new ProtectionDomain(null, null)
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   220
                });
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   221
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   222
        InnocuousForkJoinWorkerThread(ForkJoinPool pool) {
43545
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   223
            super(pool,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   224
                  ClassLoader.getSystemClassLoader(),
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   225
                  innocuousThreadGroup,
fdecd1d14d53 8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents: 43219
diff changeset
   226
                  INNOCUOUS_ACC);
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   227
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   228
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   229
        @Override // to erase ThreadLocals
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   230
        void afterTopLevelExec() {
39723
9aa34e4a0469 8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents: 36936
diff changeset
   231
            ThreadLocalRandom.eraseThreadLocals(this);
22070
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   232
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   233
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   234
        @Override // to silently fail
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   235
        public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   236
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   237
        @Override // paranoically
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   238
        public void setContextClassLoader(ClassLoader cl) {
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   239
            throw new SecurityException("setContextClassLoader");
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   240
        }
9a4f43c9b15a 8026155: Enhance ForkJoin pool
dl
parents: 18790
diff changeset
   241
    }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents:
diff changeset
   242
}