Channel

Inherits from: Object

A Go-style channel for communicating between concurrent processes.

Channels are the primary mechanism for passing values between forked processes. An unbuffered channel (Channel new) blocks the sender until a receiver is ready, and vice versa. A buffered channel (Channel new: n) allows up to n values to be queued before the sender blocks.

Test
Channel new isClosed >>> false
(Channel new: 5) capacity >>> 5
(Channel new: 3) isEmpty >>> true
(Channel new: 3) size >>> 0
Test
ch := Channel new: 5.
ch trySend: 42.
ch tryReceive >>> 42
Test
ch := Channel new: 1.
ch trySend: 'hello' >>> true
Test
ch := Channel new: 1.
ch close.
ch isClosed >>> true
Example
"Producer-consumer with a buffered channel"
| ch <Channel> |
ch := Channel new: 10.
[1 to: 10 do: [:i | ch send: i]. ch close] fork.
[ch isClosed not] whileTrue: [ch receive]

Class Methods

primitives

class new

Create a new unbuffered channel.

class new:

Create a buffered channel with the given capacity.

class select:

Wait on multiple channel operations. Returns the result of the first ready case.

class select:ifNone:

Non-blocking select. If no case is ready, evaluate block.

Instance Methods

primitives

capacity

Return the buffer capacity of the channel.

isEmpty

Return true if no values are buffered.

onReceive:

Create a select case that receives from this channel and evaluates block with the value.

onSend:do:
primClose
primIsClosed
primReceive
primSend:
primTryReceive
primTrySend:
size

Return the number of buffered values.

uncategorized

close

Close the channel. After closing, sends will fail and receives will drain any remaining buffered values, then return nil.

Test
ch := Channel new: 1.
ch close.
ch isClosed >>> true
isClosed

Return true if the channel has been closed.

Test
Channel new isClosed >>> false
next

Stream-compatible alias for receive. Receives a value from the channel.

Example
| ch <Channel> |
ch := Channel new: 1.
ch nextPut: 42.
ch next
nextPut:

Stream-compatible alias for send:. Sends a value on the channel.

Example
| ch <Channel> |
ch := Channel new: 1.
ch nextPut: 'hello'.
ch next
printString

Return a string representation of the channel.

Test
Channel new printString >>> 'a Channel'
receive

Receive a value from the channel. Blocks until a value is available or the channel is closed.

Example
| ch <Channel> |
ch := Channel new: 1.
ch send: 42.
ch receive
send:

Send a value on the channel. Blocks until a receiver is ready (unbuffered) or until buffer space is available (buffered).

Example
| ch <Channel> |
ch := Channel new: 1.
ch send: 'hello'.
ch receive
tryReceive

Attempt to receive a value without blocking. Returns the value if one was available, or nil if the channel was empty.

Test
ch := Channel new: 1.
ch trySend: 99.
ch tryReceive >>> 99
Test
(Channel new: 1) tryReceive >>> nil
trySend:

Attempt to send a value without blocking. Returns true if the value was sent, false if the channel was full or closed.

Test
ch := Channel new: 2.
ch trySend: 'a' >>> true
Test
ch := Channel new: 1.
ch trySend: 'a'.
ch trySend: 'b' >>> false