Skip to content

Work-stealing multi-OS-thread fiber scheduler (§14.18, §14.7) #455

Closed

Description

@ehartford

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

  1. Split the scheduler state into a Scheduler plus 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.
  2. Add a runtime-configured worker pool size, likely sharing [runtime] fiber_pool_size or a new fiber_worker_count with Parse [runtime] with.toml keys (fiber_stack_size, fiber_pool_size) #403. Default to one worker until this feature is explicitly enabled.
  3. Replace global ready/steal rings with work-stealing deques. Local spawn pushes to the local deque; idle workers steal from other workers.
  4. Make cancellation cross-thread safe. with_runtime_request_cancel must 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.
  5. Audit all runtime globals in rt/fiber_core_darwin.w and rt/fiber_core_windows.w for per-worker vs global ownership. Panic queues, completed-fiber tables, detached-fiber lists, and stack pools need synchronization or worker partitioning.
  6. 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.
  7. 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 :test pass.

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

  1. ehartford commented on Jun 19, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions