Search in sources :

Example 1 with LanguageContext

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;
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)

Example 2 with LanguageContext

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);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) NoSuchElementException(java.util.NoSuchElementException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Before(org.junit.Before)

Example 3 with LanguageContext

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());
}
Also used : ArrayList(java.util.ArrayList) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) ThreadLocalAction(com.oracle.truffle.api.ThreadLocalAction) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) List(java.util.List) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) ExecutionException(java.util.concurrent.ExecutionException) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) TruffleContext(com.oracle.truffle.api.TruffleContext) CountDownLatch(java.util.concurrent.CountDownLatch) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Aggregations

LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)3 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)2 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 ThreadLocalAction (com.oracle.truffle.api.ThreadLocalAction)1 TruffleContext (com.oracle.truffle.api.TruffleContext)1 Env (com.oracle.truffle.api.TruffleLanguage.Env)1 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)1 ArityException (com.oracle.truffle.api.interop.ArityException)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1