protected static final class TruffleLanguage.ContextLocalProvider<C> extends Object
Modifier and Type | Method and Description |
---|---|
<T> ContextLocal<T> |
createContextLocal(TruffleLanguage.ContextLocalFactory<C,T> factory)
Creates a new context local reference for this Truffle language.
|
<T> ContextThreadLocal<T> |
createContextThreadLocal(TruffleLanguage.ContextThreadLocalFactory<C,T> factory)
Creates a new context thread local reference for this Truffle language.
|
public <T> ContextLocal<T> createContextLocal(TruffleLanguage.ContextLocalFactory<C,T> factory)
Context local references must be created during the invocation in the
TruffleLanguage
constructor. Calling this method at a later point in time will
throw an IllegalStateException
. For each registered TruffleLanguage
subclass it is required to always produce the same number of context local references.
The values produced by the factory must not be null
and use a stable exact
value type for each instance of a registered language class. If the return value of the
factory is not stable or null
then an IllegalStateException
is
thrown. These restrictions allow the Truffle runtime to read the value more efficiently.
Usage example:
@TruffleLanguage.Registration(id = "example", name = "ExampleLanguage") public final class ExampleLanguage extends TruffleLanguage{ final ContextLocal contextLocal = locals.createContextLocal(ExampleLocal::new); @Override protected Env createContext(Env env) { return env; } @Override protected CallTarget parse(ParsingRequest request) throws Exception { return new RootNode(this) { @Override public Object execute(VirtualFrame frame) { // fast read ExampleLocal local = contextLocal.get(); // access local return ""; } }.getCallTarget(); } static final class ExampleLocal { final Env env; ExampleLocal(Env env) { this.env = env; } } }
public <T> ContextThreadLocal<T> createContextThreadLocal(TruffleLanguage.ContextThreadLocalFactory<C,T> factory)
Context thread local references must be created during the invocation in the
TruffleLanguage
constructor. Calling this method at a later point in time will
throw an IllegalStateException
. For each registered TruffleLanguage
subclass it is required to always produce the same number of context thread local
references. The values produced by the factory must not be null
and use a
stable exact value type for each instance of a registered language class. If the return
value of the factory is not stable or null
then an
IllegalStateException
is thrown. These restrictions allow the Truffle runtime to
read the value more efficiently.
Context thread locals should not contain a strong reference to the provided thread. Use a weak reference instance for that purpose.
Usage example:
@TruffleLanguage.Registration(id = "example", name = "ExampleLanguage") public static class ExampleLanguage extends TruffleLanguage{ final ContextThreadLocal threadLocal = locals.createContextThreadLocal(ExampleLocal::new); @Override protected Env createContext(Env env) { return env; } @Override protected CallTarget parse(ParsingRequest request) throws Exception { return new RootNode(this) { @Override public Object execute(VirtualFrame frame) { // fast read ExampleLocal local = threadLocal.get(); // access local return ""; } }.getCallTarget(); } static final class ExampleLocal { final Env env; final WeakReference thread; ExampleLocal(Env env, Thread thread) { this.env = env; this.thread = new WeakReference<>(thread); } } }