add ?on param to Fiber.spawn

This commit is contained in:
Simon Cruanes 2024-03-05 09:47:31 -05:00
parent f798420423
commit f9aea68d61
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 10 additions and 3 deletions

View file

@ -265,7 +265,7 @@ let spawn_top ~on f : _ t =
let ls = Task_local_storage.Direct.create () in let ls = Task_local_storage.Direct.create () in
spawn_ ~ls ~runner:on ~parent:None f spawn_ ~ls ~runner:on ~parent:None f
let spawn ?(protect = true) f : _ t = let spawn ?on ?(protect = true) f : _ t =
(* spawn [f()] with a copy of our local storage *) (* spawn [f()] with a copy of our local storage *)
let (Any p) = let (Any p) =
match get_cur () with match get_cur () with
@ -273,7 +273,12 @@ let spawn ?(protect = true) f : _ t =
| Some p -> p | Some p -> p
in in
let ls = Task_local_storage.Direct.copy p.ls in let ls = Task_local_storage.Direct.copy p.ls in
let child = spawn_ ~ls ~parent:(Some p) ~runner:p.runner f in let runner =
match on with
| Some r -> r
| None -> p.runner
in
let child = spawn_ ~ls ~parent:(Some p) ~runner f in
add_child_ ~protect p child; add_child_ ~protect p child;
child child

View file

@ -120,12 +120,14 @@ val spawn_top : on:Runner.t -> (unit -> 'a) -> 'a t
This fiber is not the child of any other fiber: its lifetime This fiber is not the child of any other fiber: its lifetime
is only determined by the lifetime of [f()]. *) is only determined by the lifetime of [f()]. *)
val spawn : ?protect:bool -> (unit -> 'a) -> 'a t val spawn : ?on:Runner.t -> ?protect:bool -> (unit -> 'a) -> 'a t
(** [spawn ~protect f] spawns a sub-fiber [f_child] (** [spawn ~protect f] spawns a sub-fiber [f_child]
from a running fiber [parent]. from a running fiber [parent].
The sub-fiber [f_child] is attached to the current fiber and fails The sub-fiber [f_child] is attached to the current fiber and fails
if the current fiber [parent] fails. if the current fiber [parent] fails.
@param on if provided, start the fiber on the given runner. If not
provided, use the parent's runner.
@param protect if true, when [f_child] fails, it does not @param protect if true, when [f_child] fails, it does not
affect [parent]. If false, [f_child] failing also affect [parent]. If false, [f_child] failing also
causes [parent] to fail (and therefore all other children causes [parent] to fail (and therefore all other children