# HG changeset patch # User Jaroslav Tulach # Date 1435047790 -7200 # Node ID 9124ca6c46b01c5ac038aa2b6c00466360ec7c1f # Parent 112aba5e3e12f931f118bf8621cca1e61fa6c24b ForeignAccess is singlethreaded. Accessing objects from other languages (via ForeignAccess) will only be possible from a previously selected thread. diff -r 112aba5e3e12 -r 9124ca6c46b0 mx.truffle/suite.py --- a/mx.truffle/suite.py Mon Jun 22 15:25:07 2015 -0700 +++ b/mx.truffle/suite.py Tue Jun 23 10:23:10 2015 +0200 @@ -169,7 +169,7 @@ "subDir" : "truffle", "sourceDirs" : ["src"], "dependencies" : [ - "com.oracle.truffle.api", + "com.oracle.truffle.api.interop", "JUNIT", ], "checkstyle" : "com.oracle.truffle.dsl.processor", diff -r 112aba5e3e12 -r 9124ca6c46b0 truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/ForeignAccess.java --- a/truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/ForeignAccess.java Mon Jun 22 15:25:07 2015 -0700 +++ b/truffle/com.oracle.truffle.api.interop/src/com/oracle/truffle/api/interop/ForeignAccess.java Tue Jun 23 10:23:10 2015 +0200 @@ -39,9 +39,11 @@ */ public final class ForeignAccess { private final Factory factory; + private final Thread initThread; private ForeignAccess(Factory faf) { this.factory = faf; + this.initThread = Thread.currentThread(); } /** @@ -150,11 +152,19 @@ return (TruffleObject) frame.getArguments()[ForeignAccessArguments.RECEIVER_INDEX]; } + private void checkThread() { + if (initThread != Thread.currentThread()) { + throw new IllegalStateException("ForeignAccess created on " + initThread.getName() + " but used on " + Thread.currentThread().getName()); + } + } + CallTarget access(Message message) { + checkThread(); return factory.accessMessage(message); } boolean canHandle(TruffleObject receiver) { + checkThread(); return factory.canHandle(receiver); } diff -r 112aba5e3e12 -r 9124ca6c46b0 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/interop/ForeignAccessSingleThreadedTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/interop/ForeignAccessSingleThreadedTest.java Tue Jun 23 10:23:10 2015 +0200 @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.api.test.interop; + +import com.oracle.truffle.api.CallTarget; +import com.oracle.truffle.api.interop.ForeignAccess; +import com.oracle.truffle.api.interop.Message; +import com.oracle.truffle.api.interop.TruffleObject; +import com.oracle.truffle.api.nodes.Node; + +import org.junit.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class ForeignAccessSingleThreadedTest implements ForeignAccess.Factory, TruffleObject { + ForeignAccess fa; + private int cnt; + + @Before + public void initInDifferentThread() throws InterruptedException { + Thread t = new Thread("Initializer") { + @Override + public void run() { + fa = ForeignAccess.create(ForeignAccessSingleThreadedTest.this); + } + }; + t.start(); + t.join(); + } + + @Test(expected = IllegalStateException.class) + public void accessNodeFromWrongThread() { + Node n = Message.IS_EXECUTABLE.createNode(); + Object ret = ForeignAccess.execute(n, null, this); + fail("Should throw an exception: " + ret); + } + + @After + public void noCallsToFactory() { + assertEquals("No calls to accessMessage or canHandle", 0, cnt); + } + + @Override + public boolean canHandle(TruffleObject obj) { + cnt++; + return true; + } + + @Override + public CallTarget accessMessage(Message tree) { + cnt++; + return null; + } + + @Override + public ForeignAccess getForeignAccess() { + return fa; + } +}