diff --git a/src/fib/fiber.ml b/src/fib/fiber.ml index 4f776c80..7fd11bf9 100644 --- a/src/fib/fiber.ml +++ b/src/fib/fiber.ml @@ -265,7 +265,7 @@ let spawn_top ~on f : _ t = let ls = Task_local_storage.Direct.create () in 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 *) let (Any p) = match get_cur () with @@ -273,7 +273,12 @@ let spawn ?(protect = true) f : _ t = | Some p -> p 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; child diff --git a/src/fib/fiber.mli b/src/fib/fiber.mli index 25787cc1..d02c4e56 100644 --- a/src/fib/fiber.mli +++ b/src/fib/fiber.mli @@ -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 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] from a running fiber [parent]. The sub-fiber [f_child] is attached to the current fiber and 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 affect [parent]. If false, [f_child] failing also causes [parent] to fail (and therefore all other children