shithub: mc

ref: 80f1e8ed2ff1a49b39ad424f388f10d434766e43
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


	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.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#)
}