mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 04:19:56 +08:00
pnpm build inlines js/node/cpu-entry.ts where js/node/cpu.ts starts it, in the Node entries and inside the inlined Node kernel worker (WORKERS nested), so the package starts SMP CPUs without source files. The page build serves web/kernel/page/: the bundle without its SMP patch section (the same bytes when there is none) and, only with a patch, the SMP bundle it makes as a second hashed asset (kernelSmpUrl, unused until the page has CPU threads).
32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Start a secondary CPU of an SMP kernel (cpu-entry.ts, core/cpu.ts) on a
|
|
// worker_threads Worker: Platform.spawnCpu on Node. Called from the
|
|
// kernel's own thread (the main thread inline, the kernel worker in worker
|
|
// mode). The worker gets the compiled module and the shared memory in its
|
|
// first message.
|
|
//
|
|
// From source, Node starts the worker from cpu-entry.ts itself. `pnpm
|
|
// build` (build.config.ts, WORKERS) replaces the URL below (it must appear
|
|
// exactly once in this file) with the bundled entry as a string literal,
|
|
// in the dist entries and inside the inlined kernel worker, which starts
|
|
// the CPUs in worker mode.
|
|
|
|
import { Worker } from "node:worker_threads";
|
|
import type { CpuEndpoint, CpuInit, CpuMessage } from "../core/cpu.ts";
|
|
|
|
export function spawnNodeCpu(init: CpuInit): CpuEndpoint {
|
|
const entry: URL | string = new URL("./cpu-entry.ts", import.meta.url);
|
|
const w = typeof entry === "string" ? new Worker(entry, { eval: true }) : new Worker(entry);
|
|
// The CPU never keeps the process alive: CPU 0's exit ends the session.
|
|
w.unref();
|
|
w.postMessage(init);
|
|
return {
|
|
listen(h) {
|
|
w.on("message", (m: CpuMessage) => h.message(m));
|
|
w.on("error", h.error);
|
|
},
|
|
terminate: () => void w.terminate(),
|
|
};
|
|
}
|