package lsp

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include Fiber end

Generals

type 'a t = 'a Fiber.t

Type of fiber. A fiber represent a suspended computation. Note that using the same fiber twice will execute it twice, which is probably not what you want. To share the result of a fiber, use an Ivar.t.

val return : 'a -> 'a t

Create a fiber that has already terminated.

val of_thunk : (unit -> 'a t) -> 'a t

Converts a thunk to a fiber, making sure the thunk runs in the context of the fiber (rather than applied in the current context).

Equivalent to (>>=) (return ()), but more explicit.

val never : 'a t

Fiber that never completes.

module O = Fiber.O
val map : 'a t -> f:('a -> 'b) -> 'b t
val bind : 'a t -> f:('a -> 'b t) -> 'b t

Forking execution

module Future = Fiber.Future
val fork : (unit -> 'a t) -> 'a Future.t t

fork f creates a sub-fiber and return a Future.t to wait its result.

val nfork : (unit -> 'a t) list -> 'a Future.t list t

nfork l is similar to fork but creates n sub-fibers.

val nfork_map : 'a list -> f:('a -> 'b t) -> 'b Future.t list t

nfork_map l ~f is the same as nfork (List.map l ~f:(fun x () -> f x)) but more efficient.

Joining

The following combinators are helpers to combine the result of several fibers into one. Note that they do not introduce parallelism.

val both : 'a t -> 'b t -> ('a * 'b) t
val sequential_map : 'a list -> f:('a -> 'b t) -> 'b list t
val sequential_iter : 'a list -> f:('a -> unit t) -> unit t

Forking + joining

The following functions combine forking 2 or more fibers followed by joining the results. For every function, we give an equivalent implementation using the more basic functions as documentation. Note however that these functions are implemented as primitives and so are more efficient that the suggested implementation.

val fork_and_join : (unit -> 'a t) -> (unit -> 'b t) -> ('a * 'b) t

For two fibers and wait for their results:

let fork_and_join f g =
  fork f >>= fun a ->
  fork g >>= fun b -> both (Future.wait a) (Future.wait b)
val fork_and_join_unit : (unit -> unit t) -> (unit -> 'a t) -> 'a t

Same but assume the first fiber returns unit:

let fork_and_join_unit f g =
  fork f >>= fun a ->
  fork g >>= fun b -> Future.wait a >>> Future.wait b
val fork_and_race : (unit -> 'a t) -> (unit -> 'b t) -> ('a, 'b) Stdune.Either.t t
val parallel_map : 'a list -> f:('a -> 'b t) -> 'b list t

Map a list in parallel:

let parallel_map l ~f =
  nfork_map l ~f >>= fun futures -> all (List.map futures ~f:Future.wait)
val parallel_iter : 'a list -> f:('a -> unit t) -> unit t

Iter over a list in parallel:

let parallel_iter l ~f =
  nfork_map l ~f >>= fun futures ->
  all_unit (List.map futures ~f:Future.wait)

Local storage

module Var = Fiber.Var

Variables local to a fiber

Error handling

val with_error_handler : (unit -> 'a t) -> on_error:(Stdune.Exn_with_backtrace.t -> unit) -> 'a t

with_error_handler f ~on_error calls on_error for every exception raised during the execution of f. This include exceptions raised when calling f () or during the execution of fibers after f () has returned. Exceptions raised by on_error are passed on to the parent error handler.

It is guaranteed that after the fiber has returned a value, on_error will never be called.

val fold_errors : (unit -> 'a t) -> init:'b -> on_error:(Stdune.Exn_with_backtrace.t -> 'b -> 'b) -> ('a, 'b) Stdune.Result.t t

fold_errors f ~init ~on_error calls on_error for every exception raised during the execution of f. This include exceptions raised when calling f () or during the execution of fibers after f () has returned.

Exceptions raised by on_error are passed on to the parent error handler.

val collect_errors : (unit -> 'a t) -> ('a, Stdune.Exn_with_backtrace.t list) Stdune.Result.t t

collect_errors f is: fold_errors f ~init:[] ~on_error:(fun e l -> e :: l)

val finalize : (unit -> 'a t) -> finally:(unit -> unit t) -> 'a t

finalize f ~finally runs finally after f () has terminated, whether it fails or succeeds.

Synchronization

module Ivar = Fiber.Ivar

Write once variables

module Mutex = Fiber.Mutex

Running fibers

val run : 'a t -> 'a option

run t runs a fiber. If the fiber doesn't complete immediately, run t returns None.

module Mvar = Fiber.Mvar

Mailbox variables

module Result : sig ... end
OCaml

Innovation. Community. Security.