author | mchung |
Mon, 22 Oct 2018 17:00:04 -0700 | |
changeset 52220 | 9c260a6b6471 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
29946 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 com.sun.org.apache.xml.internal.utils; |
|
26 |
||
27 |
/** |
|
28 |
* This is a combination of ThreadControllerWrapper's inner class SafeThread |
|
29 |
* that was introduced as a fix for CR 6607339 |
|
30 |
* and sun.misc.ManagedLocalsThread, a thread that has it's thread locals, and |
|
31 |
* inheritable thread locals erased on construction. Except the run method, |
|
32 |
* it is identical to sun.misc.ManagedLocalsThread. |
|
33 |
*/ |
|
34 |
public class SafeThread extends Thread { |
|
35 |
||
33699
26c7c103b4d9
8140606: Update library code to use internal Unsafe
chegar
parents:
29946
diff
changeset
|
36 |
private static final jdk.internal.misc.Unsafe UNSAFE; |
29946 | 37 |
private static final long THREAD_LOCALS; |
38 |
private static final long INHERITABLE_THREAD_LOCALS; |
|
39 |
||
40 |
private volatile boolean ran = false; |
|
41 |
||
42 |
public SafeThread(Runnable target) { |
|
43 |
super(target); |
|
44 |
eraseThreadLocals(); |
|
45 |
} |
|
46 |
||
47 |
public SafeThread(Runnable target, String name) { |
|
48 |
super(target, name); |
|
49 |
eraseThreadLocals(); |
|
50 |
} |
|
51 |
||
52 |
public SafeThread(ThreadGroup group, Runnable target, String name) { |
|
53 |
super(group, target, name); |
|
54 |
eraseThreadLocals(); |
|
55 |
} |
|
56 |
||
57 |
public final void run() { |
|
58 |
if (Thread.currentThread() != this) { |
|
59 |
throw new IllegalStateException("The run() method in a" |
|
60 |
+ " SafeThread cannot be called from another thread."); |
|
61 |
} |
|
62 |
synchronized (this) { |
|
63 |
if (!ran) { |
|
64 |
ran = true; |
|
65 |
} else { |
|
66 |
throw new IllegalStateException("The run() method in a" |
|
67 |
+ " SafeThread cannot be called more than once."); |
|
68 |
} |
|
69 |
} |
|
70 |
super.run(); |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Drops all thread locals (and inherited thread locals). |
|
75 |
*/ |
|
76 |
public final void eraseThreadLocals() { |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
47216
diff
changeset
|
77 |
UNSAFE.putReference(this, THREAD_LOCALS, null); |
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
47216
diff
changeset
|
78 |
UNSAFE.putReference(this, INHERITABLE_THREAD_LOCALS, null); |
29946 | 79 |
} |
80 |
||
81 |
static { |
|
33699
26c7c103b4d9
8140606: Update library code to use internal Unsafe
chegar
parents:
29946
diff
changeset
|
82 |
UNSAFE = jdk.internal.misc.Unsafe.getUnsafe(); |
29946 | 83 |
Class<?> t = Thread.class; |
84 |
try { |
|
85 |
THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("threadLocals")); |
|
86 |
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("inheritableThreadLocals")); |
|
87 |
} catch (Exception e) { |
|
88 |
throw new Error(e); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
} |