Files
scriptc/tests/corpus/native-worker-terminate-wait.ts
T
Chris Tate 095b619330 Add native workers and shared memory
- Add native worker execution with isolated module state, structured messages, and lifecycle controls.
- Support shared buffers and integer atomics with coordinated runtime ownership and cancellation.
- Extend compiler, runtime-pack, differential, and compatibility coverage for native concurrency.
2026-10-07 19:07:02 -05:00

23 lines
795 B
TypeScript

import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
if (isMainThread) {
const shared = new SharedArrayBuffer(4);
const worker = new Worker(new URL(import.meta.url), { workerData: shared });
worker.on("message", () => {
// The worker may already be parked, or may reach wait after cancellation.
worker.terminate().then((code: number) => console.log("terminated", code));
});
worker.on("exit", (code: number) => console.log("exit", code));
} else {
const values = new Int32Array(workerData as SharedArrayBuffer);
parentPort!.postMessage("ready");
try {
Atomics.wait(values, 0, 0, undefined);
console.log("unexpected wake");
} catch {
console.log("unexpected catch");
} finally {
console.log("unexpected finally");
}
}