shithub: mc

ref: f2e8e641bf8ee06fa5128c522b151a7f4d9d003b
dir: /lib/thread/spawn+plan9.myr/

View raw version
use std
use sys

pkg thread =
	type tid = uint64

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

const spawn = {fn
	fn = std.fndup(fn)
	match sys.rfork(sys.Rfproc | sys.Rfmem)
	| 0:
		fn()
		std.fnfree(fn)
		std.exit(0)
	| -1:	
		std.fnfree(fn)
		-> `std.Err "unable to spawn thread"
	| thr:	-> `std.Ok (thr : tid)
	;;
}