shithub: mc

ref: 164646bf80884c20db57978c56134fb875c9e337
dir: /lib/thread/spawn+osx.myr/

View raw version
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 castto(void#), \	/* start */
		0 castto(void#), \	/* wqthread */
		0 castto(uint32), \	/* sz */
		0 castto(uint32), \	/* dummy */
		0 castto(void#), \	/* targconc */
		0 castto(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 castto(void#))
	ret = sys.bsdthread_create( \
		fn castto(void#), \
		envptr(&fn), \
		sz castto(void#), \
		0 castto(void#), \
		0)

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

const envptr = {fn
	var repr : std.intptr[2]

	repr = (fn castto(std.intptr[2]#))#
	-> repr[0] castto(void#)
}