atomics A.2a: CAS ops + recognizer + methods, emit bails (lock)

compare_exchange/_weak wired end-to-end except LLVM emission (bails loudly;
A.2b makes it real). New IR op atomic_cmpxchg + AtomicCmpxchg{ptr, cmp, new,
val_ty, success_ordering, failure_ordering, weak}; result type = ?T (null =
SUCCESS, failure carries the actual value for retry). print arm; emit dispatch
-> emitAtomicCmpxchg (BAILS). comptime_vm arm does real single-thread CAS (read
actual / compare / store-on-equal / build ?T: success->none, failure->some;
weak == strong at comptime). Recognizer extended (atomic_cmpxchg/_weak, 6 args)
-- CAS restricted to INTEGER T (loud reject); BOTH orderings resolved via
atomicOrderingFromNode; dual-ordering validation (failure may not be
release/acq_rel nor stronger than success, via atomicOrderingRank). Methods
compare_exchange/_weak on Atomic($T) with comptime $success/$failure: Ordering.
examples/1702 locked to the bail; examples/1186 locks a rejected ordering pair.
Suite green (718/0).
This commit is contained in:
agra
2026-06-20 10:44:31 +03:00
parent 68ed732b79
commit dca396ed1f
16 changed files with 226 additions and 12 deletions

View File

@@ -33,6 +33,16 @@ 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;
// 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).
// `_weak` may fail spuriously (LLVM `cmpxchg weak`) — use it inside a retry loop.
// Two orderings: `success` (applied when the exchange happens) and `failure`
// (the load when it doesn't). The failure ordering may not be .release / .acq_rel
// and may not be stronger than the success ordering (LLVM rule, enforced at lower).
atomic_cmpxchg :: ($T: Type, ptr: *T, expected: T, desired: T, success: Ordering, failure: Ordering) -> ?T #builtin;
atomic_cmpxchg_weak :: ($T: Type, ptr: *T, expected: T, desired: T, success: Ordering, failure: Ordering) -> ?T #builtin;
// The ordering is a COMPTIME value param (`$o`): it must be known at compile
// time because LLVM atomic ordering is an instruction attribute, not a runtime
// operand. It is explicit (Rust-style — no default), so the caller always states
@@ -61,4 +71,11 @@ Atomic :: struct ($T: Type) {
fetch_xor :: (self: *Atomic(T), v: T, $o: Ordering) -> T { return atomic_fetch_xor(T, @self.value, v, o); }
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); }
// 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
// inside a retry loop. `$success` / `$failure` are comptime ordering params.
compare_exchange :: (self: *Atomic(T), expected: T, desired: T, $success: Ordering, $failure: Ordering) -> ?T { return atomic_cmpxchg(T, @self.value, expected, desired, success, failure); }
compare_exchange_weak :: (self: *Atomic(T), expected: T, desired: T, $success: Ordering, $failure: Ordering) -> ?T { return atomic_cmpxchg_weak(T, @self.value, expected, desired, success, failure); }
}