#! @file atomic.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains atomics: values that several threads, or a thread and a signal #! handler, read and write without a lock, the orders that say what each of #! them sees of the reads and writes around them, and fences. module core; #! How an atomic operation orders the reads and writes around it, as C11's #! memory model has them, which C, C++, Rust, and Zig share. #! #! Within a thread, everything happens in the order the program says. Between #! threads, a `release` store that an `acquire` load reads makes everything the #! storing thread did before the store visible to the loading thread after the #! load. `seq_cst` operations also fall in one order that every thread agrees #! on, and `relaxed` ones are atomic and nothing more. Two threads touching the #! same memory that is not atomic, at least one of them writing, with neither #! happening before the other, is a race, which is the program's mistake: what #! either then reads is not defined. pub enum order : u8 { relaxed, #! Only the operation itself is atomic. acquire, #! What follows a load stays after it. release, #! What comes before a store stays before it. acq_rel, #! Both, for operations that read and write. seq_cst, #! Both, and every `seq_cst` operation falls in one order all threads see. } #! The types an atomic holds: those of a word or less that every processor reads #! and writes whole without a lock, which are the integers, `bool`, enums, and #! pointers. The compiler decides what is lock-free, so no type can implement it. pub trait lockfree {} #! A value that threads read and write without a lock. #! #! Every operation takes an order, `seq_cst` where none is written, so code that #! does not care writes none, and code that does says exactly what it needs. #! Writing takes `&mut this` and reading `&this`, so no read-only reference #! ever writes; threads share one atomic through `&mut atomic`. #! #! It is `owning`, closing nothing, so it is never copied by accident: a copy #! would read the value without an atomic load, which is a race where another #! thread writes it. A struct holding one is not copied implicitly either, which #! is what shared state wants. #! @tparam T What it holds: an integer, `bool`, enum, or pointer. pub struct atomic : owning where T: lockfree { var _value: T; } #! Makes an atomic. #! @param value What it holds first. #! @returns The atomic. pub def atomic::new(value: T): atomic { return atomic:: { _value = value }; } #! Reads what an atomic holds. #! @param this The atomic. #! @param how The order: `relaxed`, `acquire`, or `seq_cst`, since a load #! publishes nothing. Another stops the program with a fault, #! which no switch leaves out. #! @returns What it holds. pub def atomic::load(&this, how: order = order::seq_cst): T { if how == order::release || how == order::acq_rel { misordered(0, how); } return atomic_load::((&this._value) as *T, how); } #! Writes a value into an atomic. #! @param this The atomic. #! @param value What it holds after. #! @param how The order: `relaxed`, `release`, or `seq_cst`, since a store #! sees nothing. Another stops the program with a fault, which #! no switch leaves out. pub def atomic::store(&mut this, value: T, how: order = order::seq_cst): void { if how == order::acquire || how == order::acq_rel { misordered(1, how); } atomic_store::((&mut this._value) as *mut T, value, how); } #! Writes a value into an atomic, giving what it held, in one step. #! @param this The atomic. #! @param value What it holds after. #! @param how The order. #! @returns What it held before. pub def atomic::exchange(&mut this, value: T, how: order = order::seq_cst): T { return atomic_exchange::((&mut this._value) as *mut T, value, how); } #! Writes a value into an atomic where it holds what is expected, in one step. #! @param this The atomic. #! @param expected What it must hold to be written. #! @param desired What it holds after, where it is written. #! @param success The order where it is written. #! @param failure The order where it is not, which only reads: `relaxed`, #! `acquire`, or `seq_cst`, and no stronger than what #! `success` reads. Another stops the program with a fault, #! which no switch leaves out. #! @returns What it held: as the `value` where it was written, and as the #! `error` where it was not. pub def atomic::compare_exchange( &mut this, expected: T, desired: T, success: order = order::seq_cst, failure: order = order::seq_cst ): result { if ((failures_taken >> (success as u64 * 5 + failure as u64)) & 1) == 0 { misordered(2, failure); } var found := expected; if atomic_compare_exchange::((&mut this._value) as *mut T, expected, desired, (&mut found) as *mut T, success, failure) { return result:: { value = expected }; } return result:: { error = found }; } #! As `compare_exchange`, but may fail although the atomic holds what is #! expected, which costs less on some processors, so it is meant for loops that #! try again. On x86-64 it never fails so. #! @param this The atomic. #! @param expected What it must hold to be written. #! @param desired What it holds after, where it is written. #! @param success The order where it is written. #! @param failure The order where it is not, as `compare_exchange` takes. #! @returns What it held: as the `value` where it was written, and as the #! `error` where it was not. pub def atomic::compare_exchange_weak( &mut this, expected: T, desired: T, success: order = order::seq_cst, failure: order = order::seq_cst ): result { return this.compare_exchange(expected, desired, success, failure); } #! Adds to an integer an atomic holds, wrapping, giving what it held. #! @param this The atomic. #! @param amount What is added. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_add(&mut this, amount: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_add` adds to an integer, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, amount, how); } #! Subtracts from an integer an atomic holds, wrapping, giving what it held. #! @param this The atomic. #! @param amount What is subtracted. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_sub(&mut this, amount: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_sub` subtracts from an integer, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, amount, how); } #! Keeps the bits of an integer an atomic holds that are also set in others, #! giving what it held. #! @param this The atomic. #! @param bits The others. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_and(&mut this, bits: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_and` combines the bits of an integer, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, bits, how); } #! Sets bits of an integer an atomic holds, giving what it held. #! @param this The atomic. #! @param bits The bits set. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_or(&mut this, bits: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_or` combines the bits of an integer, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, bits, how); } #! Flips bits of an integer an atomic holds, giving what it held. #! @param this The atomic. #! @param bits The bits flipped. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_xor(&mut this, bits: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_xor` combines the bits of an integer, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, bits, how); } #! Lowers an integer an atomic holds to a value where that is less, compared as #! `T` is, signed or unsigned, giving what it held. #! @param this The atomic. #! @param value The value. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_min(&mut this, value: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_min` compares integers, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, value, how); } #! Raises an integer an atomic holds to a value where that is greater, compared #! as `T` is, signed or unsigned, giving what it held. #! @param this The atomic. #! @param value The value. #! @param how The order. #! @returns What it held before. pub def atomic::fetch_max(&mut this, value: T, how: order = order::seq_cst): T { if const !(T is integer) { compile_error("`fetch_max` compares integers, and the atomic holds none."); } return atomic_fetch::((&mut this._value) as *mut T, value, how); } #! Closes an atomic, which owns nothing, so it does nothing; an atomic is #! `owning` only so that it is never copied by accident. #! @param this The atomic. pub def atomic::close(&mut this): void { } #! Orders the reads and writes around it as an atomic operation of the order #! would, without one. #! @param how The order. pub def fence(how: order): void { atomic_fence(how); } # Stops the program where an atomic operation is given an order it does not # take, as a fault. It is called only where the check fails, so where the # check holds it costs a compare, which folds away where the order is known, and # what calls it stays short enough to be emitted where it is called. # The operation is 0 for a load, 1 for a store, and 2 for a compare-exchange # failing, whose text it says. def misordered(operation: u8, how: order): void { val what := operation == 0 ? "A load's order is `relaxed`, `acquire`, or `seq_cst`" : operation == 1 ? "A store's order is `relaxed`, `release`, or `seq_cst`" : "A compare-exchange failing only reads, so its order is `relaxed`, `acquire`, or `seq_cst`, and no stronger than what it reads succeeding"; fault($"{what}, not `{how}`."); } # The orders a failed compare-exchange takes, of each it takes where it writes: # the bit `5 * success + failure` is set where that failure order only reads, as # `relaxed`, `acquire`, and `seq_cst` do, no more strongly than what the success # order reads. `relaxed` and `release` read as `relaxed`, `acquire` and # `acq_rel` as `acquire`, and `seq_cst` as itself. const failures_taken: u64 = 0x1 | 0x60 | 0x400 | 0x18000 | 0x1300000; # The operations `atomic_fetch` works, as the compilers number them, after a # load, store, exchange, and compare-exchange. const atomic_add: u32 = 4; # Adds, wrapping. const atomic_sub: u32 = 5; # Subtracts, wrapping. const atomic_and: u32 = 6; # Keeps the bits both have. const atomic_or: u32 = 7; # Keeps the bits either has. const atomic_xor: u32 = 8; # Keeps the bits one has. const atomic_min: u32 = 9; # Keeps the less, compared as `T` is. const atomic_max: u32 = 10; # Keeps the greater. # Reads `T` at the pointer, in one step, ordered as `how` says. @intrinsic def atomic_load(at: *T, how: order): T where T: lockfree; # Writes a value at the pointer, in one step. @intrinsic def atomic_store(at: *mut T, value: T, how: order): void where T: lockfree; # Writes a value at the pointer, giving what was there, in one step. @intrinsic def atomic_exchange(at: *mut T, value: T, how: order): T where T: lockfree; # Writes `desired` at the pointer where it holds `expected`, in one step, giving # whether it wrote, and writing what it held where `found` points. @intrinsic def atomic_compare_exchange(at: *mut T, expected: T, desired: T, found: *mut T, success: order, failure: order): bool where T: lockfree; # Works operation `OP` on the integer at the pointer and the operand, writing # what it gives there, in one step, and giving what was there. @intrinsic def atomic_fetch(at: *mut T, operand: T, how: order): T where T: lockfree; # Orders the reads and writes around it. @intrinsic def atomic_fence(how: order): void;