Search in sources :

Example 6 with TruffleContext

use of com.oracle.truffle.api.TruffleContext in project graal by oracle.

the class MultiThreadedLanguageTest method testAccessTruffleContextPolyglotThread.

@Test
public void testAccessTruffleContextPolyglotThread() throws Throwable {
    MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
        return true;
    };
    Engine engine = Engine.create();
    AtomicReference<Throwable> seenError = new AtomicReference<>();
    Context context = Context.newBuilder().allowCreateThread(true).engine(engine).build();
    eval(context, new Function<Env, Object>() {

        public Object apply(Env env) {
            List<Thread> createdThreads = new ArrayList<>();
            ExecutorService service = Executors.newFixedThreadPool(10, (r) -> {
                Thread t = env.createThread(r);
                t.setUncaughtExceptionHandler((thread, e) -> seenError.set(e));
                createdThreads.add(t);
                return t;
            });
            TruffleContext innerContext = env.newContextBuilder().build();
            List<Future<LanguageContext>> futures = new ArrayList<>();
            List<Future<LanguageContext>> innerContextFutures = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                innerContextFutures.add(service.submit(() -> {
                    Object prev = innerContext.enter();
                    try {
                        return MultiThreadedLanguage.getContext();
                    } finally {
                        innerContext.leave(prev);
                    }
                }));
            }
            for (int i = 0; i < 100; i++) {
                futures.add(service.submit(() -> {
                    return MultiThreadedLanguage.getContext();
                }));
            }
            try {
                for (Future<LanguageContext> future : futures) {
                    assertSame(MultiThreadedLanguage.getContext(), future.get());
                }
                LanguageContext innerLanguageContext;
                Object prev = innerContext.enter();
                innerLanguageContext = MultiThreadedLanguage.getContext();
                innerContext.leave(prev);
                for (Future<LanguageContext> future : innerContextFutures) {
                    assertSame(innerLanguageContext, future.get());
                }
                innerContext.close();
            } catch (InterruptedException e1) {
                throw new AssertionError(e1);
            } catch (ExecutionException e1) {
                throw new AssertionError(e1);
            }
            service.shutdown();
            /*
                 * We need to join all threads as unfortunately the executor service does not
                 * guarantee that all threads are immediately shutdown.
                 */
            try {
                for (Thread t : createdThreads) {
                    t.join(1000);
                }
            } catch (InterruptedException e1) {
                throw new AssertionError(e1);
            }
            return MultiThreadedLanguage.getContext();
        }
    });
    if (seenError.get() != null) {
        throw seenError.get();
    }
    engine.close();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) ThreadRequest(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.ThreadRequest) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) PolyglotException(org.graalvm.polyglot.PolyglotException) Env(com.oracle.truffle.api.TruffleLanguage.Env) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Semaphore(java.util.concurrent.Semaphore) Value(org.graalvm.polyglot.Value) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) Executors(java.util.concurrent.Executors) TruffleContext(com.oracle.truffle.api.TruffleContext) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Engine(org.graalvm.polyglot.Engine) Entry(java.util.Map.Entry) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 7 with TruffleContext

use of com.oracle.truffle.api.TruffleContext in project graal by oracle.

the class LanguageSPITest method testInnerContext.

@Test
public void testInnerContext() {
    Context context = Context.create();
    Function<Env, Object> f = new Function<Env, Object>() {

        public Object apply(Env env) {
            LanguageContext outerLangContext = LanguageSPITestLanguage.getContext();
            Object config = new Object();
            TruffleContext innerContext = env.newContextBuilder().config("config", config).build();
            Object p = innerContext.enter();
            LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
            try {
                try {
                    innerContext.close();
                    fail("context could be closed when entered");
                } catch (IllegalStateException e) {
                }
                assertEquals(0, innerLangContext.disposeCalled);
                assertEquals(config, innerLangContext.config.get("config"));
                assertNotSame(outerLangContext, innerLangContext);
                boolean assertions = false;
                assert (assertions = true) == true;
                if (assertions) {
                    boolean leaveFailed = false;
                    try {
                        innerContext.leave("foo");
                    } catch (AssertionError e) {
                        leaveFailed = true;
                    }
                    if (!leaveFailed) {
                        fail("no assertion error for leaving with the wrong object");
                    }
                }
            } finally {
                innerContext.leave(p);
            }
            assertSame(outerLangContext, LanguageSPITestLanguage.getContext());
            innerContext.close();
            try {
                innerContext.enter();
                fail("cannot be entered after closing");
            } catch (IllegalStateException e) {
            }
            innerContext.close();
            assertEquals(1, innerLangContext.disposeCalled);
            return null;
        }
    };
    eval(context, f);
    // ensure we are not yet closed
    eval(context, f);
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Function(java.util.function.Function) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Test(org.junit.Test)

Example 8 with TruffleContext

use of com.oracle.truffle.api.TruffleContext in project graal by oracle.

the class LanguageSPITest method testTruffleContext.

@Test
public void testTruffleContext() {
    Context context = Context.create();
    Function<Env, Object> f = new Function<Env, Object>() {

        public Object apply(Env env) {
            boolean assertions = false;
            assert (assertions = true) == true;
            if (!assertions) {
                fail("Tests must be run with assertions on");
            }
            // No more recursive runs inside
            LanguageSPITestLanguage.runinside = null;
            Throwable[] error = new Throwable[1];
            Thread thread = new Thread(() -> {
                try {
                    Source source = Source.newBuilder("").language(LanguageSPITestLanguage.ID).name("s").build();
                    boolean parsingFailed = false;
                    try {
                        // execute Truffle code in a fresh thread fails
                        env.parse(source).call();
                    } catch (AssertionError e) {
                        // No current context available.
                        parsingFailed = true;
                    }
                    if (!parsingFailed) {
                        fail("no assertion error \"No current context available.\"");
                    }
                    TruffleContext truffleContext = env.getContext();
                    // attach the Thread
                    Object prev = truffleContext.enter();
                    try {
                        // execute Truffle code
                        env.parse(source).call();
                    } finally {
                        // detach the Thread
                        truffleContext.leave(prev);
                    }
                } catch (Throwable t) {
                    error[0] = t;
                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            if (error[0] != null) {
                throw new AssertionError(error[0]);
            }
            boolean leaveFailed = false;
            try {
                TruffleContext truffleContext = env.getContext();
                truffleContext.leave(null);
            } catch (AssertionError e) {
                leaveFailed = true;
            }
            if (!leaveFailed) {
                fail("no assertion error for leaving without enter");
            }
            return null;
        }
    };
    eval(context, f);
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) Source(com.oracle.truffle.api.source.Source) Function(java.util.function.Function) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

TruffleContext (com.oracle.truffle.api.TruffleContext)8 Context (org.graalvm.polyglot.Context)8 Test (org.junit.Test)8 Env (com.oracle.truffle.api.TruffleLanguage.Env)5 Function (java.util.function.Function)5 ArrayList (java.util.ArrayList)4 Engine (org.graalvm.polyglot.Engine)4 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)3 HashSet (java.util.HashSet)3 Instrument (org.graalvm.polyglot.Instrument)3 EventContext (com.oracle.truffle.api.instrumentation.EventContext)2 LanguageContext (com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext)2 ThreadRequest (com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.ThreadRequest)2 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2