File: thread_smlsharp_myth.sml

package info (click to toggle)
smlsharp 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 123,732 kB
  • sloc: ansic: 16,725; sh: 4,347; makefile: 2,191; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (32 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (2)
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
_interface "./thread_smlsharp.smi"

type myth_thread_t = unit ptr
val 'a#boxed myth_create =
    _import "myth_create" : ('a -> unit ptr, 'a) -> myth_thread_t
val myth_join =
    _import "myth_join" : (myth_thread_t, unit ptr array) -> int

structure Thread =
struct
  val threadtype = "myth"
  type thread = myth_thread_t * (unit -> int)
  fun create_main (f : unit -> int) : unit ptr =
      SMLSharp_Builtin.Pointer.fromWord64
        (SMLSharp_Builtin.Word32.toWord64X
           (SMLSharp_Builtin.Word32.fromInt32
              (f ())))
      handle _ => SMLSharp_Builtin.Pointer.null ()
  fun create (f : unit -> int) =
      (myth_create (create_main, f), f) : thread
  fun join ((t,f):thread) =
      let
        val p = SMLSharp_Builtin.Array.alloc_unsafe 1
        val r = myth_join (t, p)
      in
        SMLSharp_Builtin.Pointer.keepAlive f;
        SMLSharp_Builtin.Word32.toInt32X
          (SMLSharp_Builtin.Word64.toWord32
             (SMLSharp_Builtin.Pointer.toWord64
                (SMLSharp_Builtin.Array.sub_unsafe (p, 0))))
      end
end