@@ -12,19 +12,19 @@ let string_of_msg = function
1212(* * Process primitives **)
1313type pid = int
1414
15- type _ Effect.t + = Spawn : (pid -> unit ) -> pid Effect .t
15+ effect Spawn : (pid -> unit ) -> pid
1616let spawn p = perform (Spawn p)
1717
18- type _ Effect.t + = Yield : unit Effect .t
18+ effect Yield : unit
1919let yield () = perform Yield
2020
2121(* * Communication primitives **)
22- type _ Effect.t + = Send : pid * message -> unit Effect .t
22+ effect Send : pid * message -> unit
2323let send pid data =
2424 perform (Send (pid, data));
2525 yield ()
2626
27- type _ Effect.t + = Recv : pid -> message option Effect .t
27+ effect Recv : pid -> message option
2828let rec recv pid =
2929 match perform (Recv pid) with
3030 | Some m -> m
@@ -74,17 +74,14 @@ let mailbox f =
7474 let (msg, mb) = Mailbox. pop pid ! mailbox in
7575 mailbox := mb; msg
7676 in
77- try_with f () {
78- effc = fun (type a ) (e : a Effect.t ) ->
79- match e with
80- | (Send (pid , msg )) -> Some (fun (k : (a, _) continuation ) ->
81- mailbox := Mailbox. push pid msg ! mailbox;
82- continue k () )
83- | (Recv who ) -> Some (fun k ->
84- let msg = lookup who in
85- continue k msg)
86- | _ -> None
87- }
77+ match f () with
78+ | v -> v
79+ | effect (Send (pid , msg )) k ->
80+ mailbox := Mailbox. push pid msg ! mailbox;
81+ continue k ()
82+ | effect (Recv who ) k ->
83+ let msg = lookup who in
84+ continue k msg
8885
8986(* * Process handler
9087 Slightly modified version of sched.ml **)
@@ -98,17 +95,12 @@ let run main () =
9895 let pid = ref (- 1 ) in
9996 let rec spawn f =
10097 pid := 1 + ! pid;
101- match_with f ! pid {
102- retc = (fun () -> dequeue () );
103- exnc = (fun e -> raise e);
104- effc = fun (type a ) (e : a Effect.t ) ->
105- match e with
106- | Yield -> Some (fun (k : (a, _) continuation ) ->
107- enqueue (fun () -> continue k () ); dequeue () )
108- | Spawn p -> Some (fun k ->
109- enqueue (fun () -> continue k ! pid); spawn p)
110- | _ -> None
111- }
98+ match f ! pid with
99+ | () -> dequeue ()
100+ | effect Yield k ->
101+ enqueue (fun () -> continue k () ); dequeue ()
102+ | effect (Spawn p ) k ->
103+ enqueue (fun () -> continue k ! pid); spawn p
112104 in
113105 spawn main
114106
0 commit comments