package moonpool

  1. Overview
  2. Docs

Module Moonpool_fib.FiberSource

Fibers.

A fiber is a lightweight computation that runs cooperatively alongside other fibers. In the context of moonpool, fibers have additional properties:

  • they run in a moonpool runner
  • they form a simple supervision tree, enabling a limited form of structured concurrency
Sourcetype cancel_callback = Moonpool.Exn_bt.t -> unit

A callback used in case of cancellation

Sourcetype 'a t

A fiber returning a value of type 'a.

Sourceval res : 'a t -> 'a Moonpool.Fut.t

Future result of the fiber.

Sourcetype 'a callback = 'a Moonpool.Exn_bt.result -> unit

Callbacks that are called when a fiber is done.

Sourcetype any =
  1. | Any : _ t -> any

Type erased fiber

Sourceval return : 'a -> 'a t
Sourceval fail : Moonpool.Exn_bt.t -> _ t
Sourceval self : unit -> any

self () is the current fiber. Must be run from inside a fiber.

  • raises Failure

    if not run from inside a fiber.

Sourceval peek : 'a t -> 'a Moonpool.Fut.or_error option

Peek inside the future result

Sourceval is_done : _ t -> bool

Has the fiber completed?

Sourceval is_cancelled : _ t -> bool

Has the fiber completed with a failure?

Sourceval is_success : _ t -> bool

Has the fiber completed with a value?

Sourceval await : 'a t -> 'a

await fib is like Fut.await (res fib)

Sourceval wait_block_exn : 'a t -> 'a

wait_block_exn fib is Fut.wait_block_exn (res fib). NOTE: See Fut.wait_block for warnings about deadlocks.

Sourceval wait_block : 'a t -> 'a Moonpool.Fut.or_error

wait_block fib is Fut.wait_block (res fib). NOTE: See Fut.wait_block for warnings about deadlocks.

Sourceval check_if_cancelled : unit -> unit

Check if the current fiber is cancelled, in which case this raises. Must be run from inside a fiber.

  • raises e

    if the current fiber is cancelled with exception e

  • raises Failure

    if not run from a fiber.

Sourceval yield : unit -> unit

Yield control to the scheduler from the current fiber.

  • raises Failure

    if not run from inside a fiber.

Sourcetype cancel_handle

An opaque handle for a single cancel callback in a fiber

Sourceval add_on_cancel : _ t -> cancel_callback -> cancel_handle

add_on_cancel fib cb adds cb to the list of cancel callbacks for fib. If fib is already cancelled, cb is called immediately.

Sourceval remove_on_cancel : _ t -> cancel_handle -> unit

remove_on_cancel fib h removes the cancel callback associated with handle h.

Sourceval with_on_cancel : _ t -> cancel_callback -> (unit -> 'a) -> 'a

with_on_cancel fib cb (fun () -> <e>) evaluates e in a scope in which, if the fiber fib is cancelled, cb() is called. If e returns without the fiber being cancelled, this callback is removed.

Sourceval with_on_self_cancel : cancel_callback -> (unit -> 'a) -> 'a

with_on_self_cancel cb f calls f() in a scope where cb is added to the cancel callbacks of the current fiber; and f() terminates, cb is removed from the list.

Sourceval on_result : 'a t -> 'a callback -> unit

Wait for fiber to be done and call the callback with the result. If the fiber is done already then the callback is invoked immediately with its result.

Sourceval spawn_top : on:Moonpool.Runner.t -> (unit -> 'a) -> 'a t

spawn_top ~on f spawns a new (toplevel) fiber onto the given runner. This fiber is not the child of any other fiber: its lifetime is only determined by the lifetime of f().

Sourceval spawn : ?on:Moonpool.Runner.t -> ?protect:bool -> (unit -> 'a) -> 'a t

spawn ~protect f spawns a sub-fiber f_child from a running fiber parent. The sub-fiber f_child is attached to the current fiber and fails if the current fiber parent fails.

  • parameter on

    if provided, start the fiber on the given runner. If not provided, use the parent's runner.

  • parameter protect

    if true, when f_child fails, it does not affect parent. If false, f_child failing also causes parent to fail (and therefore all other children of parent). Default is true.

Must be run from inside a fiber.

  • raises Failure

    if not run from inside a fiber.

Sourceval spawn_ignore : ?on:Moonpool.Runner.t -> ?protect:bool -> (unit -> _) -> unit

spawn_ignore f is ignore (spawn f). The fiber will still affect termination of the parent, ie. the parent will exit only after this new fiber exits.

  • parameter on

    the optional runner to use, added since 0.7

Sourceval spawn_top_ignore : on:Moonpool.Runner.t -> (unit -> _) -> unit

Like spawn_top but ignores the result.

  • since 0.7
OCaml

Innovation. Community. Security.