Library
Module
Module type
Parameter
Class
Class type
Fuseau core.
The core library contains the definition of the main scheduler, fibers, switches, and other foundations for cooperative structured concurrency.
module Fiber_handle : sig ... end
The unique name of a fiber
module Event : sig ... end
Atomic events.
val select : 'ret branch list -> 'ret
See Event.select
.
module Chan : sig ... end
Basic channels
module Exn_bt : sig ... end
Exception with backtrace
module Time : sig ... end
Time measurement
module Fiber : sig ... end
Fibers.
module FLS : sig ... end
Fiber-local storage.
Exception raised when trying to perform operations on the scheduler after it's been disposed of
module Scheduler : sig ... end
Scheduler that runs fibers.
module Event_loop : sig ... end
Abstraction over an event loop, from the pov of the scheduler.
module Resource_pool : sig ... end
Resource pool.
module Buf_pool : sig ... end
A pool of buffers to reuse.
module Cancel_handle : sig ... end
Cancelation handle.
module Iostream : sig ... end
IO streams.
val await : 'a Fiber.t -> 'a
Wait for the fiber to terminate, and return the result. If the fiber failed, this re-raises the exception.
This must be called from inside another fiber, which will be suspended if needed.
val try_await : 'a Fiber.t -> 'a Exn_bt.result
Like await
but catches exceptions.
Cancel the current fiber after delay
seconds, unless the fiber terminates first. The cancellation will use the Timeout
exception.
val ev_timeout : float -> 'a Event.t
ev_timeout duration
is an event that resolves after duration
seconds with a Error Timeout
error
val ev_deadline : float -> 'a Event.t
ev_deadline time
is an event that resolves at monotonic time t
with a Error Timeout
error
val with_cancel_callback : (Exn_bt.t -> unit) -> (unit -> 'a) -> 'a
let@ () = with_cancel_callback cb in <e>
evaluates e
in a scope in which, if the current fiber is cancelled, cb()
is called. If e
returns without the fiber being cancelled, this callback is removed.
val spawn :
?name:string ->
?propagate_cancel_to_parent:bool ->
(unit -> 'a) ->
'a Fiber.t
Must be run from inside the scheduler's thread. Spawn a new computation. This fiber has an implicit parent, which is normally the currently running fiber (the one calling spawn
). If the parent fails or is cancelled, the resulting fiber will also be cancelled (parent to child).
val spawn_from_anywhere :
?name:string ->
Scheduler.t ->
(unit -> 'a) ->
'a Fiber.t
Spawn a task from anywhere, possibly from another thread. The task will run in a subsequent call to run_iteration
in the scheduler's thread. Thread-safe, more costly than spawn
. Runs under the root switch.
val spawn_as_child_of :
?name:string ->
?propagate_cancel_to_parent:bool ->
Scheduler.t ->
_ Fiber.t ->
(unit -> 'a) ->
'a Fiber.t
Spawn a fiber in the given parent fiber's scope. See spawn
for more details on the arguments
Must be run from inside a Scheduler.t
's thread. Schedules a microtask that will run in this tick. Be careful not to create infinite sequences of micro tasks that starve the IO loop!
These microtasks do not handle effects and should try their best to not raise exceptions. Only use them for very short amount of work.
Not thread-safe.
yield ()
returns control to the scheduler and checks for cancellation. This must be called from a fiber.
val get_scheduler : unit -> Scheduler.t
This returns the scheduler on which the caller runs. It must be called from inside a fiber.
This is the loop that runs both fibers, and the IO event loop, in an interspersed way.
val main : loop:Event_loop.t -> (unit -> 'a) -> 'a
main f
runs f()
in an event loop. The value is returned when the loop has nothing else to do, even if the particular computation was finished earlier.