#! @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 { var owners: atomic; var weak: atomic; 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>` #! or an `arc>` is the usual way. An `arc` is one word, the counted #! value's address, which is never null, so `maybe>` is one word too. #! @tparam T What it holds. pub struct arc : owning, copyable, moveable { var _at: &mut arc_cell; } #! 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 : owning, copyable, moveable { var _at: *mut arc_cell; } #! Moves a value to the heap, with one owner. #! @param value The value. #! @returns Its first owner. pub def arc::new(value: T): arc { val at := allocate_for::>(size_of::>()) as *mut arc_cell; at[0] = arc_cell:: { owners = atomic::::new(1), weak = atomic::::new(1), value = move(&mut value) }; return arc:: { _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::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::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::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::weak(&this): arc_weak { _ = this._at.weak.fetch_add(1, order::relaxed); return arc_weak:: { _at = this._at as *mut arc_cell }; } #! Makes another owner of the same value. #! @param other An owner. #! @returns The new owner. pub def arc::copy_of(other: &arc): arc { _ = other._at.owners.fetch_add(1, order::relaxed); return arc:: { _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::move_of(other: &mut arc): arc { val made := arc:: { _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::close(&mut this): void { val at := this._at as *mut arc_cell; 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::(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::lock(&this): maybe> { 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:: { _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::copy_of(other: &arc_weak): arc_weak { if other._at != null { _ = other._at[0].weak.fetch_add(1, order::relaxed); } return arc_weak:: { _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::move_of(other: &mut arc_weak): arc_weak { val made := arc_weak:: { _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::close(&mut this): void { val at := this._at; return if at == null; this._at = null; arc_cell_let_go::(at); } # Lets go of the weak count's one, giving back the memory where it was the last. def arc_cell_let_go(at: *mut arc_cell): void { if at[0].weak.fetch_sub(1, order::release) == 1 { fence(order::acquire); raw_release(at as *mut u8); } }