jdk/src/java.base/share/classes/sun/misc/InnocuousThread.java
changeset 35813 2fc9abeff0d5
parent 35812 6a6ca0bd3c14
parent 35805 a460a860cdb0
child 35814 6c644cca3f3f
--- a/jdk/src/java.base/share/classes/sun/misc/InnocuousThread.java	Fri Feb 12 10:51:36 2016 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,137 +0,0 @@
-/*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package sun.misc;
-
-import java.security.AccessControlContext;
-import java.security.AccessController;
-import java.security.ProtectionDomain;
-import java.security.PrivilegedAction;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * A thread that has no permissions, is not a member of any user-defined
- * ThreadGroup and supports the ability to erase ThreadLocals.
- */
-public final class InnocuousThread extends Thread {
-    private static final jdk.internal.misc.Unsafe UNSAFE;
-    private static final long THREAD_LOCALS;
-    private static final long INHERITABLE_THREAD_LOCALS;
-    private static final ThreadGroup INNOCUOUSTHREADGROUP;
-    private static final AccessControlContext ACC;
-    private static final long INHERITEDACCESSCONTROLCONTEXT;
-    private static final long CONTEXTCLASSLOADER;
-
-    private static final AtomicInteger threadNumber = new AtomicInteger(1);
-
-    public InnocuousThread(Runnable target) {
-        this(INNOCUOUSTHREADGROUP, target,
-             "InnocuousThread-" + threadNumber.getAndIncrement());
-    }
-
-    public InnocuousThread(Runnable target, String name) {
-        this(INNOCUOUSTHREADGROUP, target, name);
-    }
-
-    public InnocuousThread(ThreadGroup group, Runnable target, String name) {
-        super(group, target, name, 0L, false);
-        UNSAFE.putOrderedObject(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
-        UNSAFE.putOrderedObject(this, CONTEXTCLASSLOADER, ClassLoader.getSystemClassLoader());
-    }
-
-    @Override
-    public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) {
-        // silently fail
-    }
-
-    @Override
-    public void setContextClassLoader(ClassLoader cl) {
-        // Allow clearing of the TCCL to remove the reference to the system classloader.
-        if (cl == null)
-            super.setContextClassLoader(null);
-        else
-            throw new SecurityException("setContextClassLoader");
-    }
-
-    /**
-     * Drops all thread locals (and inherited thread locals).
-     */
-    public final void eraseThreadLocals() {
-        UNSAFE.putObject(this, THREAD_LOCALS, null);
-        UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
-    }
-
-    // ensure run method is run only once
-    private volatile boolean hasRun;
-
-    @Override
-    public void run() {
-        if (Thread.currentThread() == this && !hasRun) {
-            hasRun = true;
-            super.run();
-        }
-    }
-
-    // Use Unsafe to access Thread group and ThreadGroup parent fields
-    static {
-        try {
-            ACC = new AccessControlContext(new ProtectionDomain[] {
-                new ProtectionDomain(null, null)
-            });
-
-            // Find and use topmost ThreadGroup as parent of new group
-            UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
-            Class<?> tk = Thread.class;
-            Class<?> gk = ThreadGroup.class;
-
-            THREAD_LOCALS = UNSAFE.objectFieldOffset
-                    (tk.getDeclaredField("threadLocals"));
-            INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
-                    (tk.getDeclaredField("inheritableThreadLocals"));
-            INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset
-                (tk.getDeclaredField("inheritedAccessControlContext"));
-            CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset
-                (tk.getDeclaredField("contextClassLoader"));
-
-            long tg = UNSAFE.objectFieldOffset(tk.getDeclaredField("group"));
-            long gp = UNSAFE.objectFieldOffset(gk.getDeclaredField("parent"));
-            ThreadGroup group = (ThreadGroup)
-                UNSAFE.getObject(Thread.currentThread(), tg);
-
-            while (group != null) {
-                ThreadGroup parent = (ThreadGroup)UNSAFE.getObject(group, gp);
-                if (parent == null)
-                    break;
-                group = parent;
-            }
-            final ThreadGroup root = group;
-            INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
-                (PrivilegedAction<ThreadGroup>) () ->
-                    { return new ThreadGroup(root, "InnocuousThreadGroup"); });
-        } catch (Exception e) {
-            throw new Error(e);
-        }
-    }
-}