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
type t = C.Types.Thread.t Ctypes.ptr
let self () =
Ctypes.addr (C.Functions.Thread.self ())
let equal =
C.Functions.Thread.equal
let thread_trampoline =
C.Functions.Thread.get_trampoline ()
let make_thread_options stack_size =
let module O = C.Types.Thread.Options in
let options = Ctypes.make O.t in
begin match stack_size with
| None ->
Ctypes.setf options O.flags O.no_flags
| Some n ->
Ctypes.setf options O.flags O.has_stack_size;
Ctypes.setf options O.stack_size (Unsigned.Size_t.of_int n)
end;
Ctypes.addr options
let create ?stack_size f =
let thread = Ctypes.addr (Ctypes.make C.Types.Thread.t) in
let f = Error.catch_exceptions f in
let f_gc_root = Ctypes.Root.create f in
let result =
C.Functions.Thread.create
thread
(make_thread_options stack_size)
thread_trampoline
f_gc_root
in
if result < 0 then begin
Ctypes.Root.release f_gc_root;
Error.result_from_c result
end
else
Result.Ok thread
let create_c ?stack_size ?(argument = Nativeint.zero) f =
let thread = Ctypes.addr (Ctypes.make C.Types.Thread.t) in
C.Functions.Thread.create_c thread (make_thread_options stack_size) f argument
|> Error.to_result thread
let join thread =
C.Blocking.Thread.join thread
|> Error.to_result ()