package fuseau

  1. Overview
  2. Docs

Module Fuseau_unixSource

Simple event loop based on Unix.select.

This library combines Fuseau's fibers with a simple event loop for IOs based on Unix.select. It's useful for simple situations or portability. For bigger system it's probably better to use another event loop.

include module type of struct include Fuseau end

Foundations

Sourcemodule Fiber_handle = Fuseau.Fiber_handle

The unique name of a fiber

Sourcemodule Event = Fuseau.Event

Atomic events.

Sourcetype 'ret branch = 'ret Event.branch =
  1. | When : 'a Event.t * ('a -> 'ret) -> 'ret branch
Sourceval select : 'ret branch list -> 'ret

Synchronization

Sourcemodule Chan = Fuseau.Chan

Basic channels

Utils

Sourcemodule Exn_bt = Fuseau.Exn_bt

Exception with backtrace

Sourcemodule Time = Fuseau.Time

Time measurement

Sourcemodule Fiber = Fuseau.Fiber

Fibers.

Sourcemodule FLS = Fuseau.FLS

Fiber-local storage.

IO event loop

Sourceexception Inactive

Exception raised when trying to perform operations on the scheduler after it's been disposed of

Sourcemodule Scheduler = Fuseau.Scheduler

Scheduler that runs fibers.

Sourcemodule Event_loop = Fuseau.Event_loop

Resource management

Sourcemodule Resource_pool = Fuseau.Resource_pool

Resource pool.

Sourcemodule Buf_pool = Fuseau.Buf_pool

A pool of buffers to reuse.

Sourcemodule Cancel_handle = Fuseau.Cancel_handle

IO streams

Sourcemodule Iostream = Fuseau.Iostream

IO streams.

Sleep

Sourceval sleep_s : float -> unit

Put the current fiber to sleep for that many seconds.

Re-exports

Sourceexception Timeout

Exception used for cancellation caused by timeout

Sourceval 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.

Sourceval try_await : 'a Fiber.t -> 'a Exn_bt.result

Like await but catches exceptions.

Sourceval cancel_after_s : float -> unit

Cancel the current fiber after delay seconds, unless the fiber terminates first. The cancellation will use the Timeout exception.

Sourceval ev_timeout : float -> 'a Event.t

ev_timeout duration is an event that resolves after duration seconds with a Error Timeout error

Sourceval ev_deadline : float -> 'a Event.t

ev_deadline time is an event that resolves at monotonic time t with a Error Timeout error

Sourceval 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.

Sourceval 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).

  • parameter propagate_cancel_to_parent

    if true (the default), if this fiber fails then the parent fiber will also fail (child to parent).

  • raises Inactive

    if the scheduler is inactive.

Sourceval 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.

  • raises Inactive

    if the scheduler is inactive.

Sourceval 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

Sourceval schedule_micro_task : (unit -> unit) -> unit

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.

  • raises Inactive

    if the scheduler is inactive.

Sourceval yield : unit -> unit

yield () returns control to the scheduler and checks for cancellation. This must be called from a fiber.

Sourceval get_scheduler : unit -> Scheduler.t

This returns the scheduler on which the caller runs. It must be called from inside a fiber.

Main loop.

This is the loop that runs both fibers, and the IO event loop, in an interspersed way.

Sourcemodule IO_unix : sig ... end

Low level Unix IOs

Sourcemodule Net : sig ... end

Networking

Sourcemodule Timer : sig ... end
Sourceval main : (unit -> 'a) -> 'a

A version of Fuseau.main that uses a Unix-based event loop.

OCaml

Innovation. Community. Security.