]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #78965 - jryans:emscripten-threads-libc, r=kennytm
authorbors <bors@rust-lang.org>
Thu, 12 Nov 2020 05:52:17 +0000 (05:52 +0000)
committerbors <bors@rust-lang.org>
Thu, 12 Nov 2020 05:52:17 +0000 (05:52 +0000)
Update thread and futex APIs to work with Emscripten

This updates the thread and futex APIs in `std` to match the APIs exposed by
Emscripten. This allows threads to run on `wasm32-unknown-emscripten` and the
thread parker to compile without errors related to the missing `futex` module.

To make use of this, Rust code must be compiled with `-C target-feature=atomics`
and Emscripten must link with `-pthread`.

I have confirmed this works well locally when building multithreaded crates.
Attempting to enable `std` thread tests currently fails for seemingly obscure
reasons and Emscripten is currently disabled in CI, so further work is needed to
have proper test coverage here.

1  2 
library/std/src/sys/unix/thread.rs

index f1ab302d30e1e70c558b6b53a9f6bbe6fcef7661,b7b72cc392fd324ee2f274a882692b1da9a68a18..cda17eb4bd23c8110f77db4d89a8313c5f25956a
@@@ -22,24 -22,6 +22,6 @@@ pub struct Thread 
  unsafe impl Send for Thread {}
  unsafe impl Sync for Thread {}
  
- // The pthread_attr_setstacksize symbol doesn't exist in the emscripten libc,
- // so we have to not link to it to satisfy emcc's ERROR_ON_UNDEFINED_SYMBOLS.
- #[cfg(not(target_os = "emscripten"))]
- unsafe fn pthread_attr_setstacksize(
-     attr: *mut libc::pthread_attr_t,
-     stack_size: libc::size_t,
- ) -> libc::c_int {
-     libc::pthread_attr_setstacksize(attr, stack_size)
- }
- #[cfg(target_os = "emscripten")]
- unsafe fn pthread_attr_setstacksize(
-     _attr: *mut libc::pthread_attr_t,
-     _stack_size: libc::size_t,
- ) -> libc::c_int {
-     panic!()
- }
  impl Thread {
      // unsafe: see thread::Builder::spawn_unchecked for safety requirements
      pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
@@@ -50,7 -32,7 +32,7 @@@
  
          let stack_size = cmp::max(stack, min_stack_size(&attr));
  
-         match pthread_attr_setstacksize(&mut attr, stack_size) {
+         match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
              0 => {}
              n => {
                  assert_eq!(n, libc::EINVAL);
                      tv_nsec: nsecs,
                  };
                  secs -= ts.tv_sec as u64;
 -                if libc::nanosleep(&ts, &mut ts) == -1 {
 +                let ts_ptr = &mut ts as *mut _;
 +                if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
                      assert_eq!(os::errno(), libc::EINTR);
                      secs += ts.tv_sec as u64;
                      nsecs = ts.tv_nsec;