package picos_std
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=862d61383e2df93a876bedcffb1fd1ddc0f96c50b0e9c07943a2aee1f0e182be
sha512=87805379017ef4a7f2c11b954625a3757a0f1431bb9ba59132202de278b3e41adbe0cdc20e3ab23b7c9a8c5a15faeb7ec79348e7d80f2b14274b00df0893b8c0
doc/picos_std.event/Picos_std_event/Event/index.html
Module Picos_std_event.Event
Source
First-class synchronous communication abstraction.
Events describe a thing that might happen in the future, or a concurrent offer or request that might be accepted or succeed, but is cancelable if some other event happens first.
See the Picos_io_select
library for an example.
ℹ️ This module intentionally mimics the Event
module provided by the OCaml POSIX threads library.
An event returning a value of type 'a
.
always value
returns an event that can always be committed to resulting in the given value
.
Composing events
choose events
return an event that offers all of the given events and then commits to at most one of them.
wrap event fn
returns an event that acts as the given event
and then applies the given function to the value in case the event is committed to.
map fn event
is equivalent to wrap event fn
.
guard thunk
returns an event that, when synchronized, calls the thunk
, and then behaves like the resulting event.
⚠️ Raising an exception from a guard thunk
will result in raising that exception out of the sync
. This may result in dropping the result of an event that committed just after the exception was raised. This means that you should treat an unexpected exception raised from sync
as a fatal error.
Consuming events
sync event
synchronizes on the given event.
Synchronizing on an event executes in three phases:
- In the first phase offers or requests are made to communicate.
- One of the offers or requests is committed to and all the other offers and requests are canceled.
- A final result is computed from the value produced by the event.
⚠️ sync event
does not wait for the canceled concurrent requests to terminate. This means that you should arrange for guaranteed cleanup through other means such as the use of structured concurrency.
select events
is equivalent to sync (choose events)
.
Primitive events
ℹ️ The Computation
concept of Picos
can be seen as a basic single-shot atomic event. This module builds on that concept to provide a composable API to concurrent services exposed through computations.
Represents a function that requests a concurrent service to update a computation.
ℹ️ The computation passed to a request may be completed by some other event at any point. All primitive requests should be implemented carefully to take that into account. If the computation is completed by some other event, then the request should be considered as canceled, take no effect, and not leak any resources.
⚠️ Raising an exception from a request
function will result in raising that exception out of the sync
. This may result in dropping the result of an event that committed just after the exception was raised. This means that you should treat an unexpected exception raised from sync
as a fatal error. In addition, you should arrange for concurrent services to report unexpected errors independently of the computation being passed to the service.
from_request { request }
creates an event from the request function.
from_computation source
creates an event that can be committed to once the given source
computation has completed.
ℹ️ Committing to some other event does not cancel the source
computation.