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

implement thread-spawn #81

Merged
merged 8 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
oligamiq marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions .vscode/settings.json
oligamiq marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}
92 changes: 51 additions & 41 deletions examples/package-lock.json

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

38 changes: 20 additions & 18 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "browser_wasi_shim_examples",
"publish": false,
"version": "0.0.0",
"license": "MIT OR Apache-2.0",
"description": "",
"repository": {
"type": "git",
"url": "git+https://github.com/bjorn3/browser_wasi_shim.git"
},
"author": "bjorn3",
"bugs": {
"url": "https://github.com/bjorn3/browser_wasi_shim/issues"
},
"homepage": "https://github.com/bjorn3/browser_wasi_shim#readme",
"dependencies": {
"xterm": "^4.18.0",
"xterm-addon-fit": "^0.5.0"
}
"name": "browser_wasi_shim_examples",
"publish": false,
"version": "0.0.0",
"license": "MIT OR Apache-2.0",
"description": "",
"repository": {
"type": "git",
"url": "git+https://github.com/bjorn3/browser_wasi_shim.git"
},
"author": "bjorn3",
"bugs": {
"url": "https://github.com/bjorn3/browser_wasi_shim/issues"
},
"homepage": "https://github.com/bjorn3/browser_wasi_shim#readme",
"dependencies": {
"@oligami/shared-object": "^0.1.1",
"browser_wasi_shim_examples": "file:",
"xterm": "^4.18.0",
"xterm-addon-fit": "^0.5.0"
}
}
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
Loading