wip: use shallow effect handlers

they still nest though, quite problematic
This commit is contained in:
Simon Cruanes 2024-02-23 20:55:37 -05:00
parent ed171c1171
commit 20e3af82fd
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -34,9 +34,9 @@ type with_suspend_handler =
-> with_suspend_handler -> with_suspend_handler
let with_suspend (WSH { on_suspend; run; resume }) (f : unit -> unit) : unit = let with_suspend (WSH { on_suspend; run; resume }) (f : unit -> unit) : unit =
let module E = Effect.Deep in let module E = Effect.Shallow in
(* effect handler *) (* effect handler *)
let effc : type e. e Effect.t -> ((e, _) E.continuation -> _) option = let rec effc : type e. e Effect.t -> ((e, _) E.continuation -> _) option =
function function
| Suspend h -> | Suspend h ->
(* TODO: discontinue [k] if current fiber (if any) is cancelled? *) (* TODO: discontinue [k] if current fiber (if any) is cancelled? *)
@ -44,8 +44,8 @@ let with_suspend (WSH { on_suspend; run; resume }) (f : unit -> unit) : unit =
(fun k -> (fun k ->
let state = on_suspend () in let state = on_suspend () in
let k' : suspension = function let k' : suspension = function
| Ok () -> E.continue k () | Ok () -> E.continue_with k () handler
| Error (exn, bt) -> E.discontinue_with_backtrace k exn bt | Error (exn, bt) -> E.discontinue_with_backtrace k exn bt handler
in in
h.handle ~run:(run state) ~resume:(resume state) k') h.handle ~run:(run state) ~resume:(resume state) k')
| Yield -> | Yield ->
@ -54,14 +54,14 @@ let with_suspend (WSH { on_suspend; run; resume }) (f : unit -> unit) : unit =
(fun k -> (fun k ->
let state = on_suspend () in let state = on_suspend () in
let k' : suspension = function let k' : suspension = function
| Ok () -> E.continue k () | Ok () -> E.continue_with k () handler
| Error (exn, bt) -> E.discontinue_with_backtrace k exn bt | Error (exn, bt) -> E.discontinue_with_backtrace k exn bt handler
in in
resume state k' @@ Ok ()) resume state k' @@ Ok ())
| _ -> None | _ -> None
in and handler = { E.retc = Fun.id; exnc = raise; effc } in
E.try_with f () { E.effc } E.continue_with (E.fiber f) () handler
(* DLA interop *) (* DLA interop *)
let prepare_for_await () : Dla_.t = let prepare_for_await () : Dla_.t =