-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from oligamiq/main
implement thread-spawn
- Loading branch information
Showing
90 changed files
with
14,508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
dist/ | ||
typings/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.vscode | ||
/dist | ||
/node_modules | ||
/types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/swcrc", | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": false, | ||
"dynamicImport": false, | ||
"decorators": false, | ||
"dts": true | ||
}, | ||
"transform": {}, | ||
"target": "esnext", | ||
"loose": false, | ||
"externalHelpers": false, | ||
"keepClassNames": true | ||
}, | ||
"minify": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# A pure javascript shim for WASI Preview 1 threads | ||
|
||
> [!WARNING] | ||
> The code in this directory is less production ready than the main browser_wasi_shim code. | ||
This project is implement threads on browser_wasi_shim | ||
|
||
# Features | ||
- [x] thread creation | ||
- [x] Filesystem wrapper accessible by multiple workers | ||
- [ ] thread pool | ||
|
||
# Building | ||
```sh | ||
$ npm install | ||
$ npm run build | ||
``` | ||
|
||
# Running the demo | ||
```sh | ||
$ git submodule update --init | ||
$ cd examples && npm install && npm run dev | ||
``` | ||
And visit http://localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.1/schema.json", | ||
"vcs": { | ||
"enabled": false, | ||
"clientKind": "git", | ||
"useIgnoreFile": false | ||
}, | ||
"files": { | ||
"ignoreUnknown": false, | ||
"ignore": [] | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space" | ||
}, | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "double" | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dependencies": { | ||
"@oligami/shared-object": "^0.1.1", | ||
"@xterm/xterm": "^5.5", | ||
"xterm-addon-fit": "^0.8.0", | ||
"@bjorn3/browser_wasi_shim": "^0.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<p id="note"> | ||
#### | ||
</p> | ||
|
||
<script type="module" src="./index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { WASIFarm, WASIFarmAnimal } from "../../src"; | ||
import { | ||
File, | ||
OpenFile, | ||
ConsoleStdout, | ||
PreopenDirectory, | ||
} from "@bjorn3/browser_wasi_shim"; | ||
|
||
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}`)), | ||
[], | ||
); | ||
|
||
console.log(farm); | ||
|
||
const worker = new Worker("worker.ts", { type: "module" }); | ||
// const worker = new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" }); | ||
|
||
console.log(worker); | ||
|
||
worker.postMessage({ | ||
wasi_ref: farm.get_ref(), | ||
}); | ||
|
||
console.log("Sent WASI ref to worker"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { thread_spawn_on_worker } from "../../src"; | ||
|
||
self.onmessage = (event) => { | ||
thread_spawn_on_worker(event.data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { WASIFarmAnimal } from "../../src"; | ||
|
||
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 | ||
{ | ||
can_thread_spawn: true, | ||
thread_spawn_worker_url: new URL("./thread_spawn.ts", import.meta.url) | ||
.href, | ||
thread_spawn_wasm: wasm, | ||
}, | ||
); | ||
|
||
await wasi.wait_worker_background_worker(); | ||
|
||
const inst = await WebAssembly.instantiate(wasm, { | ||
env: { | ||
memory: wasi.get_share_memory(), | ||
}, | ||
wasi: wasi.wasiThreadImport, | ||
wasi_snapshot_preview1: wasi.wasiImport, | ||
}); | ||
|
||
wasi.start( | ||
inst as unknown as { | ||
exports: { memory: WebAssembly.Memory; _start: () => unknown }; | ||
}, | ||
); | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<p id="note"> | ||
#### | ||
</p> | ||
|
||
<script type="module" src="./index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { OpenFile, File, ConsoleStdout } from "@bjorn3/browser_wasi_shim"; | ||
import { WASIFarm } from "../../src"; | ||
|
||
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}`)), | ||
[], | ||
); | ||
|
||
const worker = new Worker("./worker.ts", { type: "module" }); | ||
|
||
worker.postMessage({ | ||
wasi_ref: farm.get_ref(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { thread_spawn_on_worker } from "../../src"; | ||
|
||
self.onmessage = (event) => { | ||
thread_spawn_on_worker(event.data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { WASIFarmAnimal } from "../../src"; | ||
|
||
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 | ||
{ | ||
can_thread_spawn: true, | ||
thread_spawn_worker_url: new URL("./thread_spawn.ts", import.meta.url) | ||
.href, | ||
// thread_spawn_worker_url: "./thread_spawn.ts", | ||
thread_spawn_wasm: wasm, | ||
}, | ||
); | ||
|
||
await wasi.wait_worker_background_worker(); | ||
|
||
const inst = await WebAssembly.instantiate(wasm, { | ||
env: { | ||
memory: wasi.get_share_memory(), | ||
}, | ||
wasi: wasi.wasiThreadImport, | ||
wasi_snapshot_preview1: wasi.wasiImport, | ||
}); | ||
|
||
wasi.start( | ||
inst as unknown as { | ||
exports: { memory: WebAssembly.Memory; _start: () => unknown }; | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { strace } from "@bjorn3/browser_wasi_shim"; | ||
import { WASIFarmAnimal } from "../../src"; | ||
import { SharedObject } from "@oligami/shared-object"; | ||
|
||
let wasi: WASIFarmAnimal; | ||
let inst: { | ||
exports: { memory: WebAssembly.Memory; _start: () => unknown }; | ||
}; | ||
let wasm: WebAssembly.Module; | ||
|
||
let shared_clang: SharedObject; | ||
let shared_tools: SharedObject; | ||
let shared_wasm_ld: SharedObject; | ||
|
||
globalThis.onmessage = async (e) => { | ||
const { wasi_refs } = e.data; | ||
|
||
if (wasi_refs) { | ||
wasm = await WebAssembly.compileStreaming( | ||
fetch("./rust_wasm/llvm-tools/llvm-opt.wasm"), | ||
); | ||
|
||
wasi = new WASIFarmAnimal( | ||
wasi_refs, | ||
["llvm"], // args | ||
[], // env | ||
); | ||
|
||
// Memory is rewritten at this time. | ||
// inst = await WebAssembly.instantiate(wasm, { | ||
// wasi_snapshot_preview1: wasi.wasiImport, | ||
// }); | ||
inst = (await WebAssembly.instantiate(wasm, { | ||
wasi_snapshot_preview1: strace(wasi.wasiImport, []), | ||
})) as unknown as { | ||
exports: { memory: WebAssembly.Memory; _start: () => unknown }; | ||
}; | ||
|
||
console.log("wasi.start, inst", inst); | ||
|
||
const memory_reset = inst.exports.memory.buffer; | ||
const memory_reset_view = new Uint8Array(memory_reset).slice(); | ||
|
||
shared_clang = new SharedObject((...args) => { | ||
console.log("clang args", args); | ||
// If I don't reset memory, I get some kind of error. | ||
wasi.args = ["llvm", "clang", ...args]; | ||
const memory_view = new Uint8Array(inst.exports.memory.buffer); | ||
memory_view.set(memory_reset_view); | ||
wasi.start(inst); | ||
console.log("clang wasi.start done"); | ||
}, "clang"); | ||
|
||
shared_tools = new SharedObject((...args) => { | ||
console.log("tools args", args); | ||
// If I don't reset memory, I get some kind of error. | ||
wasi.args = ["llvm-tools", ...args]; | ||
const memory_view = new Uint8Array(inst.exports.memory.buffer); | ||
memory_view.set(memory_reset_view); | ||
wasi.start(inst); | ||
console.log("tools wasi.start done"); | ||
}, "llvm-tools"); | ||
|
||
shared_wasm_ld = new SharedObject((...args) => { | ||
console.log("wasm-ld args", args); | ||
// If I don't reset memory, I get some kind of error. | ||
wasi.args = ["llvm-tools", "wasm-ld", ...args]; | ||
const memory_view = new Uint8Array(inst.exports.memory.buffer); | ||
memory_view.set(memory_reset_view); | ||
wasi.start(inst); | ||
console.log("wasm-ld wasi.start done"); | ||
}, "wasm-ld"); | ||
|
||
postMessage({ ready: true }); | ||
} | ||
}; |
Oops, something went wrong.