org.pkl.thirdparty.truffle.polyglot.PolyglotFastThreadLocals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-config-java-all Show documentation
Show all versions of pkl-config-java-all Show documentation
Shaded fat Jar for pkl-config-java, a Java config library based on the Pkl config language.
/*
* Copyright (c) 2021, 2023, 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 org.pkl.thirdparty.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.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.pkl.thirdparty.truffle.api.CompilerAsserts;
import org.pkl.thirdparty.truffle.api.CompilerDirectives;
import org.pkl.thirdparty.truffle.api.TruffleLanguage;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.ContextReference;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.LanguageReference;
import org.pkl.thirdparty.truffle.api.impl.AbstractFastThreadLocal;
import org.pkl.thirdparty.truffle.api.nodes.EncapsulatingNodeReference;
import org.pkl.thirdparty.truffle.api.nodes.Node;
import org.pkl.thirdparty.truffle.api.nodes.RootNode;
import org.pkl.thirdparty.truffle.polyglot.EngineAccessor.AbstractClassLoaderSupplier;
// 0: PolyglotThreadInfo
// 1: PolyglotContextImpl
// 2: EncapsulatingNodeReference
// 3 + (languageIndex * 2 + 0): language context impl for fast access
// 3 + (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 Object NOT_ENTERED = new Object();
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 ENCAPSULATING_NODE_REFERENCE_INDEX = 2;
private static final int LANGUAGE_START = 3;
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 = createEmptyData(thread.context.engine);
data[THREAD_INDEX] = thread;
data[CONTEXT_INDEX] = thread.context;
data[ENCAPSULATING_NODE_REFERENCE_INDEX] = EngineAccessor.NODES.createEncapsulatingNodeReference(thread.getThread());
for (PolyglotLanguageContext languageContext : thread.context.contexts) {
if (languageContext.isCreated()) {
updateLanguageObjects(data, languageContext);
}
}
return data;
}
private static Object[] createFastThreadLocalsForLanguage(PolyglotLanguageInstance instance) {
Object[] data = createEmptyData(instance.language.engine);
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;
}
static Object[] createFastThreadLocals(PolyglotEngineImpl engine, PolyglotLanguageInstance[] instances) {
Object[] data = createEmptyData(engine);
for (PolyglotLanguageInstance instance : instances) {
if (instance != null) {
int index = getLanguageIndex(instance);
data[LANGUAGE_SPI_OFFSET + index] = instance.spi;
}
}
return data;
}
private static Object[] createEmptyData(PolyglotEngineImpl engine) {
return new Object[LANGUAGE_START + (engine.languages.length * LANGUAGE_ELEMENTS)];
}
private static int getLanguageIndex(PolyglotLanguageInstance instance) {
return LANGUAGE_START + (instance.language.cache.getStaticIndex() * LANGUAGE_ELEMENTS);
}
@SuppressWarnings({"unchecked"})
public static > LanguageReference createLanguageReference(Class extends TruffleLanguage>> language) {
return (LanguageReference) lookupReferences(language).languageReference;
}
@SuppressWarnings("unchecked")
public 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 Object enterLayer(RootNode root) {
PolyglotSharingLayer layer = (PolyglotSharingLayer) EngineAccessor.NODES.getSharingLayer(root);
PolyglotContextImpl context = PolyglotFastThreadLocals.getContext(layer);
// Enter the layer unless a context with that layer is entered already
if (context == null || !context.layer.equals(layer)) {
Object[] prev = IMPL.get();
IMPL.set(layer.getFastThreadLocals());
return prev;
}
return NOT_ENTERED;
}
public static void leaveLayer(Object prev) {
if (prev != NOT_ENTERED) {
IMPL.set((Object[]) prev);
}
}
public static void cleanup(Object[] threadLocals) {
Arrays.fill(threadLocals, null);
}
static EncapsulatingNodeReference getEncapsulatingNodeReference(boolean invalidateOnNull) {
/*
* It is tempting to constant fold here for single thread contexts using a Node. However, I
* was unable to measure a speedup from doing this compared to reading the fast thread local
* instead. So we do not bother here and trade a bit smaller code for fewer deoptimizations
* and less footprint (no assumptions in use).
*/
return IMPL.fastGet(ENCAPSULATING_NODE_REFERENCE_INDEX, EncapsulatingNodeReference.class, invalidateOnNull);
}
public static PolyglotThreadInfo getCurrentThread(PolyglotSharingLayer layer) {
if (CompilerDirectives.inCompiledCode() && layer != null) {
PolyglotContextImpl singleContext = layer.getSingleConstantContext();
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 PolyglotThreadInfo getCurrentThreadEngine(PolyglotEngineImpl engine) {
if (CompilerDirectives.inCompiledCode() && engine != null) {
PolyglotContextImpl singleContext = engine.singleContextValue.getConstant();
if (singleContext != null) {
PolyglotThreadInfo constantThread = singleContext.singleThreadValue.getConstant();
if (constantThread != null) {
return constantThread;
}
}
}
return IMPL.fastGet(THREAD_INDEX, PolyglotThreadInfo.class, true);
}
public static PolyglotContextImpl getContext(PolyglotSharingLayer layer) {
if (CompilerDirectives.inCompiledCode() && layer != null) {
PolyglotContextImpl value = layer.getSingleConstantContext();
if (value != null) {
return value;
}
}
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
}
public static PolyglotContextImpl getContextWithEngine(PolyglotEngineImpl engine) {
if (CompilerDirectives.inCompiledCode() && engine != null) {
PolyglotContextImpl context = engine.singleContextValue.getConstant();
if (context != null) {
return context;
}
}
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
}
public static PolyglotContextImpl getContextWithNode(Node node) {
if (CompilerDirectives.inCompiledCode()) {
PolyglotSharingLayer layer = resolveLayer(node);
if (layer != null) {
return layer.getSingleConstantContext();
}
}
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
}
@SuppressWarnings("unchecked")
public static TruffleLanguage
© 2015 - 2024 Weber Informatics LLC | Privacy Policy