com.oracle.truffle.polyglot.PolyglotFastThreadLocals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truffle-api Show documentation
Show all versions of truffle-api Show documentation
Truffle is a multi-language framework for executing dynamic languages
that achieves high performance when combined with Graal.
/*
* Copyright (c) 2021, 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
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.polyglot;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
import com.oracle.truffle.api.impl.AbstractFastThreadLocal;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.polyglot.EngineAccessor.AbstractClassLoaderSupplier;
import com.oracle.truffle.polyglot.EngineAccessor.EngineImpl;
// 0: PolyglotThreadInfo
// 1: PolyglotContextImpl
// 2 + (languageIndex * 2 + 0): language context impl for fast access
// 2 + (languageIndex * 2 + 1): language spi for fast access
final class PolyglotFastThreadLocals {
private static final AbstractFastThreadLocal IMPL = EngineAccessor.RUNTIME.getContextThreadLocal();
private static final ConcurrentHashMap, Map> CLASS_NAME_CACHE = new ConcurrentHashMap<>();
private static final ConcurrentHashMap, CachedReferences> CONTEXT_REFERENCE_CACHE = new ConcurrentHashMap<>();
private static final FinalIntMap LANGUAGE_INDEXES = new FinalIntMap();
private static final int RESERVED_NULL = -1; // never set
private static final int THREAD_INDEX = 0;
static final int CONTEXT_INDEX = 1;
private static final int LANGUAGE_START = 2;
static final int LANGUAGE_CONTEXT_OFFSET = 0;
static final int LANGUAGE_SPI_OFFSET = 1;
private static final int LANGUAGE_ELEMENTS = 2;
static void resetNativeImageState() {
CONTEXT_REFERENCE_CACHE.clear();
CLASS_NAME_CACHE.clear();
}
static Object[] createFastThreadLocals(PolyglotThreadInfo thread) {
PolyglotContextImpl context = thread.context;
assert Thread.holdsLock(context);
Object[] data = new Object[LANGUAGE_START + (thread.context.engine.languages.length * LANGUAGE_ELEMENTS)];
data[THREAD_INDEX] = thread;
data[CONTEXT_INDEX] = thread.context;
for (PolyglotLanguageContext languageContext : thread.context.contexts) {
if (languageContext.isCreated()) {
updateLanguageObjects(data, languageContext);
}
}
return data;
}
private static Object[] createFastThreadLocalsForLanguage(PolyglotLanguageInstance instance) {
Object[] data = new Object[LANGUAGE_START + (instance.language.engine.languages.length * LANGUAGE_ELEMENTS)];
data[THREAD_INDEX] = null; // not available if only engine is entered
data[CONTEXT_INDEX] = null; // not available if only engine is entered
// we take the first language we find. should we fail maybe if there is more than one?
data[getLanguageIndex(instance) + LANGUAGE_SPI_OFFSET] = instance.spi;
return data;
}
private static int getLanguageIndex(PolyglotLanguageInstance instance) {
return LANGUAGE_START + (instance.language.cache.getStaticIndex() * LANGUAGE_ELEMENTS);
}
@SuppressWarnings({"unchecked"})
public static > LanguageReference createLanguageReference(Node legacyNode, Class extends TruffleLanguage>> language) {
LanguageReference ref = createLanguageReference(language);
if (legacyNode != null) {
return new LegacyLanguageReference<>(legacyNode, ref);
}
return ref;
}
@SuppressWarnings({"unchecked"})
private static > LanguageReference createLanguageReference(Class extends TruffleLanguage>> language) {
return (LanguageReference) lookupReferences(language).languageReference;
}
@SuppressWarnings("unchecked")
@TruffleBoundary
public static ContextReference createContextReference(Node legacyNode, Class extends TruffleLanguage> language) {
ContextReference ref = createContextReference(language);
if (legacyNode != null) {
return new LegacyContextReference<>(legacyNode, ref);
}
return ref;
}
@SuppressWarnings("unchecked")
private static ContextReference createContextReference(Class extends TruffleLanguage> language) {
return (ContextReference) lookupReferences(language).contextReference;
}
public static boolean needsEnter(PolyglotContextImpl context) {
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, false) != context;
}
public static Object[] enter(PolyglotThreadInfo threadInfo) {
Object[] prev = IMPL.get();
IMPL.set(threadInfo.fastThreadLocals);
return prev;
}
public static void leave(Object[] prev) {
IMPL.set(prev);
}
public static Object enterLanguage(PolyglotLanguageInstance language) {
Object[] prev = IMPL.get();
IMPL.set(createFastThreadLocalsForLanguage(language));
return prev;
}
public static void leaveLanguage(PolyglotLanguageInstance instance, Object prev) {
assert IMPL.get()[getLanguageIndex(instance) + LANGUAGE_SPI_OFFSET] != null : "language not entered";
IMPL.set((Object[]) prev);
}
public static void cleanup(Object[] threadLocals) {
Arrays.fill(threadLocals, null);
}
public static PolyglotThreadInfo getCurrentThread(PolyglotEngineImpl inEngine) {
if (CompilerDirectives.inCompiledCode() && inEngine != null) {
PolyglotContextImpl singleContext = inEngine.singleContextValue.getConstant();
if (singleContext != null && CompilerDirectives.isPartialEvaluationConstant(singleContext)) {
PolyglotThreadInfo constantThread = singleContext.singleThreadValue.getConstant();
if (constantThread != null) {
return constantThread;
}
}
}
return IMPL.fastGet(THREAD_INDEX, PolyglotThreadInfo.class, true);
}
public static PolyglotContextImpl getContext(PolyglotEngineImpl inEngine) {
if (CompilerDirectives.inCompiledCode() && inEngine != null) {
Object value = inEngine.singleContextValue.getConstant();
if (value != null) {
return (PolyglotContextImpl) value;
}
}
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
}
@SuppressWarnings("unchecked")
public static TruffleLanguage