mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-15 22:11:23 +00:00
Implement support for WebAssembly threads
... and add a parallel raytracing demo! This commit adds enough support to `wasm-bindgen` to produce a workable wasm binary *today* with the experimental WebAssembly threads support implemented in Firefox Nightly. I've tried to comment what's going on in the commits and such, but at a high level the changes made here are: * A new transformation, living in a new `wasm-bindgen-threads-xform` crate, prepares a wasm module for parallel execution. This performs a number of mundane tasks which I hope to detail in a blog post later on. * The `--no-modules` output is enhanced with more support for when shared memory is enabled, allowing passing in the module/memory to initialize the wasm instance on multiple threads (sharing both module and memory). * The `wasm-bindgen` crate now offers the ability, in `--no-modules` mode, to get a handle on the `WebAssembly.Module` instance. * The example itself requires Xargo to recompile the standard library with atomics and an experimental feature enabled. Afterwards it experimentally also enables threading support in wasm-bindgen. I've also added hopefully enough CI support to compile this example in a builder so we can upload it and poke around live online. I hope to detail more about the technical details here in a blog post soon as well!
This commit is contained in:
32
examples/raytrace-parallel/worker.js
Normal file
32
examples/raytrace-parallel/worker.js
Normal file
@ -0,0 +1,32 @@
|
||||
// synchronously, using the browser, import out shim JS scripts
|
||||
importScripts('raytrace_parallel.js');
|
||||
|
||||
let booted = false;
|
||||
let lastPtr = null;
|
||||
|
||||
// Wait for the main thread to send us the shared module/memory. Once we've got
|
||||
// it, initialize it all with the `wasm_bindgen` global we imported via
|
||||
// `importScripts`.
|
||||
//
|
||||
// After our first message all subsequent messages are an entry point to run,
|
||||
// so we just do that.
|
||||
self.onmessage = function(args) {
|
||||
self.onmessage = event => run(event.data);
|
||||
const [module, memory] = args.data;
|
||||
wasm_bindgen(module, memory)
|
||||
.then(() => {
|
||||
booted = true;
|
||||
if (lastPtr)
|
||||
run(lastPtr);
|
||||
})
|
||||
.catch(e => setTimeout(() => { throw e; })); // propagate to main `onerror`
|
||||
};
|
||||
|
||||
function run(ptr) {
|
||||
if (!booted) {
|
||||
lastPtr = ptr;
|
||||
return;
|
||||
}
|
||||
lastPtr = null;
|
||||
wasm_bindgen.child_entry_point(ptr);
|
||||
}
|
Reference in New Issue
Block a user