Skip to content

Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471

Closed

Description

@ehartford

Summary

Replace the current i64 value facades with real generic Mutex[T] and RwLock[T] that provide scoped access and fiber-aware blocking. The existing types copy values into guards and do not lock anything. Correct locks are required for §14.17 synchronization and for With's "safe by default" concurrency story.

Spec

§14.17 lists Mutex[T] as mutual exclusion with scoped access and RwLock[T] as reader-writer lock with scoped access. All synchronization primitives are usable with with blocks, and fiber-aware synchronization operations must participate in may_suspend.

let m = Mutex[State].new(State {})
with m.enter_mut() as state:
    state.count = state.count + 1

Current state

lib/std/sync.w defines type Mutex { value: i64 }, MutexGuard { value: i64 }, MutexGuardMut { value: i64 }, and analogous RwLock types (lib/std/sync.w:5, lib/std/sync.w:10, lib/std/sync.w:22). Mutex.enter and enter_mut return guards containing copies of the i64 value (lib/std/sync.w:49, lib/std/sync.w:53), and ScopedMut.with_exit_mut mutates the guard value, not the original mutex (lib/std/sync.w:71, lib/std/sync.w:75).

There is no runtime lock handle, no waiter queue, no ownership of generic T, and no contended-lock yield. This depends on #391 because the API must distinguish Mutex[State], Mutex[i64], and guard payload types reliably.

Implementation plan

  1. After Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391, redesign lib/std/sync.w as Mutex[T], MutexGuard[T], MutexGuardMut[T], RwLock[T], RwReadGuard[T], and RwWriteGuard[T].
  2. Store T in stable heap/runtime storage owned by the lock, not copied into the guard. Guards should hold a lock handle plus a pointer/view to protected storage and unlock on with_exit/Drop.
  3. Implement runtime lock primitives in With runtime code, not helpers.c: create/destroy mutex, acquire shared/exclusive, release, and waiter wakeup. Blocked acquires must yield the current fiber.
  4. Integrate with Scoped[T] / ScopedMut[T] so with lock.enter() as value and with lock.enter_mut() as mut value work without exposing raw pointers.
  5. Mark acquire operations as may_suspend for Implement compiler-visible current-fiber suspension model #354 and make guards @[no_await_guard] so awaiting while holding a lock remains rejected.
  6. Add poisoning or explicitly choose no-poisoning semantics for panics while holding a guard. Document the choice in the stdlib spec.
  7. Ensure Drop cleanup destroys the protected T exactly once and wakes/cancels waiters on lock destruction.

Test plan

Add test/spec/spec_ss14_17_mutex_generic.w with a non-i64 struct payload mutated through with.

Add test/spec/spec_ss14_17_rwlock_generic.w with multiple readers and one writer, including writer exclusion.

Add contention behavior tests with two fibers where the second acquire waits until the first guard exits.

Add compile-error coverage:

  • awaiting while a MutexGuard/RwLock guard is live;
  • storing guard values outside scope if they are ephemeral;
  • using lock acquire inside no_suspend.

Acceptance criteria

  • Mutex[T] and RwLock[T] are generic and protect the actual T.
  • Guards provide scoped access and unlock/drop correctly.
  • Contended acquire yields the current fiber and participates in may_suspend.
  • Awaiting while a guard is live remains a compile error.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Depends on #391 and #354. Blocks #395 and interacts with #473 Send/Sync enforcement.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 14.17.1.1, 14.17.1.2, 14.17.1.5, 14.17.1.6.

Activity

  1. ehartford commented on Jun 18, 2026

    @ehartford
    ContributorAuthor

    Implemented in fcb7ccf. Mutex[T] and RwLock[T] now protect generic payloads in stable lock-owned storage, expose scoped guards, participate in no_suspend/may_suspend checking, reject awaits while guards are live, and drop protected values exactly once. Supporting compiler fixes covered generic method ABI signatures, value-ref ABI lowering, real Drop-trait destructor detection, guard analysis, and related parser/sema regressions surfaced by the implementation. Also pushed d2da7e9 to sync spec inventory entries for emit-c-header and std.ffi after rebasing onto main. Verification passed after rebase and spec sync: build, fixpoint, full test suite, test-green, last-green, and install-user, all run with 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