jdk/src/jdk.unsupported/share/classes/sun/misc/ManagedLocalsThread.java
author chegar
Sun, 24 Apr 2016 08:44:36 +0100
changeset 37607 d402d68d1994
parent 36966 4209c9e19c45
permissions -rw-r--r--
8154919: Remove superfluous jdk.unsupported from tools/launcher/modules/limitmods/LimitModsTest.java Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     4
 *
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    10
 *
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    15
 * accompanied this code).
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    16
 *
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    20
 *
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    23
 * questions.
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    24
 */
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    25
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    26
package sun.misc;
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    27
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    28
/**
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    29
 * A thread that has it's thread locals, and inheritable thread
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    30
 * locals erased on construction.
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    31
 */
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    32
public class ManagedLocalsThread extends Thread {
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 31653
diff changeset
    33
    private static final jdk.internal.misc.Unsafe UNSAFE;
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    34
    private static final long THREAD_LOCALS;
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    35
    private static final long INHERITABLE_THREAD_LOCALS;
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    36
29919
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    37
    public ManagedLocalsThread() {
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    38
        eraseThreadLocals();
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    39
    }
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    40
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    41
    public ManagedLocalsThread(Runnable target) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    42
        super(target);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    43
        eraseThreadLocals();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    44
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    45
29920
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 29919
diff changeset
    46
    public ManagedLocalsThread(String name) {
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 29919
diff changeset
    47
        super(name);
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 29919
diff changeset
    48
        eraseThreadLocals();
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 29919
diff changeset
    49
    }
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 29919
diff changeset
    50
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
    51
    public ManagedLocalsThread(ThreadGroup group, Runnable target) {
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
    52
        super(group, target);
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
    53
        eraseThreadLocals();
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
    54
    }
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29920
diff changeset
    55
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    56
    public ManagedLocalsThread(Runnable target, String name) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    57
        super(target, name);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    58
        eraseThreadLocals();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    59
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    60
29919
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    61
    public ManagedLocalsThread(ThreadGroup group, String name) {
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    62
        super(group, name);
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    63
        eraseThreadLocals();
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    64
    }
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    65
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    66
    public ManagedLocalsThread(ThreadGroup group, Runnable target, String name) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    67
        super(group, target, name);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    68
        eraseThreadLocals();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    69
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    70
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    71
    /**
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    72
     * Drops all thread locals (and inherited thread locals).
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    73
     */
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    74
    public final void eraseThreadLocals() {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    75
        UNSAFE.putObject(this, THREAD_LOCALS, null);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    76
        UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    77
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    78
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    79
    static {
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 31653
diff changeset
    80
        UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    81
        Class<?> t = Thread.class;
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    82
        try {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    83
            THREAD_LOCALS = UNSAFE.objectFieldOffset
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    84
                (t.getDeclaredField("threadLocals"));
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    85
            INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    86
                (t.getDeclaredField("inheritableThreadLocals"));
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    87
        } catch (Exception e) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    88
            throw new Error(e);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    89
        }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    90
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    91
}
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    92