Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
#! @file atomics.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the atomics the machine works on the program's memory, with
#! `core::atomic`, so that they are atomic where processes share the memory, as
#! the program built as an executable's are. The machine runs one thread, so it
#! works every order as `seq_cst`, which is never weaker than what is asked.
import sakura::compiler;
module sakura::machine;
#! Works an atomic operation on the integer at an address.
#! @param address The address.
#! @param operation The operation, as `atomic_` numbers them.
#! @param imm The access and sign, as an `atomic` instruction holds them.
#! @param a The first operand, of a store, an exchange, or a
#! combining operation, or what a compare-exchange expects.
#! @param b What a compare-exchange writes.
#! @returns What the address held, as a word the machine holds the access as.
pub def atomically(address: u64, operation: u32, imm: u64, a: u64, b: u64): u64 {
val signed := atomic_signed(imm);
return match atomic_access(imm) {
access::i8, access::u8 => signed ? atomic_on::<i8>(address, operation, a, b) : atomic_on::<u8>(address, operation, a, b),
access::i16, access::u16 => signed ? atomic_on::<i16>(address, operation, a, b) : atomic_on::<u16>(address, operation, a, b),
access::i32, access::u32 => signed ? atomic_on::<i32>(address, operation, a, b) : atomic_on::<u32>(address, operation, a, b),
else => signed ? atomic_on::<i64>(address, operation, a, b) : atomic_on::<u64>(address, operation, a, b)
};
}
# Works an atomic operation on the `T` at an address, giving what it held,
# widened to a word by its sign, as the machine holds it.
def atomic_on<T>(address: u64, operation: u32, a: u64, b: u64): u64 where T: integer, lockfree {
val cell := address as *mut atomic<T>;
var got: T = 0;
if operation == atomic_load {
got = cell[0].load();
} else if operation == atomic_store {
cell[0].store(a as T);
} else if operation == atomic_exchange {
got = cell[0].exchange(a as T);
} else if operation == atomic_compare_exchange {
match cell[0].compare_exchange(a as T, b as T) {
value(held) => got = held;
error(held) => got = held;
}
} else if operation == atomic_add {
got = cell[0].fetch_add(a as T);
} else if operation == atomic_sub {
got = cell[0].fetch_sub(a as T);
} else if operation == atomic_and {
got = cell[0].fetch_and(a as T);
} else if operation == atomic_or {
got = cell[0].fetch_or(a as T);
} else if operation == atomic_xor {
got = cell[0].fetch_xor(a as T);
} else if operation == atomic_min {
got = cell[0].fetch_min(a as T);
} else {
got = cell[0].fetch_max(a as T);
}
return (got as i64) as u64;
}