Aye documentation

std.sync

std.sync: sharing data between threads.

Mutex

pub struct Mutex {
    handle: ?[*]u8
    pub fn init() Mutex
    pub fn lock(self: *Self)
    pub fn unlock(self: *Self)
    pub fn deinit(self: *Self)
}

A lock: one thread at a time holds it. Share it by pointer: copies made after the first `lock` are the same lock, a copy made before is another. `deinit` it once, when no thread uses it.

pub fn init() Mutex

A new lock; a constant, so a global `var` can hold it.

pub fn lock(self: *Self)

Waits until no other thread holds the lock, and takes it.

pub fn unlock(self: *Self)

Releases the lock.

pub fn deinit(self: *Self)

Frees the lock, once, when no thread uses it.

ChannelError

pub error ChannelError { Closed }

The error of `Channel.send`: the channel is closed.

Channel

pub fn Channel(comptime T: type) type {
    return struct {
        mutex: Mutex
        notEmpty: Cond
        notFull: Cond
        slots: []mut ?T
        head: usize
        count: usize
        closed: bool
        alloc: Allocator
        pub fn init(alloc: Allocator, capacity: usize) AllocError!Self
        pub fn deinit(self: *mut Self)
        pub fn send(self: *mut Self, value: T) ChannelError!void
        pub fn recv(self: *mut Self) ?T
        pub fn close(self: *mut Self)
    }
}

A queue of at most `capacity` values that threads send to and receive from, in order. Share it as a `*mut Channel(T)`.

pub fn init(alloc: Allocator, capacity: usize) AllocError!Self

An empty channel with room for `capacity` (at least 1) values.

pub fn deinit(self: *mut Self)

Frees the channel, once no thread uses it.

pub fn send(self: *mut Self, value: T) ChannelError!void

Adds `value` at the end, waiting while the channel is full.

pub fn recv(self: *mut Self) ?T

Takes the first value, waiting while the channel is empty; null once it is closed and empty.

pub fn close(self: *mut Self)

No more values can be sent; receivers get the values left, then null.

Atomic

pub fn Atomic(comptime T: type) type where T: Integer {
    return struct {
        value: T
        pub fn init(value: T) Self
        pub fn load(self: *Self) T
        pub fn store(self: *mut Self, value: T)
        pub fn add(self: *mut Self, n: T) T
        pub fn sub(self: *mut Self, n: T) T
        pub fn compareSwap(self: *mut Self, expected: T, new: T) bool
    }
}

An integer that threads read and change without a lock. Every operation is sequentially consistent; `add` and `sub` wrap around.

pub fn init(value: T) Self

An atomic integer holding `value`.

pub fn load(self: *Self) T

The current value.

pub fn store(self: *mut Self, value: T)

Sets the value to `value`.

pub fn add(self: *mut Self, n: T) T

Adds `n` and returns the value before.

pub fn sub(self: *mut Self, n: T) T

Subtracts `n` and returns the value before.

pub fn compareSwap(self: *mut Self, expected: T, new: T) bool

Sets the value to `new` if it is `expected`; says whether it was.