jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java
author ihse
Fri, 12 Jun 2015 08:31:01 +0200
changeset 33957 39113ae98993
parent 29920 f81c14f472ab
child 31653 d88ff422c7fb
permissions -rw-r--r--
8085822: JEP 223: New Version-String Scheme (initial integration) Reviewed-by: erikj, dcubed, dholmes, alanb Contributed-by: Magnus Ihse Bursie <magnus.ihse.bursie@oracle.com>, Alejandro E Murillo <alejandro.murillo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     1
/*
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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 {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    33
    private static final Unsafe UNSAFE;
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
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    51
    public ManagedLocalsThread(Runnable target, String name) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    52
        super(target, name);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    53
        eraseThreadLocals();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    54
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    55
29919
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    56
    public ManagedLocalsThread(ThreadGroup group, String name) {
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    57
        super(group, name);
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    58
        eraseThreadLocals();
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    59
    }
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29904
diff changeset
    60
29904
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    61
    public ManagedLocalsThread(ThreadGroup group, Runnable target, String name) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    62
        super(group, target, name);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    63
        eraseThreadLocals();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    64
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    65
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    66
    /**
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    67
     * Drops all thread locals (and inherited thread locals).
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    68
     */
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    69
    public final void eraseThreadLocals() {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    70
        UNSAFE.putObject(this, THREAD_LOCALS, null);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    71
        UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    72
    }
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
    static {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    75
        UNSAFE = Unsafe.getUnsafe();
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    76
        Class<?> t = Thread.class;
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    77
        try {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    78
            THREAD_LOCALS = UNSAFE.objectFieldOffset
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    79
                (t.getDeclaredField("threadLocals"));
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    80
            INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    81
                (t.getDeclaredField("inheritableThreadLocals"));
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    82
        } catch (Exception e) {
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    83
            throw new Error(e);
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    84
        }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    85
    }
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    86
}
d5f909aa26bc 8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
diff changeset
    87