-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathsched.ml
112 lines (101 loc) · 2.22 KB
/
sched.ml
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(* TEST
include stdlib_alpha;
runtime5;
{ bytecode; }
{ native; }
*)
module Effect = Stdlib_alpha.Effect
module Uniqueue : sig
type 'a t
val create : unit -> 'a t
val push : 'a @ once unique -> 'a t -> unit
val pop : 'a t -> 'a @ once unique
val is_empty : 'a t -> bool
end = struct
type 'a t = 'a Queue.t
let create () = Queue.create ()
let push v t = Queue.push (Obj.magic_many v) t
let pop t = Obj.magic_unique (Queue.pop t)
let is_empty t = Queue.is_empty t
end
type ('a, 'e) op =
| Yield : (unit, 'e) op
| Fork : (local_ 'e Effect.Handler.t -> string) -> (unit, 'e) op
| Ping : (unit, 'e) op
module Eff = Effect.Make_rec (struct
type ('a, 'e) t = ('a, 'e) op
end)
open Eff
exception E
exception Pong
let say = print_string
let run main =
let run_q = Uniqueue.create () in
let enqueue k = Uniqueue.push k run_q in
let rec dequeue () =
if Uniqueue.is_empty run_q then
`Finished
else
handle (Effect.continue (Uniqueue.pop run_q) () [])
and spawn f =
handle (Eff.run f)
and handle = function
| Value "ok" ->
say ".";
dequeue ()
| Value s ->
failwith ("Unexpected result: " ^ s)
| Exception E ->
say "!";
dequeue ()
| Exception e ->
raise e
| Operation(Yield, k) ->
say ",";
enqueue k;
dequeue ()
| Operation(Fork f, k) ->
say "+";
enqueue k;
spawn f
| Operation(Ping, k) ->
say "[";
handle (Effect.discontinue k Pong [])
in
spawn main
;;
let test h =
say "A";
perform h
(Fork
(fun h ->
perform h Yield;
say "C";
perform h Yield;
let handle = function
| Value v -> v
| Exception Pong -> say "]"
| Exception e -> raise e
| Operation(_, k) -> failwith "what?"
in
let res =
Eff.run_with [h] (fun [_; h2] ->
perform h2 Ping;
failwith "no pong?")
in
handle res;
raise E));
perform h
(Fork
(fun h ->
say "B";
"ok"));
say "D";
perform h Yield;
say "E";
"ok"
;;
let () =
let `Finished = run test in
say "\n"
;;