Repository navigation
Description
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
- Implement
Oncefirst because it is useful and smaller: addtype Oncestate plusOnce.new(),call_once(fn() -> void), and fallible/panic semantics. It must be thread/fiber safe and run the initializer exactly once. - 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.
- Implement
Condvarinlib/std/sync.was a safe wrapper around that runtime primitive.waitmust release the associated mutex, suspend the current fiber, and reacquire before returning.notify_oneandnotify_allwake waiters. - Mark
Condvar.waitas a direct current-fiber suspension primitive for Implement compiler-visible current-fiber suspension model #354. It should be rejected insideno_suspendand while a@[no_await_guard]guard that is not the associated lock protocol is live. - Implement
BarrierafterCondvarusing the same waiter queue and generation count. Do not spin or busy-wait. - Define and test panic behavior: if
Once.call_oncepanics, 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 eventuallyBarrierexist with the §14.17/§18.6 names.- Blocking waits yield the current fiber rather than blocking an OS thread.
- Wait operations participate in
may_suspendand safety diagnostics. - Tests cover positive behavior and forbidden suspension contexts.
with build && with build :fixpoint && with build :testpass.
Dependencies
Depends on #354 and #471. Barrier can be a second phase after Once and Condvar.
Activity
ehartford commentedon Jun 19, 2026
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.