Channel multiplexer 2026-10-31
A process that tags values accepted from several input ports with stream identities and forwards them through a shared message-passing channel.
Channel multiplexing 2026-10-31
Sharing one message-passing channel among multiple logical streams by attaching a stream identity to each transferred value. Independent cancellation requires a protocol that conveys which stream is ending; bounded per-stream flow control can prevent head-of-line blocking from a single unready consumer.
Communicating Scala Objects 2026-10-31
A Scala library for constructing communicating concurrent processes with typed message-passing channels, parallel composition and guarded alternation.[ref]
Control channel 2026-10-31
A message-passing channel carrying protocol instructions, such as cancellation or permission to send, rather than ordinary application payloads. A reverse control channel can communicate consumer closure back to a remote producer.
Message-passing channel 2026-10-31
A communication object with an output port used to send values and an input port used to receive them. Its contract specifies ordering, capacity, sharing and closure. In synchronous message passing, a send completes through a matching receive; a buffered message-passing channel can retain accepted values until a receiver is ready.
Multiplexed channel 2026-10-31
Store the accepted but undelivered values in an ordinary FIFO queue, owned exclusively by the Communicating Scala Objects process. An input event is enabled precisely when the FIFO queue is not full, and an output event precisely when it is not empty. An output event's expression is evaluated only after that event is selected; its epilogue runs after the output completes. The following uses no buffered communication message-passing channels.
def strictBuffer[T](capacity: Int, in: ?[T], out: ![T]): PROC = proc {
require(capacity > 0)
val q = scala.collection.mutable.Queue[T]()
val poll = 1000000L // nanoseconds
try {
while (out.canOutput && (in.canInput || q.nonEmpty)) {
alt(
(q.size < capacity && in) =?=> { x => q.enqueue(x) }
| (q.nonEmpty && out) =!=> { q.front } ==> { q.dequeue(); () }
| after(poll) ==> { () }
)
}
} catch { case _: io.threadcso.process.Stopped => () }
finally { in.closeIn; out.closeOut }
}Here
canInput and canOutput mean that the port has not become permanently unable to communicate; they are not instantaneous readiness tests. A closed input event is disabled by the Communicating Scala Objects guarded alternation. The timer periodically rechecks closure even when the affected port's data event is disabled, for example when the FIFO queue is empty and the downstream port closes. It neither transfers data nor imposes a deadline on a healthy producer. This avoids silently assuming that closure of a disabled event wakes an alternation. The relevant output-event and closure conventions are documented in [ref].Write and for the input and output sequences. The loop invariant is that is followed by the contents of the FIFO queue. An input appends one value to its rear; an output removes exactly its front. Consequently,The two data events remain offered whenever their respective size conditions hold. Under a fair choice policy, an endlessly ready event is eventually selected. The timer requires the usual assumption that expiry cannot starve a ready communication forever.
When the input closes, no more values are accepted, but all remaining values stay eligible for output. Once the FIFO queue empties, the process closes its output and terminates. If the output closes first, there is no recipient for those remaining values: the process abandons them, closes the input to release the producer, and terminates. Draining after upstream closure requires a consumer that continues receiving. The positive-capacity precondition is deliberate: two separate synchronous message passing events cannot provide a capacity-zero buffer with zero discrepancy after every event; that would require a simultaneous three-party transfer.
Use a tagged union so that an idle indication cannot be confused with an actual value of type
T. Heartbeat messages are transport messages only and never reach the consumer.sealed trait InterPeer[+T]
final case class Data[T](value: T) extends InterPeer[T]
case object Idle extends InterPeer[Nothing]
case object EndPeer extends RuntimeException
case object PeerTimeout extends RuntimeExceptionHere are two small Communicating Scala Objects helpers. A timer expiring after a port has closed denotes orderly end-of-stream, rather than evidence of a silent peer. Closure can instead make the alternation throw
Stopped directly. sendWithin completes exactly one send, or throws; receiveWithin completes exactly one receive, or throws. Time arguments are positive nanosecond intervals.def sendWithin[A](p: ![A], value: A, t: Long): Unit = {
if (!p.canOutput) throw EndPeer
alt(
p =!=> { value }
| after(t) ==> {
if (!p.canOutput) throw EndPeer else throw PeerTimeout
}
)
}
def receiveWithin[A](p: ?[A], t: Long): A = {
if (!p.canInput) throw EndPeer
var result: Option[A] = None
alt(
p =?=> { x => result = Some(x) }
| after(t) ==> {
if (!p.canInput) throw EndPeer else throw PeerTimeout
}
)
result.get
}The sender waits only half the agreed interval before preparing an idle indication, leaving some scheduling allowance before the receiver's deadline. A pending data or idle send has its own deadline, so a failed receiver cannot strand the sender forever. An application with a guaranteed network-delay bound should subtract that bound, plus scheduling allowance, when choosing the heartbeat period.
def iPr[T](timeout: Long, in: ?[T], out: ![InterPeer[T]],
fail: ![Unit]): PROC = proc {
require(timeout >= 2)
var failed = false
try {
while (in.canInput && out.canOutput) {
var packet: InterPeer[T] = Idle
alt(
in =?=> { x => packet = Data(x) }
| after(timeout / 2) ==> { () }
)
// A just-accepted Data must survive a simultaneous upstream close.
if (packet != Idle || in.canInput) sendWithin(out, packet, timeout)
}
} catch {
case PeerTimeout => failed = out.canOutput
case EndPeer => ()
case _: io.threadcso.process.Stopped => ()
} finally { in.closeIn; out.closeOut }
try { if (failed) fail ! (()) }
catch { case _: io.threadcso.process.Stopped => () }
finally { fail.closeOut }
}
def iCon[T](timeout: Long, in: ?[InterPeer[T]], out: ![T],
fail: ![Unit]): PROC = proc {
require(timeout > 0)
var failed = false
try {
while (out.canOutput) {
receiveWithin(in, timeout) match {
case Idle => ()
case Data(x) => sendWithin(out, x, timeout)
}
}
} catch {
case PeerTimeout => failed = out.canOutput
case EndPeer => ()
case _: io.threadcso.process.Stopped => ()
} finally { in.closeIn; out.closeOut }
try { if (failed) fail ! (()) }
catch { case _: io.threadcso.process.Stopped => () }
finally { fail.closeOut }
}Each failure port has a supervising receiver that accepts its single notification; data-port cleanup occurs before that potentially blocking notification. The sender reports an interpeer send taking too long. The receiver reports an excessive interpeer receive delay or an excessive delay delivering a value to the consumer. Producer idleness alone is healthy and causes heartbeat messages, not a failure. A sufficiently slow consumer can ultimately cause both detectors to fire, since its backpressure reaches the sender.
Ignoring heartbeat messages, at most one genuine value is held by each intermediary with a synchronous interpeer message-passing channel; no value overtakes another. Normal upstream closure propagates after the last accepted genuine value is delivered, provided its delivery meets the deadline. The pair is not ready for another input while its sender is blocked sending, so it has the specified prefix and bounded-storage properties without every readiness property of an ideal strict buffer. Its useful guarantee isA timeout establishes suspicion, not proof of a crash: a live but delayed peer or an excessive network delay gives the same observation. There is no promise of bounded detection when the detector itself is never scheduled, nor of exact agreement between the two failure indications. A sender-side deadline measures completion of its own send: a network adapter that merely accepts values into local storage cannot confirm remote receipt. Detecting that remote failure at the sender would require confirmations over a return message-passing channel.
A channel multiplexer adds a stream identity; a channel demultiplexer removes it and selects the corresponding output. Add an explicit per-stream end marker, since closing the shared message-passing channel cannot identify which producer has finished.
End(i) is represented by None; Some(x) represents a genuine value, including a null value if the application permits one.final case class MUX[T](stream: Int, item: Option[T])The following Communicating Scala Objects implementation uses a synchronous one-to-one
mid. As in part (a), a short timer detects closure even when no communication is enabled. Only the channel multiplexer owns the input ports, and only the channel demultiplexer owns the output ports. The channel multiplexer takes at most one input value before its corresponding mid write; it never accepts a second input while that write is pending.def iMux[T](ins: Seq[?[T]], mid: ![MUX[T]]): PROC = proc {
val ended = Array.fill(ins.length)(false)
val poll = 1000000L
try {
while (mid.canOutput && ended.contains(false)) {
for (i <- ins.indices if !ended(i) && !ins(i).canInput) {
mid ! MUX[T](i, None)
ended(i) = true
}
if (ended.contains(false)) {
alt(
(| (for (i <- ins.indices) yield
(!ended(i) && ins(i)) =?=> { x => mid ! MUX(i, Some(x)) }))
| after(poll) ==> { () }
)
}
}
} catch { case _: io.threadcso.process.Stopped => () }
finally { ins.foreach(_.closeIn); mid.closeOut }
}
def iDemux[T](mid: ?[MUX[T]], outs: Seq[![T]]): PROC = proc {
val ended = Array.fill(outs.length)(false)
val poll = 1000000L
try {
while (mid.canInput && ended.contains(false) &&
outs.indices.forall(i => ended(i) || outs(i).canOutput)) {
alt(
mid =?=> { packet =>
val i = packet.stream
require(i >= 0 && i < outs.length && !ended(i))
packet.item match {
case Some(x) => outs(i) ! x
case None => ended(i) = true; outs(i).closeOut
}
}
| after(poll) ==> { () }
)
}
} catch { case _: io.threadcso.process.Stopped => () }
finally { mid.closeIn; outs.foreach(_.closeOut) }
}For each stream, the output sequence remains a prefix of its accepted input sequence. One value can be pending in the channel demultiplexer, and one in the channel multiplexer, possibly from the same stream. Hence its discrepancy is at most two:There is a shared total of two pending values, so these processes do not supply the independent readiness of a parallel collection of capacity-two strict buffers. An unready consumer blocks the channel demultiplexer's ordinary send; the channel multiplexer can accept one further value and then blocks too. This is head-of-line blocking.
The alternation may choose an input unfairly; replacing its selection policy by round-robin selection helps among simultaneously ready producers. Even fair input choice cannot compensate for a consumer that blocks the entire channel demultiplexer. Closed-input scanning does allow a producer's end marker eventually to reach its consumer, but only after the shared transport and any earlier delivery become serviceable. Its own accepted values precede its end marker, so that consumer drains cleanly.
Consumer-driven closure is more restrictive. With no return message-passing channel, the sender cannot promptly learn which producer should stop. This implementation deliberately shuts down the whole multiplexed channel when it detects any unfinished consumer closing, thereby cancelling the other pairs too. If the channel demultiplexer is already blocked on some other consumer, even that detection waits for the blocked send. Thus producer-driven ends can be represented separately but need not be delivered promptly; consumer-driven cancellation is global and need not be detected promptly. Merely adding an end marker does not solve either delay.
Use a return control channel to carry both cancellation and one-slot flow control. The latter is what prevents a silent consumer from obstructing other streams. Keep the preceding
MUX type and addsealed trait CTRL
final case class Grant(stream: Int) extends CTRL
final case class Cancel(stream: Int) extends CTRLHere is a precise event-loop specification of the modified generators.
choose means a fair Communicating Scala Objects guarded alternation over the listed input events, output events and a short closure-poll timer; every selected transition below is finite and contains no further blocking communication. An output transition commits its state change only after its selected send succeeds. The notation is operational pseudocode, with the requested signatures; it does not introduce an assumed nonblocking send primitive.def iMux[T](ins: Seq[?[T]], ctrl: ?[CTRL], mid: ![MUX[T]]): PROC
for each stream i:
credit[i] = true; inputOpen[i] = true; terminal[i] = false
pending[i] = None // Option[MUX[T]]
until every terminal[i]:
poll closures:
if !ins[i].canInput: inputOpen[i] = false
if !terminal[i] && !inputOpen[i] && pending[i].isEmpty:
pending[i] = Some(MUX(i, None)) // end follows earlier data
choose:
ctrl ? Grant(i):
if !terminal[i]: credit[i] = true
ctrl ? Cancel(i):
terminal[i] = true; pending[i] = None; ins[i].closeIn
ins[i] ? x, enabled when inputOpen[i] && !terminal[i]
&& credit[i] && pending[i].isEmpty:
credit[i] = false; pending[i] = Some(MUX(i, Some(x)))
mid ! pending[i].get, enabled when pending[i].nonEmpty:
if pending[i].get.item.isEmpty: terminal[i] = true
pending[i] = None
poll timer: no action
finally: close every ins[i] input; close mid output and ctrl input
def iDemux[T](ctrl: ![CTRL], mid: ?[MUX[T]], outs: Seq[![T]]): PROC
for each stream i:
slot[i] = None; ended[i] = false; cancelled[i] = false
replies = empty FIFO queue of CTRL
until every (cancelled[i] || (ended[i] && slot[i].isEmpty)):
poll closures:
if !cancelled[i] && !ended[i] && !outs[i].canOutput:
cancelled[i] = true; slot[i] = None
delete any queued Grant(i); append Cancel(i) to replies
if !cancelled[i] && ended[i] && slot[i].nonEmpty
&& !outs[i].canOutput:
cancelled[i] = true; slot[i] = None
if ended[i] && slot[i].isEmpty: outs[i].closeOut
choose:
mid ? MUX(i, Some(x)):
if !cancelled[i]: assert slot[i].isEmpty; slot[i] = Some(x)
mid ? MUX(i, None):
ended[i] = true
outs[i] ! slot[i].get, enabled when !cancelled[i] && slot[i].nonEmpty:
slot[i] = None
if !ended[i]: append Grant(i) to replies
ctrl ! replies.front, enabled when replies.nonEmpty:
remove replies.front
poll timer: no action
finally: close mid input, every output, and ctrl outputThere is one receive event on
mid, with the two packet forms dispatched in its body, and one send event on mid, offering a fairly chosen pending stream. There is likewise one send event on ctrl. If mid becomes exhausted, the channel demultiplexer marks any remaining streams ended, discards queued replies, and drains or cancels their slots; the sender has then finished and needs no more grants. If the control input closes, the channel multiplexer enters cleanup. If the receiver finds its control output closed, it discards queued replies and disables further control sends, but continues receiving end markers and draining its slots; sender completion must not discard already accepted values. A channel closing during an individual guarded operation is handled locally: a failed outs(i) send cancels that stream and queues Cancel(i) unless it had already ended; exhaustion of a shared port uses the cleanup rules just stated. Ports that closed while their data events were disabled are found by the poll. These rules specify closure handling as well as normal transitions.The credit starts at one and is consumed when an input is accepted. It is returned only after the corresponding output finishes. Thus the one permitted outstanding value for stream is either pending at the sender or stored at the receiver, never both. An end marker needs no credit and follows that stream's last data packet on the ordered shared message-passing channel. It can arrive while that value is still waiting for its consumer; the receiver then delays closing the output until its slot empties. The invariant isAs in a strict buffer, cancellation can discard the last outstanding value. A cancelled receiver ignores a data packet already in flight; a later grant cannot resurrect a cancelled sender. No selected data delivery blocks the event loop, and control replies are queued rather than sent inside a receive handler. In particular, there is no cycle in which the sender blocks sending data while the receiver blocks sending cancellation. Closing the control output at final receiver termination also releases the sender if a last cancellation was still queued.
Under fair event selection and eventual scheduling, an individual cancellation reaches its producer without waiting for any unrelated consumer. Producer closure similarly delivers its own end marker without waiting for another stream to drain. This gives finite flow control equivalent in ordering and termination to capacity-one strict buffers, with finite protocol delays. A physical shared message-passing channel necessarily serializes transfers, so instantaneous readiness and absolute real-time delay bounds are not identical to physically separate buffers.
A directed graph needs an explicit termination argument. After assigning its parent in a rooted tree, a vertex must still receive later traffic: having no new graph neighbours to discover is not proof that other senders have finished. Use a discovery wave together with duplicate-suppressed announcements of activated vertices. Both kinds of message identify their immediate sender, who has already activated.
sealed trait Msg { def from: NodeId }
final case class Discover(from: NodeId) extends Msg
final case class Active(from: NodeId, origin: NodeId) extends MsgLet
adj = neighbours.distinct. The actual communication objects must expose both input and output ports: the paper's Seq[![Msg]] annotation supplies only output capabilities, although the following sentence also requires receiving. Set up one shared-writer, single-reader buffered message-passing channel per vertex, publish its output in toNode, and keep its input in fromNode. Only node i reads fromNode(i). These are the two ports of the same underlying mailbox, not additional graph edges. The question's prohibition on buffered message-passing channels applies to Question 1(a), not to this question.Take , reliable delivery, eventual scheduling, finite
adj, and mailbox capacity . Every node has at most distinct incoming neighbours, including itself if there is a self-loop. The bound below ensures that no send blocks merely because a mailbox's receiver is not currently reading. Do not close these shared mailboxes on local process termination: late, redundant traffic is harmless and bounded. Their creator can release them after the parallel composition has joined. For example, the concrete setup can useval mailbox = Vector.fill(N)(N2NBuf[Msg](B, 0, 1))
val toNode: Seq[![Msg]] = mailbox
val fromNode: Seq[?[Msg]] = mailboxHere the zero writer count makes the shared output independent of individual writer departures; processes do not call its
closeOut during the search.def span: PROC = proc {
val adj = neighbours.distinct
var started = false
val known = scala.collection.mutable.Set[NodeId]()
def announce(origin: NodeId): Unit = {
if (!known(origin)) {
known += origin
for (w <- adj) toNode(w) ! Active(me, origin)
}
}
def activate(sender: NodeId): Unit = {
started = true
parent = if (me == 0) 0 else sender
for (w <- adj if me == 0 || w != parent)
toNode(w) ! Discover(me)
announce(me)
}
if (me == 0) activate(0)
while (known.size < N) {
val message = fromNode(me) ? ()
if (!started) activate(message.from)
message match {
case Discover(_) => ()
case Active(_, origin) => announce(origin)
}
}
}An announcement may be a vertex's first incoming message. It then serves as a discovery message too: its immediate sender is already active, and the receiving vertex adopts that sender as its parent in a rooted tree before forwarding anything. This preserves the required first-message rule. In particular, an inactive vertex never forwards announcements while remaining inactive, which could invalidate the parent-order argument.
For each nonroot vertex, its parent in a rooted tree activated strictly earlier. Repeated parent-following therefore cannot make a graph cycle and eventually reaches the unique initially active vertex, zero. Each parent assignment uses an actual incoming directed edge. Strong connectivity ensures eventual activation of every vertex: if an activated set were proper, a directed path from zero to an inactive vertex would have a first edge leaving that set. Its sender sends a discovery message along that edge; excluding its own parent does not exclude an inactive neighbour.
Each activation sends at most one
Discover on each outgoing directed edge. For each of the origins, each vertex forwards Active at most once on each outgoing directed edge. Therefore a mailbox receives at most messages over the whole run, proving the chosen nonblocking storage bound. Each origin's announcement eventually reaches every vertex, since every first receipt forwards it on all outgoing directed edges. Thus every known set reaches size and every process terminates, even if redundant messages remain unread:No timeout is used to conclude that the distributed work has finished. This uses larger finite mailboxes to make the termination proof simple, rather than assuming that simultaneous synchronous broadcasts cannot deadlock.Run two
dfFringe producers on synchronous one-to-one message-passing channels, and compare one received leaf from each. Catch closure separately on each input: otherwise the first closure could hide an extra value in the other fringe. An extra value or unequal pair is enough to stop immediately.def sameFringe(node1: Array[Node], root1: NodeId,
node2: Array[Node], root2: NodeId): Boolean = {
val a = OneOne[NodeId]
val b = OneOne[NodeId]
var equal = true
def next(p: ?[NodeId]): Option[NodeId] =
try { Some(p ? ()) }
catch { case _: io.threadcso.process.Stopped => None }
val compare = proc {
var done = false
try {
while (!done) {
val x = next(a)
val y = next(b)
(x, y) match {
case (None, None) => done = true
case (Some(u), Some(v)) if u == v => ()
case _ => equal = false; done = true
}
}
} finally { a.closeIn; b.closeIn }
}
(dfFringe(node1, root1, a) || dfFringe(node2, root2, b) || compare)()
equal
}After successful comparisons, the first identities in the two leaf sequences agree. The first differing pair disproves equality of sequences; simultaneous exhaustion proves it; exhaustion on exactly one side disproves it. Therefore,Closing both inputs on every comparator exit releases either producer if it is waiting in an output. The closure handler in
dfFringe then returns normally, and the parallel composition joins before equal is read. For finite trees and eventual scheduling, the comparator's next read either obtains a leaf or observes completion, so there is no deadlock caused by reading the two inputs in a fixed order.Only a constant amount of leaf data is stored. After an early mismatch, each producer may already have traversed toward its next leaf and be blocked offering it; cancellation prevents unbounded speculation or deliberate construction of the remaining fringe. Occasionally a short unequal fringe is already completely traversed when its final distinguishing value is compared. The streaming guarantee is that neither complete fringe needs to be materialized in advance, and work stops as soon as inequality is established.