Skip to content

Implement Once and fiber-aware Condvar (Barrier follows) (§14.17, §18.6) #395

Closed

Description

@ehartford

Summary

Implement the missing Once and fiber-aware Condvar pieces of std.sync, then layer Barrier on the same blocking substrate. The current stdlib has only placeholder Mutex/RwLock/Atomic surfaces, while §14.17 and §18.6 promise real synchronization primitives. This issue should be sequenced after the generic and scheduler foundations so blocked waiters yield fibers, not OS threads.

Spec

§14.17 lists Mutex[T], RwLock[T], Atomic[T], Condvar, Barrier, and Once, and requires fiber-aware synchronization operations to participate in may_suspend analysis. §18.6 maps std.sync to Mutex, RwLock, Atomic, Condvar, Barrier, and Once.

let once = Once.new()
once.call_once(|| init_global())

with lock.enter_mut() as state:
    while not state.ready:
        cond.wait(lock)

Current state

lib/std/sync.w contains only Mutex, RwLock, guards, and AtomicI64 (lib/std/sync.w:5, lib/std/sync.w:22, lib/std/sync.w:39). There are no Condvar, Barrier, or Once declarations. The existing Mutex/RwLock are non-locking i64 value facades, so this issue depends on #471 for a real lock/waiter representation.

may_suspend currently knows only direct await/select/scope-await in sema and a small MIR intrinsic set (src/SemaCheck.w:2076, src/MirSuspendCheck.w:221). Any Condvar.wait implementation that can block must join #354's current-fiber suspension model.

Implementation plan

  1. Implement Once first because it is useful and smaller: add type Once state plus Once.new(), call_once(fn() -> void), and fallible/panic semantics. It must be thread/fiber safe and run the initializer exactly once.
  2. Add runtime support for a fiber-aware waiter queue that can park the current fiber and wake waiters without blocking the OS thread. This can live beside the future lock runtime from Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471 rather than in legacy C.
  3. Implement Condvar in lib/std/sync.w as a safe wrapper around that runtime primitive. wait must release the associated mutex, suspend the current fiber, and reacquire before returning. notify_one and notify_all wake waiters.
  4. Mark Condvar.wait as a direct current-fiber suspension primitive for Implement compiler-visible current-fiber suspension model #354. It should be rejected inside no_suspend and while a @[no_await_guard] guard that is not the associated lock protocol is live.
  5. Implement Barrier after Condvar using the same waiter queue and generation count. Do not spin or busy-wait.
  6. Define and test panic behavior: if Once.call_once panics, either mark the Once poisoned or retry on the next call, but choose and document one behavior in the stdlib spec.

Test plan

Add test/spec/spec_ss14_17_once.w with multiple fibers racing to call call_once and asserting the initializer runs once.

Add test/spec/spec_ss14_17_condvar.w with a waiting fiber, notifying fiber, notify_one, notify_all, and predicate loop examples.

Add test/spec/spec_ss14_17_barrier.w after Barrier lands, covering N fibers and generation reuse.

Add compile-error tests for no_suspend: cond.wait(lock) and for waiting while an unrelated no-await guard is live.

Acceptance criteria

  • std.sync.Once, Condvar, and eventually Barrier exist with the §14.17/§18.6 names.
  • Blocking waits yield the current fiber rather than blocking an OS thread.
  • Wait operations participate in may_suspend and safety diagnostics.
  • Tests cover positive behavior and forbidden suspension contexts.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Depends on #354 and #471. Barrier can be a second phase after Once and Condvar.

Activity

  1. ehartford commented on Jun 19, 2026

    @ehartford
    ContributorAuthor

    Implemented in aa8216f. Added std.sync Once, Condvar, and Barrier with fiber-yielding waits; documented Once retry/Condvar/Barrier semantics; added direct suspension diagnostics for Once.call_once, Condvar.wait, and Barrier.wait; added associated-lock Condvar wait handling for guarded with blocks; and added positive/negative coverage for Once, Condvar, Barrier, no_suspend, and unrelated no_await_guard cases. Verification passed on rebased main: build, fixpoint, full test suite, explicit test-green, last-green, and install-user.

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