Skip to content

Commit

Permalink
feature wasi thread-spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
oligamiq committed Sep 27, 2024
1 parent 63d184e commit b389c72
Show file tree
Hide file tree
Showing 53 changed files with 3,365 additions and 1,410 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
path = test/wasi-testsuite
url = https://github.com/WebAssembly/wasi-testsuite
branch = prod/testsuite-base
[submodule "examples/wasi_multi_threads_rustc/rust_wasm"]
path = examples/wasi_multi_threads_rustc/rust_wasm
url = https://github.com/oligamiq/rust_wasm
sharrow = true
Binary file added bun.lockb
Binary file not shown.
Binary file added examples/bun.lockb
Binary file not shown.
42 changes: 23 additions & 19 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
},
"homepage": "https://github.com/bjorn3/browser_wasi_shim#readme",
"dependencies": {
"@oligami/shared-object": "^0.1.0",
"browser_wasi_shim_examples": "file:",
"xterm": "^4.18.0",
"xterm-addon-fit": "^0.5.0"
}
Expand Down
38 changes: 38 additions & 0 deletions examples/wasi_multi_threads/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<p id="note">
####
</p>

<script type="module">
import { File, OpenFile, ConsoleStdout, PreopenDirectory, WASIFarm, WASIFarmAnimal } from "../../dist/index.js";

const farm = new WASIFarm(
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
[],
{ debug: true },
);

console.log(farm);

const worker = new Worker("./worker.js", { type: "module" });
// const worker = new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" });

console.log(worker);

console.log("self.Worker", self.Worker);

worker.postMessage({
wasi_ref: farm.get_ref(),
});

console.log("Sent WASI ref to worker");

</script>
</body>
</html>
13 changes: 13 additions & 0 deletions examples/wasi_multi_threads/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
println!("Hello, world!");

let _: std::thread::JoinHandle<()> = std::thread::spawn(|| {
for i in 1..1000 {
println!("hi number {} from the spawned thread!", i);
}
});

for i in 1..1000 {
println!("hi number {} from the main thread!", i);
}
}
Binary file not shown.
5 changes: 5 additions & 0 deletions examples/wasi_multi_threads/thread_spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { thread_spawn_on_worker } from "../../dist/wasi_farm/shared_array_buffer/thread_spawn.js";

self.onmessage = (event) => {
thread_spawn_on_worker(event.data);
}
32 changes: 32 additions & 0 deletions examples/wasi_multi_threads/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WASIFarmAnimal } from "../../dist/index.js";

self.onmessage = async (e) => {
const { wasi_ref } = e.data;

const wasm = await WebAssembly.compileStreaming(fetch("./multi_thread_echo.wasm"));

const wasi = new WASIFarmAnimal(
wasi_ref,
[], // args
[], // env
{
debug: true,
can_thread_spawn: true,
thread_spawn_worker_url: (new URL("./thread_spawn.js", import.meta.url)).href,
// thread_spawn_worker_url: "./thread_spawn.js",
thread_spawn_wasm: wasm,
}
);

await wasi.wait_worker_background_worker();

let inst = await WebAssembly.instantiate(wasm, {
"env": {
memory: wasi.get_share_memory(),
},
"wasi": wasi.wasiThreadImport,
"wasi_snapshot_preview1": wasi.wasiImport,
});

wasi.start(inst);
}
Binary file added examples/wasi_multi_threads_channel/channel.wasm
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/wasi_multi_threads_channel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<p id="note">
####
</p>

<script type="module">
import { File, OpenFile, ConsoleStdout, PreopenDirectory, WASIFarm, WASIFarmAnimal } from "../../dist/index.js";

const farm = new WASIFarm(
new OpenFile(new File([])), // stdin
ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
[],
{ debug: true },
);

const worker = new Worker("./worker.js", { type: "module" });

worker.postMessage({
wasi_ref: farm.get_ref(),
});
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions examples/wasi_multi_threads_channel/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://doc.rust-lang.org/book/ch16-02-message-passing.html

use std::sync::mpsc;
use std::thread;

fn main() {
let (tx, rx) = mpsc::channel();

thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});

let received = rx.recv().unwrap();
println!("Got: {received}");
}
5 changes: 5 additions & 0 deletions examples/wasi_multi_threads_channel/thread_spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { thread_spawn_on_worker } from "../../dist/wasi_farm/shared_array_buffer/thread_spawn.js";

self.onmessage = (event) => {
thread_spawn_on_worker(event.data);
}
32 changes: 32 additions & 0 deletions examples/wasi_multi_threads_channel/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { WASIFarmAnimal } from "../../dist/index.js";

self.onmessage = async (e) => {
const { wasi_ref } = e.data;

const wasm = await WebAssembly.compileStreaming(fetch("./channel.wasm"));

const wasi = new WASIFarmAnimal(
wasi_ref,
[], // args
[], // env
{
debug: true,
can_thread_spawn: true,
thread_spawn_worker_url: (new URL("./thread_spawn.js", import.meta.url)).href,
// thread_spawn_worker_url: "./thread_spawn.js",
thread_spawn_wasm: wasm,
}
);

await wasi.wait_worker_background_worker();

let inst = await WebAssembly.instantiate(wasm, {
"env": {
memory: wasi.get_share_memory(),
},
"wasi": wasi.wasiThreadImport,
"wasi_snapshot_preview1": wasi.wasiImport,
});

wasi.start(inst);
}
Loading

0 comments on commit b389c72

Please sign in to comment.