Repository navigation
Description
Summary
Implement the §14.18 work-stealing scheduler across multiple OS threads. Today the fiber scheduler is effectively single-threaded with global ready/steal rings, so cancellation from a different OS thread and real parallel fiber execution are not exercised. This is a large runtime architecture slice and should be split into phases rather than patched opportunistically.
Spec
§14.18 requires work-stealing across OS threads. §14.7 says if a child fiber is actively running on a different OS thread, the parent fiber yields until the child reaches an interruptible point. §14.3 requires scheduler-yielding operations to remain compiler-visible.
Current state
Darwin and Windows fiber cores each maintain a single current_fiber, ready_queue, and steal_queue as global state (rt/fiber_core_darwin.w:64, rt/fiber_core_darwin.w:73, rt/fiber_core_darwin.w:76; Windows mirrors at rt/fiber_core_windows.w:63, rt/fiber_core_windows.w:72, rt/fiber_core_windows.w:75). Enqueue/dequeue functions operate on these global rings (rt/fiber_core_darwin.w:297, rt/fiber_core_darwin.w:313). There is no worker-thread-local deque and no OS-thread pool driving fibers.
with_runtime_request_cancel exists but runs inside the same scheduler state (rt/fiber_core_darwin.w:622, rt/fiber_core_windows.w:621). Therefore the spec's "actively running on a different OS thread" branch is not reachable.
Implementation plan
- Split the scheduler state into a
Schedulerplus per-worker state: current fiber, local deque, parked/wakeup primitive, and random victim selection state. Keep deterministic behavior where required by fixpoint; runtime scheduling nondeterminism must not affect compiler output. - Add a runtime-configured worker pool size, likely sharing
[runtime] fiber_pool_sizeor a newfiber_worker_countwith Parse [runtime] with.toml keys (fiber_stack_size, fiber_pool_size) #403. Default to one worker until this feature is explicitly enabled. - Replace global ready/steal rings with work-stealing deques. Local spawn pushes to the local deque; idle workers steal from other workers.
- Make cancellation cross-thread safe.
with_runtime_request_cancelmust atomically set the cancellation flag and wake the worker running or owning the target fiber. If a parent needs cleanup, it yields/parks until the child reaches an interruptible point. - Audit all runtime globals in
rt/fiber_core_darwin.wandrt/fiber_core_windows.wfor per-worker vs global ownership. Panic queues, completed-fiber tables, detached-fiber lists, and stack pools need synchronization or worker partitioning. - Keep no silent fallback: if a platform lacks the required atomic/thread primitive, the runtime should compile out multi-worker mode with a diagnostic/config rejection, not pretend to run work stealing.
- Add runtime tracing/test hooks for worker count, steals attempted/succeeded, and cross-thread cancellation so tests can assert behavior without timing guesses.
Test plan
Phase 1 tests: single-worker behavior remains identical for existing async suite.
Phase 2 tests:
- behavior test spawning many CPU-bound async tasks and asserting they complete with worker count > 1;
- runtime hook test proving at least one successful steal under a controlled workload;
- cancellation test where a parent cancels a child known to be running on another worker and cleanup still runs.
Add stress tests with many short tasks to catch queue races, plus platform-specific runs on Darwin and Windows.
Acceptance criteria
- Runtime supports configurable multi-worker fiber scheduling.
- Work can be stolen across OS threads.
- Cross-thread cancellation follows §14.7 and runs cleanup.
- Single-worker mode remains green and deterministic enough for compiler builds.
with build && with build :fixpoint && with build :testpass.
Dependencies
Needs #403 for runtime configuration if worker count is config-driven. Overlaps #354 for suspension visibility and #473/#480 for sendability across thread boundaries.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 14.18.1.3, 14.7.1.43.
Activity
ehartford commentedon Jun 19, 2026
Implemented in 0f5a5c1. Added a configurable Darwin multi-worker fiber scheduler with per-worker queues, stealing counters, worker/run-state hooks, cross-thread cancellation accounting, runtime worker-count manifest plumbing, and loud rejection for unsupported Windows multi-worker mode. Also fixed the exposed Task.join_cleanup lowering bug so cleanup consumes the task disposition instead of double-freeing at scope exit.
Verification passed after rebasing on current main: build, fixpoint, full test suite, test-green, last-green, and install-user using the project seed/release commands.