Search in sources :

Example 6 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationListenerTest method testMultiContext.

@Test
public void testMultiContext() {
    Engine engine = Engine.create();
    setupEnv(Context.newBuilder().engine(engine).build(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    Context c0 = this.context;
    TruffleContext tc0 = this.languageEnv.getContext();
    setupEnv(Context.newBuilder().engine(engine).build(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    Context c1 = this.context;
    TruffleContext tc1 = languageEnv.getContext();
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            entered.add(c);
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            left.add(c);
        }
    });
    assertList(entered);
    assertList(left);
    c0.enter();
    assertList(entered, tc0);
    assertList(left);
    c1.enter();
    assertList(entered, tc0, tc1);
    assertList(left);
    c1.leave();
    assertList(entered, tc0, tc1);
    assertList(left, tc1);
    c0.leave();
    assertList(entered, tc0, tc1);
    assertList(left, tc1, tc0);
    binding.dispose();
    c0.enter();
    c1.enter();
    c1.leave();
    c0.leave();
    assertList(entered, tc0, tc1);
    assertList(left, tc1, tc0);
    c0.close();
    c1.close();
    engine.close();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 7 with ThreadsActivationListener

use of com.oracle.truffle.api.instrumentation.ThreadsActivationListener in project graal by oracle.

the class ThreadsActivationListenerTest method testMultiThreading.

@Test
public void testMultiThreading() throws InterruptedException {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }
    });
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            synchronized (entered) {
                entered.add(c);
            }
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            synchronized (left) {
                left.add(c);
            }
        }
    });
    assertList(entered);
    assertList(left);
    TruffleContext to = languageEnv.getContext();
    context.enter();
    assertList(entered, to);
    assertList(left);
    Thread t = new Thread(new Runnable() {

        public void run() {
            assertList(entered, to);
            assertList(left);
            context.enter();
            assertList(entered, to, to);
            assertList(left);
            context.leave();
            assertList(entered, to, to);
            assertList(left, to);
        }
    });
    t.start();
    t.join();
    context.leave();
    assertList(entered, to, to);
    assertList(left, to, to);
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Aggregations

TruffleContext (com.oracle.truffle.api.TruffleContext)7 ThreadsActivationListener (com.oracle.truffle.api.instrumentation.ThreadsActivationListener)7 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)6 PolyglotException (org.graalvm.polyglot.PolyglotException)6 Test (org.junit.Test)6 CallTarget (com.oracle.truffle.api.CallTarget)5 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)5 ArrayList (java.util.ArrayList)4 LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Context (org.graalvm.polyglot.Context)2 Assumption (com.oracle.truffle.api.Assumption)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 LanguageSPIOrderTest (com.oracle.truffle.api.test.polyglot.LanguageSPIOrderTest)1 Semaphore (java.util.concurrent.Semaphore)1 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)1 Engine (org.graalvm.polyglot.Engine)1