diff --git a/src/fib/fiber.ml b/src/fib/fiber.ml index 55410d43..fdfd3830 100644 --- a/src/fib/fiber.ml +++ b/src/fib/fiber.ml @@ -171,7 +171,7 @@ let remove_on_cancel (self : _ t) h = () done -let with_cancel_callback (self : _ t) cb (k : unit -> 'a) : 'a = +let with_on_cancel (self : _ t) cb (k : unit -> 'a) : 'a = let h = add_on_cancel self cb in Fun.protect k ~finally:(fun () -> remove_on_cancel self h) @@ -284,7 +284,7 @@ let[@inline] self () : any = | None -> failwith "Fiber.self: must be run from inside a fiber." | Some f -> f -let with_self_cancel_callback cb (k : unit -> 'a) : 'a = +let with_on_self_cancel cb (k : unit -> 'a) : 'a = let (Any self) = self () in let h = add_on_cancel self cb in Fun.protect k ~finally:(fun () -> remove_on_cancel self h) diff --git a/src/fib/fiber.mli b/src/fib/fiber.mli index f1c544d2..a9850291 100644 --- a/src/fib/fiber.mli +++ b/src/fib/fiber.mli @@ -97,15 +97,16 @@ val remove_on_cancel : _ t -> cancel_handle -> unit (** [remove_on_cancel fib h] removes the cancel callback associated with handle [h]. *) -val with_cancel_callback : _ t -> cancel_callback -> (unit -> 'a) -> 'a -(** [with_cancel_callback fib cb (fun () -> )] evaluates [e] +val with_on_cancel : _ t -> cancel_callback -> (unit -> 'a) -> 'a +(** [with_on_cancel fib cb (fun () -> )] evaluates [e] in a scope in which, if the fiber [fib] is cancelled, [cb()] is called. If [e] returns without the fiber being cancelled, this callback is removed. *) -val with_self_cancel_callback : cancel_callback -> (unit -> 'a) -> 'a -(** [with_self_cancel_callback cb f] calls [f()] in a scope where - [cb] is added to the cancel callbacks of the current fiber *) +val with_on_self_cancel : cancel_callback -> (unit -> 'a) -> 'a +(** [with_on_self_cancel cb f] calls [f()] in a scope where + [cb] is added to the cancel callbacks of the current fiber; + and [f()] terminates, [cb] is removed from the list. *) val on_result : 'a t -> 'a callback -> unit (** Wait for fiber to be done and call the callback diff --git a/test/fiber/t_fib1.ml b/test/fiber/t_fib1.ml index 45fa2021..0a1ece6a 100644 --- a/test/fiber/t_fib1.ml +++ b/test/fiber/t_fib1.ml @@ -93,7 +93,7 @@ let () = let fib = F.spawn_top ~on:runner @@ fun () -> let@ () = - F.with_self_cancel_callback (fun ebt -> + F.with_on_self_cancel (fun ebt -> logf (TS.tick_get clock) "main fiber cancelled with %s" @@ Exn_bt.show ebt) in @@ -104,7 +104,7 @@ let () = let clock = ref (0 :: i :: !clock) in F.spawn ~protect:false @@ fun _n -> let@ () = - F.with_self_cancel_callback (fun _ -> + F.with_on_self_cancel (fun _ -> logf (TS.tick_get clock) "sub-fiber %d was cancelled" i) in Thread.delay (float i *. 0.001);