Module Moonpool_fib.Fls

Fiber-local storage.

This storage is associated to the current fiber, just like thread-local storage is associated with the current thread.

See Moonpool.Task_local_storage for more general information, as this is based on it.

NOTE: it's important to note that, while each fiber has its own storage, spawning a sub-fiber f2 from a fiber f1 will only do a shallow copy of the storage. Values inside f1's storage will be physically shared with f2. It is thus recommended to store only persistent values in the local storage.

include module type of struct include Moonpool.Task_local_storage end
type 'a t = 'a Picos.Fiber.FLS.t
val create : unit -> 'a t

create () makes a new key. Keys are expensive and should never be allocated dynamically or in a loop.

exception Not_set
val get_exn : 'a t -> 'a

get k gets the value for the current task for key k. Must be run from inside a task running on a runner.

val get_opt : 'a t -> 'a option

get_opt k gets the current task's value for key k, or None if not run from inside the task.

val get : 'a t -> default:'a -> 'a
val set : 'a t -> 'a -> unit

set k v sets the storage for k to v. Must be run from inside a task running on a runner.

  • raises Failure

    otherwise

val with_value : 'a t -> 'a -> (unit -> 'b) -> 'b

with_value k v f sets k to v for the duration of the call to f(). When f() returns (or fails), k is restored to its old value.

Local Hmap.t

This requires hmap to be installed.

val k_local_hmap : Hmap.t Picos.Fiber.FLS.t

A local hmap, inherited in children fibers

val get_local_hmap : unit -> Hmap.t

Access the local hmap, or an empty one if not set

val set_local_hmap : Hmap.t -> unit
val update_local_hmap : (Hmap.t -> Hmap.t) -> unit
val get_in_local_hmap_exn : 'a Hmap.key -> 'a
  • raises Invalid_argument

    if not present

val get_in_local_hmap_opt : 'a Hmap.key -> 'a option
val remove_in_local_hmap : 'a Hmap.key -> unit

Remove given key from the local hmap

val set_in_local_hmap : 'a Hmap.key -> 'a -> unit
val with_in_local_hmap : 'a Hmap.key -> 'a -> (unit -> unit) -> unit

with_in_local_hmap k v f calls f() in a context where k is bound to v in the local hmap. Then it restores the previous binding for k.