Search in sources :

Example 1 with TruffleContext

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

the class LanguageSPITest method testCloseInnerContextWithParent.

@Test
public void testCloseInnerContextWithParent() {
    Context context = Context.create();
    LanguageContext returnedInnerContext = eval(context, new Function<Env, Object>() {

        public Object apply(Env env) {
            TruffleContext innerContext = env.newContextBuilder().build();
            Object p = innerContext.enter();
            LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
            innerContext.leave(p);
            return innerLangContext;
        }
    }).asHostObject();
    context.close();
    // inner context automatically closed
    assertEquals(1, returnedInnerContext.disposeCalled);
}
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) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 2 with TruffleContext

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

the class ContextsEventsTest method testMultipleContexts.

@Test
public void testMultipleContexts() {
    final int numContexts = 5;
    final List<ContextEvent> events;
    try (Engine engine = Engine.create()) {
        Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
        TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
        events = test.events;
        Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
        for (int i = 0; i < numContexts; i++) {
            try (Context context = Context.newBuilder().engine(engine).build()) {
                assertEquals(6 * i + 1, events.size());
                context.eval(source);
            }
            assertEquals(6 * i + 6, events.size());
        }
        assertEquals(6 * numContexts, events.size());
        TruffleContext lastContext = null;
        for (int i = 0; i < numContexts; i++) {
            int ci = 6 * i;
            assertTrue(events.get(ci).created);
            assertNull(events.get(ci).language);
            assertNotEquals(lastContext, events.get(ci).context);
            lastContext = events.get(ci).context;
            assertTrue(events.get(ci + 1).created);
            assertNotNull(events.get(ci + 1).language);
            assertEquals(lastContext, events.get(ci + 1).context);
            assertTrue(events.get(ci + 2).languageInitialized);
            assertTrue(events.get(ci + 3).languageFinalized);
            assertNotNull(events.get(ci + 4).language);
            assertNull(events.get(ci + 5).language);
        }
    }
    // No more events
    assertEquals(6 * numContexts, events.size());
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TruffleContext(com.oracle.truffle.api.TruffleContext) Engine(org.graalvm.polyglot.Engine) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 3 with TruffleContext

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

the class ThreadsEventsTest method testSpawnManyThread.

@Test
public void testSpawnManyThread() {
    final int numThreads = 100;
    final List<ThreadEvent> events;
    try (Context context = Context.newBuilder().allowCreateThread(true).build()) {
        Instrument testThreadsInstrument = context.getEngine().getInstruments().get("testThreadsInstrument");
        TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
        events = test.events;
        context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(DEFINE(foo, STATEMENT), LOOP(" + numThreads + ", SPAWN(foo)), JOIN())"));
    }
    assertEquals(2 + 2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(1 + numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(1 + numThreads, startEvents.size());
    TruffleContext tcontext = events.get(0).context;
    for (ThreadEvent event : events) {
        assertEquals(tcontext, event.context);
    }
    events.clear();
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) ArrayList(java.util.ArrayList) TruffleContext(com.oracle.truffle.api.TruffleContext) Test(org.junit.Test)

Example 4 with TruffleContext

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

the class ThreadsEventsTest method testContextThreads.

@Test
public void testContextThreads() throws Exception {
    Engine engine = Engine.create();
    Instrument testThreadsInstrument = engine.getInstruments().get("testThreadsInstrument");
    TestThreadsInstrument test = testThreadsInstrument.lookup(TestThreadsInstrument.class);
    final List<ThreadEvent> events = test.events;
    Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                try (Context context = Context.newBuilder().engine(engine).build()) {
                    context.eval(source);
                }
            }
        };
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].join();
    }
    assertEquals(2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(numThreads, startEvents.size());
    // Verify that we got all threads in the events:
    Set<Thread> allThreads = new HashSet<>(Arrays.asList(threads));
    for (int i = 0; i < numThreads; i++) {
        assertTrue(allThreads.remove(startEvents.get(i).thread));
    }
    assertTrue(allThreads.toString(), allThreads.isEmpty());
    // Verify that we got 'numThreads' number of contexts:
    Set<TruffleContext> distinctContexts = new HashSet<>();
    for (ThreadEvent event : events) {
        distinctContexts.add(event.context);
    }
    assertEquals(numThreads, distinctContexts.size());
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) TruffleContext(com.oracle.truffle.api.TruffleContext) Source(org.graalvm.polyglot.Source) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Engine(org.graalvm.polyglot.Engine) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TruffleContext

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

the class MultiThreadedLanguageTest method testAccessTruffleContextFromExclusivePolyglotThread.

