/* * Copyright 2022 Magnus Madsen * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */////// Functions for message passing over channels.///pubmod Channel {use Time.TimeUnitimport java.lang.{Thread => JThread}////// Returns a new buffered channel with capacity for `n` elements.///pubdefbuffered(n: Int32): (Sender[t], Receiver[t]) \ Chan =letm = if (n<0) 0elsen; %%CHANNEL_NEW%%(m)////// Returns a new unbuffered channel (i.e. a channel with zero capacity).///pubdefunbuffered(): (Sender[t], Receiver[t]) \ Chan = buffered(0)////// Receives a message from the given channel `r`.////// Blocks until a message is dequeued.///pubdefrecv(receiver: Receiver[t]): t \ { Chan, NonDet } = %%CHANNEL_GET%%(receiver)////// Sends the message `m` on the given channel `s`.////// Blocks until the message is enqueued.///pubdefsend(m: t, sender: Sender[t]): Unit \ Chan = %%CHANNEL_PUT%%(sender, m)////// Returns a channel that receives the `Unit` message after `duration` in the given time `unit`.///pubdeftimeout(duration: Int32, unit: TimeUnit): Receiver[Unit] \ { Chan, IO } =letmillis = Int32.toInt64(duration*TimeUnit.toMilliseconds(unit));let (tx, rx) = Channel.buffered(1);spawn { JThread.sleep(millis); send((), tx) } @ Static;rx}