Skip to content

Commit

Permalink
Add FLS.remove
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Sep 13, 2024
1 parent 12af9b2 commit fa36b3e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/picos/bootstrap/picos_bootstrap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ module Fiber = struct
r.fls <- fls;
Array.unsafe_set fls key
(Sys.opaque_identity (Obj.magic value : non_float))

let remove (type a) (Fiber r : fiber) (key : a t) =
let fls = r.fls in
if key < Array.length fls then Array.unsafe_set fls key unique
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/picos/picos.mli
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,13 @@ module Fiber : sig
⚠️ It is only safe to call [set] from the fiber itself or when the fiber
is known not to be running. *)

val remove : fiber -> 'a t -> unit
(** [remove fiber key] removes the value, if any, associated with the [key]
from the storage associated with the [fiber].
⚠️ It is only safe to call [remove] from the fiber itself or when the
fiber is known not to be running. *)
end

(** {2 Interface for spawning} *)
Expand Down
2 changes: 2 additions & 0 deletions test/test_picos.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ let test_fls_basics =
Fiber.FLS.set fiber float_key 4.2;
Fiber.FLS.set fiber counter_key (Atomic.fetch_and_add counter 1);
assert (Fiber.FLS.get_exn fiber float_key = 4.2);
Fiber.FLS.remove fiber float_key;
assert (Fiber.FLS.get fiber float_key ~default:1.01 = 1.01);
assert (Fiber.FLS.get_exn fiber counter_key = 0)
end;
begin
Expand Down

0 comments on commit fa36b3e

Please sign in to comment.