Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to use locks for reflection stuff. Use single-racy reads instead. #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1029,13 +1029,16 @@ public void flushGeneratedClassesFor(String className) {
}
}

private Unsafe unsafe;
private static Unsafe unsafe;
private static Method defineClassMethod;

private Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Unsafe unsafe = ClassLoaderWeavingAdaptor.unsafe;
if (unsafe == null) {
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true);
return (Unsafe) theUnsafeField.get(null);
ClassLoaderWeavingAdaptor.unsafe = unsafe = (Unsafe) theUnsafeField.get(null);
return unsafe;
}
return unsafe;
}
Expand Down Expand Up @@ -1105,15 +1108,14 @@ private void defineClass(ClassLoader loader, String name, byte[] bytes, Protecti
}
} else {
try {
if (defineClassMethod == null) {
synchronized (lock) {
getUnsafe();
defineClassMethod =
Unsafe.class.getDeclaredMethod("defineClass", String.class,byte[].class,Integer.TYPE,Integer.TYPE, ClassLoader.class,ProtectionDomain.class);
}
Unsafe unsafe = getUnsafe();
Method defineClass = defineClassMethod;
if (defineClass == null) {
defineClass = Unsafe.class.getDeclaredMethod("defineClass", String.class,byte[].class,Integer.TYPE,Integer.TYPE, ClassLoader.class,ProtectionDomain.class);
defineClass.setAccessible(true);
defineClassMethod = defineClass;
}
defineClassMethod.setAccessible(true);
clazz = defineClassMethod.invoke(getUnsafe(), name,bytes,0,bytes.length,loader,protectionDomain);
clazz = defineClass.invoke(unsafe, name,bytes,0,bytes.length,loader,protectionDomain);
} catch (LinkageError le) {
le.printStackTrace();
// likely thrown due to defining something that already exists?
Expand All @@ -1130,8 +1132,6 @@ private void defineClass(ClassLoader loader, String name, byte[] bytes, Protecti
trace.exit("defineClass", clazz);
}
}
static Method defineClassMethod;
private static final Object lock = new Object();


// /*
Expand Down