Files
unkernel/js/web/platform.ts
Pooya Parsa 82c7d36d92 SDK: start an SMP kernel's CPUs as Web Workers in browsers
js/web/cpu.ts is the browser Platform.spawnCpu: each CPU is a module
Web Worker running runCpu(), nested in worker mode's kernel worker.
webCpuBlocker() (Platform.whyNoCpus) gives the kernel one CPU where a
thread cannot run CPUs: no cross-origin isolation, no Worker, no
Atomics.waitAsync, or a page's main thread, where CPU 0's
memory.atomic.wait32 would trap. stats().cpus says how many CPUs the
kernel got. The dist inlines the CPU worker as a Blob URL, in the web
entry and inside the web kernel worker.
2026-11-17 11:26:41 +00:00

33 lines
1.5 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// createKernel()'s browser platform: memory sized for the device (decided
// here, on the page's thread, since a worker has no say in it), worker mode
// on a Web Worker (worker.ts), and relative locations made absolute against
// the page, because the worker (a Blob URL in the package) has no useful
// base URL. Disk and network specs are built by webDevices (devices.ts).
// CPU threads for an SMP kernel (cpu.ts) start wherever the kernel runs in
// a worker of a cross-origin isolated page: worker mode's kernel worker,
// or a worker of the caller's own; never on the page's main thread
// (webCpuBlocker()).
// The entry (web.ts, or the page's web/src/sdk.ts) passes the
// default kernel's URL, so `new URL(..., import.meta.url)` stays in it.
import type { Platform } from "../core/instance.ts";
import { DEFAULT_MEMORY_FALLBACK_MIB, autoMemoryMiB } from "./memory.ts";
import { spawnWebCpu, webCpuBlocker } from "./cpu.ts";
import { webDevices } from "./devices.ts";
import { spawnWebWorker } from "./worker.ts";
export function webPlatform(defaultKernel: URL): Platform {
return {
defaultKernel: () => defaultKernel,
defaultMemoryMiB: () => autoMemoryMiB().mib,
defaultMemoryFallbackMiB: [...DEFAULT_MEMORY_FALLBACK_MIB],
spawnWorker: spawnWebWorker,
spawnCpu: spawnWebCpu,
whyNoCpus: webCpuBlocker,
absoluteUrl: (src) => new URL(src, location.href).href,
devices: webDevices,
};
}