• Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A rust `std::thread` replacement for wasm32 target

wasm_thread

License Cargo Documentation

An std::thread replacement for wasm32 target.

This crate tries to closely replicate std::thread API. Namely, it doesn't require you to bundle worker scripts and resolves wasm-bindgen shim URL automatically.

Note that some API is still missing and may be even impossible to implement given wasm limitations.

Using as a library

  • Add wasm_thread to your Cargo.toml.
  • This project supports wasm-pack targets web and no-modules. If building for web, enable the crate feature es_modules. Otherwise, this package will target no-modules by default.
  • Replace use std::thread with use wasm_thread as thread. Note that some API might be missing.
  • Build normally using wasm-pack or adapt build_wasm.sh to your project.

Notes on wasm limitations

  • In order for multiple wasm instances to share the same memory, SharedArrayBuffer is required. This means that the COOP and COEP security headers for the webpage will need to be set (see Mozilla's documentation). These may be enabled by adjusting webserver settings or using a service worker.
  • Any blocking API (thread.join(), futures::block_on(), etc) on the main thread will freeze the browser for as long as lock is maintained. This also freezes any proxied functions, which means that worker spawning, network fetches and other similar asynchronous APIs will block also and can cause a deadlock. To avoid this, either run your main() in a worker thread or use async futures.
  • Atomic locks (i32.atomic.wait to be specific) will panic on the main thread. This means that mutex.lock() will likely crash. Solution is the same as above.
  • Web workers are normally spawned by providing a script URL, however, to avoid bundling scripts this library uses URL encoded blob web_worker.js to avoid HTTP fetch. wasm_bindgen generated .js shim script is still needed and a hack is used to obtain its URL. If this for some reason does not work in your setup, please report an issue or use Builder::wasm_bindgen_shim_url() to specify explicit URL.
  • For additional information on wasm threading look at this blogpost or raytrace-parallel example.

Alternatives

For a higher-level threading solution, see wasm-bindgen-rayon, which allows one to utilize a fixed-size threadpool in web browsers.

Running examples

Simple

Native

  • Just cargo run --example simple

wasm-bindgen

  • Build with ./build_wasm.sh (bash) or ./build_wasm.ps1 (PowerShell). This custom build step is required because prebuilt standard library does not have support for atomics yet. Read more about this here.
  • Serve examples directory over HTTP with cross-origin isolation enabled and open simple.html in the browser. Inspect console output. You can use cargo install sfz as a basic HTTP server and serve with sfz examples --coi.

wasm-pack

  • Build with ./examples-wasm-pack/web-build.sh for an example targeting web, and ./examples/wasm-pack/web-build-no-module.sh for an example targeting no-modules.
  • Serve ./examples-wasm-pack/module or ./examples-wasm-pack/no-module, respectively, over HTTP and open simple.html in browser. Inspect console output.

Example output

Native:

hi number 1 from the spawned thread ThreadId(2)!
hi number 1 from the main thread ThreadId(1)!
hi number 1 from the spawned thread ThreadId(3)!
hi number 2 from the main thread ThreadId(1)!
hi number 2 from the spawned thread ThreadId(2)!
hi number 2 from the spawned thread ThreadId(3)!

Wasm:

hi number 1 from the main thread ThreadId(1)!
hi number 2 from the main thread ThreadId(1)!
hi number 1 from the spawned thread ThreadId(2)!
hi number 1 from the spawned thread ThreadId(3)!
hi number 2 from the spawned thread ThreadId(2)!
hi number 2 from the spawned thread ThreadId(3)!

As you can see wasm threads are only spawned after main() returns, because browser event loop cannot continue while main thread is blocked.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

recksplorer

Lightning Network Explorer
JavaScript
179
star
2

poolserver

Cryptocurrency mining pool written in C++ for speed. Supports Stratum.
C++
39
star
3

candle_driver

Python wrapper for candle (gs_usb) adapters.
C
11
star
4

A2DP_iPod

ESP32 based iPod emulator that lets stream bluetooth music to devices with iPod dock
C++
11
star
5

linux-rtic

An RTIC implementation for real-time Linux.
Rust
5
star
6

wow

Moved private
C++
5
star
7

modular-json-rpc

Modular JSON-RPC 2.0 library that allows easy addition of transports
TypeScript
4
star
8

eacs-esp32-old

Extensible Access Control System. ESP32 based door controller.
C++
4
star
9

sht25

Rust driver for SHT25 temperature/humidity sensor
Rust
2
star
10

eacs-server

Server for the Extensible Access Control System
TypeScript
2
star
11

eacs-user-auth

Extensible Access Control System. User Authentication Module.
TypeScript
1
star
12

Lightsaber

C
1
star
13

RFIDServer

TypeScript
1
star
14

futex-queue

An efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications.
Rust
1
star
15

eacs-socket

Extensible Access Control System. Socket library which provides a wrapper for RPC over WebSockets with JWT authentication.
TypeScript
1
star
16

pcp-mutex

A Priority Ceiling Protocol (PCP) mutex, based on Linux PI futex. Allows efficient and deadlock free execution.
Rust
1
star
17

covid19_spaceapps_mission

A pandemic simulation game. http://playoversight.co/
JavaScript
1
star
18

PN532Extended

Extended version of Arduino PN532 library which provides a flexible interface for various tag types
C++
1
star
19

RFIDModule

C++
1
star
20

JSBot

HTML5 Runescape Bot
JavaScript
1
star
21

eacs-tag-auth

Extensible Access Control System. RFID Tag Authentication Module.
TypeScript
1
star