Files
std/thread/channel.sakura
T
2026-11-14 15:32:55 -06:00

113 lines
3.7 KiB
Plaintext

#! @file channel.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains channels, which hand values from the threads sending them to those
#! receiving them, in the order they were sent.
import system;
import std::containers;
module std::thread;
#! A queue that hands values from the threads sending them to those receiving
#! them: each value sent is received once, by one receiver, in the order one
#! sender sent them. A bound makes senders wait while it holds that many, and a
#! receiver waits while it holds none. Closing it wakes every waiter: senders get
#! back what they would send, receivers get nothing, and what it still held is
#! closed.
#! @tparam T What it hands over, which it owns until it is received.
pub struct channel<T> : owning {
var _held: mutex<passage<T>>;
var _sent: condition; # Notified where a value arrives.
var _taken: condition; # Notified where room is made.
}
# What a channel's mutex guards.
struct passage<T> {
var items: queue<T>;
var bound: u64;
var closed: bool;
}
#! Makes a channel.
#! @param capacity How many values it holds before senders wait, or zero for
#! no bound.
#! @returns The channel.
pub def channel<T>::new(capacity: u64): channel<T> {
return channel::<T> {
_held = mutex::<passage<T>>::new(passage::<T> { items = queue::<T>::new(), bound = capacity, closed = false }),
_sent = condition::new(),
_taken = condition::new()
};
}
#! Sends a value, waiting while the channel holds as many as its bound.
#! @param this The channel.
#! @param value The value, which a receiver owns once it receives it.
#! @returns Nothing, or the value back where the channel is closed.
pub def channel<T>::send(&mut this, value: T): result<void, T> {
var held := this._held.lock();
while !held.value().closed && held.value().bound != 0 && held.value().items.length >= held.value().bound {
this._taken.wait(&mut held);
}
if held.value().closed {
return result::<void, T> { error = move(value) };
}
held.value().items.push(move(value));
this._sent.notify_one();
return result::<void, T> { value = void {} };
}
#! Receives the value sent first of those the channel holds, waiting while it
#! holds none.
#! @param this The channel.
#! @returns The value, or nothing once the channel is closed.
pub def channel<T>::receive(&mut this): maybe<T> {
var held := this._held.lock();
while held.value().items.length == 0 && !held.value().closed {
this._sent.wait(&mut held);
}
return this.take(&mut held);
}
#! Receives the value sent first of those the channel holds, without waiting.
#! @param this The channel.
#! @returns The value, or nothing where it holds none.
pub def channel<T>::try_receive(&mut this): maybe<T> {
var held := this._held.lock();
return this.take(&mut held);
}
#! How many values a channel holds.
#! @param this The channel.
#! @returns How many.
pub def channel<T>::length(&mut this): u64 {
var held := this._held.lock();
return held.value().items.length;
}
#! Closes a channel: every thread waiting on it wakes, senders get back what
#! they send from then on, receivers get nothing, and what it held is closed.
#! @param this The channel.
pub def channel<T>::close(&mut this): void {
var held := this._held.lock();
held.value().closed = true;
held.value().items.close();
this._sent.notify_all();
this._taken.notify_all();
}
# Takes the value at the front of the channel, where it holds one.
def channel<T>::take(&mut this, held: &mut guard<passage<T>>): maybe<T> {
val there := &mut held.value();
if there.items.length == 0 {
return nothing {};
}
var front: T = move(there.items.peek());
there.items.pop();
this._taken.notify_one();
return move(&mut front);
}