/
ThreadAccess.java
201 lines (175 loc) · 8.45 KB
/
ThreadAccess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Quasar: lightweight threads and actors for the JVM.
* Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.concurrent.util;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import static co.paralleluniverse.common.reflection.ReflectionUtil.accessible;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.security.*;
import java.util.*;
/**
*
* @author pron
*/
public class ThreadAccess {
private static final VarHandle TARGET;
private static final VarHandle THREAD_LOCALS;
private static final VarHandle INHERITABLE_THREAD_LOCALS;
private static final VarHandle CONTEXT_CLASS_LOADER;
private static final VarHandle INHERITED_ACCESS_CONTROL_CONTEXT;
private static final Constructor threadLocalMapConstructor;
private static final Constructor threadLocalMapInheritedConstructor;
// private static final Method threadLocalMapSet;
private static final Field threadLocalMapTableField;
private static final Field threadLocalMapSizeField;
private static final Field threadLocalMapThresholdField;
private static final Class threadLocalMapEntryClass;
private static final Constructor threadLocalMapEntryConstructor;
private static final Field threadLocalMapEntryValueField;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
l = MethodHandles.privateLookupIn(Thread.class, l);
TARGET = l.findVarHandle(Thread.class, "target", Runnable.class); // l.unreflectVarHandle(Thread.class.getDeclaredField("target"));
THREAD_LOCALS = l.unreflectVarHandle(Thread.class.getDeclaredField("threadLocals"));
INHERITABLE_THREAD_LOCALS = l.unreflectVarHandle(Thread.class.getDeclaredField("inheritableThreadLocals"));
CONTEXT_CLASS_LOADER = l.unreflectVarHandle(Thread.class.getDeclaredField("contextClassLoader"));
VarHandle _inheritedAccessControlContext = null;
try {
_inheritedAccessControlContext = l.unreflectVarHandle(Thread.class.getDeclaredField("inheritedAccessControlContext"));
} catch (NoSuchFieldException e) {
}
INHERITED_ACCESS_CONTROL_CONTEXT = _inheritedAccessControlContext;
Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
threadLocalMapConstructor = accessible(threadLocalMapClass.getDeclaredConstructor(ThreadLocal.class, Object.class));
threadLocalMapInheritedConstructor = accessible(threadLocalMapClass.getDeclaredConstructor(threadLocalMapClass));
// threadLocalMapSet = accessible(threadLocalMapClass.getDeclaredMethod("set", ThreadLocal.class, Object.class));
threadLocalMapTableField = accessible(threadLocalMapClass.getDeclaredField("table"));
threadLocalMapSizeField = accessible(threadLocalMapClass.getDeclaredField("size"));
threadLocalMapThresholdField = accessible(threadLocalMapClass.getDeclaredField("threshold"));
threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry");
threadLocalMapEntryConstructor = accessible(threadLocalMapEntryClass.getDeclaredConstructor(ThreadLocal.class, Object.class));
threadLocalMapEntryValueField = accessible(threadLocalMapEntryClass.getDeclaredField("value"));
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
public static Runnable getTarget(Thread thread) {
return (Runnable) TARGET.get(thread);
}
public static void setTarget(Thread thread, Runnable target) {
TARGET.set(thread, target);
}
public static Object getThreadLocals(Thread thread) {
return (Object) THREAD_LOCALS.get(thread);
}
public static void setThreadLocals(Thread thread, Object threadLocals) {
THREAD_LOCALS.set(thread, threadLocals);
}
public static Object getInheritableThreadLocals(Thread thread) {
return (Object) INHERITABLE_THREAD_LOCALS.get(thread);
}
public static void setInheritableThreadLocals(Thread thread, Object inheritableThreadLocals) {
INHERITABLE_THREAD_LOCALS.set(thread, inheritableThreadLocals);
}
private static Object createThreadLocalMap(ThreadLocal tl, Object firstValue) {
try {
return threadLocalMapConstructor.newInstance(tl, firstValue);
} catch (ReflectiveOperationException ex) {
throw new AssertionError(ex);
}
}
public static Object createInheritedMap(Object inheritableThreadLocals) {
try {
return threadLocalMapInheritedConstructor.newInstance(inheritableThreadLocals);
} catch (ReflectiveOperationException ex) {
throw new AssertionError(ex);
}
}
// public static void set(Thread t, ThreadLocal tl, Object value) {
// Object map = getThreadLocals(t);
// if (map != null)
// set(map, tl, value);
// else
// setThreadLocals(t, createThreadLocalMap(tl, value));
// }
//
// private static void set(Object map, ThreadLocal tl, Object value) {
// try {
// threadLocalMapSet.invoke(map, tl, value);
// } catch (ReflectiveOperationException e) {
// throw new AssertionError(e);
// }
// }
// createInheritedMap works only for InheritableThreadLocals
public static Object cloneThreadLocalMap(Object orig) {
try {
Object clone = createThreadLocalMap(new ThreadLocal(), null);
Object origTable = threadLocalMapTableField.get(orig);
final int len = Array.getLength(origTable);
Object tableClone = Array.newInstance(threadLocalMapEntryClass, len);
for (int i = 0; i < len; i++) {
Object entry = Array.get(origTable, i);
if (entry != null)
Array.set(tableClone, i, cloneThreadLocalMapEntry(entry));
}
threadLocalMapTableField.set(clone, tableClone);
threadLocalMapSizeField.setInt(clone, threadLocalMapSizeField.getInt(orig));
threadLocalMapThresholdField.setInt(clone, threadLocalMapThresholdField.getInt(orig));
return clone;
} catch (ReflectiveOperationException ex) {
throw new AssertionError(ex);
}
}
private static Object cloneThreadLocalMapEntry(Object entry) {
try {
final ThreadLocal key = ((Reference<ThreadLocal>) entry).get();
final Object value = threadLocalMapEntryValueField.get(entry);
return threadLocalMapEntryConstructor.newInstance(key, value);
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
public static Map<ThreadLocal, Object> toMap(Object threadLocalMap) {
try {
final Map<ThreadLocal, Object> map = new HashMap<>();
Object table = threadLocalMapTableField.get(threadLocalMap);
final int len = Array.getLength(table);
for (int i = 0; i < len; i++) {
Object entry = Array.get(table, i);
if (entry != null)
map.put(((Reference<ThreadLocal>) entry).get(), threadLocalMapEntryValueField.get(entry));
}
return map;
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
public static ClassLoader getContextClassLoader(Thread thread) {
return (ClassLoader) CONTEXT_CLASS_LOADER.get(thread);
}
public static void setContextClassLoader(Thread thread, ClassLoader classLoader) {
CONTEXT_CLASS_LOADER.set(thread, classLoader);
}
public static AccessControlContext getInheritedAccessControlContext(Thread thread) {
if (INHERITED_ACCESS_CONTROL_CONTEXT == null)
return null;
return (AccessControlContext) INHERITED_ACCESS_CONTROL_CONTEXT.get(thread);
}
public static void setInheritedAccessControlContext(Thread thread, AccessControlContext accessControlContext) {
if (INHERITED_ACCESS_CONTROL_CONTEXT != null)
INHERITED_ACCESS_CONTROL_CONTEXT.set(thread, accessControlContext);
}
}