-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathsched.ml
More file actions
29 lines (27 loc) · 761 Bytes
/
sched.ml
File metadata and controls
29 lines (27 loc) · 761 Bytes
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
type _ eff += Fork : (unit -> unit) -> unit eff
type _ eff += Yield : unit eff
let fork f = Effect.perform (Fork f)
let yield () = Effect.perform Yield
(* A concurrent round-robin scheduler using
* effect handlers *)
let run main =
let run_q = Queue.create () in
let enqueue k = Queue.push k run_q in
let dequeue () =
if Queue.is_empty run_q
then ()
else Effect.Deep.continue (Queue.pop run_q) ()
in
let rec spawn f =
(* Effect handler => instantiates fiber *)
match f () with
| () -> dequeue ()
| exception e ->
( print_string (Printexc.to_string e);
dequeue () )
| effect Yield, k ->
( enqueue k; dequeue () )
| effect (Fork f), k ->
( enqueue k; spawn f )
in
spawn main