use of com.oracle.truffle.api.test.polyglot.ProxyLanguage 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.test.polyglot.ProxyLanguage 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.test.polyglot.ProxyLanguage 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.test.polyglot.ProxyLanguage in project graal by oracle.
the class TruffleContextTest method testParallelForceClose.
@Test
public void testParallelForceClose() throws InterruptedException {
setupEnv(Context.newBuilder().allowAllAccess(true).option("engine.TriggerUncaughtExceptionHandlerForCancel", "true").build(), new ProxyLanguage() {
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
});
TruffleContext tc = languageEnv.newContextBuilder().build();
List<Thread> threads = new ArrayList<>();
List<AtomicReference<Throwable>> exceptions = new ArrayList<>();
Semaphore waitUntilStart = new Semaphore(0);
for (int i = 0; i < 100; i++) {
Thread t = languageEnv.createThread(() -> {
com.oracle.truffle.api.source.Source s = com.oracle.truffle.api.source.Source.newBuilder(InstrumentationTestLanguage.ID, "EXPRESSION", "").build();
CallTarget target = LanguageContext.get(null).getEnv().parsePublic(s);
while (true) {
target.call();
// at least one thread should have started execution
waitUntilStart.release();
}
}, tc);
AtomicReference<Throwable> exception = new AtomicReference<>();
t.setUncaughtExceptionHandler((thread, e) -> {
exception.set(e);
});
exceptions.add(exception);
t.start();
threads.add(t);
}
// 10s ought to be enough for anybody
if (!waitUntilStart.tryAcquire(10000, TimeUnit.MILLISECONDS)) {
for (AtomicReference<Throwable> e : exceptions) {
if (e.get() != null) {
throw new AssertionError(e.get());
}
}
throw new AssertionError("failed to wait for execution");
}
assertFalse(tc.isClosed());
for (int i = 0; i < threads.size(); i++) {
assertNull(exceptions.get(i).get());
}
tc.closeCancelled(null, "testreason");
for (int i = 0; i < threads.size(); i++) {
threads.get(i).join();
}
for (int i = 0; i < threads.size(); i++) {
Throwable e = exceptions.get(i).get();
assertNotNull(e);
assertEquals(getCancelExecutionClass(), e.getClass());
assertEquals("testreason", e.getMessage());
assertTrue(tc.isClosed());
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class VariablesScopeTest method createDefaultScopeLanguage.
private static void createDefaultScopeLanguage(Context context) {
ProxyLanguage language = new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
return new RootNode(ProxyLanguage.get(null)) {
@Node.Child
private DefaultRootBlockNode block = insert(new DefaultRootBlockNode());
@Override
protected boolean isInstrumentable() {
return true;
}
@Override
public Object execute(VirtualFrame frame) {
return block.execute(frame);
}
}.getCallTarget();
}
};
ProxyLanguage.setDelegate(language);
context.initialize(ProxyLanguage.ID);
}
Aggregations