protected static final class TruffleInstrument.ContextLocalProvider extends Object
Modifier and Type | Method and Description |
---|---|
<T> ContextLocal<T> |
createContextLocal(TruffleInstrument.ContextLocalFactory<T> factory)
Creates a new context local reference for this instrument.
|
<T> ContextThreadLocal<T> |
createContextThreadLocal(TruffleInstrument.ContextThreadLocalFactory<T> factory)
Creates a new context thread local reference for this Truffle language.
|
public <T> ContextLocal<T> createContextLocal(TruffleInstrument.ContextLocalFactory<T> factory)
created
.
Context local references must be created during the invocation in the
TruffleInstrument
constructor. Calling this method at a later point in time will
throw an IllegalStateException
. For each registered TruffleInstrument
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 instrument 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:
@TruffleInstrument.Registration(id = "example", name = "Example Instrument") public static class ExampleInstrument extends TruffleInstrument { final ContextLocallocal = locals.createContextLocal(ExampleLocal::new); @Override protected void onCreate(Env env) { ExecutionEventListener listener = new ExecutionEventListener() { public void onEnter(EventContext context, VirtualFrame frame) { ExampleLocal value = local.get(); // use context local value; } public void onReturnValue(EventContext context, VirtualFrame frame, Object result) { } public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) { } }; env.getInstrumenter().attachExecutionEventListener( SourceSectionFilter.ANY, listener); } static class ExampleLocal { final TruffleContext context; ExampleLocal(TruffleContext context) { this.context = context; } } }
public <T> ContextThreadLocal<T> createContextThreadLocal(TruffleInstrument.ContextThreadLocalFactory<T> factory)
created
.
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 produces 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:
@TruffleInstrument.Registration(id = "example", name = "Example Instrument") public static class ExampleInstrument extends TruffleInstrument { final ContextThreadLocallocal = locals.createContextThreadLocal(ExampleLocal::new); @Override protected void onCreate(Env env) { ExecutionEventListener listener = new ExecutionEventListener() { public void onEnter(EventContext context, VirtualFrame frame) { ExampleLocal value = local.get(); // use thread local value; assert value.thread.get() == Thread.currentThread(); } public void onReturnValue(EventContext context, VirtualFrame frame, Object result) { } public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) { } }; env.getInstrumenter().attachExecutionEventListener( SourceSectionFilter.ANY, listener); } static class ExampleLocal { final TruffleContext context; final WeakReference thread; ExampleLocal(TruffleContext context, Thread thread) { this.context = context; this.thread = new WeakReference<>(thread); } } }