package picos

  1. Overview
  2. Docs

Fiber local storage

Fiber local storage is intended for use as a low overhead storage mechanism for fiber extensions. For example, one might associate a priority value with each fiber for a scheduler that uses a priority queue or one might use FLS to store unique id values for fibers.

Here is an example of how one might define a fiber id:

let fiber_id_key =
  let next = Atomic.make 0 in
  Fiber.FLS.new_key
  @@ Computed (fun () ->
      Atomic.fetch_and_add next 1)

Here is an example of how one might define a fiber priority:

let fiber_priority_key =
  Fiber.FLS.new_key (Constant 0)

A priority based scheduler could then use get to access the priority of a fiber highly efficiently. Fibers that are fine with the default priority do not necessarily need to store a priority at all.

type !'a key

Represents a key for storing values of type 'a in storage associated with fibers.

type 'a initial =
  1. | Constant of 'a
  2. | Computed of unit -> 'a

Type to specify initial values for fibers.

val new_key : 'a initial -> 'a key

new_key initial allocates a new key for associating values in storage associated with fibers. The initial value for every fiber is either the given Constant or is Computed with the given function. If the initial value is a constant, no value needs to be stored unless the value is explicitly updated.

⚠️ New keys should not be created dynamically.

val get : t -> 'a key -> 'a

get fiber key returns the value associated with the key in the storage associated with the fiber.

⚠️ It is only safe to call get from the fiber itself.

val set : t -> 'a key -> 'a -> unit

set fiber key value sets the value associated with the key to the given value in the storage associated with the fiber.

⚠️ It is only safe to call set from the fiber itself.

OCaml

Innovation. Community. Security.