package picos_std
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=862d61383e2df93a876bedcffb1fd1ddc0f96c50b0e9c07943a2aee1f0e182be
sha512=87805379017ef4a7f2c11b954625a3757a0f1431bb9ba59132202de278b3e41adbe0cdc20e3ab23b7c9a8c5a15faeb7ec79348e7d80f2b14274b00df0893b8c0
doc/picos_std.structured/Picos_std_structured/Control/index.html
Module Picos_std_structured.ControlSource
Basic control operations and exceptions for structured concurrency.
An exception that is used to signal fibers, typically by canceling them, that they should terminate by letting the exception propagate.
ℹ️ Within this library, the Terminate exception does not, by itself, indicate an error. Raising it inside a fiber forked within the structured concurrency constructs of this library simply causes the relevant part of the tree of fibers to be terminated.
⚠️ If Terminate is raised in the main fiber of a Bundle, and no other exceptions are raised within any fiber inside the bundle, the bundle will then, of course, raise the Terminate exception after all of the fibers have been terminated.
An exception that can be used to collect exceptions, typically indicating errors, from multiple fibers.
ℹ️ The Terminate exception is not considered an error within this library and the structuring constructs do not include it in the list of Errors.
raise_if_canceled () checks whether the current fiber has been canceled and if so raises the exception that the fiber was canceled with.
ℹ️ Within this library fibers are canceled using the Terminate exception.
yield () asks the current fiber to be rescheduled.
sleep ~seconds suspends the current fiber for the specified number of seconds.
protect thunk forbids propagation of cancelation for the duration of thunk ().
ℹ️ With the constructs provided by this library it is not possible to prevent a fiber from being canceled, but it is possible for a fiber to forbid the scheduler from propagating cancelation to the fiber.
block () suspends the current fiber until it is canceled at which point the cancelation exception will be raised.
terminate_after ~seconds thunk arranges to terminate the execution of thunk on the current fiber after the specified timeout in seconds.
Using terminate_after one can attempt any blocking operation that supports cancelation with a timeout. For example, one could try to read an Ivar with a timeout
let peek_in ~seconds ivar =
match
Control.terminate_after ~seconds @@ fun () ->
Ivar.read ivar
with
| value -> Some value
| exception Control.Terminate -> Noneor one could try to connect a socket with a timeout
let try_connect_in ~seconds socket sockaddr =
match
Control.terminate_after ~seconds @@ fun () ->
Unix.connect socket sockaddr
with
| () -> true
| exception Control.Terminate -> falseusing the Picos_io.Unix module.
The optional callstack argument specifies the number of callstack entries to capture with the Terminate exception. The default is 0.
As an example, terminate_after could be implemented using Bundle as follows:
let terminate_after ?callstack ~seconds thunk =
Bundle.join_after @@ fun bundle ->
Bundle.terminate_after ?callstack ~seconds bundle;
thunk ()