Skip to content

Implement generic Atomic[T] per §14.17.1 #394

Closed

Description

@ehartford

Summary

Implement the generic Atomic[T] API promised by §14.17.1 and expose it through std.sync. The compiler already has partial atomic MIR/codegen plumbing, but the stdlib user surface is only a non-atomic AtomicI64 facade. This is foundational for correct synchronization and for later fiber-aware locks.

Spec

§14.17.1 specifies Atomic[T] for integer and pointer types with Order values .Relaxed, .Acquire, .Release, .AcqRel, and .SeqCst. It requires new, load, store, swap, fetch_add/sub/and/or/xor/min/max, compare_exchange, compare_exchange_weak, and std.sync.fence, with compile-time ordering validity rules.

let a = Atomic[i64].new(0)
let old = a.fetch_add(1, .SeqCst)
a.store(5, .Release)

Current state

lib/std/sync.w exposes AtomicI64 { value: i64 }, atomic_new, atomic_load, AtomicI64.store, and AtomicI64.add, all implemented as ordinary value operations (lib/std/sync.w:39, lib/std/sync.w:114, lib/std/sync.w:127). That does not provide atomicity, generic type support, or ordering arguments.

The compiler already recognizes a generic Atomic intrinsic family in dispatch tables: codegen classifies Atomic.load/store/swap/fetch_* / compare_exchange* (src/CodegenDispatch.w:4816) and MIR lowering has matching intrinsic names (src/MirLower.w:6025). Sema has return-type special cases for owner name Atomic (src/SemaCheck.w:9850). The missing piece is a real stdlib type/API plus complete sema validation. This should wait for #391 because current generic instance erasure can confuse Atomic[i32] and Atomic[str].

Implementation plan

  1. After Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391, replace the AtomicI64 facade in lib/std/sync.w with pub type Atomic[T] and pub enum Order: i32 variants .Relaxed, .Acquire, .Release, .AcqRel, .SeqCst.
  2. Add method declarations for every §14.17.1 operation, backed by the existing MIR intrinsics. Do not implement these as ordinary field loads/stores.
  3. Tighten sema in src/SemaCheck.w: T must be an integer or pointer type; reject Atomic[str], Atomic[Vec[i32]], and non-Copy payloads with Atomic[T] requires integer or pointer T.
  4. Complete ordering validation in validate_atomic_ordering: .store cannot use .Acquire/.AcqRel; .load cannot use .Release/.AcqRel; compare-exchange failure ordering cannot be .Release/.AcqRel and cannot be stronger than success.
  5. Ensure LLVM and C backends lower all operations consistently. CCodegen.w already has ATOMIC_LOAD/STORE/SWAP helpers; add missing fetch and CAS variants or fail loudly until implemented.
  6. Add std.sync.fence(order) as an intrinsic-backed function using MirIntrinsic.ATOMIC_FENCE (src/MirLower.w:8159).
  7. Remove or keep AtomicI64 only as a compatibility alias to Atomic[i64]; do not leave it as a non-atomic independent implementation.

Test plan

Add test/spec/spec_ss14_17_atomic_generic.w with positive Atomic[i32], Atomic[u64], and pointer atomic coverage for load/store/swap/fetch_add/fetch_and/fetch_or/fetch_xor/CAS.

Add test/compile_errors/err_atomic_non_atomic_type.w for Atomic[str] and Atomic[Vec[i32]].

Add ordering diagnostics:

  • err_atomic_store_acquire.w;
  • err_atomic_load_release.w;
  • err_atomic_cas_bad_failure_order.w;
  • err_atomic_cas_failure_stronger_than_success.w.

Add backend parity behavior tests for fetch/CAS if both LLVM and emit-C are expected to support them.

Acceptance criteria

  • std.sync.Atomic[T] exists for integer and pointer T.
  • All §14.17.1 operations typecheck with correct return types and order parameters.
  • Invalid T and invalid order combinations fail at compile time.
  • Operations lower to real atomic instructions or backend atomics, not ordinary loads/stores.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Depends on #391. Blocks or unblocks parts of #471 and #395.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Implemented in 7a25f9d with supporting unsafe callback migration fix in 1c3d4eb. Added generic Atomic[T] for integer and pointer payloads, memory-order validation, compare_exchange Result semantics, fence support, emit-C lowering, legacy AtomicI64 compatibility, and positive/negative coverage. Verification passed with project seed/release commands: build, fixpoint, full :test, and explicit :test-green.

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