cancellation in main

This commit is contained in:
Simon Cruanes 2024-06-26 11:20:29 -04:00
parent b47da2396e
commit b208506841
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 11 additions and 7 deletions

View file

@ -10,7 +10,7 @@ type cancel_callback = Exn_bt.t -> unit
let prom_of_fut : 'a Fut.t -> 'a Fut.promise =
Fut.Private_.unsafe_promise_of_fut
module Private_ = struct
module Private0 = struct
type 'a t = {
id: Handle.t; (** unique identifier for this fiber *)
state: 'a state A.t; (** Current state in the lifetime of the fiber *)
@ -40,13 +40,9 @@ module Private_ = struct
match A.get self.state with
| Alive _ -> false
| Terminating_or_done _ -> true
let cancel_from_outside (self : _ t) ebt : unit =
Fut.fulfill_idempotent (Fut.Private_.unsafe_promise_of_fut self.res)
@@ Error ebt
end
include Private_
include Private0
let create_ ~ls ~runner () : 'a t =
let id = Handle.generate_fresh () in
@ -321,3 +317,9 @@ let yield () : unit =
check_if_cancelled_ self;
Suspend_.yield ();
check_if_cancelled_ self
module Private_ = struct
include Private0
let cancel_from_outside = resolve_as_failed_
end

View file

@ -8,7 +8,9 @@ let main (f : Runner.t -> 'a) : 'a =
Fiber.on_result fiber (fun _ -> Runner.shutdown_without_waiting runner);
(* run the main thread *)
Fifo_pool.Private_.run_thread st runner ~on_exn:(fun e bt ->
raise (Oh_no (Exn_bt.make e bt)));
let ebt = Exn_bt.make e bt in
Fiber.Private_.cancel_from_outside fiber ebt;
raise (Oh_no ebt));
match Fiber.peek fiber with
| Some (Ok x) -> x
| Some (Error ebt) -> Exn_bt.raise ebt