public static enum TruffleLanguage.ContextPolicy extends Enum<TruffleLanguage.ContextPolicy>
languages
per context. I.e.
the policy specifies the degree of sharing that is allowed between multiple language
contexts. The default policy is exclusive
. Every language is encouraged to
try to support a context policy that is as permissive as possible, where exclusive
is the least and shared
is the most permissive policy.
Parse caching
is scoped per
language
instance, therefore the context policy influences its
behavior.
If multiple sharable languages are used at the same time, nodes of sharable languages may
adopt nodes of a non-sharable languages indirectly, e.g. through Truffle interoperability.
This would complicate language implementations as they would always need to support sharing
for their nodes. To avoid this problem, Truffle uses sharing layers. Sharing layers ensure
that every node created by a language instance can only be observed by a single language
instance at a time. Sharing layers operate on the principle that all languages share the code
with the same language instance or no language shares. As a convenient consequence any call
to TruffleLanguage.LanguageReference.get(Node)
with an adopted Node
can fold to a constant
during partial evaluation.
For any sharing to take place a context must be created with sharing enabled. By default
sharing is disabled for polyglot contexts. Sharing can be enabled by specifying an
explicit engine
or using an option. Before any language is
used for sharing TruffleLanguage.initializeMultipleContexts()
is invoked. It is
guaranteed that sharing between multiple contexts is
initialized
before any language context
is created
.
Enum.EnumDesc<E extends Enum<E>>
Enum Constant and Description |
---|
EXCLUSIVE
Use one exclusive
TruffleLanguage instance per language context instance. |
REUSE
Use a single
TruffleLanguage instance per context instance, but allow the reuse
of a language instance when a context was disposed . |
SHARED
Use one
TruffleLanguage instance for many language context instances. |
Modifier and Type | Method and Description |
---|---|
static TruffleLanguage.ContextPolicy |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static TruffleLanguage.ContextPolicy[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final TruffleLanguage.ContextPolicy EXCLUSIVE
TruffleLanguage
instance per language context instance.
Using this policy has the following implications:
Parse caching
is scoped per
language
instance. This means the language context instance may
be directly stored in instance fields of AST nodes without the use of
TruffleLanguage.ContextReference
. The use of TruffleLanguage.ContextReference
is still recommended to
simplify migration to more permissive policies.
multi-threading
(default
behavior) then the language instance is guaranteed to be used from one thread at a time
only. Cached ASTs will not be used from multiple threads at the same time.
TruffleLanguage.initializeMultipleContexts()
is guaranteed to never be
invoked.
public static final TruffleLanguage.ContextPolicy REUSE
TruffleLanguage
instance per context instance, but allow the reuse
of a language instance when a context was disposed
. This policy is useful when parsed ASTs should be shared, but multiple thread
execution of the AST is not yet supported by the language.
Using this policy has the following implications:
Parse caching
is scoped per
language
instance. This means language context instances must NOT
be directly stored in instance fields of AST nodes and the TruffleLanguage.ContextReference
must
be used instead.
allow
access from multiple
threads (default behavior) then the language instance is guaranteed to be used from one
thread at a time only. In this case also cached ASTs will not be used from multiple
threads at the same time. If the language allows access from multiple threads then also
ASTs may be used from multiple threads at the same time.
TruffleLanguage.initializeMultipleContexts()
will be invoked to notify the
language that multiple contexts will be used with one language instance.
Language
instance fields must only be used for data that can
be shared across multiple contexts.
public static final TruffleLanguage.ContextPolicy SHARED
TruffleLanguage
instance for many language context instances.
Using this policy has the following implications:
Parse caching
is scoped per
language
instance. With multiple language instances per context,
context instances must not be directly stored in instance fields of AST nodes and
the TruffleLanguage.ContextReference
must be used instead.
language
instance and parsed ASTs may be
called from multiple threads at the same time independent of whether multiple thread
access is allowed
for the
language.
TruffleLanguage.initializeMultipleContexts()
will be invoked to notify the
language that multiple contexts will be used with one language instance.
Language
instance fields must only be used for data that can
be shared across multiple contexts and mutable data held by the language instance must be
synchronized to support concurrent access.
public static TruffleLanguage.ContextPolicy[] values()
public static TruffleLanguage.ContextPolicy valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null