Repository navigation
Description
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
- After Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391, replace the
AtomicI64facade inlib/std/sync.wwithpub type Atomic[T]andpub enum Order: i32variants.Relaxed,.Acquire,.Release,.AcqRel,.SeqCst. - Add method declarations for every §14.17.1 operation, backed by the existing MIR intrinsics. Do not implement these as ordinary field loads/stores.
- Tighten sema in
src/SemaCheck.w:Tmust be an integer or pointer type; rejectAtomic[str],Atomic[Vec[i32]], and non-Copy payloads withAtomic[T] requires integer or pointer T. - Complete ordering validation in
validate_atomic_ordering:.storecannot use.Acquire/.AcqRel;.loadcannot use.Release/.AcqRel; compare-exchange failure ordering cannot be.Release/.AcqReland cannot be stronger than success. - Ensure LLVM and C backends lower all operations consistently.
CCodegen.walready hasATOMIC_LOAD/STORE/SWAPhelpers; add missing fetch and CAS variants or fail loudly until implemented. - Add
std.sync.fence(order)as an intrinsic-backed function usingMirIntrinsic.ATOMIC_FENCE(src/MirLower.w:8159). - Remove or keep
AtomicI64only as a compatibility alias toAtomic[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 pointerT.- All §14.17.1 operations typecheck with correct return types and order parameters.
- Invalid
Tand 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 :testpass.
Activity
ehartford commentedon Jun 17, 2026
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.