Backport from sid to buster
[hcoop/debian/mlton.git] / regression / thread-switch.sml
CommitLineData
7f918cf1
CE
1(*
2 * On my 400MhZ system, thread-switch 10000000 takes 4.98s, which comes out to
3 * 2,008,032 switches per second.
4 *)
5
6structure Main =
7struct
8
9type int = Int.int
10open MLton
11open Thread
12
13datatype t = T of (int * t) Thread.t
14
15val done: Thread.Runnable.t option ref = ref NONE
16
17fun loop (n: int, T t): unit =
18 if n = 0
19 then switch (fn _ => valOf (!done))
20 else
21 let
22 val (n, t) = switch (fn t' => prepare (t, (n - 1, T t')))
23 in
24 loop(n, t)
25 end
26
27fun main () =
28 let
29 val numSwitches =
30 case CommandLine.arguments () of
31 [] => 1000
32 | s :: _ => valOf (Int.fromString s)
33 in
34 switch (fn cur =>
35 (done := SOME (prepare (cur, ()))
36 ; prepare (new loop, (numSwitches, T (new loop)))))
37 ; print "ok\n"
38 end
39
40end
41
42val _ = Main.main ()
43(* SMLofNJ.exportFn
44 * ("thread-switch", fn _ => (Main.main(); OS.Process.success))
45 *)