This module contains the implementation of a fair and data-loss free IO multiplexing primitive.
The main entrypoint for users is Selectable.one
and the various functions to produce
Selector
s from other modules.
The core data structure for racing on winning a Selectable.one
if multiple event sources are ready
at the same time. A Task
can try to finish the waiter by calling Waiter.race
.
- promise : IO.Promise (Except IO.Error α)
Instances For
Swap out the IO.Promise
within the Waiter
. Note that the part which determines whether the
Waiter
is finished is not swapped out.
Equations
- w.withPromise p = { finished := Std.Internal.IO.Async.Waiter.finished✝ w, promise := p }
Instances For
Try to atomically finish the Waiter
. If the race for finishing it is won, win
is executed
with the internal IO.Promise
of the Waiter
. This promise must under all circumstances be
resolved by win
. If the race is lost some cleanup work can be done in lose
.
Equations
Instances For
Atomically checks whether the Waiter
has already finished. Note that right after this function
call ends this might have already changed.
Equations
Instances For
An event source that can be multiplexed using Selectable.one
, see the documentation of
Selectable.one
for how the protocol of communicating with a Selector
works.
Attempts to retrieve a piece of data from the event source in a non-blocking fashion, returning
some
if data is available andnone
otherwise.Registers a
Waiter
with the event source. Once data is available, the event source should attempt to callWaiter.race
and resolve theWaiter
's promise if it wins. It is crucial that data is never actually consumed from the event source unlessWaiter.race
wins in order to prevent data loss.A cleanup function that is called once any
Selector
has won theSelectable.one
race.
Instances For
An event source together with a continuation to call on data obtained from that event source,
usually used together in conjunction with Selectable.one
.
- case :: (
- β : Type
The event source.
The continuation that is called on results from the event source.
- )
Instances For
Performs fair and data-loss free multiplexing on the Selectable
s in selectables
.
The protocol for this is as follows:
- The
selectables
are shuffled randomly. - Run
Selector.tryFn
for each element inselectables
. If any succeed, the correspondingSelectable.cont
is executed and its result is returned immediately. - If none succeed, a
Waiter
is registered with eachSelector
usingSelector.registerFn
. Once one of them resolves theWaiter
, allSelector.unregisterFn
functions are called, and theSelectable.cont
of the winningSelector
is executed and returned.
Equations
- One or more equations did not get rendered due to their size.