Skip to content

Commit 06e98e6

Browse files
committed
Experiment to use shallow effects in picos.fifos
1 parent 849dea9 commit 06e98e6

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

Diff for: lib/picos_mux.fifo/picos_mux_fifo.ml

+24-19
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ let[@inline never] quota_non_positive () = invalid_arg "quota must be positive"
44

55
type ready =
66
| Spawn of Fiber.t * (Fiber.t -> unit)
7-
| Continue of Fiber.t * (unit, unit) Effect.Deep.continuation
7+
| Continue of Fiber.t * (unit, unit) Effect.Shallow.continuation
88
| Resume of
99
Fiber.t
10-
* ((exn * Printexc.raw_backtrace) option, unit) Effect.Deep.continuation
11-
| Return of Fiber.Maybe.t * (unit, unit) Effect.Deep.continuation
10+
* ( (exn * Printexc.raw_backtrace) option,
11+
unit )
12+
Effect.Shallow.continuation
13+
| Return of Fiber.Maybe.t * (unit, unit) Effect.Shallow.continuation
1214

1315
module Mpscq = Picos_aux_mpscq
1416

@@ -20,13 +22,13 @@ type t = {
2022
resume :
2123
Trigger.t ->
2224
Fiber.t ->
23-
((exn * Printexc.raw_backtrace) option, unit) Effect.Deep.continuation ->
25+
((exn * Printexc.raw_backtrace) option, unit) Effect.Shallow.continuation ->
2426
unit;
25-
current : ((Fiber.t, unit) Effect.Deep.continuation -> unit) option;
26-
yield : ((unit, unit) Effect.Deep.continuation -> unit) option;
27-
return : ((unit, unit) Effect.Deep.continuation -> unit) option;
28-
discontinue : ((unit, unit) Effect.Deep.continuation -> unit) option;
29-
handler : (unit, unit) Effect.Deep.handler;
27+
current : ((Fiber.t, unit) Effect.Shallow.continuation -> unit) option;
28+
yield : ((unit, unit) Effect.Shallow.continuation -> unit) option;
29+
return : ((unit, unit) Effect.Shallow.continuation -> unit) option;
30+
discontinue : ((unit, unit) Effect.Shallow.continuation -> unit) option;
31+
handler : (unit, unit) Effect.Shallow.handler;
3032
quota : int;
3133
mutable fiber : Fiber.Maybe.t;
3234
mutable remaining_quota : int;
@@ -38,19 +40,20 @@ let rec next t =
3840
| Spawn (fiber, main) ->
3941
t.fiber <- Fiber.Maybe.of_fiber fiber;
4042
t.remaining_quota <- t.quota;
41-
Effect.Deep.match_with main fiber t.handler
43+
let k = Effect.Shallow.fiber main in
44+
Effect.Shallow.continue_with k fiber t.handler
4245
| Return (fiber, k) ->
4346
t.fiber <- fiber;
4447
t.remaining_quota <- t.quota;
45-
Effect.Deep.continue k ()
48+
Effect.Shallow.continue_with k () t.handler
4649
| Continue (fiber, k) ->
4750
t.fiber <- Fiber.Maybe.of_fiber fiber;
4851
t.remaining_quota <- t.quota;
49-
Fiber.continue fiber k ()
52+
Fiber.continue_with fiber k () t.handler
5053
| Resume (fiber, k) ->
5154
t.fiber <- Fiber.Maybe.of_fiber fiber;
5255
t.remaining_quota <- t.quota;
53-
Fiber.resume fiber k
56+
Fiber.resume_with fiber k t.handler
5457
| exception Mpscq.Empty ->
5558
t.fiber <- Fiber.Maybe.nothing;
5659
if t.num_alive_fibers <> 0 then begin
@@ -102,7 +105,7 @@ let run_fiber ?quota ?fatal_exn_handler:(exnc : _ = raise) fiber main =
102105
Some
103106
(fun k ->
104107
let fiber = Fiber.Maybe.to_fiber t.fiber in
105-
Effect.Deep.continue k fiber)
108+
Effect.Shallow.continue_with k fiber t.handler)
106109
and yield =
107110
Some
108111
(fun k ->
@@ -115,7 +118,7 @@ let run_fiber ?quota ?fatal_exn_handler:(exnc : _ = raise) fiber main =
115118
let remaining_quota = t.remaining_quota - 1 in
116119
if 0 < remaining_quota then begin
117120
t.remaining_quota <- remaining_quota;
118-
Effect.Deep.continue k ()
121+
Effect.Shallow.continue_with k () t.handler
119122
end
120123
else begin
121124
Mpscq.push t.ready (Return (t.fiber, k));
@@ -125,10 +128,10 @@ let run_fiber ?quota ?fatal_exn_handler:(exnc : _ = raise) fiber main =
125128
Some
126129
(fun k ->
127130
let fiber = Fiber.Maybe.to_fiber t.fiber in
128-
Fiber.continue fiber k ())
131+
Fiber.continue_with fiber k () t.handler)
129132
and handler = { retc; exnc; effc }
130133
and[@alert "-handler"] effc :
131-
type a. a Effect.t -> ((a, _) Effect.Deep.continuation -> _) option =
134+
type a. a Effect.t -> ((a, _) Effect.Shallow.continuation -> _) option =
132135
function
133136
| Fiber.Current -> t.current
134137
| Fiber.Spawn r ->
@@ -150,7 +153,9 @@ let run_fiber ?quota ?fatal_exn_handler:(exnc : _ = raise) fiber main =
150153
| () -> t.return
151154
| exception exn ->
152155
let bt = Printexc.get_raw_backtrace () in
153-
Some (fun k -> Effect.Deep.discontinue_with_backtrace k exn bt)
156+
Some
157+
(fun k ->
158+
Effect.Shallow.discontinue_with_backtrace k exn bt t.handler)
154159
end
155160
| Trigger.Await trigger ->
156161
Some
@@ -161,7 +166,7 @@ let run_fiber ?quota ?fatal_exn_handler:(exnc : _ = raise) fiber main =
161166
let remaining_quota = t.remaining_quota - 1 in
162167
if 0 < remaining_quota then begin
163168
t.remaining_quota <- remaining_quota;
164-
Fiber.resume fiber k
169+
Fiber.resume_with fiber k t.handler
165170
end
166171
else begin
167172
Mpscq.push t.ready (Resume (fiber, k));

0 commit comments

Comments
 (0)