@Test
public void testAccessTruffleContextFromExclusivePolyglotThread() throws Throwable {
    // don't allow multi-threading in this test as every context
    // is used exclusively by one thread.
    MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
        return req.singleThreaded;
    };
    final int iterations = 10;
    final int innerIterations = 10;
    AtomicReference<Throwable> lastError = new AtomicReference<>();
    UncaughtExceptionHandler uncaughtHandler = (run, e) -> lastError.set(e);
    Context polyglotContext = Context.newBuilder().allowCreateThread(true).build();
    ConcurrentHashMap<LanguageContext, String> seenContexts = new ConcurrentHashMap<>();
    eval(polyglotContext, new Function<Env, Object>() {

        @SuppressWarnings("hiding")
        public Object apply(Env env) {
            List<Thread> threads = new ArrayList<>();
            List<TruffleContext> contexts = new ArrayList<>();
            for (int i = 0; i < iterations; i++) {
                TruffleContext context = env.newContextBuilder().build();
                Thread thread = env.createThread(() -> {
                    assertUniqueContext();
                    List<Thread> innerThreads = new ArrayList<>();
                    List<TruffleContext> innerContexts = new ArrayList<>();
                    for (int j = 0; j < innerIterations; j++) {
                        TruffleContext innerContext = env.newContextBuilder().build();
                        Thread innerThread = env.createThread(() -> {
                            assertUniqueContext();
                        }, innerContext);
                        innerThread.setUncaughtExceptionHandler(uncaughtHandler);
                        innerThread.start();
                        innerThreads.add(innerThread);
                        innerContexts.add(innerContext);
                    }
                    for (Thread innerThread : innerThreads) {
                        try {
                            innerThread.join();
                        } catch (InterruptedException e) {
                        }
                    }
                    for (TruffleContext innerContext : innerContexts) {
                        innerContext.close();
                    }
                }, context);
                thread.setUncaughtExceptionHandler(uncaughtHandler);
                thread.start();
                threads.add(thread);
                contexts.add(context);
            }
            for (Thread thread : threads) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                }
            }
            for (TruffleContext context : contexts) {
                context.close();
            }
            return null;
        }

        private LanguageContext assertUniqueContext() {
            LanguageContext languageContext = MultiThreadedLanguage.getContext();
            Assert.assertNotNull(languageContext);
            Assert.assertFalse(seenContexts.containsKey(languageContext));
            seenContexts.put(languageContext, "");
            return languageContext;
        }
    });
    Assert.assertEquals(221, initializeCount.get());
    Assert.assertEquals(initializeCount.get() - 1, disposeCount.get());
    Assert.assertEquals(0, initializeMultiThreadingCount.get());
    // Test that the same context is available in threads when created with Env.getContext()
    MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
        return true;
    };
    eval(polyglotContext, new Function<Env, Object>() {

        @SuppressWarnings("hiding")
        public Object apply(Env env) {
            List<Thread> threads = new ArrayList<>();
            LanguageContext languageContext = MultiThreadedLanguage.getContext();
            for (int i = 0; i < iterations; i++) {
                Thread thread = env.createThread(() -> {
                    LanguageContext threadContext = MultiThreadedLanguage.getContext();
                    assertSame(languageContext, threadContext);
                    List<Thread> innerThreads = new ArrayList<>();
                    List<TruffleContext> innerContexts = new ArrayList<>();
                    for (int j = 0; j < innerIterations; j++) {
                        Thread innerThread = env.createThread(() -> {
                            LanguageContext innerThreadContext = MultiThreadedLanguage.getContext();
                            assertSame(languageContext, innerThreadContext);
                        }, env.getContext());
                        innerThread.setUncaughtExceptionHandler(uncaughtHandler);
                        innerThread.start();
                        innerThreads.add(innerThread);
                    }
                    for (Thread innerThread : innerThreads) {
                        try {
                            innerThread.join();
                        } catch (InterruptedException e) {
                        }
                    }
                    for (TruffleContext innerContext : innerContexts) {
                        innerContext.close();
                    }
                }, env.getContext());
                thread.setUncaughtExceptionHandler(uncaughtHandler);
                thread.start();
                threads.add(thread);
            }
            for (Thread thread : threads) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                }
            }
            return null;
        }
    });
    if (lastError.get() != null) {
        throw lastError.get();
    }
    polyglotContext.close();
    Assert.assertEquals(331, initializeCount.get());
    Assert.assertEquals(initializeCount.get(), disposeCount.get());
    Assert.assertEquals(1, initializeMultiThreadingCount.get());
}
Also used : 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) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) ArrayList(java.util.ArrayList) List(java.util.List) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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