Skip to content

Making thread-local initialization explicit #99

@tlively

Description

@tlively

One of the biggest problems identified with thread-local globals so far is the complexity and cost of ensuring they are initialized before they are used. One option we've considered is eagerly initializing the TLS for every instance when a new thread is created and eagerly creating the TLS for every thread when a new instance is created. This obviously scales very poorly. The other option we've considered is initializing TLS lazily upon entering a particular instance on a particular thread for the first time, but that would slow down all indirect calls because they would have to check whether they are crossing an instance boundary and need to initialize TLS.

I recently developed an idea for another option, which is to give up on ensuring that thread-local storage is available by the time it is accessed, and instead trap if it has not been explicitly initialized yet for the current thread. This was always going to be what happened when you called a thread-bound imported JS function before binding it on the current thread, so the new idea is to extend that pattern to thread-local globals as well. This semantics means that we need neither expensive eager initialization nor expensive lazy initialization, and ideally the initialization check on access could be made fast by using a trap handler.

The API for this explicit instantiation could be attached to some kind of shareable handle to a Module/Instance hybrid. This handle would be vended by a WebAssembly.Instance and could provide access to the shared exports of the instance on any thread, but it would only provide access to the unshared exports (if any) on a thread once it has been instantiated with the unshared and thread-local imports on that thread. The TLS would be allocated and initialized as part of this per-thread instantiation.

This would be essentially committing to the instance-per-thread model, at least as far as TLS is concerned. My hope is that this will be acceptable in the component model because the thread.spawn builtin will be able to identify any necessary thread-local imports from the thread-local reflection of the module/instance graph and can be specified as invoking the embedder API for doing the per-thread instantiation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions