1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
(* TEST *)
(*
Test handling of continuations created by a domain that has since terminated.
Bug report and testcase by Ziga Luksic, see:
https://github.com/ocamllabs/ocaml-multicore/issues/175
*)
open Effect
open Effect.Deep
type _ t += Poke : unit t
type result = Done | Poking of (unit -> result)
(* Debug help. *)
let print s = print_string s; Format.pp_print_flush Format.std_formatter ()
(* Just poke the handler n times. *)
let rec poke = function
| 0 -> ()
| n -> perform Poke; poke (n-1)
(* The handler inside the domain, that captures the continuation whenever
it gets poked. *)
let domain_handler f =
match_with f ()
{ retc = (fun () -> Done);
exnc = (fun e -> raise e);
effc = fun (type a) (e : a t) ->
match e with
| Poke -> Some (fun (k : (a, _) continuation) ->
Poking (fun () ->
print "...";
ignore (continue k ());
print "success\n";
Done))
| _ -> None }
(* Re-runs the poker that happened inside a domain. *)
let rerunner = function
| Poking f -> f () (*re-runs the function*)
| Done -> Done
(* Test. *)
let test n =
(* Messy handler wrapping. *)
let inner () = domain_handler (fun () -> poke n) in
rerunner (Domain.join (Domain.spawn inner))
let _ = test 100 |> ignore; print_endline "done"
|