1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import java.security.AccessControlContext;
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.security.ProtectionDomain;
  31 import sun.security.util.SecurityConstants;
  32 
  33 /**
  34  * Represents a Thread returned by Thread.currentThread() when invoked in the
  35  * context of a Fiber.
  36  */
  37 
  38 class ShadowThread extends Thread {
  39     private static final ThreadGroup SHADOW_THREAD_GROUP = shadowThreadGroup();
  40     private static final AccessControlContext INNOCUOUS_ACC = innocuousACC();
  41 
  42     // the Fiber that this object shadows
  43     private final Fiber<?> fiber;
  44 
  45     /**
  46      * Creates a shadow thread for the given fiber using the given inheritable
  47      * context.
  48      *
  49      * All shadow threads are in the same thread group and use the same innocuous
  50      * access control context.
  51      */
  52     ShadowThread(Fiber<?> fiber, InheritableThreadContext ctxt) {
  53         super(SHADOW_THREAD_GROUP,
  54                 "Fiber",
  55                 ctxt.contextClassLoader(),
  56                 ctxt.inheritableThreadLocals(),
  57                 INNOCUOUS_ACC);
  58         this.fiber = fiber;
  59     }
  60 
  61     Fiber<?> fiber() {
  62         return fiber;
  63     }
  64 
  65     @Override
  66     public void start() {
  67         throw new IllegalStateException();
  68     }
  69 
  70     @Override
  71     public void interrupt() {
  72         if (Thread.currentThread() != this) {
  73             checkAccess();
  74         }
  75         fiber.interrupt();
  76     }
  77 
  78     @Override
  79     public boolean isInterrupted() {
  80         return fiber.isInterrupted();
  81     }
  82 
  83     @Override
  84     public StackTraceElement[] getStackTrace() {
  85         SecurityManager sm;
  86         if (this != Thread.currentThread()
  87             && ((sm = System.getSecurityManager()) != null)) {
  88             sm.checkPermission(SecurityConstants.GET_STACK_TRACE_PERMISSION);
  89         }
  90         return fiber.getStackTrace();
  91     }
  92 
  93     @Override
  94     public Thread.State getState() {
  95         return fiber.getState();
  96     }
  97 
  98     /**
  99      * The thread group for the shadow threads.
 100      */
 101     private static ThreadGroup shadowThreadGroup() {
 102         return AccessController.doPrivileged(new PrivilegedAction<ThreadGroup>() {
 103             public ThreadGroup run() {
 104                 ThreadGroup group = Thread.currentCarrierThread().getThreadGroup();
 105                 for (ThreadGroup p; (p = group.getParent()) != null; )
 106                     group = p;
 107                 return new ThreadGroup(group, "ShadowThreads");
 108             }});
 109     }
 110 
 111     /**
 112      * Return an AccessControlContext that doesn't support any permissions.
 113      */
 114     private static AccessControlContext innocuousACC() {
 115         return new AccessControlContext(new ProtectionDomain[] {
 116             new ProtectionDomain(null, null)
 117         });
 118     }
 119 }