Skip to content

Commit

Permalink
[GR-35703] ProcessHandler for TruffleIsolates.
Browse files Browse the repository at this point in the history
PullRequest: graal/10706
  • Loading branch information
tzezula committed Feb 5, 2022
2 parents 5af81b2 + 5e3b523 commit 749e1e3
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ public Thread newThread(Runnable r) {
@Override
public void run() {
setContextClassLoader(getClass().getClassLoader());
try (AutoCloseable scope = runtime.openCompilerThreadScope()) {
try (AutoCloseable compilerThreadScope = runtime.openCompilerThreadScope();
AutoCloseable polyglotThreadScope = GraalRuntimeAccessor.ENGINE.createPolyglotThreadScope()) {
super.run();
if (compilationExecutorService.allowsCoreThreadTimeOut()) {
// If core threads are always kept alive (no timeout), the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -91,6 +91,7 @@
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractValueDispatch;
import org.graalvm.polyglot.io.FileSystem;
import org.graalvm.polyglot.io.MessageTransport;
import org.graalvm.polyglot.io.ProcessHandler;

/**
* An execution engine for Graal {@linkplain Language guest languages} that allows to inspect the
Expand Down Expand Up @@ -1019,6 +1020,21 @@ public FileSystem newDefaultFileSystem() {
throw noPolyglotImplementationFound();
}

@Override
public ProcessHandler newDefaultProcessHandler() {
throw noPolyglotImplementationFound();
}

@Override
public boolean isDefaultProcessHandler(ProcessHandler processHandler) {
return false;
}

@Override
public ThreadScope createThreadScope() {
return null;
}

@Override
public <S, T> Object newTargetTypeMapping(Class<S> sourceType, Class<T> targetType, Predicate<S> acceptsValue, Function<S, T> convertValue, TargetMappingPrecedence precedence) {
return new Object();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,18 @@ public FileSystem newDefaultFileSystem() {
return getNext().newDefaultFileSystem();
}

public ProcessHandler newDefaultProcessHandler() {
return getNext().newDefaultProcessHandler();
}

public boolean isDefaultProcessHandler(ProcessHandler processHandler) {
return getNext().isDefaultProcessHandler(processHandler);
}

public ThreadScope createThreadScope() {
return getNext().createThreadScope();
}

/**
* Creates a union of all available option descriptors including prev implementations. This
* allows to validate the full set of options.
Expand All @@ -1106,4 +1118,22 @@ protected OptionDescriptors createEngineOptionDescriptors() {
return OptionDescriptors.EMPTY;
}

public final AbstractPolyglotImpl getRootImpl() {
AbstractPolyglotImpl current = this;
while (current.prev != null) {
current = current.prev;
}
return current;
}

public abstract static class ThreadScope implements AutoCloseable {

protected ThreadScope(AbstractPolyglotImpl engineImpl) {
Objects.requireNonNull(engineImpl);
}

@Override
public abstract void close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ protected AbstractFastThreadLocal getFastThreadLocalImpl() {
return SubstrateFastThreadLocal.SINGLETON;
}

@Override
protected AutoCloseable openCompilerThreadScope() {
return new CompilerThreadScope();
}

private void initializeAtRuntime(OptimizedCallTarget callTarget) {
truffleCompiler.initialize(getOptionsForCompiler(callTarget), callTarget, true);
if (SubstrateTruffleOptions.isMultiThreaded()) {
Expand Down Expand Up @@ -421,21 +416,4 @@ public boolean hasNextTier() {
}

}

private static final class CompilerThreadScope implements AutoCloseable {

CompilerThreadScope() {
open();
}

// Substituted by EnterpriseTruffleFeature
private void open() {
}

// Substituted by EnterpriseTruffleFeature
@Override
public void close() {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ public abstract <T, G> Iterator<T> mergeHostGuestFrames(Object instrumentEnv, St
public abstract boolean getNeedsAllEncodings();

public abstract boolean requireLanguageWithAllEncodings(Object encoding);

public abstract AutoCloseable createPolyglotThreadScope();
}

public abstract static class LanguageSupport extends Support {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,8 @@ public Process createSubProcess(Object polyglotLanguageContext, List<String> cmd

@Override
public boolean hasDefaultProcessHandler(Object polyglotLanguageContext) {
return ProcessHandlers.isDefault(((PolyglotLanguageContext) polyglotLanguageContext).context.config.processHandler);
PolyglotLanguageContext context = (PolyglotLanguageContext) polyglotLanguageContext;
return context.getImpl().getRootImpl().isDefaultProcessHandler(context.context.config.processHandler);
}

@Override
Expand Down Expand Up @@ -1619,6 +1620,11 @@ public Object getGuestToHostCodeCache(Object polyglotContextImpl) {
public Object installGuestToHostCodeCache(Object polyglotContextImpl, Object cache) {
return ((PolyglotContextImpl) polyglotContextImpl).getHostContext().getLanguageInstance().installGuestToHostCodeCache(cache);
}

@Override
public AutoCloseable createPolyglotThreadScope() {
return PolyglotImpl.getInstance().getRootImpl().createThreadScope();
}
}

abstract static class AbstractClassLoaderSupplier implements Supplier<ClassLoader> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ public PolyglotContextImpl createContext(OutputStream configOut, OutputStream co
if (!ALLOW_CREATE_PROCESS) {
throw PolyglotEngineException.illegalArgument("Cannot allowCreateProcess() because the privilege is removed at image build time");
}
useProcessHandler = processHandler != null ? processHandler : ProcessHandlers.newDefaultProcessHandler();
useProcessHandler = processHandler != null ? processHandler : getImpl().newDefaultProcessHandler();
} else {
useProcessHandler = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.graalvm.polyglot.io.ByteSequence;
import org.graalvm.polyglot.io.FileSystem;
import org.graalvm.polyglot.io.MessageTransport;
import org.graalvm.polyglot.io.ProcessHandler;
import org.graalvm.polyglot.proxy.Proxy;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
Expand Down Expand Up @@ -456,6 +457,21 @@ public FileSystem newDefaultFileSystem() {
return FileSystems.newDefaultFileSystem();
}

@Override
public ProcessHandler newDefaultProcessHandler() {
return ProcessHandlers.newDefaultProcessHandler();
}

@Override
public boolean isDefaultProcessHandler(ProcessHandler processHandler) {
return ProcessHandlers.isDefault(processHandler);
}

@Override
public ThreadScope createThreadScope() {
return null;
}

@Override
public AbstractHostAccess createHostAccess() {
return new PolyglotHostAccess(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.polyglot.PolyglotEngineImpl.CancelExecution;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.ThreadScope;

final class PolyglotThread extends Thread {

Expand Down Expand Up @@ -127,10 +128,11 @@ public Object execute(VirtualFrame frame) {
}

@TruffleBoundary
@SuppressWarnings("try")
private static Object executeImpl(PolyglotLanguageContext languageContext, PolyglotThread thread, PolyglotThreadRunnable run) {
Object[] prev = languageContext.enterThread(thread);
assert prev == null; // is this assertion correct?
try {
try (ThreadScope scope = languageContext.getImpl().getRootImpl().createThreadScope()) {
run.execute();
} catch (CancelExecution cancel) {
if (PolyglotEngineOptions.TriggerUncaughtExceptionHandlerForCancel.getValue(languageContext.context.engine.getEngineOptionValues())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.graalvm.polyglot.impl.AbstractPolyglotImpl;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.ThreadScope;
import org.graalvm.polyglot.io.ProcessHandler;

final class ProcessHandlers {
Expand Down Expand Up @@ -122,8 +125,8 @@ private ProcessDecorator(
this.owner = new WeakReference<>(owner);
this.command = command;
this.delegate = delegate;
this.outCopier = out == null ? null : new CopierThread(createThreadName(owner, command, "stdout"), delegate.getInputStream(), out);
this.errCopier = err == null ? null : new CopierThread(createThreadName(owner, command, "stderr"), delegate.getErrorStream(), err);
this.outCopier = out == null ? null : new CopierThread(owner.getImpl(), createThreadName(owner, command, "stdout"), delegate.getInputStream(), out);
this.errCopier = err == null ? null : new CopierThread(owner.getImpl(), createThreadName(owner, command, "stderr"), delegate.getErrorStream(), err);
if (outCopier != null) {
outCopier.start();
}
Expand Down Expand Up @@ -234,23 +237,27 @@ private static final class CopierThread extends Thread {

private static final int BUFSIZE = 8192;

private final AbstractPolyglotImpl polyglot;
private final InputStream in;
private final OutputStream out;
private final byte[] buffer;

CopierThread(String name, InputStream in, OutputStream out) {
CopierThread(AbstractPolyglotImpl polyglot, String name, InputStream in, OutputStream out) {
Objects.requireNonNull(polyglot, "Polyglot must be non null.");
Objects.requireNonNull(name, "Name must be non null.");
Objects.requireNonNull(in, "In must be non null.");
Objects.requireNonNull(out, "Out must be non null.");
setName(name);
this.polyglot = polyglot;
this.in = in;
this.out = out;
this.buffer = new byte[BUFSIZE];
}

@Override
@SuppressWarnings("try")
public void run() {
try {
try (ThreadScope scope = polyglot.getRootImpl().createThreadScope()) {
while (true) {
if (isInterrupted()) {
return;
Expand Down

0 comments on commit 749e1e3

Please sign in to comment.