-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.dats
55 lines (43 loc) · 1.07 KB
/
thread.dats
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
46
47
48
49
50
51
52
53
54
55
#define ATS_DYNLOADFLAG 0
%{^
#include <pthread.h>
%}
staload "./thread.sats"
local
#include "share/atspre_staload.hats"
abst@ype pthread_t = $extype "pthread_t"
assume tid = pthread_t
in
implement thread_self () =
$extfcall (tid, "pthread_self")
implement thread_spawn (f) = let
staload UN = "prelude/SATS/unsafe.sats"
fun apply (f: () -<lincloptr1> void): ptr = let
val _ = f ()
val _ = cloptr_free ($UN.castvwtp0{cloptr0} f)
in
the_null_ptr
end
extern fun pthread_create (&pthread_t? >> _,
int,
(()-<lincloptr1> void) -> ptr,
() -<lincloptr1> void): int = "mac#pthread_create"
var tid: tid
val g = $UN.castvwtp1{cloptr0} f
val ret = pthread_create (tid, 0, apply, f)
in
if ret != 0
then let
val _ = cloptr_free ($UN.castvwtp0{cloptr0} g)
val _ = assert_errmsg (ret = 0, "thread_spawn_env: failed")
in tid end
else let
val _ = $UN.castvwtp0{void} g
in tid end
end
implement thread_join (tid) = let
val ret = $extfcall (int, "pthread_join", tid, 0)
in
assert_errmsg (ret = 0, "thread_join failed.")
end
end