shithub: mc

ref: 303798469e8afe0e514c667f3c8c7ae2c65e58c5
dir: /lib/thread/spawn+plan9.myr/

View raw version
use std
use sys
use "tls"

pkg thread =
	type tid = uint64

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

const spawn = {fn
	var p, s

	fn = std.fndup(fn)
	match sys.rfork(sys.Rfproc | sys.Rfmem)
	| 0:
		if ntlsslots > Staticcap
			p = getpriv()
			s = std.slalloc(ntlsslots)
			p[0] = (s : void#)
		;;
		fn()
		std.fnfree(fn)
		if ntlsslots > Staticcap
			p = getpriv()
			s = (p[0] : void##)[:ntlsslots]
			std.slfree(s)
		;;
		std.exit(0)
	| -1:	
		std.fnfree(fn)
		-> `std.Err "unable to spawn thread"
	| thr:	-> `std.Ok (thr : tid)
	;;
}