diff --git a/src/runtime-definition.ts b/src/runtime-definition.ts new file mode 100644 index 0000000..09126f1 --- /dev/null +++ b/src/runtime-definition.ts @@ -0,0 +1,28 @@ +import { availableParallelism } from 'node:os'; +import { define } from './define.ts'; +import type { Balancer } from './runner.ts'; + +/** + * Configuration for a global runtime, defined in a side-effect-free module + * via `define()` so every thread can resolve the identical config by module + * identity. Workers never receive the config by serialization — they import + * the defining module (follow-up plan); on the main thread it configures the + * lazily-booted global pool. + */ +export interface RuntimeDefinition { + /** Number of worker threads. Defaults to `os.availableParallelism()`. */ + size?: number; + /** Load balancing strategy. Defaults to least-busy. */ + balance?: Balancer; + /** Max ms to wait for in-flight tasks during `runtime.shutdown()`. */ + shutdownTimeout?: number; +} + +/** + * Moroutine's built-in runtime definition — used when no custom definition is + * registered. It is "just another runtime": the only default-vs-custom + * difference anywhere is which definition boots. + */ +export const defaultRuntime: RuntimeDefinition = define(import.meta, { + size: availableParallelism(), +}); diff --git a/test/runtime-definition.test.ts b/test/runtime-definition.test.ts new file mode 100644 index 0000000..fd47ca0 --- /dev/null +++ b/test/runtime-definition.test.ts @@ -0,0 +1,18 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { availableParallelism } from 'node:os'; +import { defaultRuntime } from '../src/runtime-definition.ts'; +import { getDefineId } from '../src/define.ts'; + +describe('defaultRuntime definition', () => { + it('is a define()-branded value', () => { + const id = getDefineId(defaultRuntime); + assert.ok(id !== undefined); + assert.match(id!, /runtime-definition\.ts#\d+$/); + }); + + it('carries default config', () => { + assert.equal(defaultRuntime.size, availableParallelism()); + assert.equal(defaultRuntime.balance, undefined); // undefined → pool default (leastBusy) + }); +});