]> git.lizzy.rs Git - rust.git/commit
std: Implement TLS for wasm32-unknown-unknown
authorAlex Crichton <alex@alexcrichton.com>
Wed, 10 Oct 2018 06:10:25 +0000 (23:10 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 11 Oct 2018 16:57:55 +0000 (09:57 -0700)
commitcbe9f33b8bb94b262f91bc8c6f61a7f6518164b4
tree6c99c8a6297c759d13c42054a53f6ef1e52029c9
parenteae47a40484a085f699c3175d51e74901c607e65
std: Implement TLS for wasm32-unknown-unknown

This adds an implementation of thread local storage for the
`wasm32-unknown-unknown` target when the `atomics` feature is
implemented. This, however, comes with a notable caveat of that it
requires a new feature of the standard library, `wasm-bindgen-threads`,
to be enabled.

Thread local storage for wasm (when `atomics` are enabled and there's
actually more than one thread) is powered by the assumption that an
external entity can fill in some information for us. It's not currently
clear who will fill in this information nor whose responsibility it
should be long-term. In the meantime there's a strategy being gamed out
in the `wasm-bindgen` project specifically, and the hope is that we can
continue to test and iterate on the standard library without committing
to a particular strategy yet.

As to the details of `wasm-bindgen`'s strategy, LLVM doesn't currently
have the ability to emit custom `global` values (thread locals in a
`WebAssembly.Module`) so we leverage the `wasm-bindgen` CLI tool to do
it for us. To that end we have a few intrinsics, assuming two global values:

* `__wbindgen_current_id` - gets the current thread id as a 32-bit
  integer. It's `wasm-bindgen`'s responsibility to initialize this
  per-thread and then inform libstd of the id. Currently `wasm-bindgen`
  performs this initialization as part of the `start` function.
* `__wbindgen_tcb_{get,set}` - in addition to a thread id it's assumed
  that there's a global available for simply storing a pointer's worth
  of information (a thread control block, which currently only contains
  thread local storage). This would ideally be a native `global`
  injected by LLVM, but we don't have a great way to support that right
  now.

To reiterate, this is all intended to be unstable and purely intended
for testing out Rust on the web with threads. The story is very likely
to change in the future and we want to make sure that we're able to do
that!
src/libstd/Cargo.toml
src/libstd/sys/wasm/mutex_atomics.rs
src/libstd/sys/wasm/thread.rs
src/libstd/sys/wasm/thread_local_atomics.rs
src/libstd/thread/local.rs
src/libstd/thread/mod.rs