Files
std/containers/arc.sakura
T
2026-11-10 11:26:34 -06:00

170 lines
5.5 KiB
Plaintext

#! @file arc.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `arc`, one value on the heap that every copy owns, counted
#! atomically, for values threads share, as Rust's `Arc`; and `arc_weak`, which
#! refers to it without keeping it.
#!
#! Copies count with a relaxed add, as nothing is read through the count, and
#! close with a subtraction that releases, so what each owner did to the value
#! comes before the last owner closes it, which acquires before it does, as
#! Rust's `Arc` and C++'s `shared_ptr` do.
module std::containers;
# What an `arc` points at: the counts, then the value. The weak count holds one
# for all the owners together, as `rc_cell` does.
struct arc_cell<T> {
var owners: atomic<u64>;
var weak: atomic<u64>;
var value: T;
}
#! One value on the heap, owned by every copy, which `copy` makes, on any
#! thread, and closed where the last copy closes.
#!
#! Threads reading what they share need nothing more, but what they change
#! through `get` is guarded as any memory threads share is: an `arc<mutex<T>>`
#! or an `arc<atomic<T>>` is the usual way. An `arc` is one word, the counted
#! value's address, which is never null, so `maybe<arc<T>>` is one word too.
#! @tparam T What it holds.
pub struct arc<T> : owning, copyable, moveable {
var _at: &mut arc_cell<T>;
}
#! A reference to a value an `arc` owns that does not keep it: `lock` gives an
#! owner while any other is left.
#! @tparam T What it refers to.
pub struct arc_weak<T> : owning, copyable, moveable {
var _at: *mut arc_cell<T>;
}
#! Moves a value to the heap, with one owner.
#! @param value The value.
#! @returns Its first owner.
pub def arc<T>::new(value: T): arc<T> {
val at := allocate_for::<arc_cell<T>>(size_of::<arc_cell<T>>()) as *mut arc_cell<T>;
at[0] = arc_cell::<T> {
owners = atomic::<u64>::new(1),
weak = atomic::<u64>::new(1),
value = move(&mut value)
};
return arc::<T> { _at = through(at) };
}
#! Lends the value an `arc` owns.
#! @param this The owner.
#! @returns The value, good while any owner is left.
pub def arc<T>::get(&this): &T {
return &this._at.value;
}
#! Lends the value an `arc` owns, to be changed, which threads guard as they do
#! any memory they share.
#! @param this The owner.
#! @returns The value, good while any owner is left.
pub def arc<T>::get(&mut this): &mut T {
return &mut this._at.value;
}
#! How many owners share the value, which other threads may change at once.
#! @param this An owner.
#! @returns How many, this one among them.
pub def arc<T>::count(&this): u64 {
return this._at.owners.load(order::relaxed);
}
#! A reference to the value that does not keep it.
#! @param this An owner.
#! @returns The reference.
pub def arc<T>::weak(&this): arc_weak<T> {
_ = this._at.weak.fetch_add(1, order::relaxed);
return arc_weak::<T> { _at = this._at as *mut arc_cell<T> };
}
#! Makes another owner of the same value.
#! @param other An owner.
#! @returns The new owner.
pub def arc<T>::copy_of(other: &arc<T>): arc<T> {
_ = other._at.owners.fetch_add(1, order::relaxed);
return arc::<T> { _at = other._at };
}
#! Moves an owner, leaving the one moved from empty.
#! @param other The owner to move from.
#! @returns The owner it was.
pub def arc<T>::move_of(other: &mut arc<T>): arc<T> {
val made := arc::<T> { _at = other._at };
(other as *mut u64)[0] = 0;
return made;
}
#! Closes an owner: the value too where it was the last, and the memory where no
#! weak reference is left either.
#! @param this The owner, which is empty after.
pub def arc<T>::close(&mut this): void {
val at := this._at as *mut arc_cell<T>;
return if at == null;
(this as *mut u64)[0] = 0;
return if at[0].owners.fetch_sub(1, order::release) != 1;
fence(order::acquire);
close(&mut at[0].value);
arc_cell_let_go::<T>(at);
}
#! An owner of the value a weak reference refers to, where any is left.
#! @param this The weak reference.
#! @returns The new owner, or nothing where the last closed.
pub def arc_weak<T>::lock(&this): maybe<arc<T>> {
return nothing {} if this._at == null;
var count := this._at[0].owners.load(order::relaxed);
while count != 0 {
if this._at[0].owners.compare_exchange_weak(count, count + 1, order::acquire, order::relaxed) is error(found) {
count = found;
} else {
return arc::<T> { _at = through(this._at) };
}
}
return nothing {};
}
#! Makes another weak reference to the same value.
#! @param other A weak reference.
#! @returns The new one.
pub def arc_weak<T>::copy_of(other: &arc_weak<T>): arc_weak<T> {
if other._at != null {
_ = other._at[0].weak.fetch_add(1, order::relaxed);
}
return arc_weak::<T> { _at = other._at };
}
#! Moves a weak reference, leaving the one moved from empty.
#! @param other The weak reference to move from.
#! @returns The weak reference it was.
pub def arc_weak<T>::move_of(other: &mut arc_weak<T>): arc_weak<T> {
val made := arc_weak::<T> { _at = other._at };
other._at = null;
return made;
}
#! Closes a weak reference, giving back the memory where it was the last
#! reference of any kind.
#! @param this The weak reference, which is empty after.
pub def arc_weak<T>::close(&mut this): void {
val at := this._at;
return if at == null;
this._at = null;
arc_cell_let_go::<T>(at);
}
# Lets go of the weak count's one, giving back the memory where it was the last.
def arc_cell_let_go<T>(at: *mut arc_cell<T>): void {
if at[0].weak.fetch_sub(1, order::release) == 1 {
fence(order::acquire);
raw_release(at as *mut u8);
}
}