Fiber.FLS
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 to specify initial values for fibers.
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.
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.