Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plan to support threads/SharedArrayBuffer? #9

Open
Pistonight opened this issue Oct 1, 2024 · 2 comments
Open

Any plan to support threads/SharedArrayBuffer? #9

Pistonight opened this issue Oct 1, 2024 · 2 comments

Comments

@Pistonight
Copy link

Pistonight commented Oct 1, 2024

threads proposal has reached phase 4 - https://webassembly.org/features/

Seems like it would be possible to build a wrapper for something like std::thread::spawn that, under the hood, sends the closure to another worker (possibly by boxing and maybe needs to turn it into a raw pointer)

pub fn spawn_thread<T, F>(f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send +'static, T: Send + 'static {
    // (std/crossbeam should work, maybe one_shot works too?)
    let (send, recv) = std::sync::mpsc::channel();
    let (out_send, out_recv) = std::sync::mpsc::channel();

    // create a worker with js_sys that:
    // - init the wasm module (produced by wasm-bindgen) inside the worker with the same (shared) WebAssembly.Memory
    // - recv and execute the closure
    // - send the result back

    // send the closure to the worker
    send.send(f).unwrap()
    JoinHandle { recv: out_recv }
}

(if this won't work please educate me why)

@j-devel
Copy link
Contributor

j-devel commented Oct 9, 2024

build a wrapper for something like std::thread::spawn

Hi, it sounds quite feasible to me (by intuition, haven't done any PoC yet).

under the hood, sends the closure to another worker

We already got it working.

// - init the wasm module (produced by wasm-bindgen) inside the worker with the same (shared) WebAssembly.Memory

I guess, we can do something similar to what https://github.com/RReverser/wasm-bindgen-rayon is doing.

@Pistonight
Copy link
Author

Pistonight commented Oct 10, 2024

I actually got a PoC working https://github.com/Pistonite/wasm-bindgen-spawn which I plan to use for my next project. For anyone looking for solutions, I also found https://github.com/chemicstry/wasm_thread which is a more elaborate implementation for more stuff in std::thread module. (I am curious why wasm_thread is not showing up in any google results)

We already got it working.

I meant sending the closure as a raw pointer instead of serializing/deserializing, using shared memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants