Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Simplify thread-local management of the store#201

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:simplify-tls
Jun 9, 2025
Merged

Simplify thread-local management of the store#201
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:simplify-tls

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit is a simplification of the management of thread locals used to transfer a store across the Future::poll boundary to pass it from the caller to internal futures we're polling. Previously there were two thread locals where a store would migrate between them as appropriate, and these two thread locals were adjacent to the rest of concurrent.rs which accessed it from a number of locations. After this commit all TLS accesses are isolated in a single tls.rs file which is intended to be managable to audit in its entirety.

Both TLS globals have been folded in a single global that is two pointers large, storing a dyn VMStore pointer. The store now has internal state associated with the current instance being polled as well which removes th need for some extra state to be passed in these globals. Additionally functions such as with_local_instance where two arguments were produced are now split into one argument (the store) comes from TLS and the other (the instance) comes from a closed over argument in futures/etc. The culmination of this is that all TLS management goes through a smaller set of primitives than before which is intended to be easier to audit for unsafe.

This commit is a simplification of the management of thread locals used
to transfer a store across the `Future::poll` boundary to pass it from
the caller to internal futures we're polling. Previously there were two
thread locals where a store would migrate between them as appropriate,
and these two thread locals were adjacent to the rest of `concurrent.rs`
which accessed it from a number of locations. After this commit all TLS
accesses are isolated in a single `tls.rs` file which is intended to be
managable to audit in its entirety.

Both TLS globals have been folded in a single global that is two
pointers large, storing a `dyn VMStore` pointer. The store now has
internal state associated with the current instance being polled as well
which removes th need for some extra state to be passed in these
globals. Additionally functions such as `with_local_instance` where two
arguments were produced are now split into one argument (the store)
comes from TLS and the other (the instance) comes from a closed over
argument in futures/etc. The culmination of this is that all TLS
management goes through a smaller set of primitives than before which is
intended to be easier to audit for unsafe.
Comment thread crates/wasmtime/src/runtime/component/concurrent/tls.rs Outdated
/// values (e.g. futures) and that internally in the runtime we aren't doing
/// anything "weird" with threads for example.
pub unsafe trait VMStore {
pub unsafe trait VMStore: 'static {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No big deal, but curious why you decided to add this bound again. Was it unavoidable given the use in TLS?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic problem was this where I wanted to convert from &mut dyn VMStore-as-an-argument to *mut dyn VMStore-stored-somewhere-else. Lifetime inference works differently in these situations so as an argument it's &mut (dyn VMStore + '_) but store in a struct it's *mut (dyn VMStore + 'static) (I think at least). In lieu of sprinkling 'static bounds around I figured it's easiest to just add it here where it's already true because Store<T>: 'static all the time anyway now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. If we needed to, I suspect we could avoid the bound by using &mut StoreOpaque and *mut StoreOpaque instead, but we don't need to.

Co-authored-by: Joel Dice <joel.dice@fermyon.com>
@alexcrichton alexcrichton enabled auto-merge June 9, 2025 14:32
@alexcrichton alexcrichton added this pull request to the merge queue Jun 9, 2025
Merged via the queue into bytecodealliance:main with commit b0b72ca Jun 9, 2025
42 checks passed
@alexcrichton alexcrichton deleted the simplify-tls branch June 9, 2025 15:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants