summaryrefslogtreecommitdiff
path: root/lib/thread/spawn+osx.myr
blob: 3c2f391a94e07d001531b12dfc37a29f3e81dc1f (plain)
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
use sys
use std

pkg thread =
	type tid = uint64

	const spawn : (fn : (-> void) -> std.result(tid, byte[:]))
;;


const Stacksz = 8*std.MiB
extern const exit : (-> void)

const spawn = {fn
	-> spawnstk(fn, Stacksz)
}

const spawnstk = {fn, sz
	var tid : tid, ret


	ret = sys.bsdthread_create( \
		startthread castto(void#), \
		&fn castto(void#), \
		sz castto(void#), \
		&tid castto(void#), \
		0)

	if ret == -1 castto(void#)
		-> `std.Fail "couldn't spawn thread"
	;;
	-> `std.Ok tid castto(tid)
}

const startthread = {fn : (-> void)# -> void
	fn#()
	std.write(1, "...bye mom\n")
	sys.bsdthread_terminate(0 castto(void#), 0, 0, 0)
}