#! @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 : owning { var _held: mutex>; var _sent: condition; # Notified where a value arrives. var _taken: condition; # Notified where room is made. } # What a channel's mutex guards. struct passage { var items: queue; 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::new(capacity: u64): channel { return channel:: { _held = mutex::>::new(passage:: { items = queue::::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::send(&mut this, value: T): result { 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:: { error = move(value) }; } held.value().items.push(move(value)); this._sent.notify_one(); return result:: { 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::receive(&mut this): maybe { 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::try_receive(&mut this): maybe { 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::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::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::take(&mut this, held: &mut guard>): maybe { 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); }