Skip to content

Commit cdbac0c

Browse files
committed
Experiment to use shallow effects in picos_mux.fifo
1 parent 84d5428 commit cdbac0c

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/picos_mux.fifo/picos_mux_fifo.ml

Lines changed: 29 additions & 20 deletions
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.t * (unit, unit) Effect.Deep.continuation
10+
* ( (exn * Printexc.raw_backtrace) option,
11+
unit )
12+
Effect.Shallow.continuation
13+
| Return of Fiber.t * (unit, unit) Effect.Shallow.continuation
1214

1315
module Mpscq = Picos_aux_mpscq
1416

@@ -20,13 +22,15 @@ type t = {
2022
mutable 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-
mutable current : ((Fiber.t, unit) Effect.Deep.continuation -> unit) option;
26-
mutable yield : ((unit, unit) Effect.Deep.continuation -> unit) option;
27-
mutable return : ((unit, unit) Effect.Deep.continuation -> unit) option;
28-
mutable discontinue : ((unit, unit) Effect.Deep.continuation -> unit) option;
29-
mutable handler : (unit, unit) Effect.Deep.handler;
27+
mutable current :
28+
((Fiber.t, unit) Effect.Shallow.continuation -> unit) option;
29+
mutable yield : ((unit, unit) Effect.Shallow.continuation -> unit) option;
30+
mutable return : ((unit, unit) Effect.Shallow.continuation -> unit) option;
31+
mutable discontinue :
32+
((unit, unit) Effect.Shallow.continuation -> unit) option;
33+
mutable handler : (unit, unit) Effect.Shallow.handler;
3034
quota : int;
3135
mutable fiber : Fiber.Maybe.t;
3236
mutable remaining_quota : int;
@@ -45,10 +49,12 @@ let rec next t =
4549
| Return (fiber, _) ->
4650
Fiber.Maybe.of_fiber fiber);
4751
match ready with
48-
| Spawn (fiber, main) -> Effect.Deep.match_with main fiber t.handler
49-
| Return (_, k) -> Effect.Deep.continue k ()
50-
| Continue (fiber, k) -> Fiber.continue fiber k ()
51-
| Resume (fiber, k) -> Fiber.resume fiber k
52+
| Spawn (fiber, main) ->
53+
let k = Effect.Shallow.fiber main in
54+
Effect.Shallow.continue_with k fiber t.handler
55+
| Return (_, k) -> Effect.Shallow.continue_with k () t.handler
56+
| Continue (fiber, k) -> Fiber.continue_with fiber k () t.handler
57+
| Resume (fiber, k) -> Fiber.resume_with fiber k t.handler
5258
end
5359
| exception Mpscq.Empty ->
5460
t.fiber <- Fiber.Maybe.nothing;
@@ -97,7 +103,7 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
97103
exnc = (match fatal_exn_handler with None -> raise | Some exnc -> exnc);
98104
effc =
99105
(fun (type a) (e : a Effect.t) :
100-
((a, _) Effect.Deep.continuation -> _) option ->
106+
((a, _) Effect.Shallow.continuation -> _) option ->
101107
match e with
102108
| Fiber.Current -> t.current
103109
| Fiber.Spawn r ->
@@ -121,7 +127,9 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
121127
| exception exn ->
122128
let bt = Printexc.get_raw_backtrace () in
123129
Some
124-
(fun k -> Effect.Deep.discontinue_with_backtrace k exn bt)
130+
(fun k ->
131+
Effect.Shallow.discontinue_with_backtrace k exn bt
132+
t.handler)
125133
end
126134
| Trigger.Await trigger ->
127135
Some
@@ -133,7 +141,7 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
133141
let remaining_quota = t.remaining_quota - 1 in
134142
if 0 < remaining_quota then begin
135143
t.remaining_quota <- remaining_quota;
136-
Fiber.resume fiber k
144+
Fiber.resume_with fiber k t.handler
137145
end
138146
else begin
139147
Mpscq.push t.ready (Resume (fiber, k));
@@ -165,7 +173,7 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
165173
Some
166174
(fun k ->
167175
let fiber = Fiber.Maybe.to_fiber t.fiber in
168-
Effect.Deep.continue k fiber);
176+
Effect.Shallow.continue_with k fiber t.handler);
169177
t.yield <-
170178
Some
171179
(fun k ->
@@ -178,7 +186,7 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
178186
let remaining_quota = t.remaining_quota - 1 in
179187
if 0 < remaining_quota then begin
180188
t.remaining_quota <- remaining_quota;
181-
Effect.Deep.continue k ()
189+
Effect.Shallow.continue_with k () t.handler
182190
end
183191
else begin
184192
Mpscq.push t.ready (Return (Fiber.Maybe.to_fiber t.fiber, k));
@@ -188,8 +196,9 @@ let run_fiber ?quota ?fatal_exn_handler fiber main =
188196
Some
189197
(fun k ->
190198
let fiber = Fiber.Maybe.to_fiber t.fiber in
191-
Fiber.continue fiber k ());
192-
Effect.Deep.match_with main fiber t.handler
199+
Fiber.continue_with fiber k () t.handler);
200+
let k = Effect.Shallow.fiber main in
201+
Effect.Shallow.continue_with k fiber t.handler
193202

194203
let[@inline never] run ?quota ?fatal_exn_handler fiber main computation =
195204
run_fiber ?quota ?fatal_exn_handler fiber main;

0 commit comments

Comments
 (0)