1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent;
  37 
  38 import java.security.AccessControlContext;
  39 import java.security.AccessController;
  40 import java.security.PrivilegedAction;
  41 import java.security.ProtectionDomain;
  42 
  43 /**
  44  * A thread managed by a {@link ForkJoinPool}, which executes
  45  * {@link ForkJoinTask}s.
  46  * This class is subclassable solely for the sake of adding
  47  * functionality -- there are no overridable methods dealing with
  48  * scheduling or execution.  However, you can override initialization
  49  * and termination methods surrounding the main task processing loop.
  50  * If you do create such a subclass, you will also need to supply a
  51  * custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to
  52  * {@linkplain ForkJoinPool#ForkJoinPool(int, ForkJoinWorkerThreadFactory,
  53  * UncaughtExceptionHandler, boolean, int, int, int, Predicate, long, TimeUnit)
  54  * use it} in a {@code ForkJoinPool}.
  55  *
  56  * @since 1.7
  57  * @author Doug Lea
  58  */
  59 public class ForkJoinWorkerThread extends Thread {
  60     /*
  61      * ForkJoinWorkerThreads are managed by ForkJoinPools and perform
  62      * ForkJoinTasks. For explanation, see the internal documentation
  63      * of class ForkJoinPool.
  64      *
  65      * This class just maintains links to its pool and WorkQueue.  The
  66      * pool field is set immediately upon construction, but the
  67      * workQueue field is not set until a call to registerWorker
  68      * completes. This leads to a visibility race, that is tolerated
  69      * by requiring that the workQueue field is only accessed by the
  70      * owning thread.
  71      *
  72      * Support for (non-public) subclass InnocuousForkJoinWorkerThread
  73      * requires that we break quite a lot of encapsulation (via helper
  74      * methods in ThreadLocalRandom) both here and in the subclass to
  75      * access and set Thread fields.
  76      */
  77 
  78     final ForkJoinPool pool;                // the pool this thread works in
  79     final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics
  80 
  81     /**
  82      * Creates a ForkJoinWorkerThread operating in the given thread group and
  83      * pool.
  84      *
  85      * @param group thread group, can be null
  86      * @param pool the pool this thread works in
  87      * @throws NullPointerException if pool is null
  88      */
  89     protected ForkJoinWorkerThread(ThreadGroup group, ForkJoinPool pool) {
  90         // Use a placeholder until a useful name can be set in registerWorker
  91         super(group, "aForkJoinWorkerThread");
  92         this.pool = pool;
  93         this.workQueue = pool.registerWorker(this);
  94     }
  95 
  96     /**
  97      * Creates a ForkJoinWorkerThread operating in the given pool.
  98      *
  99      * @param pool the pool this thread works in
 100      * @throws NullPointerException if pool is null
 101      */
 102     protected ForkJoinWorkerThread(ForkJoinPool pool) {
 103         this(null, pool);
 104     }
 105 
 106     /**
 107      * Version for use by the default pool.  Supports setting the
 108      * context class loader.  This is a separate constructor to avoid
 109      * affecting the protected constructor.
 110      */
 111     ForkJoinWorkerThread(ForkJoinPool pool, ClassLoader ccl) {
 112         super("aForkJoinWorkerThread");
 113         super.setContextClassLoader(ccl);
 114         this.pool = pool;
 115         this.workQueue = pool.registerWorker(this);
 116     }
 117 
 118     /**
 119      * Version for InnocuousForkJoinWorkerThread.
 120      */
 121     ForkJoinWorkerThread(ForkJoinPool pool,
 122                          ClassLoader ccl,
 123                          ThreadGroup threadGroup,
 124                          AccessControlContext acc) {
 125         super(threadGroup, null, "aForkJoinWorkerThread");
 126         super.setContextClassLoader(ccl);
 127         ThreadLocalRandom.setInheritedAccessControlContext(this, acc);
 128         ThreadLocalRandom.eraseThreadLocals(this); // clear before registering
 129         this.pool = pool;
 130         this.workQueue = pool.registerWorker(this);
 131     }
 132 
 133     /**
 134      * Returns the pool hosting this thread.
 135      *
 136      * @return the pool
 137      */
 138     public ForkJoinPool getPool() {
 139         return pool;
 140     }
 141 
 142     /**
 143      * Returns the unique index number of this thread in its pool.
 144      * The returned value ranges from zero to the maximum number of
 145      * threads (minus one) that may exist in the pool, and does not
 146      * change during the lifetime of the thread.  This method may be
 147      * useful for applications that track status or collect results
 148      * per-worker-thread rather than per-task.
 149      *
 150      * @return the index number
 151      */
 152     public int getPoolIndex() {
 153         return workQueue.getPoolIndex();
 154     }
 155 
 156     /**
 157      * Initializes internal state after construction but before
 158      * processing any tasks. If you override this method, you must
 159      * invoke {@code super.onStart()} at the beginning of the method.
 160      * Initialization requires care: Most fields must have legal
 161      * default values, to ensure that attempted accesses from other
 162      * threads work correctly even before this thread starts
 163      * processing tasks.
 164      */
 165     protected void onStart() {
 166     }
 167 
 168     /**
 169      * Performs cleanup associated with termination of this worker
 170      * thread.  If you override this method, you must invoke
 171      * {@code super.onTermination} at the end of the overridden method.
 172      *
 173      * @param exception the exception causing this thread to abort due
 174      * to an unrecoverable error, or {@code null} if completed normally
 175      */
 176     protected void onTermination(Throwable exception) {
 177     }
 178 
 179     /**
 180      * This method is required to be public, but should never be
 181      * called explicitly. It performs the main run loop to execute
 182      * {@link ForkJoinTask}s.
 183      */
 184     public void run() {
 185         if (workQueue.array == null) { // only run once
 186             Throwable exception = null;
 187             try {
 188                 onStart();
 189                 pool.runWorker(workQueue);
 190             } catch (Throwable ex) {
 191                 exception = ex;
 192             } finally {
 193                 try {
 194                     onTermination(exception);
 195                 } catch (Throwable ex) {
 196                     if (exception == null)
 197                         exception = ex;
 198                 } finally {
 199                     pool.deregisterWorker(this, exception);
 200                 }
 201             }
 202         }
 203     }
 204 
 205     /**
 206      * Non-public hook method for InnocuousForkJoinWorkerThread.
 207      */
 208     void afterTopLevelExec() {
 209     }
 210 
 211     /**
 212      * A worker thread that has no permissions, is not a member of any
 213      * user-defined ThreadGroup, uses the system class loader as
 214      * thread context class loader, and erases all ThreadLocals after
 215      * running each top-level task.
 216      */
 217     static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread {
 218         /** The ThreadGroup for all InnocuousForkJoinWorkerThreads */
 219         private static final ThreadGroup innocuousThreadGroup =
 220             AccessController.doPrivileged(new PrivilegedAction<>() {
 221                 public ThreadGroup run() {
 222                     ThreadGroup group = Thread.currentThread().getThreadGroup();
 223                     for (ThreadGroup p; (p = group.getParent()) != null; )
 224                         group = p;
 225                     return new ThreadGroup(
 226                         group, "InnocuousForkJoinWorkerThreadGroup");
 227                 }});
 228 
 229         /** An AccessControlContext supporting no privileges */
 230         private static final AccessControlContext INNOCUOUS_ACC =
 231             new AccessControlContext(
 232                 new ProtectionDomain[] { new ProtectionDomain(null, null) });
 233 
 234         InnocuousForkJoinWorkerThread(ForkJoinPool pool) {
 235             super(pool,
 236                   ClassLoader.getSystemClassLoader(),
 237                   innocuousThreadGroup,
 238                   INNOCUOUS_ACC);
 239         }
 240 
 241         @Override // to erase ThreadLocals
 242         void afterTopLevelExec() {
 243             ThreadLocalRandom.eraseThreadLocals(this);
 244         }
 245 
 246         @Override // to silently fail
 247         public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { }
 248 
 249         @Override // paranoically
 250         public void setContextClassLoader(ClassLoader cl) {
 251             if (cl != null && ClassLoader.getSystemClassLoader() != cl)
 252                 throw new SecurityException("setContextClassLoader");
 253         }
 254     }
 255 }