ScopedValue: Capturing current bindings
Andrew Haley
aph-open at littlepinkcloud.com
Tue May 30 09:13:45 UTC 2023
On 5/23/23 15:07, Attila Kelemen wrote:
> What I wish for is a way to capture the current scoped values, and
> later execute code with the captured scoped values.
Thanks for the thought. An earlier version of scoped values did permit
this to be done, but it was removed.
That's because, for reasons of integrity, we need to be able to guarantee
that, when a binding scope terminates, the value is no longer bound in
any thread. And that it will never be re-bound without an explicit binding
operation.
The scope used by a scoped value binding is the current scope, at runtime.
This is by design. The only way to capture a scope and use it in a different
context is by a thread, in a structured concurrency context.
Because of this rule we can safely do things like
MemType memoryRegion = allocateMem()
try {
ScopedValue.where(REGION, memoryRegion).run(someActions);
} finally {
memoryRegion.close();
}
... in the secure knowledge that no one will ever see REGION.get() return a
value after the region has been closed.
This is correctness by construction: rather than detecting some errors
at runtime, we make them impossible.
--
Andrew Haley (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the loom-dev
mailing list