This commit is contained in:
Simon Cruanes 2024-03-04 21:38:50 -05:00
parent 533b6e5ce2
commit 9cb10a79e6
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 10 additions and 9 deletions

View file

@ -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)

View file

@ -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 () -> <e>)] evaluates [e]
val with_on_cancel : _ t -> cancel_callback -> (unit -> 'a) -> 'a
(** [with_on_cancel fib cb (fun () -> <e>)] 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

View file

@ -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);