Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed May 19, 2024
1 parent fd282ef commit 5793d1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
17 changes: 12 additions & 5 deletions packages/wasi-threads/src/thread-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ export interface ThreadManagerOptionsChild extends ThreadManagerOptionsBase {
const WASI_THREADS_MAX_TID = 0x1FFFFFFF

export function checkSharedWasmMemory (wasmMemory?: WebAssembly.Memory | null): void {
if (wasmMemory ? !isSharedArrayBuffer(wasmMemory.buffer) : (typeof SharedArrayBuffer === 'undefined')) {
throw new Error(
'Multithread features require shared wasm memory. ' +
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking'
)
if (wasmMemory) {
if (!isSharedArrayBuffer(wasmMemory.buffer)) {
throw new Error(
'Multithread features require shared wasm memory. ' +
'Try to compile with `-matomics -mbulk-memory` and use `--import-memory --shared-memory` during linking, ' +
'then create WebAssembly.Memory with `shared: true` option'
)
}
} else {
if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('Current environment does not support SharedArrayBuffer, threads are not available!')
}
}
}

Expand Down
21 changes: 17 additions & 4 deletions packages/wasi-threads/src/wasi-threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,24 @@ export class WASIThreads {
}

const threadSpawn = (startArg: number, errorOrTid?: number): number => {
checkSharedWasmMemory(this.wasmMemory)

const EAGAIN = 6
const isNewABI = errorOrTid !== undefined

try {
checkSharedWasmMemory(this.wasmMemory)
} catch (err) {
this.PThread?.printErr(err.stack)
if (isNewABI) {
const struct = new Int32Array(this.wasmMemory.buffer, errorOrTid!, 2)
Atomics.store(struct, 0, 1)
Atomics.store(struct, 1, EAGAIN)
Atomics.notify(struct, 1)
return 1
} else {
return -EAGAIN
}
}

if (!isNewABI) {
const malloc = this.wasmInstance.exports.malloc as Function
errorOrTid = wasm64 ? Number(malloc(BigInt(8))) : malloc(8)
Expand Down Expand Up @@ -211,8 +226,6 @@ export class WASIThreads {
}
}
} catch (e) {
const EAGAIN = 6

Atomics.store(struct, 0, 1)
Atomics.store(struct, 1, EAGAIN)
Atomics.notify(struct, 1)
Expand Down

0 comments on commit 5793d1f

Please sign in to comment.