forked from sebmarkbage/ocamlrun-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsched.ml
More file actions
29 lines (25 loc) · 688 Bytes
/
sched.ml
File metadata and controls
29 lines (25 loc) · 688 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
effect Fork : (unit -> unit) -> unit
effect Yield : unit
let fork f = perform (Fork f)
let yield () = perform Yield
(* A concurrent round-robin scheduler *)
let run main =
let run_q = Queue.create () in
let enqueue k = Queue.push k run_q in
let rec dequeue () =
if Queue.is_empty run_q then ()
else 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