Search in sources :

Example 46 with Instrument

use of org.graalvm.polyglot.Instrument in project graal by oracle.

the class PolyglotEngineImpl method ensureClosed.

@Override
public synchronized void ensureClosed(boolean cancelIfExecuting, boolean ignoreCloseFailure) {
    if (!closed) {
        PolyglotContextImpl[] localContexts = contexts.toArray(new PolyglotContextImpl[0]);
        /*
             * Check ahead of time for open contexts to fail early and avoid closing only some
             * contexts.
             */
        if (!cancelIfExecuting && !ignoreCloseFailure) {
            for (PolyglotContextImpl context : localContexts) {
                synchronized (context) {
                    if (context.hasActiveOtherThread(false)) {
                        throw new IllegalStateException(String.format("One of the context instances is currently executing. " + "Set cancelIfExecuting to true to stop the execution on this thread."));
                    }
                }
            }
        }
        for (PolyglotContextImpl context : localContexts) {
            try {
                boolean closeCompleted = context.closeImpl(cancelIfExecuting, cancelIfExecuting);
                if (!closeCompleted && !cancelIfExecuting && !ignoreCloseFailure) {
                    throw new IllegalStateException(String.format("One of the context instances is currently executing. " + "Set cancelIfExecuting to true to stop the execution on this thread."));
                }
            } catch (Throwable e) {
                if (!ignoreCloseFailure) {
                    throw e;
                }
            }
        }
        if (cancelIfExecuting) {
            getCancelHandler().waitForClosing(localContexts);
        }
        contexts.clear();
        for (Instrument instrument : idToPublicInstrument.values()) {
            PolyglotInstrument instrumentImpl = (PolyglotInstrument) getAPIAccess().getImpl(instrument);
            try {
                instrumentImpl.ensureClosed();
            } catch (Throwable e) {
                if (!ignoreCloseFailure) {
                    throw e;
                }
            }
        }
        for (PolyglotLanguage language : idToLanguage.values()) {
            if (language.isInitialized()) {
                language.requireProfile().notifyEngineDisposed();
            }
        }
        ENGINES.remove(this);
        closed = true;
    }
}
Also used : Instrument(org.graalvm.polyglot.Instrument)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2