Modules

Atomics

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Atomics";

Lock-free reads and updates of a shared integer, pointer or bool.

Targets must be 1, 2, 4 or 8 bytes and naturally aligned. compare_and_swap is built into the language.

Example↗
counter: s64;
before := atomic_add(*counter, 5);
assert(before == 0 && counter == 5);

Procedures#

(T: Type) -> bool

True when T is a 1, 2, 4 or 8 byte integer, bool, float, pointer, enum or procedure, the types the atomic procedures accept.

(dest: *$T, to_add: $I) -> T

Adds to_add to *dest and returns the value *dest held before.

hits: s32 = 10;
before := atomic_add(*hits, 3);
assert(before == 10 && hits == 13);
↗
(dest: *$T, value: T)

Sets *dest to *dest & value.

flags: u32 = 0b1100;
atomic_and(*flags, 0b1010);
assert(flags == 0b1000);
↗
(dest: *$T, value: T)

Sets *dest to *dest | value.

flags: u32 = 0b0100;
atomic_or(*flags, 0b0001);
assert(flags == 0b0101);
↗
(dest: *$T, value: $I)

Sets *dest to *dest ^ value.

flags: u32 = 0b0110;
atomic_xor(*flags, 0b0011);
assert(flags == 0b0101);
↗

Constants#

:: atomic_exchange

Stores value in dest and returns what was there before.

x := 1;
old := atomic_swap(*x, 2);
assert(old == 1 && x == 2);
↗
:: atomic_load

Returns the value at pointer.

x := 7;
assert(atomic_read(*x) == 7);
↗
:: atomic_store

Stores value at pointer.

x := 0;
atomic_write(*x, 9);
assert(x == 9);
↗