Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+195
View File
@@ -0,0 +1,195 @@
# Tests of atomics: each operation at each width, of integers, `bool`, enums,
# and pointers, the orders that make no sense stopping the program, and two
# processes adding to memory they share, which loses nothing only where every
# addition is one step.
import system;
import std::testing;
# Whether running the function stops the program. It runs in a copy of the
# program that writes nothing, so that this one goes on and the report of the
# stop is not mixed into the tests'.
def stops_atomically(run: def(): void): bool {
when os == windows {
val stopped := copied(run);
return !(stopped.exited && stopped.code == 0);
}
val child := fork();
if child == 0 {
val out: fd = fd::stdout;
val err: fd = fd::stderr;
out.close();
err.close();
run();
exit(0);
}
var status: process_status;
return wait(child, &mut status) >= 0 && !(status.exited && status.code == 0);
}
# Works every operation on an atomic of `T`, given a value below another, compared
# as `T` is, where of an unsigned type the higher has its top bit set, so that
# comparing it as signed would order them the other way, and the least and
# greatest of `T`.
def operations_of<T>(t: &mut tester, low: T, high: T, least: T, most: T): void where T: integer, lockfree {
var a := atomic::<T>::new(low);
t.expects(a.load() == low);
a.store(high);
t.expects(a.load() == high);
t.expects(a.exchange(low) == high && a.load() == low);
# Adding and subtracting wrap.
a.store(most);
t.expects(a.fetch_add(1) == most && a.load() == least);
t.expects(a.fetch_sub(1) == least && a.load() == most);
a.store(low);
t.expects(a.fetch_add(high) == low);
val sum := a.load();
t.expects(a.fetch_sub(high) == sum && a.load() == low);
# Bits, against the operators.
a.store(high);
t.expects(a.fetch_and(low) == high && a.load() == (high & low));
a.store(high);
t.expects(a.fetch_or(low) == high && a.load() == (high | low));
a.store(high);
t.expects(a.fetch_xor(low) == high && a.load() == (high ^ low));
# The least and greatest, compared as `T` is.
a.store(low);
t.expects(a.fetch_min(high) == low && a.load() == low);
t.expects(a.fetch_max(high) == low && a.load() == high);
t.expects(a.fetch_max(low) == high && a.load() == high);
t.expects(a.fetch_min(low) == high && a.load() == low);
# A compare-exchange writes where it finds what it expects, and otherwise gives
# what it found.
match a.compare_exchange(low, high) {
value(found) => t.expects(found == low && a.load() == high);
error(_) => t.expects(false);
}
match a.compare_exchange(low, most, order::acq_rel, order::acquire) {
value(_) => t.expects(false);
error(found) => t.expects(found == high && a.load() == high);
}
match a.compare_exchange_weak(high, least, order::release, order::relaxed) {
value(found) => t.expects(found == high && a.load() == least);
error(_) => t.expects(false);
}
}
@test def every_operation_works_at_every_width(t: &mut tester): void {
operations_of::<u8>(t, 100, 200, 0, 255);
operations_of::<i8>(t, -56, 100, -128, 127);
operations_of::<u16>(t, 0x1234, 0xF00D, 0, 0xFFFF);
operations_of::<i16>(t, -4000, 0x1234, -32768, 32767);
operations_of::<u32>(t, 7, 0x80000001, 0, 0xFFFFFFFF);
operations_of::<i32>(t, -2000000000, 2000000000, -2147483648, 2147483647);
operations_of::<u64>(t, 3, 0x8000000000000003, 0, 0xFFFFFFFFFFFFFFFF);
operations_of::<i64>(t, -0x7000000000000000, 0x7000000000000000, -0x7FFFFFFFFFFFFFFF - 1, 0x7FFFFFFFFFFFFFFF);
}
@test def every_order_is_taken_where_it_makes_sense(t: &mut tester): void {
var a := atomic::<u32>::new(1);
for how in [order::relaxed, order::acquire, order::seq_cst] {
t.expects(a.load(how) == 1);
}
for how in [order::relaxed, order::release, order::seq_cst] {
a.store(2, how);
t.expects(a.load() == 2);
}
for how in [order::relaxed, order::acquire, order::release, order::acq_rel, order::seq_cst] {
t.expects(a.fetch_add(1, how) == 2 && a.exchange(2, how) == 3);
fence(how);
}
t.expects(a.compare_exchange(2, 5, order::seq_cst, order::seq_cst) is value);
t.expects(a.compare_exchange(5, 6, order::acq_rel, order::acquire) is value);
t.expects(a.compare_exchange(6, 7, order::release, order::relaxed) is value);
t.expects(a.compare_exchange(0, 8, order::relaxed, order::relaxed) is error);
t.expects(a.load() == 7);
}
enum light { red, amber, green }
@test def bools_enums_and_pointers_are_atomic(t: &mut tester): void {
var flag := atomic::<bool>::new(false);
t.expects(!flag.exchange(true) && flag.load());
t.expects(flag.compare_exchange(true, false) is value && !flag.load());
t.expects(flag.compare_exchange(true, true) is error);
var lamp := atomic::<light>::new(light::red);
lamp.store(light::green);
t.expects(lamp.exchange(light::amber) == light::green);
match lamp.compare_exchange(light::red, light::green) {
value(_) => t.expects(false);
error(found) => t.expects(found == light::amber);
}
val block := raw_allocate(16);
var at := atomic::<*mut u8>::new(null);
t.expects(at.exchange(block) == null && at.load() == block);
t.expects(at.compare_exchange(block, block + 8) is value && at.load() == block + 8);
raw_release(block);
}
@test def orders_that_make_no_sense_stop_the_program(t: &mut tester): void {
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.load(order::release); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.load(order::acq_rel); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); a.store(1, order::acquire); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); a.store(1, order::acq_rel); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::seq_cst, order::release); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::seq_cst, order::acq_rel); }));
# A failure no stronger than what the success reads.
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::relaxed, order::acquire); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::release, order::acquire); }));
t.expects(stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::acq_rel, order::seq_cst); }));
t.expects(!stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.compare_exchange(0, 1, order::acq_rel, order::acquire); }));
t.expects(!stops_atomically(def() { var a := atomic::<u64>::new(0); _ = a.load(order::acquire); a.store(1, order::release); }));
}
# How many times each process adds.
const contended: u64 = 1000000;
# Sharing a page between processes takes `fork` and `mmap`, which Linux has.
when os == linux {
@test def two_processes_adding_to_memory_they_share_lose_nothing(t: &mut tester): void {
# A page both processes keep after `fork`, rather than a copy each.
val page := system::linux::syscall(9, 0, 4096, 3, 0x21, 0xFFFFFFFFFFFFFFFF, 0);
t.expects(page > 0);
return if page <= 0;
val sum := page as u64 as *mut atomic<u64>;
val tries := (page as u64 + 8) as *mut atomic<u32>;
val bits := (page as u64 + 16) as *mut atomic<u16>;
val most := (page as u64 + 24) as *mut atomic<i32>;
val child := fork();
val own: u64 = child == 0 ? 1 : 2;
for i in 0..contended {
_ = sum[0].fetch_add(1);
# A loop of compare-exchanges adds as well, trying again where the other
# process wrote first.
var seen := tries[0].load(order::relaxed);
while true {
match tries[0].compare_exchange_weak(seen, seen + 1, order::acq_rel, order::relaxed) {
value(_) => break;
error(found) => seen = found;
}
}
_ = most[0].fetch_max((i * 2 + own) as i32);
if i % 1000 == 0 {
_ = bits[0].fetch_or((1 << (own * 4 + i / 1000 % 4)) as u16);
}
}
if child == 0 {
exit(0);
}
var status: process_status;
t.expects(wait(child, &mut status) >= 0 && status.exited && status.code == 0);
t.expects(sum[0].load() == 2 * contended);
t.expects(tries[0].load() == 2 * contended as u32);
t.expects(bits[0].load() == 0xFF0);
t.expects(most[0].load() == ((contended - 1) * 2 + 2) as i32);
_ = system::linux::syscall(11, page as u64, 4096, 0, 0, 0, 0);
}
}