feat hmap FLS: do not fail if run outside of a fiber

This commit is contained in:
Simon Cruanes 2024-12-04 10:27:04 -05:00
parent ea1af6ed22
commit 6ab9a691bf
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -9,18 +9,22 @@ let k_local_hmap : Hmap.t FLS.t = FLS.create ()
(** Access the local [hmap], or an empty one if not set *) (** Access the local [hmap], or an empty one if not set *)
let[@inline] get_local_hmap () : Hmap.t = let[@inline] get_local_hmap () : Hmap.t =
let fiber = get_current_fiber_exn () in match TLS.get_exn k_cur_fiber with
FLS.get fiber ~default:Hmap.empty k_local_hmap | exception TLS.Not_set -> Hmap.empty
| fiber -> FLS.get fiber ~default:Hmap.empty k_local_hmap
let[@inline] set_local_hmap (h : Hmap.t) : unit = let[@inline] set_local_hmap (h : Hmap.t) : unit =
let fiber = get_current_fiber_exn () in match TLS.get_exn k_cur_fiber with
FLS.set fiber k_local_hmap h | exception TLS.Not_set -> ()
| fiber -> FLS.set fiber k_local_hmap h
let[@inline] update_local_hmap (f : Hmap.t -> Hmap.t) : unit = let[@inline] update_local_hmap (f : Hmap.t -> Hmap.t) : unit =
let fiber = get_current_fiber_exn () in match TLS.get_exn k_cur_fiber with
let h = FLS.get fiber ~default:Hmap.empty k_local_hmap in | exception TLS.Not_set -> ()
let h = f h in | fiber ->
FLS.set fiber k_local_hmap h let h = FLS.get fiber ~default:Hmap.empty k_local_hmap in
let h = f h in
FLS.set fiber k_local_hmap h
(** @raise Invalid_argument if not present *) (** @raise Invalid_argument if not present *)
let get_in_local_hmap_exn (k : 'a Hmap.key) : 'a = let get_in_local_hmap_exn (k : 'a Hmap.key) : 'a =