shithub: mc

ref: 203628871eb48f200087851741dbb5416b4e07a7
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	: 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


	ret = sys.bsdthread_create( \
		(fn	: void#), \
		envptr(&fn), \
		(sz	: void#), \
		(0	: void#), \
		0)

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

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

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