Files
sx/library/modules/std/atomic.sx
agra acf31839ea atomics A.0.5: full ordering surface (comptime $o: Ordering)
Migrate Atomic methods from seq_cst-only to the explicit ordering surface now
that comptime value params work on generic-struct methods (workers 3c4305f /
d7a6857 / d95ba0a):

- atomic.sx: load/store take a comptime $o: Ordering (explicit, Rust-style; no
  default, matching design 4.6). a.load(.acquire) -> 'load atomic .. acquire'.
- call.zig: atomicOrderingFromNode resolves a comptime-bound ordering identifier
  via comptimeIntNamed (+ atomicOrderingFromTag); documents the sx-Ordering <->
  IR-AtomicOrdering declaration-order invariant. The per-op validity guard fires
  through the method path (a.load(.release) is a compile error).
- 1700 migrated to explicit orderings (output unchanged 7/42/43).

Suite green (715/0).
2026-06-20 10:04:39 +03:00

46 lines
1.9 KiB
Plaintext

// Atomics — `Atomic($T)` wrapper + `Ordering`, over the compiler's atomic
// load/store (later: rmw/cas/fence) IR ops. Consumers reach these through
// std.sx (`Atomic` / `Ordering` re-exports), never by importing this file.
//
// Atomicity is a property of the OPERATION, not the storage: `Atomic(T)` is a
// transparent 1-field wrapper with `T`'s exact layout/size/align. The ops are
// `#builtin` intrinsics recognized by name at lower-time
// (`tryLowerAtomicIntrinsic`, src/ir/lower/call.zig) and emitted as dedicated
// atomic IR ops; the `Ordering` argument MUST be a constant enum literal.
#import "modules/std/core.sx";
Ordering :: enum {
relaxed; // → LLVM Monotonic
acquire; // → LLVM Acquire
release; // → LLVM Release
acq_rel; // → LLVM AcquireRelease
seq_cst; // → LLVM SequentiallyConsistent
}
// Compiler intrinsics. Not called directly by users — `Atomic(T)`'s methods
// forward to them. Recognized by name in lowering; the `Ordering` arg MUST be a
// constant enum literal (a non-literal is a loud diagnostic).
atomic_load :: ($T: Type, ptr: *T, o: Ordering) -> T #builtin;
atomic_store :: ($T: Type, ptr: *T, v: T, o: Ordering) #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
// the ordering: `a.load(.acquire)`, `a.store(v, .release)`. An invalid
// combination (`a.load(.release)`) is a compile error.
Atomic :: struct ($T: Type) {
value: T;
init :: (v: T) -> Atomic(T) {
return .{ value = v };
}
load :: (self: *Atomic(T), $o: Ordering) -> T {
return atomic_load(T, @self.value, o);
}
store :: (self: *Atomic(T), v: T, $o: Ordering) {
atomic_store(T, @self.value, v, o);
}
}