atomics A.3a: swap + fence ops + recognizer, emit bails (lock)

swap (atomicrmw xchg) and a standalone fence wired end-to-end except LLVM
emission (both bail loudly; A.3b makes them real).
- RmwKind += xchg; atomic_swap intrinsic + swap method reuse the atomic_rmw op.
- new atomic_fence op (+ AtomicFence) — ordering-only, void; fence($o)/atomic_fence
  intrinsic; recognizer rejects .relaxed (LLVM has no monotonic fence).
- comptime_vm: xchg = store operand/return old; fence = no-op (single-thread).
- examples 1703 (swap) + 1704 (fence) locked to bails; 1187 (relaxed-fence reject).
- 1186 converted to a direct-intrinsic call → stable user-file diagnostic span
  (the lib-forward-site span shifted when atomic.sx grew — fragile-snapshot fix).

Also fixes a latent A.2 comptime-CAS bug found while here: the success/null
has_value write was 'writeWord(addr, SIZE=0, val=1)' — a 0-byte no-op, correct
ONLY because allocBytes zero-inits (REJECTED-PATTERNS 'coincidentally correct').
Now writes the flag explicitly (size=1, val=0). Suite green (721/0).
This commit is contained in:
agra
2026-06-20 13:47:08 +03:00
parent 79895be401
commit fca4304f83
21 changed files with 129 additions and 9 deletions

View File

@@ -33,6 +33,14 @@ atomic_fetch_xor :: ($T: Type, ptr: *T, operand: T, o: Ordering) -> T #builtin;
atomic_fetch_min :: ($T: Type, ptr: *T, operand: T, o: Ordering) -> T #builtin;
atomic_fetch_max :: ($T: Type, ptr: *T, operand: T, o: Ordering) -> T #builtin;
// Swap (exchange): store `operand`, return the OLD value. Integer T.
atomic_swap :: ($T: Type, ptr: *T, operand: T, o: Ordering) -> T #builtin;
// Standalone memory fence. The ordering may NOT be `.relaxed` (LLVM has no
// monotonic/unordered fence). `$o` is a comptime ordering param.
atomic_fence :: (o: Ordering) #builtin;
fence :: ($o: Ordering) { atomic_fence(o); }
// Compare-exchange intrinsics — integer T only. The result is `?T`:
// `null` = SUCCESS (the stored value equalled `expected`, replaced by `desired`);
// a present value is the ACTUAL current value on failure (for a retry loop).
@@ -72,6 +80,9 @@ Atomic :: struct ($T: Type) {
fetch_min :: (self: *Atomic(T), v: T, $o: Ordering) -> T { return atomic_fetch_min(T, @self.value, v, o); }
fetch_max :: (self: *Atomic(T), v: T, $o: Ordering) -> T { return atomic_fetch_max(T, @self.value, v, o); }
// Swap: store `v`, return the value BEFORE the swap (integer T).
swap :: (self: *Atomic(T), v: T, $o: Ordering) -> T { return atomic_swap(T, @self.value, v, o); }
// Compare-exchange (integer T). Returns `?T`: `null` on success (the value
// equalled `expected` and is now `desired`); on failure the ACTUAL current
// value (retry with it). `compare_exchange_weak` may fail spuriously — use it