use of com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext in project graal by oracle.
the class ProxyLanguage method createContext.
@Override
protected LanguageContext createContext(com.oracle.truffle.api.TruffleLanguage.Env env) {
if (wrapper) {
delegate.languageInstance = this;
LanguageContext c = delegate.createContext(env);
if (delegate.onCreate != null) {
delegate.onCreate.accept(c);
}
return c;
} else {
LanguageContext c = new LanguageContext(env);
if (onCreate != null) {
onCreate.accept(c);
}
return c;
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext in project graal by oracle.
the class HostExceptionTest method before.
@Before
public void before() {
context = Context.newBuilder().allowAllAccess(true).build();
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected LanguageContext createContext(Env contextEnv) {
env = contextEnv;
return super.createContext(contextEnv);
}
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
RootNode rootNode;
switch(request.getSource().getCharacters().toString()) {
case "catcher":
rootNode = new CatcherRootNode();
break;
case "runner":
rootNode = new RunnerRootNode();
break;
case "rethrower":
rootNode = new RethrowerRootNode();
break;
default:
throw new IllegalArgumentException();
}
return RootNode.createConstantNode(new CatcherObject(rootNode.getCallTarget())).getCallTarget();
}
});
context.initialize(ProxyLanguage.ID);
context.enter();
assertNotNull(env);
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext in project graal by oracle.
the class TruffleSafepointTest method testNoSafepointAfterThreadDispose.
@Test
public void testNoSafepointAfterThreadDispose() {
final ThreadLocal<Boolean> tl = new ThreadLocal<>();
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
@TruffleBoundary
protected void initializeThread(LanguageContext context, Thread thread) {
tl.set(Boolean.TRUE);
}
@Override
@TruffleBoundary
protected void disposeThread(LanguageContext context, Thread thread) {
tl.set(Boolean.FALSE);
}
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
});
forEachConfig((threads, events) -> {
CountDownLatch awaitThreadStart = new CountDownLatch(1);
AtomicBoolean threadsStopped = new AtomicBoolean();
try (TestSetup setup = setupSafepointLoop(threads, new NodeCallable() {
@TruffleBoundary
public boolean call(@SuppressWarnings("hiding") TestSetup setup, TestRootNode node) {
try {
List<Throwable> errors = Collections.synchronizedList(new ArrayList<>());
List<Thread> polyglotThreads = new ArrayList<>();
try {
for (int i = 0; i < threads; i++) {
Thread t = node.setup.env.createThread(() -> {
do {
TruffleContext context = node.setup.env.getContext();
TruffleSafepoint safepoint = TruffleSafepoint.getCurrent();
boolean prevSideEffects = safepoint.setAllowSideEffects(false);
try {
context.leaveAndEnter(null, () -> {
// cached thread local
return null;
});
} finally {
safepoint.setAllowSideEffects(prevSideEffects);
}
} while (!threadsStopped.get());
});
t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(@SuppressWarnings("hiding") Thread t, Throwable e) {
threadsStopped.set(true);
e.printStackTrace();
errors.add(e);
}
});
t.start();
polyglotThreads.add(t);
}
} finally {
awaitThreadStart.countDown();
}
for (Thread thread : polyglotThreads) {
thread.join();
}
for (Throwable t : errors) {
throw new AssertionError("thread threw error ", t);
}
return true;
} catch (InterruptedException e1) {
throw new AssertionError(e1);
}
}
})) {
try {
awaitThreadStart.await();
// important to let leaving and submitting race against each other
threadsStopped.set(true);
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < events; i++) {
futures.add(setup.env.submitThreadLocal(null, new ThreadLocalAction(true, false) {
@Override
protected void perform(Access access) {
assertEquals(Boolean.TRUE, tl.get());
}
}));
}
for (Future<?> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
throw new AssertionError(e);
}
}
} catch (InterruptedException e) {
throw new AssertionError(e);
}
setup.stopAndAwait();
}
});
ProxyLanguage.setDelegate(new ProxyLanguage());
}
Aggregations