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();
}
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();
}
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();
}
Aggregations