std.channel.Channel
Atomicclass pub builtin Channel[T]
A multi-producer, multi-consumer FIFO queue.
Channels allow for multiple producers and consumers, uses FIFO ordering, and are bounded. When sending a message to a channel that's full, the sending process is blocked until space becomes available.
Channels use atomic reference counting and are dropped (along with any pending
messages) when the last reference to the channel is dropped. Channels are
treated as value types and are sendable to other processes without the need
for the recover
expression.
Static methods
new
Show source codeHide source code
fn pub static new(size: Int) -> Channel[uni T] {
Channel(inko_channel_new(size))
}
fn pub static new(size: Int) -> Channel[uni T]
Returns a new channel that can store the given number of messages.
If you specify a value less than 1, the size is set to 1.
Instance methods
clone
Show source codeHide source code
fn pub clone -> Channel[T] {
self
}
fn pub clone -> Channel[T]
Creates a clone of self
.
receive
Show source codeHide source code
fn pub receive -> uni T {
inko_channel_receive(_INKO.process, @state) as uni T
}
fn pub receive -> uni T
Receives a message from the channel.
This method blocks the current process until a message is delivered.
Examples
let chan = Channel.new(size: 1)
chan.send(1)
chan.receive # => 1
receive_until
Show source codeHide source code
fn pub receive_until(deadline: ref Instant) -> Option[uni T] {
match
inko_channel_receive_until(
_INKO.state,
_INKO.process,
@state,
deadline.to_int,
)
{
case { @tag = 0, @value = v } -> Option.Some(v as uni T)
case _ -> Option.None
}
}
fn pub receive_until(deadline: ref Instant) -> Option[uni T]
Receives a message, returning a None
if no message is received when the
deadline is met.
import std.time (Instant)
import std.time (Duration)
let duration = Duration.from_secs(1)
let chan = Channel.new(size: 1)
chan.receive_until(deadline: Instant.new + duration) # => Option.None
chan.send(1)
chan.receive_until(deadline: Instant.new + duration) # => Option.Some(1)
send
Show source codeHide source code
fn pub send(value: uni T) {
inko_channel_send(_INKO.state, _INKO.process, @state, value as UInt64)
}
fn pub send(value: uni T)
Sends a message to the channel.
If the channel is full, the current process is blocked until space is available in the channel.
Examples
let chan = Channel.new(size: 4)
chan.send(1)
chan.send(2)
try_receive
Show source codeHide source code
fn pub try_receive -> Option[uni T] {
match inko_channel_try_receive(_INKO.process, @state) {
case { @tag = 0, @value = v } -> Option.Some(v as uni T)
case _ -> Option.None
}
}
fn pub try_receive -> Option[uni T]
Receives a message from the channel without blocking the sender.
If a message is availabe, it's returned as a Some
, otherwise a None
is
returned.
Examples
let chan = Channel.new(size: 1)
chan.try_receive # => Option.None
chan.send(1)
chan.try_receive # => Option.Some(1)
Implemented traits
Clone
impl Clone[Channel[T]] for Channel
Drop
impl Drop for Channel