Backport from sid to buster
[hcoop/debian/mlton.git] / regression / thread-switch-share.sml
CommitLineData
7f918cf1
CE
1(* Access the current stack in the heap via a MLton.share object trace. *)
2val rt : MLton.Thread.Runnable.t option ref = ref NONE
3
4fun stats () =
5 let
6 val () = MLton.share rt
7 in
8 ()
9 end
10
11fun switcheroo () =
12 MLton.Thread.switch
13 (fn t => let
14 val () = rt := SOME (MLton.Thread.prepare (t, ()))
15 val () = stats ()
16 in
17 valOf (!rt)
18 end)
19
20(* tuple option array *)
21val a = Array.tabulate (100, fn i => SOME (i mod 2, i mod 3))
22val () = Array.update (a, 0, NONE)
23
24fun touch () =
25 let
26 val size = MLton.size a
27 val sum =
28 Array.foldr (fn (NONE,sum) => sum
29 | (SOME (a, b),sum) => a + b + sum)
30 0 a
31 in
32 (size, sum)
33 end
34
35val (size1,sum1) = touch ()
36val () = switcheroo ()
37val (size2,sum2) = touch ()
38val _ = print (concat ["size1 >= size2 = ", Bool.toString (size1 >= size2), "\n"])
39val _ = print (concat ["sum1 = sum2 = ", Bool.toString (sum1 >= sum2), "\n"])