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