Search in sources :

Example 46 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class InstrumentationTest method testAsynchronousStacks4.

@Test
public void testAsynchronousStacks4() throws IOException, InterruptedException {
    String code = "ROOT(DEFINE(af11, ROOT(STATEMENT))," + "DEFINE(af12, ROOT(CALL(af11)))," + "DEFINE(af13, ROOT(CALL(af12)))," + "DEFINE(af14, ROOT(CALL(af13)))," + "DEFINE(af21, ROOT(STATEMENT, SPAWN(af14)))," + "DEFINE(af22, ROOT(CALL(af21)))," + "DEFINE(af23, ROOT(CALL(af22)))," + "DEFINE(af24, ROOT(CALL(af23)))," + "DEFINE(f1, ROOT(STATEMENT, SPAWN(af24)))," + "DEFINE(f2, ROOT(CALL(f1)))," + "DEFINE(f3, ROOT(CALL(f2)))," + "DEFINE(f4, ROOT(CALL(f3)))," + "CALL(f4))";
    Source source = Source.create(InstrumentationTestLanguage.ID, code);
    teardown();
    CountDownLatch instrumentationFinished = new CountDownLatch(1);
    Context asyncContext = Context.newBuilder().engine(Engine.create()).allowCreateThread(true).build();
    AsynchronousStacksInstrument testInstrument = new AsynchronousStacksInstrument();
    testInstrument.instrumentationFinished = instrumentationFinished;
    setupEnv(asyncContext, new ProxyLanguage() {

        // Not to block the context's thread access
        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }
    }, testInstrument);
    run(source);
    instrumentationFinished.await();
    run(Source.create(InstrumentationTestLanguage.ID, "JOIN()"));
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) CountDownLatch(java.util.concurrent.CountDownLatch) Source(org.graalvm.polyglot.Source) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test)

Example 47 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ContextInterruptCloseCancelOrExitTest method testExitFromExitNotification.

@Test
public void testExitFromExitNotification() {
    setupEnv(Context.newBuilder(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) {
            return new RootNode(languageInstance) {

                @Override
                public Object execute(VirtualFrame frame) {
                    LanguageContext languageContext = LanguageContext.get(this);
                    languageContext.getEnv().getContext().closeExited(this, 1);
                    return 0;
                }
            }.getCallTarget();
        }

        @Override
        protected void exitContext(LanguageContext ctx, ExitMode exitMode, int exitCode) {
            ctx.getEnv().getContext().closeExited(DUMMY_NODE, 5);
        }
    });
    try {
        context.eval(ProxyLanguage.ID, "");
    } catch (PolyglotException pe) {
        if (!pe.isExit() || pe.getExitStatus() != 1) {
            throw pe;
        }
    }
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 48 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ContextInterruptCloseCancelOrExitTest method testCancelPolyglotThreadWhileClosing.

