171 lines
6.1 KiB
Plaintext
171 lines
6.1 KiB
Plaintext
import std::io;
|
|||
|
|
import std::process;
|
||
|
|
import system;
|
||
|
|
|
||
|
|
extern import "kernel32.dll" when os == windows;
|
||
|
|
|
||
|
|
# Atomics of every width and kind, their operations giving what they held, and
|
||
|
|
# two processes adding to a page they share, which loses nothing.
|
||
|
|
|
||
|
|
def show<T>(name: strview, a: &mut atomic<T>, low: T, high: T): void where T: integer, lockfree {
|
||
|
|
a.store(low);
|
||
|
|
val added := a.fetch_add(high);
|
||
|
|
val subtracted := a.fetch_sub(low, order::acq_rel);
|
||
|
|
val anded := a.fetch_and(high, order::relaxed);
|
||
|
|
val ored := a.fetch_or(low);
|
||
|
|
val xored := a.fetch_xor(high, order::release);
|
||
|
|
val least := a.fetch_min(low);
|
||
|
|
val most := a.fetch_max(high, order::acquire);
|
||
|
|
val swapped := a.exchange(low);
|
||
|
|
io::println($"{name}: {added} {subtracted} {anded} {ored} {xored} {least} {most} {swapped} {a.load()}");
|
||
|
|
match a.compare_exchange(low, high) {
|
||
|
|
value(found) => io::print($" wrote over {found}");
|
||
|
|
error(found) => io::print($" found {found}");
|
||
|
|
}
|
||
|
|
match a.compare_exchange(low, high, order::release, order::relaxed) {
|
||
|
|
value(found) => io::println($", wrote over {found}");
|
||
|
|
error(found) => io::println($", found {found}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
enum state { idle, busy, done }
|
||
|
|
|
||
|
|
struct counters {
|
||
|
|
var hits: atomic<u64>;
|
||
|
|
var flags: atomic<u8>;
|
||
|
|
}
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
var a8 := atomic::<u8>::new(0);
|
||
|
|
var b8 := atomic::<i8>::new(0);
|
||
|
|
var a16 := atomic::<u16>::new(0);
|
||
|
|
var b16 := atomic::<i16>::new(0);
|
||
|
|
var a32 := atomic::<u32>::new(0);
|
||
|
|
var b32 := atomic::<i32>::new(0);
|
||
|
|
var a64 := atomic::<u64>::new(0);
|
||
|
|
var b64 := atomic::<i64>::new(0);
|
||
|
|
show::<u8>("u8", &mut a8, 100, 200);
|
||
|
|
show::<i8>("i8", &mut b8, -100, 90);
|
||
|
|
show::<u16>("u16", &mut a16, 0x7FFF, 0xFFF0);
|
||
|
|
show::<i16>("i16", &mut b16, -30000, 30000);
|
||
|
|
show::<u32>("u32", &mut a32, 5, 0xFFFFFFFE);
|
||
|
|
show::<i32>("i32", &mut b32, -2147483647, 2147483647);
|
||
|
|
show::<u64>("u64", &mut a64, 1, 0xFFFFFFFFFFFFFFFF);
|
||
|
|
show::<i64>("i64", &mut b64, -9223372036854775807, 9223372036854775807);
|
||
|
|
|
||
|
|
var flag := atomic::<bool>::new(false);
|
||
|
|
val was := flag.exchange(true);
|
||
|
|
io::println($"bool: {was} {flag.load()} {flag.compare_exchange(true, false) is value} {flag.load()}");
|
||
|
|
|
||
|
|
var now := atomic::<state>::new(state::idle);
|
||
|
|
now.store(state::busy, order::release);
|
||
|
|
val before := now.exchange(state::done);
|
||
|
|
io::println($"enum: {before == state::busy} {now.load(order::acquire) == state::done}");
|
||
|
|
|
||
|
|
var words: u64[4] = [10, 20, 30, 40];
|
||
|
|
var at := atomic::<*mut u64>::new(&mut words[0] as *mut u64);
|
||
|
|
match at.compare_exchange(&mut words[0] as *mut u64, &mut words[2] as *mut u64) {
|
||
|
|
value(_) => io::println($"pointer: {at.load()[0]}");
|
||
|
|
error(_) => io::println("pointer: refused");
|
||
|
|
}
|
||
|
|
|
||
|
|
var held := counters { hits = atomic::<u64>::new(0), flags = atomic::<u8>::new(0) };
|
||
|
|
for i in 0..10u {
|
||
|
|
_ = held.hits.fetch_add(i);
|
||
|
|
_ = held.flags.fetch_or((1 << (i % 8)) as u8);
|
||
|
|
}
|
||
|
|
fence(order::seq_cst);
|
||
|
|
io::println($"struct: {held.hits.load()} {held.flags.load()}");
|
||
|
|
|
||
|
|
# A page both processes keep, rather than a copy each.
|
||
|
|
val page := shared_page();
|
||
|
|
val sum := page as *mut atomic<u64>;
|
||
|
|
val tries := (page + 8) as *mut atomic<u32>;
|
||
|
|
val child := copy();
|
||
|
|
for _ in 0..100000 {
|
||
|
|
_ = sum[0].fetch_add(1);
|
||
|
|
var seen := tries[0].load(order::relaxed);
|
||
|
|
while true {
|
||
|
|
match tries[0].compare_exchange_weak(seen, seen + 3, order::acq_rel, order::relaxed) {
|
||
|
|
value(_) => break;
|
||
|
|
error(found) => seen = found;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if child == 0 {
|
||
|
|
exit(0);
|
||
|
|
}
|
||
|
|
var status: process_status;
|
||
|
|
_ = wait(child, &mut status);
|
||
|
|
io::println($"shared: {status.exited} {sum[0].load()} {tries[0].load()}");
|
||
|
|
}
|
||
|
|
|
||
|
|
# The page both processes add to, and the copy of the program adding with this
|
||
|
|
# one, whose number `copy` gives, or zero in the copy. Linux keeps a page mapped
|
||
|
|
# shared in the copy `fork` makes. Windows cannot fork, so there the copy is the
|
||
|
|
# program started again, writing nothing, which maps the page by the name it is
|
||
|
|
# started with.
|
||
|
|
when os == linux {
|
||
|
|
def shared_page(): u64 {
|
||
|
|
return system::linux::syscall(9, 0, 4096, 3, 0x21, 0xFFFFFFFFFFFFFFFF, 0) as u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
def copy(): i64 {
|
||
|
|
return fork();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
# The variable the copy is started with, holding the name of the page.
|
||
|
|
val page_variable: strview = "SAKURA_ATOMICS_PAGE";
|
||
|
|
|
||
|
|
# The page, as this program mapped it.
|
||
|
|
var mapped: u64 = 0;
|
||
|
|
|
||
|
|
# The name of the page: the one the copy was given, or one of this program's
|
||
|
|
# own.
|
||
|
|
def page_name(): string {
|
||
|
|
if process::variable(page_variable) is some(given) {
|
||
|
|
return string::from(given, nothing {});
|
||
|
|
}
|
||
|
|
return $"sakura-atomics-{getpid()}";
|
||
|
|
}
|
||
|
|
|
||
|
|
def shared_page(): u64 {
|
||
|
|
val name := page_name();
|
||
|
|
var wide: u16[64];
|
||
|
|
for i in 0..name.length {
|
||
|
|
wide[i] = name[i] as u16;
|
||
|
|
}
|
||
|
|
val mapping := pages::CreateFileMappingW(0xFFFFFFFFFFFFFFFF, null, 0x04, 0, 4096, (&wide) as *u16);
|
||
|
|
mapped = pages::MapViewOfFile(mapping, 0x02, 0, 0, 4096);
|
||
|
|
return mapped;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The copy is slow to start, so it says it has mapped the page, in the word
|
||
|
|
# after what the two add to, and the program waits for that, for ten seconds
|
||
|
|
# at most, so that the two add at once, as after `fork`.
|
||
|
|
def copy(): i64 {
|
||
|
|
val ready := (mapped + 12) as *mut atomic<u32>;
|
||
|
|
if process::variable(page_variable) is some(_) {
|
||
|
|
ready[0].store(1);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
var device := path::parse(null_device) else (_) { return -1; };
|
||
|
|
var quiet := io::file::open(&device, open_flags::write) else (_) { return -1; };
|
||
|
|
val started := respawn($"{page_variable}={page_name()}".view(), fd::stdin, quiet.handle, fd::stderr);
|
||
|
|
val until := clock(clock_kind::steady) + 10000000000;
|
||
|
|
while started > 0 && ready[0].load() == 0 && clock(clock_kind::steady) < until {
|
||
|
|
_ = sleep(1000000);
|
||
|
|
}
|
||
|
|
return started;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# What Windows gives of memory that processes share: a mapping of the system's
|
||
|
|
# own file of pages, by a name, and a view of it.
|
||
|
|
extern module pages when os == windows;
|
||
|
|
|
||
|
|
pub def CreateFileMappingW(
|
||
|
|
file: u64, security: *mut u8, protection: u32, size_high: u32, size_low: u32, name: *u16
|
||
|
|
): u64;
|
||
|
|
pub def MapViewOfFile(mapping: u64, access: u32, offset_high: u32, offset_low: u32, size: u64): u64;
|