ref: b1cd018c05d289e45529d5e15b928a1cae177223
dir: /lib/thread/spawn+osx.myr/
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)
extern const start : (-> void)
const __init__ = {
	var ret
	ret = sys.bsdthread_register(\
		(start	: void#), \	/* start */
		(0	: void#), \	/* wqthread */
		(0	: uint32), \	/* sz */
		(0	: uint32), \	/* dummy */
		(0	: void#), \	/* targconc */
		(0	: uint32))	/* queueoff */
	if ret < 0
		std.fatal("unable to init threads: {}", ret)
	;;
}
const spawn = {fn
	-> spawnstk(fn, Stacksz)
}
const spawnstk = {fn, sz
	var tid : tid, ret
	std.put("...hi? fn={}\n", (fn : void#))
	ret = sys.bsdthread_create( \
		(fn	: void#), \
		envptr(&fn), \
		(sz	: void#), \
		(0	: void#), \
		0)
	if ret == (-1 : void#)
		-> `std.Fail "couldn't spawn thread"
	;;
	-> `std.Ok (ret : tid)
}
const envptr = {fn
	var repr : std.intptr[2]
	repr = (fn : std.intptr[2]#)#
	-> (repr[0] : void#)
}