File: thread-switch.sml

package info (click to toggle)
mlton 20061107-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,828 kB
  • ctags: 61,118
  • sloc: ansic: 11,446; makefile: 1,339; sh: 1,160; lisp: 900; pascal: 256; asm: 97
file content (45 lines) | stat: -rw-r--r-- 948 bytes parent folder | download | duplicates (7)
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
(*
 * On my 400MhZ system, thread-switch 10000000 takes 4.98s, which comes out to
 * 2,008,032 switches per second.
 *)
   
structure Main =
struct

type int = Int.int
open MLton
open Thread

datatype t = T of (int * t) Thread.t

val done: Thread.Runnable.t option ref = ref NONE
   
fun loop (n: int, T t): unit =
   if n = 0
      then switch (fn _ => valOf (!done))
   else
      let
         val (n, t) = switch (fn t' => prepare (t, (n - 1, T t')))
      in
         loop(n, t)
      end
   
fun main () =
   let
      val numSwitches =
         case CommandLine.arguments () of
            [] => 1000
          | s :: _ => valOf (Int.fromString s)
   in
      switch (fn cur =>
              (done := SOME (prepare (cur, ()))
               ; prepare (new loop, (numSwitches, T (new loop)))))
      ; print "ok\n"
   end

end

val _ = Main.main ()
(*    SMLofNJ.exportFn
 *    ("thread-switch", fn _ => (Main.main(); OS.Process.success))
 *)