use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationListenerTest method testMultiContext.
@Test
public void testMultiContext() {
Engine engine = Engine.create();
setupEnv(Context.newBuilder().engine(engine).build(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
});
Context c0 = this.context;
TruffleContext tc0 = this.languageEnv.getContext();
setupEnv(Context.newBuilder().engine(engine).build(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
});
Context c1 = this.context;
TruffleContext tc1 = languageEnv.getContext();
List<TruffleContext> entered = new ArrayList<>();
List<TruffleContext> left = new ArrayList<>();
EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {
@TruffleBoundary
public void onEnterThread(TruffleContext c) {
entered.add(c);
}
@TruffleBoundary
public void onLeaveThread(TruffleContext c) {
left.add(c);
}
});
assertList(entered);
assertList(left);
c0.enter();
assertList(entered, tc0);
assertList(left);
c1.enter();
assertList(entered, tc0, tc1);
assertList(left);
c1.leave();
assertList(entered, tc0, tc1);
assertList(left, tc1);
c0.leave();
assertList(entered, tc0, tc1);
assertList(left, tc1, tc0);
binding.dispose();
c0.enter();
c1.enter();
c1.leave();
c0.leave();
assertList(entered, tc0, tc1);
assertList(left, tc1, tc0);
c0.close();
c1.close();
engine.close();
}
use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationListenerTest method testMultiThreading.
@Test
public void testMultiThreading() throws InterruptedException {
setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
});
List<TruffleContext> entered = new ArrayList<>();
List<TruffleContext> left = new ArrayList<>();
instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {
@TruffleBoundary
public void onEnterThread(TruffleContext c) {
synchronized (entered) {
entered.add(c);
}
}
@TruffleBoundary
public void onLeaveThread(TruffleContext c) {
synchronized (left) {
left.add(c);
}
}
});
assertList(entered);
assertList(left);
TruffleContext to = languageEnv.getContext();
context.enter();
assertList(entered, to);
assertList(left);
Thread t = new Thread(new Runnable() {
public void run() {
assertList(entered, to);
assertList(left);
context.enter();
assertList(entered, to, to);
assertList(left);
context.leave();
assertList(entered, to, to);
assertList(left, to);
}
});
t.start();
t.join();
context.leave();
assertList(entered, to, to);
assertList(left, to, to);
}
Aggregations