flix

0.67.2

Concurrent.Channel.Mpmc

enum Mpmc[a: Type, _r: Eff]Source
case Mpmc(MpmcAdmin, MutDeque[a, Static])

An enum that represents a multiple-producer, multiple-consumer (mpmc) channel.

The tuple corresponds to ( admin - The administration info of the channel. elementDeque - a deque of the channel elements but only used as a queue. )

The region type parameter is here to avoid losing type constraints in Monomorpher/Lowering. Mpmc[t, Static] should always be used.

-- Conditions and Signalling -- When a channel needs to notify threads about available space or elements then every relevant thread is signalled. This means the if multiple threads are waiting to retrieve an element and an element arrives they all get woken up and most of them will again wait on a new element.

-- Difference of waitingGetters and waitingPutters -- The reason for the difference between waitingGetters and waitingPutters is the select expression. A thread can only wait for one condition which is a problem when a select expression requires waiting for a set of channels. Therefore the thread makes a condition that represents the select expression and hands that to every channel involved. put is not supported in select so a single condition can be reused for all insertions.

-- Locks -- Every condition is tied to a lock but a lock can exist on its own. The channel has a lock that needs to be held to operate on elementDeque, size, waitingGetters and waitingPutters. The waitingPutters condition is tied to the channel lock while the conditions in waitingPutters comes from locks created by the get function or the select function.

-- Difference between buffered and unbuffered channels --

An unbuffered channel works almost like a buffered channel with a buffer size of 1. The difference is in the behaviour of the thread executing put.

For a buffered channel, put blocks until space is available. Once it is, the value is added to the channel and put returns.

For an unbuffered channel, the thread executing put needs to perform a synchronous rendezvous with the thread executing get.

-- Mpmc / MpmcAdmin split -- The select function must work on heterogeneous channel collections, which cannot happen without existential types. This means that the non-polymorphic aspects relevant to select is split into its own structure on which select can operate.