use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationListenerTest method testExceptionImpl.
private void testExceptionImpl(Runnable body, boolean throwOnEnter, boolean throwOnLeave) {
setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
});
ThreadsActivationListener listener = new ThreadsActivationListener() {
@Override
public void onEnterThread(TruffleContext c) {
if (throwOnEnter) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new RuntimeException();
}
}
@Override
public void onLeaveThread(TruffleContext c) {
if (throwOnLeave) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new RuntimeException();
}
}
};
EventBinding<? extends ThreadsActivationListener> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(listener);
try {
body.run();
} finally {
binding.dispose();
}
}
use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationListenerTest method testInternalContext.
@Test
public void testInternalContext() {
setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
});
Context c0 = this.context;
TruffleContext tc0 = this.languageEnv.getContext();
c0.enter();
TruffleContext ic0 = this.languageEnv.newContextBuilder().build();
TruffleContext ic0CreatorHandle = ic0;
Object prev = ic0.enter(null);
// look language handle on the context it is not the same as
// the creator handle. The creator handle can be closed.
ic0 = LanguageContext.get(null).getEnv().getContext();
ic0.leave(null, prev);
c0.leave();
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);
assertSame(tc0, ic0.getParent());
assertList(entered);
assertList(left);
prev = ic0.enter(null);
assertList(entered, ic0);
assertList(left);
ic0.leave(null, prev);
assertList(entered, ic0);
assertList(left, ic0);
prev = tc0.enter(null);
assertList(entered, ic0, tc0);
assertList(left, ic0);
Object prev1 = ic0.enter(null);
assertList(entered, ic0, tc0, ic0);
assertList(left, ic0);
ic0.leave(null, prev1);
assertList(entered, ic0, tc0, ic0);
assertList(left, ic0, ic0);
tc0.leave(null, prev);
assertList(entered, ic0, tc0, ic0);
assertList(left, ic0, ic0, tc0);
binding.dispose();
prev = tc0.enter(null);
prev1 = ic0.enter(null);
ic0.leave(null, prev1);
tc0.leave(null, prev);
assertList(entered, ic0, tc0, ic0);
assertList(left, ic0, ic0, tc0);
ic0CreatorHandle.close();
}
use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationListenerTest method testSingleContext.
@Test
public void testSingleContext() {
setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
});
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);
context.enter();
TruffleContext to = languageEnv.getContext();
assertList(entered, to);
assertList(left);
context.leave();
assertList(entered, to);
assertList(left, to);
context.enter();
assertList(entered, to, to);
assertList(left, to);
context.enter();
assertList(entered, to, to, to);
assertList(left, to);
context.leave();
assertList(entered, to, to, to);
assertList(left, to, to);
context.leave();
assertList(entered, to, to, to);
assertList(left, to, to, to);
context.eval(ProxyLanguage.ID, "");
assertList(entered, to, to, to, to);
assertList(left, to, to, to, to);
binding.dispose();
context.enter();
context.leave();
assertList(entered, to, to, to, to);
assertList(left, to, to, to, to);
}
use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class ThreadsActivationCompilationTest method testThreadActivationCompilation.
@Test
public void testThreadActivationCompilation() {
setupEnv(Context.newBuilder().allowExperimentalOptions(true).option("engine.CompileImmediately", "false").option("engine.BackgroundCompilation", "false").build());
AtomicReference<Boolean> compiledEnter = new AtomicReference<>();
AtomicReference<Boolean> compiledLeave = new AtomicReference<>();
compiledEnter.set(Boolean.FALSE);
compiledLeave.set(Boolean.FALSE);
Assumption singleContext = Truffle.getRuntime().createAssumption();
instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {
@Override
public void onLeaveThread(TruffleContext c) {
if (singleContext.isValid()) {
CompilerAsserts.partialEvaluationConstant(c);
CompilerAsserts.partialEvaluationConstant(this);
}
if (CompilerDirectives.inCompiledCode()) {
compiledLeave.set(Boolean.TRUE);
}
}
@Override
public void onEnterThread(TruffleContext c) {
if (singleContext.isValid()) {
CompilerAsserts.partialEvaluationConstant(c);
CompilerAsserts.partialEvaluationConstant(this);
}
if (CompilerDirectives.inCompiledCode()) {
compiledEnter.set(Boolean.TRUE);
}
}
});
context.enter();
compiledEnter.set(Boolean.FALSE);
compiledLeave.set(Boolean.FALSE);
OptimizedCallTarget target = (OptimizedCallTarget) new RootNode(null) {
@Override
public Object execute(VirtualFrame frame) {
TruffleContext tc = (TruffleContext) frame.getArguments()[0];
Object prev = tc.enter(this);
if (CompilerDirectives.inCompiledCode()) {
if (!compiledEnter.get()) {
CompilerDirectives.shouldNotReachHere();
}
}
tc.leave(this, prev);
if (CompilerDirectives.inCompiledCode()) {
if (!compiledLeave.get()) {
CompilerDirectives.shouldNotReachHere();
}
}
return null;
}
}.getCallTarget();
TruffleContext tc = LanguageContext.get(null).getEnv().getContext();
singleContext.invalidate();
target.call(tc);
target.compile(true);
assertTrue(target.isValidLastTier());
target.call(tc);
assertTrue(target.isValidLastTier());
assertTrue(compiledEnter.get());
assertTrue(compiledLeave.get());
context.leave();
context.close();
}
use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.
the class TruffleContextTest method testCancelledAndResourceExhausted.
@Test
public void testCancelledAndResourceExhausted() throws InterruptedException {
setupEnv();
AtomicReference<Throwable> error = new AtomicReference<>();
Thread t = new Thread(() -> {
try {
context.eval(InstrumentationTestLanguage.ID, "LOOP(infinity, STATEMENT)");
} catch (Throwable e) {
error.set(e);
}
});
// avoid need for multi-threading
context.leave();
AtomicReference<TruffleContext> enter = new AtomicReference<>();
Semaphore waitUntilEntered = new Semaphore(0);
instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {
@TruffleBoundary
public void onEnterThread(TruffleContext tc) {
enter.set(tc);
waitUntilEntered.release();
}
public void onLeaveThread(TruffleContext tc) {
}
});
t.start();
if (!waitUntilEntered.tryAcquire(10000, TimeUnit.MILLISECONDS)) {
throw new AssertionError(error.get());
}
TruffleContext tc = enter.get();
tc.closeResourceExhausted(null, "testError");
t.join();
assertNotNull(error.get());
assertTrue(error.get().toString(), error.get() instanceof PolyglotException);
PolyglotException e = (PolyglotException) error.get();
assertEquals("testError", e.getMessage());
assertTrue(e.isCancelled());
assertTrue(e.isResourceExhausted());
}
Aggregations