Documentation

Std.Sync.Channel

This module contains the implementation of Std.Channel. Std.Channel is a multi-producer multi-consumer FIFO channel that offers both bounded and unbounded buffering as well as synchronous and asynchronous APIs.

Additionally Std.CloseableChannel is provided in case closing the channel is of interest. The two are distinct as the non closable Std.Channel can never throw errors which makes for cleaner code.

Errors that may be thrown while interacting with the channel API.

  • closed : Error

    Tried to send to a closed channel.

  • alreadyClosed : Error

    Tried to close an already closed channel.

Instances For

    A multi-producer multi-consumer FIFO channel that offers both bounded and unbounded buffering and an asynchronous API, to switch into synchronous mode use CloseableChannel.sync.

    Additionally Std.CloseableChannel can be closed if necessary, unlike Std.Channel. This introduces a need for error handling in some cases, thus it is usually easier to use Std.Channel if applicable.

    Equations
      Instances For

        A multi-producer multi-consumer FIFO channel that offers both bounded and unbounded buffering and a synchronous API. This type acts as a convenient layer to use a channel in a blocking fashion and is not actually different from the original channel.

        Additionally Std.CloseableChannel.Sync can be closed if necessary, unlike Std.Channel.Sync. This introduces the need to handle errors in some cases, thus it is usually easier to use Std.Channel if applicable.

        Equations
          Instances For

            Create a new channel, if:

            • capacity is none it will be unbounded (the default)
            • capacity is some 0 it will always force a rendezvous between sender and receiver
            • capacity is some n with n > 0 it will use a buffer of size n and begin blocking once it is filled
            Equations
              Instances For

                Try to send a value to the channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

                Equations
                  Instances For

                    Send a value through the channel, returning a task that will resolve once the transmission could be completed. Note that the task may resolve to Except.error if the channel was closed before it could be completed.

                    Equations
                      Instances For

                        Closes the channel, returns Except.ok when called the first time, otherwise Except.error. When a channel is closed:

                        • no new values can be sent successfully anymore
                        • all blocked consumers are resolved to none (as no new messages can be sent they will never resolve)
                        • if there are already values waiting to be received they can still be received by subsequent recv calls
                        Equations
                          Instances For

                            Return true if the channel is closed.

                            Equations
                              Instances For

                                Try to receive a value from the channel, if this can be completed right away without blocking return some value, otherwise return none.

                                Equations
                                  Instances For

                                    Receive a value from the channel, returning a task that will resolve once the transmission could be completed. Note that the task may resolve to none if the channel was closed before it could be completed.

                                    Equations
                                      Instances For

                                        Creates a Selector that resolves once ch has data available and provides that that data. In particular if ch is closed while waiting on this Selector and no data is available already this will resolve to none.

                                        Equations
                                          Instances For

                                            ch.forAsync f calls f for every message received on ch.

                                            Note that if this function is called twice, each message will only arrive at exactly one invocation.

                                            @[inline]

                                            This function is a no-op and just a convenient way to expose the synchronous API of the channel.

                                            Equations
                                              Instances For
                                                @[inline]
                                                def Std.CloseableChannel.Sync.new {α : Type} (capacity : Option Nat := none) :

                                                Create a new channel, if:

                                                • capacity is none it will be unbounded (the default)
                                                • capacity is some 0 it will always force a rendezvous between sender and receiver
                                                • capacity is some n with n > 0 it will use a buffer of size n and begin blocking once it is filled
                                                Equations
                                                  Instances For
                                                    @[inline]
                                                    def Std.CloseableChannel.Sync.trySend {α : Type} (ch : Sync α) (v : α) :

                                                    Try to send a value to the channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

                                                    Equations
                                                      Instances For
                                                        def Std.CloseableChannel.Sync.send {α : Type} (ch : Sync α) (v : α) :

                                                        Send a value through the channel, blocking until the transmission could be completed. Note that this function may throw an error when trying to send to an already closed channel.

                                                        Equations
                                                          Instances For
                                                            @[inline]

                                                            Closes the channel, returns Except.ok when called the first time, otherwise Except.error. When a channel is closed:

                                                            • no new values can be sent successfully anymore
                                                            • all blocked consumers are resolved to none (as no new messages can be sent they will never resolve)
                                                            • if there are already values waiting to be received they can still be received by subsequent recv calls
                                                            Equations
                                                              Instances For
                                                                @[inline]

                                                                Return true if the channel is closed.

                                                                Equations
                                                                  Instances For
                                                                    @[inline]

                                                                    Try to receive a value from the channel, if this can be completed right away without blocking return some value, otherwise return none.

                                                                    Equations
                                                                      Instances For

                                                                        Receive a value from the channel, blocking until the transmission could be completed. Note that the return value may be none if the channel was closed before it could be completed.

                                                                        Equations
                                                                          Instances For

                                                                            for msg in ch.sync do ... receives all messages in the channel until it is closed.

                                                                            Equations
                                                                              structure Std.Channel (α : Type) :

                                                                              A multi-producer multi-consumer FIFO channel that offers both bounded and unbounded buffering and an asynchronous API, to switch into synchronous mode use Channel.sync.

                                                                              If a channel needs to be closed to indicate some sort of completion event use Std.CloseableChannel instead. Note that Std.CloseableChannel introduces a need for error handling in some cases, thus Std.Channel is usually easier to use if applicable.

                                                                              Instances For

                                                                                A multi-producer multi-consumer FIFO channel that offers both bounded and unbounded buffering and a synchronous API. This type acts as a convenient layer to use a channel in a blocking fashion and is not actually different from the original channel.

                                                                                If a channel needs to be closed to indicate some sort of completion event use Std.CloseableChannel.Sync instead. Note that Std.CloseableChannel.Sync introduces a need for error handling in some cases, thus Std.Channel.Sync is usually easier to use if applicable.

                                                                                Equations
                                                                                  Instances For
                                                                                    @[inline]
                                                                                    def Std.Channel.new {α : Type} (capacity : Option Nat := none) :

                                                                                    Create a new channel, if:

                                                                                    • capacity is none it will be unbounded (the default)
                                                                                    • capacity is some 0 it will always force a rendezvous between sender and receiver
                                                                                    • capacity is some n with n > 0 it will use a buffer of size n and begin blocking once it is filled
                                                                                    Equations
                                                                                      Instances For
                                                                                        @[inline]
                                                                                        def Std.Channel.trySend {α : Type} (ch : Channel α) (v : α) :

                                                                                        Try to send a value to the channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

                                                                                        Equations
                                                                                          Instances For
                                                                                            def Std.Channel.send {α : Type} (ch : Channel α) (v : α) :

                                                                                            Send a value through the channel, returning a task that will resolve once the transmission could be completed.

                                                                                            Equations
                                                                                              Instances For
                                                                                                @[inline]
                                                                                                def Std.Channel.tryRecv {α : Type} (ch : Channel α) :

                                                                                                Try to receive a value from the channel, if this can be completed right away without blocking return some value, otherwise return none.

                                                                                                Equations
                                                                                                  Instances For
                                                                                                    def Std.Channel.recv {α : Type} [Inhabited α] (ch : Channel α) :

                                                                                                    Receive a value from the channel, returning a task that will resolve once the transmission could be completed. Note that the task may resolve to none if the channel was closed before it could be completed.

                                                                                                    Equations
                                                                                                      Instances For

                                                                                                        Creates a Selector that resolves once ch has data available and provides that that data.

                                                                                                        Equations
                                                                                                          Instances For
                                                                                                            partial def Std.Channel.forAsync {α : Type} [Inhabited α] (f : αBaseIO Unit) (ch : Channel α) (prio : Task.Priority := Task.Priority.default) :

                                                                                                            ch.forAsync f calls f for every message received on ch.

                                                                                                            Note that if this function is called twice, each message will only arrive at exactly one invocation.

                                                                                                            @[inline]
                                                                                                            def Std.Channel.sync {α : Type} (ch : Channel α) :
                                                                                                            Sync α

                                                                                                            This function is a no-op and just a convenient way to expose the synchronous API of the channel.

                                                                                                            Equations
                                                                                                              Instances For
                                                                                                                @[inline]
                                                                                                                def Std.Channel.Sync.new {α : Type} (capacity : Option Nat := none) :

                                                                                                                Create a new channel, if:

                                                                                                                • capacity is none it will be unbounded (the default)
                                                                                                                • capacity is some 0 it will always force a rendezvous between sender and receiver
                                                                                                                • capacity is some n with n > 0 it will use a buffer of size n and begin blocking once it is filled
                                                                                                                Equations
                                                                                                                  Instances For
                                                                                                                    @[inline]
                                                                                                                    def Std.Channel.Sync.trySend {α : Type} (ch : Sync α) (v : α) :

                                                                                                                    Try to send a value to the channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

                                                                                                                    Equations
                                                                                                                      Instances For
                                                                                                                        def Std.Channel.Sync.send {α : Type} (ch : Sync α) (v : α) :

                                                                                                                        Send a value through the channel, blocking until the transmission could be completed.

                                                                                                                        Equations
                                                                                                                          Instances For
                                                                                                                            @[inline]
                                                                                                                            def Std.Channel.Sync.tryRecv {α : Type} (ch : Sync α) :

                                                                                                                            Try to receive a value from the channel, if this can be completed right away without blocking return some value, otherwise return none.

                                                                                                                            Equations
                                                                                                                              Instances For
                                                                                                                                def Std.Channel.Sync.recv {α : Type} [Inhabited α] (ch : Sync α) :

                                                                                                                                Receive a value from the channel, blocking until the transmission could be completed.

                                                                                                                                Equations
                                                                                                                                  Instances For

                                                                                                                                    for msg in ch.sync do ... receives all messages in the channel until it is closed.

                                                                                                                                    Equations