-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathJITHandle.java
More file actions
92 lines (76 loc) · 3.21 KB
/
JITHandle.java
File metadata and controls
92 lines (76 loc) · 3.21 KB
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
/*
*
*/
package org.python.modules.jffi;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
*/
final class JITHandle {
private static final int THRESHOLD = Integer.getInteger("jython.ctypes.compile.threshold", 100);
private final JITSignature jitSignature;
private volatile boolean compilationFailed = false;
private final AtomicInteger counter = new AtomicInteger(0);
private final JITCompiler compiler;
private WeakReference<Class<? extends Invoker>> compiledClassRef = null;
JITHandle(JITCompiler compiler, JITSignature signature, boolean compilationFailed) {
this.compiler = compiler;
this.jitSignature = signature;
this.compilationFailed = compilationFailed;
}
final boolean compilationFailed() {
return compilationFailed;
}
final Invoker compile(com.kenai.jffi.Function function, NativeDataConverter resultConverter, NativeDataConverter[] parameterConverters) {
if (compilationFailed || counter.incrementAndGet() < THRESHOLD) {
return null;
}
Class<? extends Invoker> compiledClass;
synchronized (this) {
if (compiledClassRef == null || (compiledClass = compiledClassRef.get()) == null) {
compiledClass = newInvokerClass(jitSignature);
if (compiledClass == null) {
compilationFailed = true;
return null;
}
compiler.registerClass(this, compiledClass);
compiledClassRef = new WeakReference<Class<? extends Invoker>>(compiledClass);
}
}
try {
Constructor<? extends Invoker> cons = compiledClass.getDeclaredConstructor(com.kenai.jffi.Function.class,
NativeDataConverter.class, NativeDataConverter[].class, Invoker.class);
return cons.newInstance(function, resultConverter, parameterConverters,
createFallbackInvoker(function, jitSignature));
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
Class<? extends Invoker> newInvokerClass(JITSignature jitSignature) {
JITMethodGenerator generator = null;
JITMethodGenerator[] generators = {
new FastIntMethodGenerator(),
new FastLongMethodGenerator(),
new FastNumericMethodGenerator(),};
for (int i = 0; i < generators.length; i++) {
if (generators[i].isSupported(jitSignature)) {
generator = generators[i];
break;
}
}
if (generator == null) {
return null;
}
return new AsmClassBuilder(generator, jitSignature).build();
}
static Invoker createFallbackInvoker(com.kenai.jffi.Function function, JITSignature signature) {
NativeType[] parameterTypes = new NativeType[signature.getParameterCount()];
for (int i = 0; i < parameterTypes.length; i++) {
parameterTypes[i] = signature.getParameterType(i);
}
return DefaultInvokerFactory.getFactory().createInvoker(function, parameterTypes, signature.getResultType());
}
}