@Test
public void testCancelPolyglotThreadWhileClosing() throws ExecutionException, InterruptedException, IOException {
    enterContext = false;
    AtomicReference<Runnable> runOnNaturalContextExit = new AtomicReference<>();
    ProxyLanguage proxyLanguage = new ProxyLanguage() {

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }

        @Override
        protected void exitContext(LanguageContext ctx, ExitMode exitMode, int exitCode) {
            if (exitMode == ExitMode.NATURAL && runOnNaturalContextExit.get() != null) {
                runOnNaturalContextExit.get().run();
            }
        }
    };
    setupEnv(Context.newBuilder().allowCreateThread(true), proxyLanguage);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
        CountDownLatch cancelLatch = new CountDownLatch(1);
        Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo,CONSTANT(42),LOOP(infinity,STATEMENT)),SPAWN(foo))", "InfiniteLoop").build();
        attachListener(cancelLatch::countDown, instrumentEnv);
        Future<?> future = executorService.submit(() -> {
            context.eval(source);
        });
        cancelLatch.await();
        future.get();
        runOnNaturalContextExit.set(() -> {
            /*
                 * Cancel must work even when entered.
                 */
            context.close(true);
        });
        context.close();
        assertContextState("CLOSED_CANCELLED");
    } finally {
        executorService.shutdownNow();
        executorService.awaitTermination(100, TimeUnit.SECONDS);
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Source(org.graalvm.polyglot.Source) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 49 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ContextInterruptCloseCancelOrExitTest method testNestedExitOnHost.

@Test
public void testNestedExitOnHost() {
    setupEnv(Context.newBuilder(ProxyLanguage.ID, InstrumentationTestLanguage.ID).allowHostClassLoading(true).allowHostClassLookup((s) -> true).allowHostAccess(HostAccess.ALL), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) {
            return new RootNode(languageInstance) {

                @Override
                public Object execute(VirtualFrame frame) {
                    LanguageContext languageContext = LanguageContext.get(this);
                    Object javaClass = languageContext.getEnv().lookupHostSymbol(ContextInterruptCloseCancelOrExitTest.class.getName());
                    try {
                        InteropLibrary.getUncached().invokeMember(javaClass, "callExit");
                    } catch (UnsupportedMessageException | ArityException | UnknownIdentifierException | UnsupportedTypeException e) {
                        throw new RuntimeException(e);
                    }
                    return 0;
                }
            }.getCallTarget();
        }

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }
    });
    staticCtx = context;
    try {
        context.eval(ProxyLanguage.ID, "");
        Assert.fail();
    } catch (PolyglotException pe) {
        if (!pe.isExit()) {
            throw pe;
        }
    }
    context.close();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) PolyglotException(org.graalvm.polyglot.PolyglotException) ArityException(com.oracle.truffle.api.interop.ArityException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 50 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class MultiThreadingTest method testInitMultiThreading.

@Test
public void testInitMultiThreading() throws ExecutionException, InterruptedException {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    try {
        int multiThreadingInits = 0;
        int lastIterationNo = 0;
        AtomicBoolean multiThreadingInitNotExecuted = new AtomicBoolean();
        for (int itNo = 0; itNo < 10000 && !multiThreadingInitNotExecuted.get(); itNo++) {
            lastIterationNo = itNo;
            CountDownLatch enterLeaveLoopLatch = new CountDownLatch(1);
            enterContext = false;
            AtomicBoolean testEnd = new AtomicBoolean();
            AtomicBoolean multiThreadingInitialized = new AtomicBoolean();
            setupEnv(Context.newBuilder(), new ProxyLanguage() {

                @Override
                protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
                    return true;
                }

                @Override
                protected void initializeMultiThreading(LanguageContext c) {
                    multiThreadingInitialized.set(true);
                }
            });
            AtomicInteger enteredCount = new AtomicInteger();
            Future<?> future1 = executorService.submit(() -> {
                TruffleContext truffleContext = languageEnv.getContext();
                Object prev = truffleContext.enter(INVALID_NODE);
                truffleContext.leave(INVALID_NODE, prev);
                enterLeaveLoopLatch.countDown();
                while (!testEnd.get()) {
                    prev = truffleContext.enter(INVALID_NODE);
                    int localEnteredCount = enteredCount.incrementAndGet();
                    if (localEnteredCount > 1 && !multiThreadingInitialized.get()) {
                        multiThreadingInitNotExecuted.set(true);
                    }
                    enteredCount.decrementAndGet();
                    truffleContext.leave(INVALID_NODE, prev);
                }
            });
            Future<?> future2 = executorService.submit(() -> {
                TruffleContext truffleContext = languageEnv.getContext();
                try {
                    enterLeaveLoopLatch.await();
                } catch (InterruptedException ie) {
                    throw new AssertionError(ie);
                }
                Object prev = truffleContext.enter(INVALID_NODE);
                int localEnteredCount = enteredCount.incrementAndGet();
                if (localEnteredCount > 1 && !multiThreadingInitialized.get()) {
                    multiThreadingInitNotExecuted.set(true);
                }
                enteredCount.decrementAndGet();
                truffleContext.leave(INVALID_NODE, prev);
                testEnd.set(true);
            });
            enterLeaveLoopLatch.await();
            future1.get();
            future2.get();
            context.close();
            if (multiThreadingInitialized.get()) {
                multiThreadingInits++;
            }
        }
        Assert.assertTrue("Multi-threading was never initialized!", multiThreadingInits > 0);
        Assert.assertFalse("Multi-threading was not initialized even though more than one thread was entered at the same time! lastIterationNo = " + lastIterationNo, multiThreadingInitNotExecuted.get());
    } finally {
        executorService.shutdownNow();
        executorService.awaitTermination(100, TimeUnit.SECONDS);
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) CountDownLatch(java.util.concurrent.CountDownLatch) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Aggregations

ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)64 Test (org.junit.Test)47 CallTarget (com.oracle.truffle.api.CallTarget)32 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)28 PolyglotException (org.graalvm.polyglot.PolyglotException)26 RootNode (com.oracle.truffle.api.nodes.RootNode)21 Source (org.graalvm.polyglot.Source)21 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)20 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)20 Context (org.graalvm.polyglot.Context)17 TruffleContext (com.oracle.truffle.api.TruffleContext)16 Before (org.junit.Before)16 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)14 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)10 ArrayList (java.util.ArrayList)10 ExecutionException (java.util.concurrent.ExecutionException)10 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)9 ExecutorService (java.util.concurrent.ExecutorService)9